Skip to content

Commit

Permalink
Changed default behavior of SnowflakeDialect to disable the use of / …
Browse files Browse the repository at this point in the history
…division operator as floor div.

Changed flag div_is_floor_div to False.
  • Loading branch information
sfc-gh-jmartinezramirez committed Nov 22, 2024
1 parent 9157932 commit a76947a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/snowflake/sqlalchemy/snowdialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class SnowflakeDialect(default.DefaultDialect):
colspecs = colspecs
ischema_names = ischema_names

# target database treats the / division operator as “floor division”
div_is_floordiv = False

# all str types must be converted in Unicode
convert_unicode = True

Expand Down
30 changes: 30 additions & 0 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,33 @@ def test_outer_lateral_join():
str(stmt.compile(dialect=snowdialect.dialect()))
== "SELECT colname AS label \nFROM abc JOIN LATERAL flatten(PARSE_JSON(colname2)) AS anon_1 GROUP BY colname"
)


def test_division_operator():
col1 = column("col1")
col2 = column("col2")
stmt = col1 / col2
assert str(stmt.compile(dialect=snowdialect.dialect())) == "col1 / col2"


def test_division_operator_with_denominator_expr():
col1 = column("col1")
col2 = column("col2")
stmt = col1 / func.sqrt(col2)
assert str(stmt.compile(dialect=snowdialect.dialect())) == "col1 / sqrt(col2)"


def test_floor_division_operator():
col1 = column("col1")
col2 = column("col2")
stmt = col1 // col2
assert str(stmt.compile(dialect=snowdialect.dialect())) == "FLOOR(col1 / col2)"


def test_floor_division_operator_with_denominator_expr():
col1 = column("col1")
col2 = column("col2")
stmt = col1 // func.sqrt(col2)
assert (
str(stmt.compile(dialect=snowdialect.dialect())) == "FLOOR(col1 / sqrt(col2))"
)

0 comments on commit a76947a

Please sign in to comment.