Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): respect order of ops in FloorDivide #10353

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ibis/backends/sql/compilers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def visit_Clip(self, op, *, arg, lower, upper):
return arg

def visit_FloorDivide(self, op, *, left, right):
return self.cast(self.f.floor(left / right), op.dtype)
return self.cast(self.f.floor(sge.paren(left) / sge.paren(right)), op.dtype)

def visit_Ceil(self, op, *, arg):
return self.cast(self.f.ceil(arg), op.dtype)
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/sql/compilers/flink.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
return sge.TryCast(this=arg, to=type_mapper.from_ibis(to))

def visit_FloorDivide(self, op, *, left, right):
return self.f.floor(left / right)
return self.f.floor(sge.paren(left) / sge.paren(right))

Check warning on line 333 in ibis/backends/sql/compilers/flink.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/sql/compilers/flink.py#L333

Added line #L333 was not covered by tests

def visit_JSONGetItem(self, op, *, arg, index):
assert isinstance(op.index, ops.Literal)
Expand Down
7 changes: 7 additions & 0 deletions ibis/backends/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,13 @@ def test_simple_math_functions_columns(
backend.assert_series_equal(result, expected)


def test_floor_divide_precedence(con):
# Check that we compile to 16 / (4 / 2) and not 16 / 4 / 2
expr = ibis.literal(16) // (4 / ibis.literal(2))
result = int(con.execute(expr))
assert result == 8


# we add one to double_col in this test to make sure the common case works (no
# domain errors), and we test the backends' various failure modes in each
# backend's test suite
Expand Down