Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust the tests that rely on the old Object('json') data type #299

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions examples/insert_ephemeral_columns.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,47 @@
import { createClient } from '@clickhouse/client' // or '@clickhouse/client-web'

// Ephemeral columns documentation: https://clickhouse.com/docs/en/sql-reference/statements/create/table#ephemeral
// This example is inspired by https://github.com/ClickHouse/clickhouse-js/issues/217
void (async () => {
const tableName = 'insert_ephemeral_columns'
const client = createClient({
clickhouse_settings: {
allow_experimental_object_type: 1, // allows JSON type usage
},
})
const client = createClient({})

await client.command({
query: `
CREATE OR REPLACE TABLE ${tableName}
(
event_type LowCardinality(String) DEFAULT JSONExtractString(message_raw, 'type'),
repo_name LowCardinality(String) DEFAULT JSONExtractString(message_raw, 'repo', 'name'),
message JSON DEFAULT message_raw,
message_raw String EPHEMERAL
id UInt64,
message String DEFAULT message_default,
message_default String EPHEMERAL
)
ENGINE MergeTree()
ORDER BY (event_type, repo_name)
ORDER BY (id)
`,
})

await client.insert({
table: tableName,
values: [
{
message_raw: {
type: 'MyEventType',
repo: {
name: 'foo',
},
},
id: '42',
message_default: 'foo',
},
{
message_raw: {
type: 'SomeOtherType',
repo: {
name: 'bar',
},
},
id: '144',
message_default: 'bar',
},
],
format: 'JSONEachRow',
// The name of the ephemeral column has to be specified here
// to trigger the default values logic for the rest of the columns
columns: ['message_raw'],
columns: ['id', 'message_default'],
})

const rows = await client.query({
query: `SELECT * FROM ${tableName}`,
format: 'JSONCompactEachRowWithNames',
query: `SELECT *
FROM ${tableName}`,
format: 'JSONEachRow',
})

console.info(await rows.text())
console.info(await rows.json())
await client.close()
})()
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ describe('data types', () => {
o: { a: 2, b: { c: 3, d: [4, 5, 6] } },
},
]
const table = await createTableWithFields(client, 'o JSON', {
const table = await createTableWithFields(client, `o Object('json')`, {
allow_experimental_object_type: 1,
})
await insertAndAssert(table, values)
Expand Down
Loading
Loading