-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to specify or exclude insert columns (#218)
- Loading branch information
Showing
9 changed files
with
606 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
out | ||
dist | ||
node_modules | ||
webpack |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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 | ||
}, | ||
}) | ||
|
||
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 | ||
) | ||
ENGINE MergeTree() | ||
ORDER BY (event_type, repo_name) | ||
`, | ||
}) | ||
|
||
await client.insert({ | ||
table: tableName, | ||
values: [ | ||
{ | ||
message_raw: { | ||
type: 'MyEventType', | ||
repo: { | ||
name: 'foo', | ||
}, | ||
}, | ||
}, | ||
{ | ||
message_raw: { | ||
type: 'SomeOtherType', | ||
repo: { | ||
name: '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'], | ||
}) | ||
|
||
const rows = await client.query({ | ||
query: `SELECT * FROM ${tableName}`, | ||
format: 'JSONCompactEachRowWithNames', | ||
}) | ||
|
||
console.info(await rows.text()) | ||
await client.close() | ||
})() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { createClient } from '@clickhouse/client' // or '@clickhouse/client-web' | ||
|
||
void (async () => { | ||
const tableName = 'insert_exclude_columns' | ||
const client = createClient() | ||
|
||
await client.command({ | ||
query: ` | ||
CREATE OR REPLACE TABLE ${tableName} | ||
(id UInt32, message String) | ||
ENGINE MergeTree() | ||
ORDER BY (id) | ||
`, | ||
}) | ||
|
||
/** | ||
* Explicitly specifying a list of columns to insert the data into | ||
*/ | ||
await client.insert({ | ||
table: tableName, | ||
values: [{ message: 'foo' }], | ||
format: 'JSONEachRow', | ||
// `id` column value for this row will be zero | ||
columns: ['message'], | ||
}) | ||
|
||
await client.insert({ | ||
table: tableName, | ||
values: [{ id: 42 }], | ||
format: 'JSONEachRow', | ||
// `message` column value for this row will be an empty string | ||
columns: ['id'], | ||
}) | ||
|
||
/** | ||
* Alternatively, it is possible to exclude certain columns instead | ||
*/ | ||
await client.insert({ | ||
table: tableName, | ||
values: [{ message: 'bar' }], | ||
format: 'JSONEachRow', | ||
// `id` column value for this row will be zero | ||
columns: { | ||
except: ['id'], | ||
}, | ||
}) | ||
|
||
await client.insert({ | ||
table: tableName, | ||
values: [{ id: 144 }], | ||
format: 'JSONEachRow', | ||
// `message` column value for this row will be an empty string | ||
columns: { | ||
except: ['message'], | ||
}, | ||
}) | ||
|
||
const rows = await client.query({ | ||
query: `SELECT * FROM ${tableName} ORDER BY id, message DESC`, | ||
format: 'JSONEachRow', | ||
}) | ||
|
||
console.info(await rows.json()) | ||
await client.close() | ||
})() |
Oops, something went wrong.