Skip to content

Commit

Permalink
fix(bigquery): truncate when casting float to int
Browse files Browse the repository at this point in the history
BREAKING CHANGE: when using the bigquery backend, casting float to int
will no longer round floats to the nearest integer
  • Loading branch information
sudonhim authored and cpcloud committed May 2, 2023
1 parent 34a0c59 commit 267d8e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ibis/backends/bigquery/registry.py
Expand Up @@ -56,6 +56,12 @@ def bigquery_cast_interval_to_integer(compiled_arg, from_, to):
return f"EXTRACT({from_.resolution.upper()} from {compiled_arg})"


@bigquery_cast.register(str, dt.Floating, dt.Integer)
def bigquery_cast_floating_to_integer(compiled_arg, from_, to):
"""Convert FLOAT64 to INT64 without rounding."""
return f"CAST(TRUNC({compiled_arg}) AS INT64)"


@bigquery_cast.register(str, dt.DataType, dt.DataType)
def bigquery_cast_generate(compiled_arg, from_, to):
"""Cast to desired type."""
Expand Down
6 changes: 6 additions & 0 deletions ibis/backends/bigquery/tests/system/test_client.py
Expand Up @@ -96,6 +96,12 @@ def test_cast_string_to_date(alltypes, df):
tm.assert_series_equal(result, expected)


def test_cast_float_to_int(alltypes, df):
result = (alltypes.float_col - 2.55).cast("int64").to_pandas().sort_values()
expected = (df.float_col - 2.55).astype("int64").sort_values()
tm.assert_series_equal(result, expected, check_names=False)


def test_has_partitions(alltypes, parted_alltypes, con):
col = con.partition_column
assert col not in alltypes.columns
Expand Down

0 comments on commit 267d8e1

Please sign in to comment.