Skip to content

Commit

Permalink
Use is_supported_operation in tests and remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
wence- committed Jul 23, 2024
1 parent 990265f commit 5e0c747
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 42 deletions.
38 changes: 1 addition & 37 deletions python/cudf_polars/cudf_polars/utils/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,7 @@

import cudf._lib.pylibcudf as plc

__all__ = ["from_polars", "downcast_arrow_lists", "have_compatible_resolution"]


def have_compatible_resolution(lid: plc.TypeId, rid: plc.TypeId):
"""
Do two datetime typeids have matching resolution for a binop.
Parameters
----------
lid
Left type id
rid
Right type id
Returns
-------
True if resolutions are compatible, False otherwise.
Notes
-----
Polars has different casting rules for combining
datetimes/durations than libcudf, and while we don't encode the
casting rules fully, just reject things we can't handle.
Precondition for correctness: both lid and rid are timelike.
"""
if lid == rid:
return True
# Timestamps are smaller than durations in the libcudf enum.
lid, rid = sorted([lid, rid])
if lid == plc.TypeId.TIMESTAMP_MILLISECONDS:
return rid == plc.TypeId.DURATION_MILLISECONDS
elif lid == plc.TypeId.TIMESTAMP_MICROSECONDS:
return rid == plc.TypeId.DURATION_MICROSECONDS
elif lid == plc.TypeId.TIMESTAMP_NANOSECONDS:
return rid == plc.TypeId.DURATION_NANOSECONDS
return False
__all__ = ["from_polars", "downcast_arrow_lists"]


def downcast_arrow_lists(typ: pa.DataType) -> pa.DataType:
Expand Down
18 changes: 13 additions & 5 deletions python/cudf_polars/tests/expressions/test_literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import polars as pl

import cudf._lib.pylibcudf as plc

from cudf_polars.testing.asserts import (
assert_gpu_result_equal,
assert_ir_translation_raises,
Expand Down Expand Up @@ -64,11 +66,17 @@ def test_timelike_literal(timestamp, timedelta):
adjusted=timestamp + timedelta,
two_delta=timedelta + timedelta,
)
schema = q.collect_schema()
time_type = schema["time"]
delta_type = schema["delta"]
if dtypes.have_compatible_resolution(
dtypes.from_polars(time_type).id(), dtypes.from_polars(delta_type).id()
schema = {k: dtypes.from_polars(v) for k, v in q.collect_schema().items()}
if plc.binaryop.is_supported_operation(
schema["adjusted"],
schema["time"],
schema["delta"],
plc.binaryop.BinaryOperator.ADD,
) and plc.binaryop.is_supported_operation(
schema["two_delta"],
schema["delta"],
schema["delta"],
plc.binaryop.BinaryOperator.ADD,
):
assert_gpu_result_equal(q)
else:
Expand Down

0 comments on commit 5e0c747

Please sign in to comment.