Skip to content

Commit

Permalink
Merge pull request #97 from go-faster/feat/proto-auto-datetime64
Browse files Browse the repository at this point in the history
feat(proto.auto): support DateTime64
  • Loading branch information
ernado authored May 2, 2022
2 parents 92910f3 + 268a98b commit b8cb924
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
23 changes: 16 additions & 7 deletions proto/col_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import "github.com/go-faster/errors"

// ColAuto is column that is initialized during decoding.
type ColAuto struct {
Data Column
Data Column
DataType ColumnType
}

// Infer and initialize Column from ColumnType.
func (c *ColAuto) Infer(t ColumnType) error {
if c.Data != nil && !c.Type().Conflicts(t) {
// Already ok.
c.DataType = t // update subtype if needed
return nil
}
if c.inferNumeric(t) {
c.DataType = t
return nil
}
switch t {
Expand All @@ -25,23 +28,29 @@ func (c *ColAuto) Infer(t ColumnType) error {
c.Data = new(ColDateTime)
case ColumnTypeDate:
c.Data = new(ColDate)

default:
if t.Base() == ColumnTypeLowCardinality {
elem := t.Elem()
if elem == ColumnTypeString {
switch t.Base() {
case ColumnTypeLowCardinality:
if t.Elem() == ColumnTypeString {
c.Data = &ColLowCardinality{
Index: new(ColStr),
}
c.DataType = t
return nil
}
} else if t.Base() == ColumnTypeDateTime {
case ColumnTypeDateTime:
c.Data = new(ColDateTime)
c.DataType = t
return nil
case ColumnTypeDateTime64:
c.Data = new(ColDateTime)
c.DataType = t
return nil
}
return errors.Errorf("automatic column inference not supported for %q", t)
}

c.DataType = t
return nil
}

Expand All @@ -57,7 +66,7 @@ type InferColumn interface {
}

func (c ColAuto) Type() ColumnType {
return c.Data.Type()
return c.DataType
}

func (c ColAuto) Rows() int {
Expand Down
8 changes: 2 additions & 6 deletions proto/col_auto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ func TestColAuto_Infer(t *testing.T) {
ColumnTypeIPv6,
ColumnTypeLowCardinality.Sub(ColumnTypeString),
ColumnTypeDateTime.Sub("Europe/Berlin"),
ColumnTypeDateTime64.Sub("9"),
} {
auto := r.Data.(InferColumn)
require.NoError(t, auto.Infer(columnType))
// timezone-agnostic - see note about time zone in DateTime.Time
if auto.Type().Base() == ColumnTypeDateTime {
require.Equal(t, auto.Type(), columnType.Base())
} else {
require.Equal(t, auto.Type(), columnType)
}
require.Equal(t, columnType, auto.Type())
auto.Reset()
require.Equal(t, 0, auto.Rows())
}
Expand Down

0 comments on commit b8cb924

Please sign in to comment.