Skip to content

Commit f3b8301

Browse files
Backport #68323 to 24.8: Fix small value DateTime64 constant folding in nested subquery for remote
1 parent b54f79e commit f3b8301

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/Analyzer/ConstantNode.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,10 @@ ASTPtr ConstantNode::toASTImpl(const ConvertToASTOptions & options) const
177177
* It could also lead to ambiguous parsing because we don't know if the string literal represents a date or a Decimal64 literal.
178178
* For this reason, we use a string literal representing a date instead of a Decimal64 literal.
179179
*/
180-
if (WhichDataType(constant_value_type->getTypeId()).isDateTime64())
180+
const auto & constant_value_end_type = removeNullable(constant_value_type); /// if Nullable
181+
if (WhichDataType(constant_value_end_type->getTypeId()).isDateTime64())
181182
{
182-
const auto * date_time_type = typeid_cast<const DataTypeDateTime64 *>(constant_value_type.get());
183+
const auto * date_time_type = typeid_cast<const DataTypeDateTime64 *>(constant_value_end_type.get());
183184
DecimalField<Decimal64> decimal_value;
184185
if (constant_value_literal.tryGet<DecimalField<Decimal64>>(decimal_value))
185186
{
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
0 1970-01-01 00:00:00.000
2+
0 1970-01-01 00:00:05.000
3+
0 1970-01-01 00:45:25.456789
4+
0 1970-01-01 00:53:25.456789123
5+
0 \N
6+
1 1970-01-01 00:00:00.000
7+
5 1970-01-01 00:00:00.000
8+
2 1970-01-01 00:00:02.456
9+
3 1970-01-01 00:00:04.811
10+
4 1970-01-01 00:10:05.000
11+
4 1970-01-01 00:10:05.000
12+
1 1970-01-01 00:00:00.000
13+
2 1970-01-01 00:00:02.456
14+
3 1970-01-01 00:00:04.811
15+
5 1970-01-01 00:00:00.000
16+
0
17+
0
18+
5
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
-- Tags: shard
2+
set session_timezone = 'UTC'; -- don't randomize the session timezone
3+
SET allow_experimental_analyzer = 1;
4+
5+
select *, (select toDateTime64(0, 3)) from remote('127.0.0.1', system.one) settings prefer_localhost_replica=0;
6+
select *, (select toDateTime64(5, 3)) from remote('127.0.0.1', system.one) settings prefer_localhost_replica=0;
7+
select *, (select toDateTime64('1970-01-01 00:45:25.456789', 6)) from remote('127.0.0.1', system.one) settings prefer_localhost_replica=0;
8+
select *, (select toDateTime64('1970-01-01 00:53:25.456789123', 9)) from remote('127.0.0.1', system.one) settings prefer_localhost_replica=0;
9+
select *, (select toDateTime64(null,3)) from remote('127.0.0.1', system.one) settings prefer_localhost_replica=0;
10+
11+
create database if not exists shard_0;
12+
create database if not exists shard_1;
13+
14+
drop table if exists shard_0.dt64_03222;
15+
drop table if exists shard_1.dt64_03222;
16+
drop table if exists distr_03222_dt64;
17+
18+
create table shard_0.dt64_03222(id UInt64, dt DateTime64(3)) engine = MergeTree order by id;
19+
create table shard_1.dt64_03222(id UInt64, dt DateTime64(3)) engine = MergeTree order by id;
20+
create table distr_03222_dt64 (id UInt64, dt DateTime64(3)) engine = Distributed(test_cluster_two_shards_different_databases, '', dt64_03222);
21+
22+
insert into shard_0.dt64_03222 values(1, toDateTime64('1970-01-01 00:00:00.000',3));
23+
insert into shard_0.dt64_03222 values(2, toDateTime64('1970-01-01 00:00:02.456',3));
24+
insert into shard_1.dt64_03222 values(3, toDateTime64('1970-01-01 00:00:04.811',3));
25+
insert into shard_1.dt64_03222 values(4, toDateTime64('1970-01-01 00:10:05',3));
26+
insert into shard_1.dt64_03222 values(5, toDateTime64(0,3));
27+
28+
--Output : 1,5 2,3,4 4 1,2,3,5 0 0 5
29+
select id, dt from distr_03222_dt64 where dt = (select toDateTime64(0,3)) order by id;
30+
select id, dt from distr_03222_dt64 where dt > (select toDateTime64(0,3)) order by id;
31+
select id, dt from distr_03222_dt64 where dt > (select toDateTime64('1970-01-01 00:10:00.000',3)) order by id;
32+
select id, dt from distr_03222_dt64 where dt < (select toDateTime64(5,3)) order by id;
33+
34+
select count(*) from distr_03222_dt64 where dt > (select toDateTime64('2024-07-20 00:00:00',3));
35+
select count(*) from distr_03222_dt64 where dt > (select now());
36+
select count(*) from distr_03222_dt64 where dt < (select toDateTime64('2004-07-20 00:00:00',3));
37+
38+
39+
drop table if exists shard_0.dt64_03222;
40+
drop table if exists shard_1.dt64_03222;
41+
drop table if exists distr_03222_dt64;
42+
43+
drop database shard_0;
44+
drop database shard_1;

0 commit comments

Comments
 (0)