Created
March 17, 2026 13:45
-
-
Save kavirajk/6ca03e82d2e59eb1c55b9faf35d5afca to your computer and use it in GitHub Desktop.
Variant ClickHouse Go null
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "github.com/ClickHouse/clickhouse-go/v2" | |
| ) | |
| func main() { | |
| conn, err := clickhouse.Open(&clickhouse.Options{ | |
| Addr: []string{"localhost:9000"}, | |
| }) | |
| if err != nil { | |
| panic(err) | |
| } | |
| defer conn.Close() | |
| ctx := context.Background() | |
| for _, q := range []string{ | |
| "SET allow_experimental_dynamic_type = 1", | |
| "SET output_format_native_use_flattened_dynamic_and_json_serialization = 1", | |
| "DROP TABLE IF EXISTS demo_null_dynamic", | |
| "CREATE TABLE demo_null_dynamic (c Dynamic) ENGINE = Memory", | |
| } { | |
| if err := conn.Exec(ctx, q); err != nil { | |
| panic(err) | |
| } | |
| } | |
| batch, err := conn.PrepareBatch(ctx, "INSERT INTO demo_null_dynamic (c)") | |
| if err != nil { | |
| panic(err) | |
| } | |
| if err := batch.Append("hello"); err != nil { | |
| panic(err) | |
| } | |
| if err := batch.Append(nil); err != nil { // NULL row | |
| panic(err) | |
| } | |
| if err := batch.Send(); err != nil { | |
| panic(err) | |
| } | |
| // Correct: scan into *clickhouse.Dynamic | |
| rows, err := conn.Query(ctx, "SELECT c FROM demo_null_dynamic") | |
| if err != nil { | |
| panic(err) | |
| } | |
| for i := 0; rows.Next(); i++ { | |
| var dest clickhouse.Dynamic | |
| if err := rows.Scan(&dest); err != nil { | |
| panic(err) | |
| } | |
| fmt.Printf(" [*Dynamic] row %d: value=%v type=%q Nil()=%v\n", | |
| i, dest.Any(), dest.Type(), dest.Nil()) | |
| } | |
| rows.Close() | |
| // Broken: scan into *any — NULL leaves destination unchanged | |
| rows, err = conn.Query(ctx, "SELECT c FROM demo_null_dynamic") | |
| if err != nil { | |
| panic(err) | |
| } | |
| for i := 0; rows.Next(); i++ { | |
| var dest any = "string value" | |
| if err := rows.Scan(&dest); err != nil { | |
| panic(err) | |
| } | |
| fmt.Printf(" [*any] row %d: value=%v unchanged-on-null=%v\n", | |
| i, dest, dest == "sentinel") | |
| } | |
| rows.Close() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment