Skip to content

Conversation

@treysp
Copy link
Collaborator

@treysp treysp commented Nov 18, 2025

Notes:

  • Bigquery does not document the specific algorithm they use, so results may differ from DuckDB (which uses t-digest)
  • DuckDB does not support the BQ RESPECT NULLS option

def _parse_approx_quantiles(self) -> t.Optional[exp.Expression]:
# APPROX_QUANTILES([DISTINCT] expression, number [{IGNORE | RESPECT} NULLS])
distinct = self._match(TokenType.DISTINCT)
this = self._parse_assignment()
Copy link
Owner

Choose a reason for hiding this comment

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

why did you choose parse_assignment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

user error - i was walking through the parsing steps for a subquery, hit _parse_assignment for a where and incorrectly used that more broadly

should this be _parse_expression or _parse_lambda?

Copy link
Owner

Choose a reason for hiding this comment

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

is a lambda allowed here? or an alias or a disjunction? most likely you want bitwise, but do your research

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it accepts TRUE OR FALSE, so will use _parse_disjunction

distinct = self._match(TokenType.DISTINCT)
this = self._parse_assignment()
if distinct:
this = self.expression(exp.Distinct, expressions=[this])
Copy link
Owner

Choose a reason for hiding this comment

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

this should probably be an argument to approx_quantiles as opposed to first class distinct node, @georgesittas what do you think

"Expected comma between expression and number of quantiles in APPROX_QUANTILES"
)

expression = self._parse_assignment()
Copy link
Owner

Choose a reason for hiding this comment

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

ditto

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

user error - i was walking through the parsing steps for a subquery, hit _parse_assignment for a where and incorrectly used that more broadly

should this be _parse_expression or _parse_lambda?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this one accepts only positive integer literals and expr like CAST(2.1 AS INTEGER)

_parse_type aligns with that (but is strict and enforces number positivity)

i'll relax up to _parse_bitwise

@treysp treysp force-pushed the trey/approx-quantiles branch from b25bcd4 to 726e0df Compare November 20, 2025 19:24
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.

3 participants