-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow to specify a list of columns for insert
#217
Comments
I think this is because the right syntax for an insert operation here will be: insert into github_json_ephemeral (message_raw) values ('...') ^ see only We can add an option to restrict the insert columns, as it is useful. The only option I see as a workaround before we add this feature is to use import { createClient } from '@clickhouse/client'
void (async () => {
const table = 'github_json_ephemeral'
const client = createClient({
clickhouse_settings: {
date_time_input_format: 'best_effort',
allow_experimental_object_type: 1,
},
})
await client.command({
query: `
CREATE OR REPLACE TABLE ${table}
(
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)`,
})
const messagesRaw = [
{
type: 'MyEventType',
repo: {
name: 'foo',
},
},
{
type: 'SomeOtherType',
repo: {
name: 'bar',
},
},
]
// Generates ('val1'),('val2'), ...
const messagesRawValues = messagesRaw
.map((msg) => {
return `('${JSON.stringify(msg)}')`
})
.join(',')
const insertQuery = `INSERT INTO ${table} (message_raw) VALUES ${messagesRawValues}`
await client.command({
query: insertQuery,
})
const result = await client.query({
query: `SELECT * FROM ${table}`,
format: 'JSONCompactEachRow',
})
console.log('Result:', JSON.stringify(await result.json()))
})() Prints:
NB: see |
insert
@cjroebuck, added in 0.2.8. See the release notes. |
Describe the bug
Not sure if this is a clickhouse-js specific bug or just clickhouse in general.
Whenever I try to insert JSON as a raw
String
withEPHEMERAL
column and then have derived columns using various JSONExtract* functions, I can't seem to get it to work. I'm using the example from the JSON docs here.Steps to reproduce
Run the following code to setup tables and insert test rows.
Expected behaviour
Output:
Actual behaviour
The derived columns are blank and the JSON column is empty:
Removing the EPHEMERAL directive from the column, I get the following output:
Notice that
repo_name
field is empty butevent_type
has been successfully extracted from the JSON. So it seems the extract fromrepo.name
is also not working. This is a secondary issue.Configuration
Environment
ClickHouse server
The text was updated successfully, but these errors were encountered: