From f9bf08a0c209e648bdd83f8c1ede620994015571 Mon Sep 17 00:00:00 2001 From: Matthew Murray Date: Tue, 23 Sep 2025 11:51:40 -0400 Subject: [PATCH 1/2] Cast inputs to true division from decimal to float --- .../cudf_polars/cudf_polars/dsl/translate.py | 21 +++++++++++++++++-- .../tests/expressions/test_numeric_binops.py | 14 +++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/python/cudf_polars/cudf_polars/dsl/translate.py b/python/cudf_polars/cudf_polars/dsl/translate.py index 842053045fe..6527c361f18 100644 --- a/python/cudf_polars/cudf_polars/dsl/translate.py +++ b/python/cudf_polars/cudf_polars/dsl/translate.py @@ -919,13 +919,30 @@ def _( dtype: DataType, schema: Schema, ) -> expr.Expr: + left = translator.translate_expr(n=node.left, schema=schema) + right = translator.translate_expr(n=node.right, schema=schema) if plc.traits.is_boolean(dtype.plc) and node.op == pl_expr.Operator.TrueDivide: dtype = DataType(pl.Float64()) + if node.op == pl_expr.Operator.TrueDivide and ( + plc.traits.is_fixed_point(left.dtype.plc) + or plc.traits.is_fixed_point(right.dtype.plc) + ): + f64 = DataType(pl.Float64()) + return expr.Cast( + dtype, + expr.BinOp( + f64, + expr.BinOp._MAPPING[node.op], + expr.Cast(f64, left), + expr.Cast(f64, right), + ), + ) + return expr.BinOp( dtype, expr.BinOp._MAPPING[node.op], - translator.translate_expr(n=node.left, schema=schema), - translator.translate_expr(n=node.right, schema=schema), + left, + right, ) diff --git a/python/cudf_polars/tests/expressions/test_numeric_binops.py b/python/cudf_polars/tests/expressions/test_numeric_binops.py index 690b4173b8c..21fe3c17457 100644 --- a/python/cudf_polars/tests/expressions/test_numeric_binops.py +++ b/python/cudf_polars/tests/expressions/test_numeric_binops.py @@ -2,6 +2,8 @@ # SPDX-License-Identifier: Apache-2.0 from __future__ import annotations +from decimal import Decimal + import pytest import polars as pl @@ -106,3 +108,15 @@ def test_true_div_boolean_column(divisor): q = df.select(pl.col("a") / divisor) assert_gpu_result_equal(q) + + +def test_true_div_with_decimals(): + df = pl.LazyFrame( + { + "foo": [Decimal("1.00"), Decimal("2.00"), Decimal("3.00"), None], + "bar": [Decimal("4.00"), Decimal("5.00"), Decimal("6.00"), Decimal("1.00")], + }, + schema={"foo": pl.Decimal(15, 2), "bar": pl.Decimal(15, 2)}, + ) + q = df.select(pl.col("bar") / pl.col("foo")) + assert_gpu_result_equal(q) From 50206a65510b9edecfcc7e2da819bd65c5ab3884 Mon Sep 17 00:00:00 2001 From: Matthew Murray Date: Wed, 24 Sep 2025 12:19:42 -0400 Subject: [PATCH 2/2] ignore dtype check --- python/cudf_polars/tests/expressions/test_numeric_binops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/cudf_polars/tests/expressions/test_numeric_binops.py b/python/cudf_polars/tests/expressions/test_numeric_binops.py index 21fe3c17457..280437def50 100644 --- a/python/cudf_polars/tests/expressions/test_numeric_binops.py +++ b/python/cudf_polars/tests/expressions/test_numeric_binops.py @@ -11,6 +11,7 @@ from cudf_polars.testing.asserts import ( assert_gpu_result_equal, ) +from cudf_polars.utils.versions import POLARS_VERSION_LT_132 dtypes = [ pl.Int8, @@ -119,4 +120,4 @@ def test_true_div_with_decimals(): schema={"foo": pl.Decimal(15, 2), "bar": pl.Decimal(15, 2)}, ) q = df.select(pl.col("bar") / pl.col("foo")) - assert_gpu_result_equal(q) + assert_gpu_result_equal(q, check_dtypes=not POLARS_VERSION_LT_132)