From d18742106c0c57e6d505c320635396bc8475dcdf Mon Sep 17 00:00:00 2001 From: Juan Martinez Ramirez Date: Fri, 22 Nov 2024 18:08:55 -0600 Subject: [PATCH] Added warning for use of div_is_floor_div with `True` value. Added tests to validate results of true and floor divisions using `force_div_is_floordiv` flag. --- src/snowflake/sqlalchemy/base.py | 10 ++++++++++ tests/test_core.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/snowflake/sqlalchemy/base.py b/src/snowflake/sqlalchemy/base.py index 02e4f741..8b29dd43 100644 --- a/src/snowflake/sqlalchemy/base.py +++ b/src/snowflake/sqlalchemy/base.py @@ -5,6 +5,7 @@ import itertools import operator import re +import warnings from typing import List from sqlalchemy import exc as sa_exc @@ -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("\\", "\\\\") diff --git a/tests/test_core.py b/tests/test_core.py index 63f097db..574b4d04 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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 @@ -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)], + )