Skip to content

Conversation

@fivetran-felixhuang
Copy link
Collaborator

annotate type for MODE function snowflake

https://docs.snowflake.com/en/sql-reference/functions/mode

pass


class Mode(AggFunc):
Copy link
Collaborator

@geooo109 geooo109 Dec 2, 2025

Choose a reason for hiding this comment

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

This function is supported by other dialects.

For example: Databricks mode supports up to 2 arguments:
mode(expr [, deterministic ])

We should verify if other databases support this function (from their docs online), and find a common representation for all of them. Then add parsing tests for each of them validate_identity or validate_all.

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 support 3 patterns:

Pattern 1: Simple Aggregate

Mode(this=category, deterministic=None)
-> Snowflake: MODE(category)

Pattern 2: With Deterministic Parameter

Mode(this=category, deterministic=TRUE/FALSE)
-> Databricks/Spark: mode(category, true)

Pattern 3: Empty Function with WITHIN GROUP

WithinGroup(
this=Mode(this=None, deterministic=None),
expression=Order(expressions=[Ordered(this=category)])
)
-> PostgreSQL: mode() WITHIN GROUP (ORDER BY category)

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.

Nice work Felix! Leaving a few comments to further clean this up, should be good to merge afterwards:

Comment on lines 60 to 62
self.validate_identity("SELECT MODE(category)")
self.validate_identity("SELECT MODE(price) AS most_common FROM products")
self.validate_identity("SELECT MODE(status) OVER (PARTITION BY region) FROM orders")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think MODE exists on BQ, couldn't find it in their functions nor does it work:

bigquery> SELECT mode(1);

Function not found: mode; Did you mean mod? at [1:8]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed

Comment on lines 28 to 29
self.validate_identity("SELECT MODE(x)")
self.validate_identity("SELECT MODE(category) FROM table1")
Copy link
Collaborator

Choose a reason for hiding this comment

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

These 2 tests are identical, we can keep one of those:

Suggested change
self.validate_identity("SELECT MODE(x)")
self.validate_identity("SELECT MODE(category) FROM table1")
self.validate_identity("SELECT MODE(x)")

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

Comment on lines 60 to 61
self.validate_identity("SELECT MODE(category)")
self.validate_identity("SELECT MODE(price) AS most_common FROM products")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto, the alias or the FROM <table> shouldn't make a difference in the test, we're mostly interested in testing MODE's args.

Suggested change
self.validate_identity("SELECT MODE(category)")
self.validate_identity("SELECT MODE(price) AS most_common FROM products")
self.validate_identity("SELECT MODE(category)")

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

Comment on lines +17 to +18
self.validate_identity("SELECT MODE(category)")
self.validate_identity("SELECT MODE(price, TRUE) AS deterministic_mode FROM products")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto. Also, instead of repeating tests in each file we can also put the common ones in identity_function.sql such as MODE(x)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the problem is that Postgres only seems to support semantics like "SELECT MODE() WITHIN GROUP (ORDER BY category) FROM table1", not "SELECT MODE(x)"

Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

Feel free to merge after addressing Vaggelis' comments.

@georgesittas georgesittas merged commit 0d211f2 into main Dec 3, 2025
8 checks passed
@georgesittas georgesittas deleted the annotate_MODE_snowflake branch December 3, 2025 17:38
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