Skip to content

Commit

Permalink
feat(bigquery): add BIGNUMERIC type support
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-kwitt authored and cpcloud committed Jan 28, 2023
1 parent 4a04c9b commit 5c98ea4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
15 changes: 9 additions & 6 deletions ibis/backends/bigquery/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ def trans_type(t):

@ibis_type_to_bigquery_type.register(dt.Decimal)
def trans_numeric(t):
if (t.precision, t.scale) not in [(38, 9), (None, None)]:
raise TypeError(
"BigQuery only supports decimal types with precision of 38 and "
"scale of 9"
)
return "NUMERIC"
if (t.precision, t.scale) == (76, 38):
return 'BIGNUMERIC'
if (t.precision, t.scale) in [(38, 9), (None, None)]:
return "NUMERIC"
raise TypeError(
"BigQuery only supports decimal types with precision of 38 and "
f"scale of 9 (NUMERIC) or precision of 76 and scale of 38 (BIGNUMERIC). "
f"Current precision: {t.precision}. Current scale: {t.scale}"
)


@ibis_type_to_bigquery_type.register(dt.JSON)
Expand Down
3 changes: 2 additions & 1 deletion ibis/backends/bigquery/tests/unit/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def test_no_ambiguities():
param(
"array<struct<a: string>>", "ARRAY<STRUCT<a STRING>>", id="array<struct>"
),
param(dt.Decimal(38, 9), "NUMERIC", id="decimal"),
param(dt.Decimal(38, 9), "NUMERIC", id="decimal-numeric"),
param(dt.Decimal(76, 38), "BIGNUMERIC", id="decimal-bignumeric"),
],
)
def test_simple(datatype, expected):
Expand Down
34 changes: 34 additions & 0 deletions ibis/backends/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,40 @@ def test_exists(batting, awards_players, method_name):
],
id="decimal-big",
),
param(
ibis.literal(decimal.Decimal("1.1"), type=dt.Decimal(76, 38)),
{
'bigquery': "BIGNUMERIC",
'snowflake': "VARCHAR",
'sqlite': "real",
'trino': 'decimal(2,1)',
"duckdb": "DECIMAL(18,3)",
"postgres": "numeric",
},
marks=[
pytest.mark.broken(
['clickhouse'],
"Code: 46. DB::Exception: Unknown function Decimal: "
"While processing toTypeName(Decimal('1.2')).",
raises=ClickhouseDriverOperationalError,
),
pytest.mark.broken(
['impala'],
"impala.error.HiveServer2Error: AnalysisException: Syntax error in line 1:"
"SELECT typeof(Decimal('1.2')) AS `TypeOf(Decimal('1.2'))"
"Encountered: DECIMAL"
"Expected: ALL, CASE, CAST, DEFAULT, DISTINCT, EXISTS, FALSE, IF, "
"INTERVAL, LEFT, NOT, NULL, REPLACE, RIGHT, TRUNCATE, TRUE, IDENTIFIER"
"CAUSED BY: Exception: Syntax error",
),
pytest.mark.broken(
['duckdb'],
"(duckdb.ParserException) Parser Error: Width must be between 1 and 38!",
raises=sqlalchemy.exc.ProgrammingError,
),
],
id="decimal-big",
),
param(
ibis.array([1.0, 2.0, 3.0]),
{
Expand Down

0 comments on commit 5c98ea4

Please sign in to comment.