Skip to content

Conversation

@fivetran-ashashankar
Copy link
Collaborator

No description provided.

@fivetran-ashashankar fivetran-ashashankar changed the title annotate type function for DECODE feat(snowflake): add type annotation for DECODE function support Oct 27, 2025
@fivetran-ashashankar
Copy link
Collaborator Author

fivetran-ashashankar commented Oct 27, 2025

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.
Copy link
Collaborator

@VaggelisD VaggelisD left a 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:

Comment on lines 566 to 567
if len(expressions) % 2 == 1:
return_types.append(expressions[-1].type)
Copy link
Collaborator

@VaggelisD VaggelisD Oct 28, 2025

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:

  1. One argument is the input <expr>
  2. N pairs of <search1> , <result1>
  3. [Optional] The <default> value

So if we don't have (3) there'd be 2*N + 1 arguments

Copy link
Collaborator Author

@fivetran-ashashankar fivetran-ashashankar Oct 28, 2025

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

Comment on lines +2319 to +2320
DECODE(x, 1, 100, 2, 200, 0);
INT;
Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added tests

Comment on lines 561 to 563
return_types = []
for i in range(2, len(expressions), 2):
return_types.append(expressions[i].type)
Copy link
Collaborator

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

Copy link
Collaborator Author

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants