Skip to content

Fix floordiv (//) for float/numeric by int with div_is_floordiv dialects#13191

Open
r266-tech wants to merge 1 commit intosqlalchemy:mainfrom
r266-tech:fix/floordiv-float-by-int
Open

Fix floordiv (//) for float/numeric by int with div_is_floordiv dialects#13191
r266-tech wants to merge 1 commit intosqlalchemy:mainfrom
r266-tech:fix/floordiv-float-by-int

Conversation

@r266-tech
Copy link
Copy Markdown

When a dialect has div_is_floordiv=True (e.g. PostgreSQL), integer / already truncates toward zero. However, visit_floordiv_binary was skipping the FLOOR() wrapper whenever the right operand was Integer, even if the left operand was Float or Numeric. This produced incorrect SQL like foo.float_col / foo.int_col instead of FLOOR(foo.float_col / foo.int_col).

Fix: only skip FLOOR() when both operands are Integer.

Before (PostgreSQL dialect):

float_col // int_col:    foo.float_col / foo.int_col       (WRONG - no floor)
numeric_col // int_col:  foo.numeric_col / foo.int_col     (WRONG - no floor)
int_col // int_col2:     foo.int_col / foo.int_col2         (correct)

After:

float_col // int_col:    FLOOR(foo.float_col / foo.int_col)  (CORRECT)
numeric_col // int_col:  FLOOR(foo.numeric_col / foo.int_col) (CORRECT)
int_col // int_col2:     foo.int_col / foo.int_col2           (still correct)

Fixes: #10528

cc @zzzeek

…loordiv dialects

When a dialect has div_is_floordiv=True (e.g. PostgreSQL), integer
division already truncates. However, the visit_floordiv_binary method
was skipping the FLOOR() wrapper whenever the *right* operand was
Integer, even if the *left* operand was Float or Numeric. This caused
incorrect SQL like float_col / int_col instead of
FLOOR(float_col / int_col).

Fix: only skip FLOOR() when *both* operands are Integer.

Fixes: sqlalchemy#10528
@zzzeek zzzeek requested a review from sqla-tester March 23, 2026 14:44
Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is sqla-tester setting up my work on behalf of zzzeek to try to get revision c9cbc47 of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Copy Markdown
Collaborator

New Gerrit review created for change c9cbc47: https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/6629

@CaselIT
Copy link
Copy Markdown
Member

CaselIT commented Mar 23, 2026

Looks ok to me

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Mar 27, 2026

hi -

some existing tests need to be adjusted for the new syntax, can you update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Floor division between decimal numerator and integer denominator is wrong

4 participants