Skip to content

Commit 1ebc2ee

Browse files
committed
fix(clickhouse): handle nullable JSON columns in normalization
Add support for `Nullable(JSON)` type in ClickHouse connector by: - Recognizing `Nullable(JSON)` in schema type mapping - Using `JSONExtract` with proper type casting for both JSON and Nullable(JSON) columns - Casting nullable JSON fields to `Nullable(JSON)` in table functions
1 parent fa9c3c0 commit 1ebc2ee

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

flow/connectors/clickhouse/clickhouse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func GetTableSchemaForTable(tm *protos.TableMapping, columns []driver.ColumnType
426426
qkind = types.QValueKindArrayBoolean
427427
case "Array(Date)":
428428
qkind = types.QValueKindArrayDate
429-
case "JSON":
429+
case "JSON", "Nullable(JSON)":
430430
qkind = types.QValueKindJSON
431431
default:
432432
if strings.Contains(column.DatabaseTypeName(), "Decimal") {

flow/connectors/clickhouse/normalize_query.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,25 @@ func (t *NormalizeQueryGenerator) BuildQuery(ctx context.Context) (string, error
205205
peerdb_clickhouse.QuoteIdentifier(dstColName),
206206
)
207207
}
208-
case "JSON", "Nullable(JSON)":
209-
fmt.Fprintf(&projection,
210-
"JSONExtractString(_peerdb_data, %s)::JSON AS %s,",
211-
peerdb_clickhouse.QuoteLiteral(colName),
212-
peerdb_clickhouse.QuoteIdentifier(dstColName),
213-
)
214-
if t.enablePrimaryUpdate {
215-
fmt.Fprintf(&projectionUpdate,
216-
"JSONExtractString(_peerdb_match_data, %s)::JSON AS %s,",
208+
case "JSON", "Nullable(JSON)":
209+
stringType := strings.Replace(clickHouseType, "JSON", "String", 1)
210+
211+
fmt.Fprintf(&projection,
212+
"JSONExtract(_peerdb_data, %s, '%s')::%s AS %s,",
217213
peerdb_clickhouse.QuoteLiteral(colName),
214+
stringType,
215+
clickHouseType,
218216
peerdb_clickhouse.QuoteIdentifier(dstColName),
219217
)
220-
}
218+
if t.enablePrimaryUpdate {
219+
fmt.Fprintf(&projectionUpdate,
220+
"JSONExtract(_peerdb_match_data, %s, '%s')::%s AS %s,",
221+
peerdb_clickhouse.QuoteLiteral(colName),
222+
stringType,
223+
clickHouseType,
224+
peerdb_clickhouse.QuoteIdentifier(dstColName),
225+
)
226+
}
221227

222228
default:
223229
projLen := projection.Len()

flow/connectors/clickhouse/table_function.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func jsonFieldExpressionConverter(
5151
return sourceFieldIdentifier, nil
5252
}
5353

54+
if field.Nullable {
55+
return fmt.Sprintf("CAST(%s, 'Nullable(JSON)')", sourceFieldIdentifier), nil
56+
}
57+
5458
return fmt.Sprintf("CAST(%s, 'JSON')", sourceFieldIdentifier), nil
5559
}
5660

0 commit comments

Comments
 (0)