Skip to content

BigQuery NUMERIC transpiled to DuckDB DECIMAL loses precision #6583

@nikchha

Description

@nikchha

Hello :) and another issue:

BigQuery's NUMERIC defaults to NUMERIC(38, 9). When transpiling to DuckDB, the output DECIMAL without precision defaults to DECIMAL(18, 3), causing precision loss.

Fully reproducible code snippet

import sqlglot
result = sqlglot.transpile(
    "SELECT CAST(x AS NUMERIC) FROM t",
    read="bigquery",
    write="duckdb"
)[0]
print(result)

Output:

SELECT CAST(x AS DECIMAL) FROM t

Expected:

SELECT CAST(x AS DECIMAL(38, 9)) FROM t

Why current output is wrong

BigQuery NUMERIC preserves 9 decimal places. DuckDB DECIMAL without precision defaults to 3 decimal places.

-- BigQuery:
SELECT CAST(1.123456789 AS NUMERIC);
-- Returns: 1.123456789
-- DuckDB with current transpilation:
SELECT CAST(1.123456789 AS DECIMAL);
-- Returns: 1.123 (precision loss)
-- DuckDB with expected transpilation:
SELECT CAST(1.123456789 AS DECIMAL(38, 9));
-- Returns: 1.123456789

Official Documentation

Version

Tested on main (28.4.2.dev1)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions