-
Notifications
You must be signed in to change notification settings - Fork 288
(pick to 3.0-dev): IF function with GROUPING in ROLLUP #22776
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
Conversation
### Root Cause The issue occurs in the type checking logic of the IF() function (iffCheck in operatorSet.go): 1. When IF() receives mixed types (string and numeric), it tries to unify them to a single return type 2. The function iterates through retOperatorIffSupports array to find the best type match 3. Previously, numeric types (int8, int16, int32, etc.) were listed before string types (varchar, char, etc.) 4. This caused the system to attempt converting 'All years' (string) to int type 5. While the type system allows varchar→int implicit cast (for numeric strings like '123'), the actual cast of 'All years' to int fails at runtime ### Solution Added special handling logic in iffCheck function in /pkg/sql/plan/function/operatorSet.go to detect mixed string/numeric types and prioritize string type as the return type. Approved by: @heni02, @ouyuanning
PR Compliance Guide 🔍(Compliance updated until commit cfa6e4a)Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label Previous compliance checksCompliance check up to commit 33a1e9d
|
||||||||||||||||||||||||||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||||
User description
What type of PR is this?
Which issue(s) this PR fixes:
issue #19998
What this PR does / why we need it:
Root Cause
The issue occurs in the type checking logic of the IF() function (iffCheck in operatorSet.go):
When IF() receives mixed types (string and numeric), it tries to unify them to a single return type
The function iterates through retOperatorIffSupports array to find the best type match
Previously, numeric types (int8, int16, int32, etc.) were listed before string types (varchar, char, etc.)
This caused the system to attempt converting 'All years' (string) to int type
While the type system allows varchar→int implicit cast (for numeric strings like '123'), the actual cast of 'All years' to int fails at runtime
Solution
Added special handling logic in iffCheck function in /pkg/sql/plan/function/operatorSet.go to detect mixed string/numeric types and
prioritize string type as the return type.
PR Type
Bug fix
Description
Fix IF function type checking with mixed string/numeric types
Prioritize string return type when mixing string and numeric operands
Prevent runtime cast errors for non-numeric strings like 'All years'
Add comprehensive test coverage for mixed-type IF expressions
Diagram Walkthrough
File Walkthrough
operatorSet.go
Add mixed type handling in IF type checkingpkg/sql/plan/function/operatorSet.go
iffCheckfunction to detect mixedstring/numeric types
varchar as return type
setMaxWidthFromSourceto preserve width information from sourcetypes
sourcevariable declaration to accommodate new logicoperatorSet_test.go
Add unit tests for mixed-type IF expressionspkg/sql/plan/function/operatorSet_test.go
Test_IffCheck_MixedTypeswith three test casestype
rollup.sql
Add comprehensive ROLLUP test cases for mixed typestest/distributed/cases/window/rollup.sql
rollup.result
Add expected results for mixed-type ROLLUP teststest/distributed/cases/window/rollup.result
labels