ClickHouse / clickhouse-js

Official JS client for ClickHouse DB
https://clickhouse.com
Apache License 2.0
225 stars 27 forks source link

Incorrect datetime query parameters #107

Closed genzgd closed 1 year ago

genzgd commented 2 years ago

Converting a Javascript date object to a date parameter does not seem to work correctly:

( async () => {
    const client = createClient();
    await client.exec({query: 'DROP TABLE IF EXISTS params'});
    await client.exec({
        query: 'CREATE TABLE IF NOT EXISTS params (key UInt64, value_1 String, value_2 DateTime) ' +
            'Engine MergeTree ORDER BY key'
    });
    await client.exec({query: "INSERT INTO params VALUES(5, {v1:String}, {v2:DateTime})",
        query_params: {'v1': 'today', 'v2': Date.now()}})
    let result = await client.query({query: 'SELECT * FROM params', format: 'TSVWithNamesAndTypes'});
    let text = await result.text();
    console.log(text)
})()

The Date.now() ends up in 2051:


key value_1 value_2
UInt64  String  DateTime
5   today   2051-05-07 21:24:46
shabashev-ivan commented 1 year ago

Hi @genzgd

The problem is that Date.now() returns a number, not a Date object. Therefore, there is no problem in the library, just in the example you need to use new Date() instead of Date.now()

genzgd commented 1 year ago

Ah, my lack of Javascript skills is showing. Thanks!