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

SNOW-1812881: Division broken #543

Open
jochenott opened this issue Nov 18, 2024 · 2 comments · May be fixed by #545
Open

SNOW-1812881: Division broken #543

jochenott opened this issue Nov 18, 2024 · 2 comments · May be fixed by #545
Assignees
Labels
bug Something isn't working status-pr_pending_merge A PR is made and is under review status-triage_done Initial triage done, will be further handled by the driver team

Comments

@jochenott
Copy link

Please answer these questions before submitting your issue. Thanks!

  1. What version of Python are you using? 3.12 (but irrelevant)
  2. What operating system and processor architecture are you using? macOS-14.7.1-arm64-arm-64bit (but irrelevant)
  3. What are the component versions in the environment (pip freeze)? The relevant ones are: sqlalchemy 2.0.36 and snowflake-sqlalchemy 1.6.1
  4. What did you do? Create a statement like a / b and it does not compile correctly.
from snowflake.sqlalchemy.snowdialect import SnowflakeDialect
from sqlalchemy.sql.compiler import SQLCompiler
import sqlalchemy as sa

dialect = SnowflakeDialect()

stmt = sa.Column("a") / sa.func.sqrt(sa.Column("b"))
print(str(SQLCompiler(dialect, stmt)))

This prints a / CAST(sqrt(b) AS NUMERIC). However, the cast in the denominator should not happen, and this cast leads to wrong results (!).

The fix is to set div_is_floordiv = False in SnowflakeDialect, see e.g. this as a test:

from snowflake.sqlalchemy.snowdialect import SnowflakeDialect
from sqlalchemy.sql.compiler import SQLCompiler
import sqlalchemy as sa

class FixedDialect(SnowflakeDialect):
   div_is_floordiv = False

dialect = FixedDialect()

stmt = sa.Column("a") / sa.func.sqrt(sa.Column("b"))
print(str(SQLCompiler(dialect, stmt)))

BTW: fixing div_is_floordiv also fixes the integer division operator, i.e. //. Right now, this is also wrong:

stmt = sa.Column("a", sa.Integer()) // sa.Column("b", sa.Integer())
print(str(SQLCompiler(dialect, stmt)))

currently prints "a / b". Using FixedDialect from above, it correctly prints FLOOR(a / b).

@jochenott jochenott added bug Something isn't working needs triage labels Nov 18, 2024
@github-actions github-actions bot changed the title Division broken SNOW-1812881: Division broken Nov 18, 2024
@sfc-gh-dszmolka
Copy link
Contributor

hi - thank you for submitting this issue with all these details and the possible solution too, really appreciated! we'll look

@sfc-gh-dszmolka sfc-gh-dszmolka added status-triage_done Initial triage done, will be further handled by the driver team and removed needs triage labels Nov 18, 2024
@sfc-gh-jmartinezramirez sfc-gh-jmartinezramirez added the status-in_progress Issue is worked on by the driver team label Nov 19, 2024
@sfc-gh-dszmolka
Copy link
Contributor

#545

@sfc-gh-dszmolka sfc-gh-dszmolka added status-pr_pending_merge A PR is made and is under review and removed status-in_progress Issue is worked on by the driver team labels Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working status-pr_pending_merge A PR is made and is under review status-triage_done Initial triage done, will be further handled by the driver team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants