Repository navigation
Expand file tree
/
Copy pathdynamic_variant_json.ts
More file actions
72 lines (71 loc) · 2.01 KB
/
Copy pathdynamic_variant_json.ts
File metadata and controls
72 lines (71 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { createClient } from "@clickhouse/client";
const tableName = `chjs_dynamic_variant_json`;
const client = createClient({
// Since 25.3, all these types are no longer experimental and are enabled by default
// However, if you are using an older version of ClickHouse, you might need these settings
// to be able to create tables with such columns.
clickhouse_settings: {
// Variant was introduced in ClickHouse 24.1
// https://clickhouse.com/docs/sql-reference/data-types/variant
allow_experimental_variant_type: 1,
// Dynamic was introduced in ClickHouse 24.5
// https://clickhouse.com/docs/sql-reference/data-types/dynamic
allow_experimental_dynamic_type: 1,
// (New) JSON was introduced in ClickHouse 24.8
// https://clickhouse.com/docs/sql-reference/data-types/newjson
allow_experimental_json_type: 1,
},
});
await client.command({
query: `
CREATE OR REPLACE TABLE ${tableName}
(
id UInt64,
var Variant(Int64, String),
dynamic Dynamic,
json JSON
)
ENGINE MergeTree
ORDER BY id
`,
});
// Sample representation in JSONEachRow format
const values = [
{
id: 1,
var: 42,
dynamic: "foo",
json: {
foo: "x",
},
},
{
id: 2,
var: "str",
// A number will default to Int64; it could be also represented as a string in JSON* family formats
// using `output_format_json_quote_64bit_integers` setting (default is 0 since CH 25.8).
// See https://clickhouse.com/docs/en/operations/settings/formats#output_format_json_quote_64bit_integers
dynamic: 144,
json: {
bar: 10,
},
},
];
await client.insert({
table: tableName,
format: "JSONEachRow",
values,
});
const rs = await client.query({
query: `
SELECT *,
variantType(var),
dynamicType(dynamic),
dynamicType(json.foo),
dynamicType(json.bar)
FROM ${tableName}
`,
format: "JSONEachRow",
});
console.log(await rs.json());
await client.close();