-
Couldn't load subscription status.
- Fork 1k
feat(snowflake): add type annotation for DECODE function support #6199
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
base: main
Are you sure you want to change the base?
feat(snowflake): add type annotation for DECODE function support #6199
Conversation
|
Decode function support is already available . added support to return type annotation for DECODE function. |
The annotation now determines the output type based solely on the return values (ret1, ret2, ..., default), ignoring comparison values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a tough function to annotate, nice work! Leaving a few comments:
sqlglot/dialects/snowflake.py
Outdated
| if len(expressions) % 2 == 1: | ||
| return_types.append(expressions[-1].type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That'd be if the total number of expressions is even, right? Because:
- One argument is the input
<expr> - N pairs of
<search1> , <result1> - [Optional] The
<default>value
So if we don't have (3) there'd be 2*N + 1 arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree. Thank you. DECODE(x, 1, 'a', 2, 'b', 'default'). length 6.
in case of even length , return types can be obtained from the default. Fixed the code to check for even length
| DECODE(x, 1, 100, 2, 200, 0); | ||
| INT; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add test cases with mixed types e.g integers and floats:
snowflake> WITH t0 AS (SELECT DECODE(100, 100, 1, 90, 2, 5.5) AS col) SELECT SYSTEM$TYPEOF(col), col FROM t0;
COL_TYPE | COL
NUMBER(2,1) | 1.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added tests
sqlglot/dialects/snowflake.py
Outdated
| return_types = [] | ||
| for i in range(2, len(expressions), 2): | ||
| return_types.append(expressions[i].type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Nit] We can make this a one-liner with list comprehension
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated to use a one-liner list comprehension.
…implemented. Fixed the annotate_decode_case function to identify the returntype based on default param and added more tests
No description provided.