Skip to content

Commit

Permalink
fix: infer Nullable/Array/LowCardinality with reflection
Browse files Browse the repository at this point in the history
helps re #152
  • Loading branch information
serprex committed Oct 13, 2024
1 parent 780d68d commit 475ba40
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 120 deletions.
4 changes: 0 additions & 4 deletions proto/cmd/ch-gen-col/infer.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ package proto
func inferGenerated(t ColumnType) Column {
switch t {
{{- range . }}
case ColumnTypeArray.Sub({{ .ColumnType }}):
return new({{ .Type }}).Array()
case ColumnTypeNullable.Sub({{ .ColumnType }}):
return new({{ .Type }}).Nullable()
case {{ .ColumnType }}:
return new({{ .Type }})
{{- end }}
Expand Down
53 changes: 37 additions & 16 deletions proto/col_auto.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proto

import (
"reflect"
"strings"

"github.com/go-faster/errors"
Expand Down Expand Up @@ -36,20 +37,8 @@ func (c *ColAuto) Infer(t ColumnType) error {
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):
c.Data = new(ColStr).Array()
case ColumnTypeNullable.Sub(ColumnTypeString):
c.Data = new(ColStr).Nullable()
case ColumnTypeLowCardinality.Sub(ColumnTypeString):
c.Data = new(ColStr).LowCardinality()
case ColumnTypeArray.Sub(ColumnTypeLowCardinality.Sub(ColumnTypeString)):
c.Data = new(ColStr).LowCardinality().Array()
case ColumnTypeBool:
c.Data = new(ColBool)
case ColumnTypeDateTime:
Expand All @@ -60,12 +49,44 @@ func (c *ColAuto) Infer(t ColumnType) error {
c.Data = NewMap[string, string](new(ColStr), new(ColStr))
case ColumnTypeUUID:
c.Data = new(ColUUID)
case ColumnTypeArray.Sub(ColumnTypeUUID):
c.Data = new(ColUUID).Array()
case ColumnTypeNullable.Sub(ColumnTypeUUID):
c.Data = new(ColUUID).Nullable()
default:
switch t.Base() {
case ColumnTypeArray:
inner := new(ColAuto)
inner.Infer(ColumnType(t.Elem()))

Check failure on line 56 in proto/col_auto.go

View workflow job for this annotation

GitHub Actions / lint / run

Error return value of `inner.Infer` is not checked (errcheck)
innerValue := reflect.ValueOf(inner.Data)
arrayMethod := innerValue.MethodByName("Array")
if arrayMethod.IsValid() && arrayMethod.Type().NumOut() == 1 {
if col, ok := arrayMethod.Call(nil)[0].Interface().(Column); ok {
c.Data = col
c.DataType = t
return nil
}
}
case ColumnTypeNullable:
inner := new(ColAuto)
inner.Infer(ColumnType(t.Elem()))

Check failure on line 68 in proto/col_auto.go

View workflow job for this annotation

GitHub Actions / lint / run

Error return value of `inner.Infer` is not checked (errcheck)
innerValue := reflect.ValueOf(inner.Data)
nullableMethod := innerValue.MethodByName("Nullable")
if nullableMethod.IsValid() && nullableMethod.Type().NumOut() == 1 {
if col, ok := nullableMethod.Call(nil)[0].Interface().(Column); ok {
c.Data = col
c.DataType = t
return nil
}
}
case ColumnTypeLowCardinality:
inner := new(ColAuto)
inner.Infer(ColumnType(t.Elem()))

Check failure on line 80 in proto/col_auto.go

View workflow job for this annotation

GitHub Actions / lint / run

Error return value of `inner.Infer` is not checked (errcheck)
innerValue := reflect.ValueOf(inner.Data)
lowCardinalityMethod := innerValue.MethodByName("LowCardinality")
if lowCardinalityMethod.IsValid() && lowCardinalityMethod.Type().NumOut() == 1 {
if col, ok := lowCardinalityMethod.Call(nil)[0].Interface().(Column); ok {
c.Data = col
c.DataType = t
return nil
}
}
case ColumnTypeDateTime:
v := new(ColDateTime)
if err := v.Infer(t); err != nil {
Expand Down
100 changes: 0 additions & 100 deletions proto/col_auto_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 475ba40

Please sign in to comment.