Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cast milliseconds to DateTime

I have Kafka integration objects:

CREATE TABLE topic_kafka
(
    topic_data String
) ENGINE = Kafka()
SETTINGS
    kafka_broker_list = 'kafka:9092',
    kafka_topic_list = 'topic',
    kafka_group_name = 'clickhouse_group',
    kafka_format = 'JSONAsString',
    kafka_num_consumers = 1;

CREATE TABLE topic
(
    time DateTime64(3),
    user_id Int32 NOT NULL,
    version String
) ENGINE = MergeTree()
ORDER BY (user_id, time);

CREATE MATERIALIZED VIEW topic_consumer 
TO topic AS
SELECT
    JSONExtract(topic_data, 'time', 'Int64') as time, 
    toInt32(JSON_VALUE(topic_data, '$.data.user_id')) as user_id,
    JSON_VALUE(topic_data, '$.data.version') as version
FROM topic_kafka;

And Kafka topic of json data with nested objects, like this:

{"time":1639387657456,"data":{"user_id":42,"version":"1.2.3"}}

The problem is that time has values 2282-12-31 00:00:00.000 in the topic table.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It also can be checked with the following query:

select cast (1639387657456 as DateTime64(3)) as dt

But for DML query below implicit date convertation works fine, as the documentation states:

insert into topic (time, user_id) values ( 1640811600000, 42)

I’ve found that such cast works fine too:

select cast (1639387657.456 as DateTime64(3)) as dt

Looks like I’ve missed something from the documentation.

What is the problem with view topic_consumer above? Is it ok to divide milliseconds by 1000 to convert it to DateTime explicitly?

>Solution :

fromUnixTimestamp64Milli

https://clickhouse.com/docs/en/sql-reference/functions/type-conversion-functions/#tounixtimestamp64nano

select fromUnixTimestamp64Milli(toInt64(1640811600000));
┌─fromUnixTimestamp64Milli(toInt64(1640811600000))─┐
│                          2021-12-29 21:00:00.000 │
└──────────────────────────────────────────────────┘
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading