Skip to content

Commit 3ddb71b

Browse files
authored
Improve logging of NewType (#1249)
Before this change, `NewType(nil)` would return a unhelpful error message: ``` Unsupported type '<nil>' of value: <nil> in /quesma/clickhouse/schema.go:273 ``` Improve the log by providing the "origin" of the `nil` value (in most cases the column name with that `nil` value).
1 parent d46cf78 commit 3ddb71b

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

quesma/clickhouse/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func BuildAttrsMap(m SchemaMap, config *ChTableConfig) (map[string][]interface{}
156156
if a.Type.CanConvert(value) {
157157
result[a.KeysArrayName] = append(result[a.KeysArrayName], name)
158158
result[a.ValuesArrayName] = append(result[a.ValuesArrayName], fmt.Sprintf("%v", value))
159-
result[a.TypesArrayName] = append(result[a.TypesArrayName], NewType(value).String())
159+
result[a.TypesArrayName] = append(result[a.TypesArrayName], NewType(value, name).String())
160160

161161
matched = true
162162
break

quesma/clickhouse/schema.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func ResolveType(clickHouseTypeName string) reflect.Type {
231231
}
232232

233233
// 'value': value of a field, from unmarshalled JSON
234-
func NewType(value any) Type {
234+
func NewType(value any, valueOrigin string) Type {
235235
isFloatInt := func(f float64) bool {
236236
return math.Mod(f, 1.0) == 0.0
237237
}
@@ -258,7 +258,7 @@ func NewType(value any) Type {
258258
cols := make([]*Column, len(valueCasted))
259259
for k, v := range valueCasted {
260260
if v != nil {
261-
cols = append(cols, &Column{Name: k, Type: NewType(v), Codec: Codec{Name: ""}})
261+
cols = append(cols, &Column{Name: k, Type: NewType(v, fmt.Sprintf("%s.%s", valueOrigin, k)), Codec: Codec{Name: ""}})
262262
}
263263
}
264264
return MultiValueType{Name: "Tuple", Cols: cols}
@@ -267,10 +267,10 @@ func NewType(value any) Type {
267267
// empty array defaults to string for now, maybe change needed or error returned
268268
return CompoundType{Name: "Array", BaseType: NewBaseType("String")}
269269
}
270-
return CompoundType{Name: "Array", BaseType: NewType(valueCasted[0])}
270+
return CompoundType{Name: "Array", BaseType: NewType(valueCasted[0], fmt.Sprintf("%s[0]", valueOrigin))}
271271
}
272272

273-
logger.Warn().Msgf("Unsupported type '%T' of value: %v.", value, value)
273+
logger.Warn().Msgf("Unsupported type '%T' of value: %v (origin: %s).", value, value, valueOrigin)
274274

275275
// value can be nil, so should return something reasonable here
276276
return BaseType{Name: "String", GoType: reflect.TypeOf("")}

quesma/ingest/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func JsonToColumns(m SchemaMap, chConfig *clickhouse.ChTableConfig) []CreateTabl
9494
if value == nil { // HACK ALERT -> We're treating null values as strings for now, so that we don't completely discard documents with empty values
9595
fTypeString = "Nullable(String)"
9696
} else {
97-
fType := clickhouse.NewType(value)
97+
fType := clickhouse.NewType(value, name)
9898

9999
// handle "field":{} case (Elastic Agent sends such JSON fields) by ignoring them
100100
if multiValueType, ok := fType.(clickhouse.MultiValueType); ok && len(multiValueType.Cols) == 0 {
@@ -315,7 +315,7 @@ func BuildAttrsMap(m SchemaMap, config *clickhouse.ChTableConfig) (map[string][]
315315
if a.Type.CanConvert(value) {
316316
result[a.KeysArrayName] = append(result[a.KeysArrayName], name)
317317
result[a.ValuesArrayName] = append(result[a.ValuesArrayName], fmt.Sprintf("%v", value))
318-
result[a.TypesArrayName] = append(result[a.TypesArrayName], clickhouse.NewType(value).String())
318+
result[a.TypesArrayName] = append(result[a.TypesArrayName], clickhouse.NewType(value, name).String())
319319

320320
matched = true
321321
break

quesma/ingest/processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func addInvalidJsonFieldsToAttributes(attrsMap map[string][]interface{}, invalid
258258
for k, v := range invalidJson {
259259
newAttrsMap[chLib.DeprecatedAttributesKeyColumn] = append(newAttrsMap[chLib.DeprecatedAttributesKeyColumn], k)
260260
newAttrsMap[chLib.DeprecatedAttributesValueColumn] = append(newAttrsMap[chLib.DeprecatedAttributesValueColumn], v)
261-
newAttrsMap[chLib.DeprecatedAttributesValueType] = append(newAttrsMap[chLib.DeprecatedAttributesValueType], chLib.NewType(v).String())
261+
newAttrsMap[chLib.DeprecatedAttributesValueType] = append(newAttrsMap[chLib.DeprecatedAttributesValueType], chLib.NewType(v, k).String())
262262
}
263263
return newAttrsMap
264264
}

0 commit comments

Comments
 (0)