Skip to content

Commit

Permalink
Merge pull request #12376 from Mytherin/castisinvertible
Browse files Browse the repository at this point in the history
Fix #11921 - varchar -> timestamp casts are not invertible
  • Loading branch information
Mytherin committed Jun 4, 2024
2 parents f74ba25 + 0819fef commit f6b154b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
12 changes: 1 addition & 11 deletions src/planner/expression/bound_cast_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,7 @@ bool BoundCastExpression::CastIsInvertible(const LogicalType &source_type, const
}
break;
case LogicalTypeId::VARCHAR:
switch (target_type.id()) {
case LogicalTypeId::TIMESTAMP:
case LogicalTypeId::TIMESTAMP_NS:
case LogicalTypeId::TIMESTAMP_MS:
case LogicalTypeId::TIMESTAMP_SEC:
case LogicalTypeId::TIMESTAMP_TZ:
return true;
default:
return false;
}
break;
return false;
default:
break;
}
Expand Down
50 changes: 50 additions & 0 deletions test/sql/optimizer/expression/test_timestamp_offset.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# name: test/sql/optimizer/expression/test_timestamp_offset.test
# description: Test pushdown of varchars converted to timestamps
# group: [expression]

statement ok
PRAGMA enable_verification

statement ok
create or replace table table1 (
timestamp_str varchar
);

statement ok
insert into table1 values ('2024-05-03 01:00:00'), ('2024-05-03 01:00:02');

query II
select timestamp_str, cast(timestamp_str as timestamp)
from table1
where cast(timestamp_str as timestamp) > cast('2024-05-03 01:00:00' as timestamp);
----
2024-05-03 01:00:02 2024-05-03 01:00:02

statement ok
truncate table table1;

statement ok
insert into table1 values ('2024-05-03T01:00:00+00:00'), ('2024-05-03T01:00:02+00:00');

query II
select timestamp_str, cast(timestamp_str as timestamp)
from table1
where cast(timestamp_str as timestamp) > cast('2024-05-03 01:00:00' as timestamp);
----
2024-05-03T01:00:02+00:00 2024-05-03 01:00:02

query II
select timestamp_str, cast(timestamp_str as timestamp)
from table1
where cast(timestamp_str as timestamp) > cast('2024-05-03T01:00:00+00:00' as timestamp);
----
2024-05-03T01:00:02+00:00 2024-05-03 01:00:02

query II
select * from (
select timestamp_str, cast(timestamp_str as timestamp) as timestamp_column
from table1
)
where timestamp_column > cast('2024-05-03 01:00:00' as timestamp);
----
2024-05-03T01:00:02+00:00 2024-05-03 01:00:02

0 comments on commit f6b154b

Please sign in to comment.