Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should not omit cast function when convert date/timestamp type to datetime in if/ifnull/case when function #26485

Closed
yuqi1129 opened this issue Jul 22, 2021 · 2 comments
Labels
severity/moderate sig/execution SIG execution type/bug The issue is confirmed as a bug. type/stale This issue has not been updated for a long time.

Comments

@yuqi1129
Copy link
Contributor

yuqi1129 commented Jul 22, 2021

Bug Report!

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

run master

select if (1, date '2020-12-12', time '12:12:12');
select if (0, date '2020-12-12', time '12:12:12');

2. What did you expect to see? (Required)

image
image

In MySQL, no matter the if conditions, the return type is determinate, no matter condition is true or not

3. What did you see instead (Required)

image

image

in TiDB, return type of if function is variable and thus cause the result is wrong.

4. What is your TiDB version? (Required)

master branch

@yuqi1129 yuqi1129 added the type/bug The issue is confirmed as a bug. label Jul 22, 2021
@yuqi1129
Copy link
Contributor Author

yuqi1129 commented Jul 22, 2021

The problem lies here.
image

When cast date/timestamp to datetime, we deliberately omit the cast function. when execute the sql

select if (1, date '2020-12-12', time '12:12:12');

the optimizer will use foldConstant rule and convert it to select date '2020-12-12' and thus return type is date

Maybe we should not just abandon the cast function when convert date/timstamp to datetime, but if we simply omit the that logic, the plan of many SQL will change(plan will be add cast function ). so, what should we fix this problem?

@jebter
Copy link

jebter commented Jun 25, 2024

TiDB root@127.0.0.1:test> select if (1, date '2020-12-12', time '12:12:12');
+--------------------------------------------+
| if (1, date '2020-12-12', time '12:12:12') |
+--------------------------------------------+
| 2020-12-12 00:00:00                        |
+--------------------------------------------+

1 row in set
Time: 0.005s
TiDB root@127.0.0.1:test> select if (0, date '2020-12-12', time '12:12:12');
+--------------------------------------------+
| if (0, date '2020-12-12', time '12:12:12') |
+--------------------------------------------+
| 2024-06-25 12:12:12                        |
+--------------------------------------------+

1 row in set
Time: 0.003s
TiDB root@127.0.0.1:test> select tidb_version() \G
***************************[ 1. row ]***************************
tidb_version() | Release Version: v8.1.0
Edition: Community
Git Commit Hash: 945d07c5d5c7a1ae212f6013adfb187f2de24b23
Git Branch: HEAD
UTC Build Time: 2024-05-21 03:52:40
GoVersion: go1.21.10
Race Enabled: false
Check Table Before Drop: false
Store: tikv

Please try the latest version. I will close it,if there are any updates, you can reopen it.

@jebter jebter closed this as completed Jun 25, 2024
@jebter jebter added the type/stale This issue has not been updated for a long time. label Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity/moderate sig/execution SIG execution type/bug The issue is confirmed as a bug. type/stale This issue has not been updated for a long time.
Projects
None yet
Development

No branches or pull requests

2 participants