Skip to content

Commit

Permalink
feat(proto): support Nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Jun 19, 2022
1 parent 4d3f525 commit b7fc8bd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions proto/col_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func (c *ColAuto) Infer(t ColumnType) error {
return nil
}
switch t {
case ColumnTypeNothing:
c.Data = new(ColNothing)
case ColumnTypeNullable.Sub(ColumnTypeNothing):
c.Data = new(ColNothing).Nullable()
case ColumnTypeArray.Sub(ColumnTypeNothing):
c.Data = new(ColNothing).Array()
case ColumnTypeString:
c.Data = new(ColStr)
case ColumnTypeArray.Sub(ColumnTypeString):
Expand Down
3 changes: 3 additions & 0 deletions proto/col_auto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func TestColAuto_Infer(t *testing.T) {
"IntervalSecond",
"IntervalMinute",
ColumnType(IntervalHour.String()),
ColumnTypeNothing,
"Nullable(Nothing)",
"Array(Nothing)",
} {
r := AutoResult("foo")
require.NoError(t, r.Data.(Inferable).Infer(columnType))
Expand Down
57 changes: 57 additions & 0 deletions proto/col_nothing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package proto

import "fmt"

// Nothing represents NULL value.
type Nothing struct{}

// ColNothing represents column of null values.
// Value is row count.
//
// https://clickhouse.com/docs/ru/sql-reference/data-types/special-data-types/nothing
type ColNothing int

func (c *ColNothing) Append(v Nothing) {
*c += 1
}

func (c ColNothing) Row(i int) Nothing {
if i >= int(c) {
panic(fmt.Sprintf("[%d] of [%d]Nothing", i, c))
}
return Nothing{}
}

func (c ColNothing) Type() ColumnType {
return ColumnTypeNothing
}

func (c ColNothing) Rows() int {
return int(c)
}

func (c *ColNothing) DecodeColumn(r *Reader, rows int) error {
*c = ColNothing(rows)
_, err := r.ReadRaw(rows)
return err
}

func (c *ColNothing) Reset() {
*c = 0
}

func (c *ColNothing) Nullable() *ColNullable[Nothing] {
return &ColNullable[Nothing]{
Values: c,
}
}

func (c *ColNothing) Array() *ColArr[Nothing] {
return &ColArr[Nothing]{
Data: c,
}
}

func (c ColNothing) EncodeColumn(b *Buffer) {
b.Ensure(int(c))
}
1 change: 1 addition & 0 deletions proto/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ const (
ColumnTypeDecimal256 ColumnType = "Decimal256"
ColumnTypePoint ColumnType = "Point"
ColumnTypeInterval ColumnType = "Interval"
ColumnTypeNothing ColumnType = "Nothing"
)

// colWrap wraps Column with type t.
Expand Down
13 changes: 13 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,19 @@ func TestClient_Query(t *testing.T) {
}), "select table")
require.Equal(t, proto.Interval{Scale: proto.IntervalWeek, Value: 1}, data.Row(0))
})
t.Run("SelectNothing", func(t *testing.T) {
t.Parallel()
conn := Conn(t)

data := proto.NewColNullable[proto.Nothing](new(proto.ColNothing))
require.NoError(t, conn.Do(ctx, Query{
Body: "SELECT NULL as w",
Result: proto.Results{
{Name: "w", Data: data},
},
}), "select table")
require.False(t, data.Row(0).Set)
})
}

func TestClientCompression(t *testing.T) {
Expand Down

0 comments on commit b7fc8bd

Please sign in to comment.