diff --git a/proto/col_auto.go b/proto/col_auto.go index fb5b9bee..b6743c75 100644 --- a/proto/col_auto.go +++ b/proto/col_auto.go @@ -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 { @@ -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 } @@ -57,7 +66,7 @@ type InferColumn interface { } func (c ColAuto) Type() ColumnType { - return c.Data.Type() + return c.DataType } func (c ColAuto) Rows() int { diff --git a/proto/col_auto_test.go b/proto/col_auto_test.go index 2a1c5bec..722af91f 100644 --- a/proto/col_auto_test.go +++ b/proto/col_auto_test.go @@ -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()) }