Skip to content

Commit

Permalink
Added warning for use of div_is_floor_div with True value.
Browse files Browse the repository at this point in the history
Added tests to validate results of true and floor divisions using `force_div_is_floordiv` flag.
  • Loading branch information
sfc-gh-jmartinezramirez committed Nov 23, 2024
1 parent 5578bd9 commit 85f5fd0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/snowflake/sqlalchemy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import itertools
import operator
import re
import warnings
from typing import List

from sqlalchemy import exc as sa_exc
Expand Down Expand Up @@ -799,6 +800,15 @@ def visit_join(self, join, asfrom=False, from_linter=None, **kwargs):
+ join.onclause._compiler_dispatch(self, from_linter=from_linter, **kwargs)
)

def visit_floordiv_binary(self, binary, operator, **kw):
if self.dialect.div_is_floordiv and IS_VERSION_20:
warnings.warn(
"div_is_floordiv value will be changed to False in a future release. This will generate a behavior change on true and floor division. Please review https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#python-division-operator-performs-true-division-for-all-backends-added-floor-division",
PendingDeprecationWarning,
stacklevel=2,
)
return super().visit_floordiv_binary(binary, operator, **kw)

def render_literal_value(self, value, type_):
# escape backslash
return super().render_literal_value(value, type_).replace("\\", "\\\\")
Expand Down
4 changes: 4 additions & 0 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def test_outer_lateral_join():
)


@pytest.mark.feature_v20
def test_division_operator_with_force_div_is_floordiv_false():
col1 = column("col1", Integer)
col2 = column("col2", Integer)
Expand All @@ -134,6 +135,7 @@ def test_division_operator_with_force_div_is_floordiv_false():
)


@pytest.mark.feature_v20
def test_division_operator_with_denominator_expr_force_div_is_floordiv_false():
col1 = column("col1", Integer)
col2 = column("col2", Integer)
Expand All @@ -144,6 +146,7 @@ def test_division_operator_with_denominator_expr_force_div_is_floordiv_false():
)


@pytest.mark.feature_v20
def test_division_operator_with_force_div_is_floordiv_default_true():
col1 = column("col1", Integer)
col2 = column("col2", Integer)
Expand All @@ -153,6 +156,7 @@ def test_division_operator_with_force_div_is_floordiv_default_true():
)


@pytest.mark.feature_v20
def test_division_operator_with_denominator_expr_force_div_is_floordiv_default_true():
col1 = column("col1", Integer)
col2 = column("col2", Integer)
Expand Down
31 changes: 30 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
text,
)
from sqlalchemy.exc import DBAPIError, NoSuchTableError, OperationalError
from sqlalchemy.sql import and_, not_, or_, select
from sqlalchemy.sql import and_, literal, not_, or_, select
from sqlalchemy.sql.ddl import CreateTable
from sqlalchemy.testing.assertions import eq_

import snowflake.connector.errors
import snowflake.sqlalchemy.snowdialect
Expand Down Expand Up @@ -1863,3 +1864,31 @@ def test_snowflake_sqlalchemy_as_valid_client_type():
snowflake.connector.connection.DEFAULT_CONFIGURATION[
"internal_application_version"
] = origin_internal_app_version


@pytest.mark.feature_v20
def test_division_force_div_is_floordiv_default():
engine = create_engine(URL(**CONNECTION_PARAMETERS))
expected_warning = "div_is_floordiv value will be changed to False in a future release. This will generate a behavior change on true and floor division. Please review https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#python-division-operator-performs-true-division-for-all-backends-added-floor-division"
with pytest.warns(PendingDeprecationWarning, match=expected_warning):
with engine.connect() as conn:
eq_(
conn.execute(
select(literal(5) / literal(10), literal(5) // literal(10))
).fetchall(),
[(0.5, 0.5)],
)


@pytest.mark.feature_v20
def test_division_force_div_is_floordiv_false():
engine = create_engine(
URL(**CONNECTION_PARAMETERS), **{"force_div_is_floordiv": False}
)
with engine.connect() as conn:
eq_(
conn.execute(
select(literal(5) / literal(10), literal(5) // literal(10))
).fetchall(),
[(0.5, 0)],
)

0 comments on commit 85f5fd0

Please sign in to comment.