Clickhouse: Default values don’t work when using JSONEachRow format w/o specifying column names

Created on 17 Aug 2016  Β·  5Comments  Β·  Source: ClickHouse/ClickHouse

The code:

DROP TABLE IF EXISTS test_table;

CREATE TABLE test_table (
  a String,
  b Date,
  c DEFAULT sipHash64(a),
  d MATERIALIZED sipHash64(a)
) ENGINE = MergeTree(b, a, 8192);

INSERT INTO test_table (a, b) values ('1', '2016-01-01');

INSERT INTO test_table (a, b) FORMAT JSONEachRow {"a":"2","b":"2016-01-02"}

INSERT INTO test_table values ('3', '2016-01-03'); -- throws an error

INSERT INTO test_table FORMAT JSONEachRow {"a":"4","b":"2016-01-04"} -- doesn’t throw an error

SELECT *, d FROM test_table ORDER BY a;

The result:

β”Œβ”€a─┬──────────b─┬────────────────────c─┬────────────────────d─┐
β”‚ 1 β”‚ 2016-01-01 β”‚  5003827105613308882 β”‚  5003827105613308882 β”‚
β”‚ 2 β”‚ 2016-01-02 β”‚ 11449545338359147399 β”‚ 11449545338359147399 β”‚
β”‚ 4 β”‚ 2016-01-04 β”‚                    0 β”‚  3672830208859661989 β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The expected result:

β”Œβ”€a─┬──────────b─┬────────────────────c─┬────────────────────d─┐
β”‚ 1 β”‚ 2016-01-01 β”‚  5003827105613308882 β”‚  5003827105613308882 β”‚
β”‚ 2 β”‚ 2016-01-02 β”‚ 11449545338359147399 β”‚ 11449545338359147399 β”‚
β”‚ 4 β”‚ 2016-01-04 β”‚  3672830208859661989 β”‚  3672830208859661989 β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Most helpful comment

Works in master. It needs insert_sample_with_metadata to be set.

DROP TABLE IF EXISTS test_table;

CREATE TABLE test_table (
  a String,
  b Date,
  c DEFAULT sipHash64(a),
  d MATERIALIZED sipHash64(a)
) ENGINE = MergeTree(b, a, 8192);

SET insert_sample_with_metadata=1;
INSERT INTO test_table FORMAT JSONEachRow {"a":"4","b":"2016-01-04"}
SELECT a, c, d, sipHash64(a) FROM test_table ORDER BY a;

4       5406800756778728304     5406800756778728304     5406800756778728304

P.S. There's a mistake in sample. 3672830208859661989 == sipHash64('3'), not sipHash64('4').

All 5 comments

It is known (documented) issue.
https://clickhouse.yandex/reference_en.html#JSONEachRow

N.B. when using JSONEachRow format, complex default values are not supported, so when omitting a column its value will be zeros or empty string depending on its type.

The reason is that complex default values need expression evaluation, but data formats currently are independent of expressions interpreters.

But we already have many requests to implement this functionality, so we have to do it.

+1 to fix

+1 to fix

+1 to fix

Works in master. It needs insert_sample_with_metadata to be set.

DROP TABLE IF EXISTS test_table;

CREATE TABLE test_table (
  a String,
  b Date,
  c DEFAULT sipHash64(a),
  d MATERIALIZED sipHash64(a)
) ENGINE = MergeTree(b, a, 8192);

SET insert_sample_with_metadata=1;
INSERT INTO test_table FORMAT JSONEachRow {"a":"4","b":"2016-01-04"}
SELECT a, c, d, sipHash64(a) FROM test_table ORDER BY a;

4       5406800756778728304     5406800756778728304     5406800756778728304

P.S. There's a mistake in sample. 3672830208859661989 == sipHash64('3'), not sipHash64('4').

Was this page helpful?
0 / 5 - 0 ratings