apla / node-clickhouse

Yandex ClickHouse driver for nodejs
MIT License
217 stars 50 forks source link

Cannot use JSONEachRow format #67

Open vibl opened 3 years ago

vibl commented 3 years ago

I was trying to sending an INSERT query with a JSONEachRow format and any of my attempts failed.

While debugging I found two problems:

The line https://github.com/apla/node-clickhouse/blob/5ac5a8fd3dbc8a0888a3513f4d2c4e5485c68f9c/src/clickhouse.js#L272 overrides the options.format = "JSONEachRow" I have explicitly set. Is that on purpose? If so, why?

The regexp https://github.com/apla/node-clickhouse/blob/5ac5a8fd3dbc8a0888a3513f4d2c4e5485c68f9c/src/clickhouse.js#L269 should have a \b on each side of the parentheses group, otherwise e.g. JSON is matched instead of JSONEachRow.

So:

 var formatRegexp = /FORMAT\s+\b(BlockTabSeparated|CSV|CSVWithNames|JSON|JSONCompact|JSONEachRow|Native|Null|Pretty|PrettyCompact|PrettyCompactMonoBlock|PrettyNoEscapes|PrettyCompactNoEscapes|PrettySpaceNoEscapes|PrettySpace|RowBinary|TabSeparated|TabSeparatedRaw|TabSeparatedWithNames|TabSeparatedWithNamesAndTypes|TSKV|Values|Vertical|XML)\b/i; 

So options.format ended up being set as JSON whatever I was doing.

vincenzon commented 2 years ago

I hit this issue as well. In case others do a fix is to explicitly set the format option:

const clickhouseStream = clickhouse.query(
      `insert into some_table`,
      { format: "JSONEachRow" }
    );

instead of:

const clickhouseStream = clickhouse.query(
      `insert into some_table format JSONEachRow`,
    );