expression: fix wrong result type for greatest/least #30589
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: close #30261
Problem Summary:
When arguments of greatest/least are mixed with temporalType and string, tidb will
1、Convert all args as String
2、EvalString to get arg value
3、Parse string as datetime. If it's a valid datetime, use string(datetime) to compare. Otherwise use the result of EvalString to compare.
But it is not compitable with mysql
1、if { arg.is_temporal, EvalTime to get arg value (https://github.com/mysql/mysql-server/blob/8.0/sql/item_cmpfunc.cc#L1498) } else { EvalString to get arg value, and parse string to MYSQL_TIMESTAMP_DATE/MYSQL_TIMESTAMP_DATETIME(https://github.com/mysql/mysql-server/blob/8.0/sql/item_func.cc#L3667-L3678, https://github.com/mysql/mysql-server/blob/8.0/sql/item_cmpfunc.cc#L1520-L1524) }
2、compare with time value (https://github.com/mysql/mysql-server/blob/8.0/sql/item_func.cc#L3708)
3、if there any err, use the result of EvalString to compare (https://github.com/mysql/mysql-server/blob/8.0/sql/item_func.cc#L3745-L3748)
What is changed and how it works?
Port the code of https://github.com/mysql/mysql-server/blob/8.0/sql/item_cmpfunc.cc#L1520-L1524
TODO:
When arguments of greatest/least are mixed with time, duration, string, the result is not compitable with mysql.
Such as SELECT GREATEST(cast('000:59:59' as time), cast('2000' as YEAR), '2002-01-01');
Check List
Tests
Side effects
Documentation
Release note