Skip to content

Fix #35484 - re-allow sqlalchemy declarative system to use % in read_sql. #36275

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,9 +1159,7 @@ def run_transaction(self):

def execute(self, *args, **kwargs):
"""Simple passthrough to SQLAlchemy connectable"""
return self.connectable.execution_options(no_parameters=True).execute(
*args, **kwargs
)
return self.connectable.execution_options().execute(*args, **kwargs)

def read_table(
self,
Expand Down Expand Up @@ -1291,6 +1289,11 @@ def read_query(
read_sql

"""
if isinstance(sql, str) and params is None:
from sqlalchemy.sql import text

sql = text(sql)

args = _convert_params(sql, params)

result = self.execute(*args)
Expand Down
85 changes: 85 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@
"mysql": "SELECT * FROM iris WHERE `Name` LIKE '%'",
"postgresql": "SELECT * FROM iris WHERE \"Name\" LIKE '%'",
},
"read_no_parameters_with_doublepercent": {
"sqlite": "SELECT 1 %% 2 as x",
"mysql": "SELECT 1 %% 2 as x",
"postgresql": "SELECT 1 %% 2 as x",
},
"read_parameters_with_percent": {
"sqlite": "SELECT ? as x, 2 % 3 as y",
"mysql": "SELECT %s as x, 2 % 3 as y",
"postgresql": "SELECT %s as x, 2 % 3 as y",
},
"read_parameters_with_doublepercent": {
"sqlite": "SELECT ? as x, 2 %% 3 as y",
"mysql": "SELECT %s as x, 2 %% 3 as y",
"postgresql": "SELECT %s as x, 2 %% 3 as y",
},
"read_named_parameters_with_doublepercent": {
"sqlite": "SELECT :name as x, 2 %% 3 as y",
"mysql": "SELECT %(name)s as x, 2 %% 3 as y",
"postgresql": "SELECT %(name)s as x, 2 %% 3 as y",
},
"create_view": {
"sqlite": """
CREATE VIEW iris_view AS
Expand Down Expand Up @@ -435,6 +455,43 @@ def _read_sql_iris_no_parameter_with_percent(self):
iris_frame = self.pandasSQL.read_query(query, params=None)
self._check_iris_loaded_frame(iris_frame)

def _read_sql_parameter_with_percent(self):
if self.flavor in {"postgresql", "mysql"}:
pytest.xfail("dialect DBI does not support parameters and single % ops")
query = SQL_STRINGS["read_parameters_with_percent"][self.flavor]
frame = self.pandasSQL.read_query(query, params=[1])
tm.assert_frame_equal(frame, DataFrame({"x": [1], "y": [2]}))

def _read_sql_iris_no_parameter_with_doublepercent(self):
# Note that this is marked xfail in the method wrapping it...
query = SQL_STRINGS["read_no_parameters_with_doublepercent"][self.flavor]
frame = self.pandasSQL.read_query(query)
tm.assert_frame_equal(frame, DataFrame({"x": [1]}))

def _read_sql_no_parameter_with_declarative_percent(self):
from sqlalchemy import sql

query = sql.text("SELECT 1 % 2 as x")
frame = self.pandasSQL.read_query(query)
tm.assert_frame_equal(frame, DataFrame({"x": [1]}))

@pytest.mark.xfail
def _read_sql_parameter_with_doublepercent(self):
if self.flavor == "sqlite":
pytest.xfail("sqlite DBI does not use doublepercent syntax")

query = SQL_STRINGS["read_parameters_with_doublepercent"][self.flavor]
frame = self.pandasSQL.read_query(query, params=[1])
tm.assert_frame_equal(frame, DataFrame({"x": [1], "y": [2]}))

def _read_sql_named_parameter_with_doublepercent(self):
if self.flavor == "sqlite":
pytest.xfail("sqlite DBI does not use doublepercent syntax")

query = SQL_STRINGS["read_named_parameters_with_doublepercent"][self.flavor]
frame = self.pandasSQL.read_query(query, params={"name": 1})
tm.assert_frame_equal(frame, DataFrame({"x": [1], "y": [2]}))

def _to_sql(self, method=None):
self.drop_table("test_frame1")

Expand Down Expand Up @@ -1286,6 +1343,25 @@ def test_read_sql_parameter(self):
def test_read_sql_named_parameter(self):
self._read_sql_iris_named_parameter()

def test_read_sql_no_parameter_with_percent(self):
self._read_sql_iris_no_parameter_with_percent()

@pytest.mark.xfail
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add an explict xfail / message or reason

def test_read_sql_no_parameter_with_doublepercent(self):
self._read_sql_iris_no_parameter_with_doublepercent()

def test_read_sql_no_parameter_with_declarative_percent(self):
self._read_sql_no_parameter_with_declarative_percent()

def test_read_sql_parameter_with_percent(self):
self._read_sql_parameter_with_percent()

def test_read_sql_parameter_with_doublepercent(self):
self._read_sql_parameter_with_doublepercent()

def test_read_sql_named_parameter_with_doublepercent(self):
self._read_sql_named_parameter_with_doublepercent()

def test_to_sql(self):
self._to_sql()

Expand Down Expand Up @@ -2169,6 +2245,15 @@ def test_read_sql_parameter(self):
def test_read_sql_named_parameter(self):
self._read_sql_iris_named_parameter()

def test_read_sql_parameter_with_percent(self):
self._read_sql_parameter_with_percent()

def test_read_sql_parameter_with_doublepercent(self):
self._read_sql_parameter_with_doublepercent()

def test_read_sql_named_parameter_with_doublepercent(self):
self._read_sql_named_parameter_with_doublepercent()

def test_to_sql(self):
self._to_sql()

Expand Down