Skip to content

Conversation

@treysp
Copy link
Collaborator

@treysp treysp commented Dec 12, 2025

PARTIALLY TRANSPILABLE: matches Snowflake for typical numeric ranges/scales, but for high-precision inputs results may differ due to floating-point promotion/rounding in DuckDB.

Snowflake supports FLOOR(x[, scale]), where:

  • flooring is always toward negative infinity
  • scale shifts the flooring position (positive scale = decimal place, negative scale = tens/hundreds, etc.)
  • non-integer scale is accepted but effectively casted to an integer before execution

DuckDB doesn’t natively support the scale arg, so:

  • We rewrite FLOOR(x, n) as FLOOR(x * POWER(10, n_int)) / POWER(10, n_int) to execute floor against the input shifted by 10^n_int (so only a single decimal place)
    • NOTE: the division operation returns a float even if both inputs are fixed-width
  • We pass that value to DuckDB’s ROUND(..., n) to address tiny accumulated floating point error

@github-actions
Copy link
Contributor

github-actions bot commented Dec 12, 2025

SQLGlot Integration Test Results

Comparing:

  • this branch (sqlglot:trey/sf-duckdb-floor, sqlglot version: trey/sf-duckdb-floor)
  • baseline (main, sqlglot version: 28.3.1.dev13)

⚠️ Limited to dialects: duckdb, snowflake

By Dialect

dialect main sqlglot:trey/sf-duckdb-floor difference links
duckdb -> duckdb 3539/4003 passed (88.4%) 3549/4003 passed (88.7%) ⬆ improved by 0.2% full result / delta
snowflake -> duckdb 307/1085 passed (28.3%) 306/1085 passed (28.2%) ⬇ regressed by 0.1% full result / delta
snowflake -> snowflake 975/1085 passed (89.9%) 975/1085 passed (89.9%) No change full result / delta

Overall

main: 6173 total, 4821 passed (pass rate: 78.1%), sqlglot version: 28.3.1.dev13

sqlglot:trey/sf-duckdb-floor: 6173 total, 4830 passed (pass rate: 78.2%), sqlglot version: trey/sf-duckdb-floor

Difference: ⬆ improved by 0.1%

@treysp treysp force-pushed the trey/sf-duckdb-floor branch from 0715ee5 to b5a58f4 Compare December 15, 2025 15:54
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.

Seems reasonable.

@treysp treysp merged commit 6fa5e9c into main Dec 15, 2025
9 checks passed
@treysp treysp deleted the trey/sf-duckdb-floor branch December 15, 2025 17:54
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