Skip to content

Commit af24c8e

Browse files
authored
fix(compiler): respect order of ops in FloorDivide (#10353)
1 parent 11b4e3a commit af24c8e

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

ibis/backends/sql/compilers/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ def visit_Clip(self, op, *, arg, lower, upper):
847847
return arg
848848

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

852852
def visit_Ceil(self, op, *, arg):
853853
return self.cast(self.f.ceil(arg), op.dtype)

ibis/backends/sql/compilers/flink.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def visit_TryCast(self, op, *, arg, to):
330330
return sge.TryCast(this=arg, to=type_mapper.from_ibis(to))
331331

332332
def visit_FloorDivide(self, op, *, left, right):
333-
return self.f.floor(left / right)
333+
return self.f.floor(sge.paren(left) / sge.paren(right))
334334

335335
def visit_JSONGetItem(self, op, *, arg, index):
336336
assert isinstance(op.index, ops.Literal)

ibis/backends/tests/test_numeric.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,13 @@ def test_simple_math_functions_columns(
902902
backend.assert_series_equal(result, expected)
903903

904904

905+
def test_floor_divide_precedence(con):
906+
# Check that we compile to 16 / (4 / 2) and not 16 / 4 / 2
907+
expr = ibis.literal(16) // (4 / ibis.literal(2))
908+
result = int(con.execute(expr))
909+
assert result == 8
910+
911+
905912
# we add one to double_col in this test to make sure the common case works (no
906913
# domain errors), and we test the backends' various failure modes in each
907914
# backend's test suite

0 commit comments

Comments
 (0)