Skip to content

Commit cedffec

Browse files
aalbuhashhar
authored andcommitted
Fix caching for parameterized statement execution mode
1 parent 232e58d commit cedffec

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

tests/integration/test_dbapi_integration.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212
import math
13+
import time as t
1314
import uuid
1415
from datetime import date, datetime, time, timedelta, timezone
1516
from decimal import Decimal
@@ -27,7 +28,7 @@
2728
import trino
2829
from tests.integration.conftest import trino_version
2930
from trino import constants
30-
from trino.dbapi import Cursor, DescribeOutput
31+
from trino.dbapi import Cursor, DescribeOutput, TimeBoundLRUCache
3132
from trino.exceptions import NotSupportedError, TrinoQueryError, TrinoUserError
3233
from trino.transaction import IsolationLevel
3334

@@ -1782,6 +1783,41 @@ def test_rowcount_insert(trino_connection):
17821783
assert cur.rowcount == 1
17831784

17841785

1786+
@pytest.mark.parametrize(
1787+
"legacy_prepared_statements",
1788+
[
1789+
True,
1790+
pytest.param(False, marks=pytest.mark.skipif(
1791+
trino_version() <= '417',
1792+
reason="EXECUTE IMMEDIATE was introduced in version 418")),
1793+
None
1794+
]
1795+
)
1796+
def test_prepared_statement_capability_autodetection(legacy_prepared_statements, run_trino):
1797+
# start with an empty cache
1798+
trino.dbapi.must_use_legacy_prepared_statements = TimeBoundLRUCache(1024, 3600)
1799+
user_name = f"user_{t.monotonic_ns()}"
1800+
1801+
_, host, port = run_trino
1802+
connection = trino.dbapi.Connection(
1803+
host=host,
1804+
port=port,
1805+
user=user_name,
1806+
legacy_prepared_statements=legacy_prepared_statements,
1807+
)
1808+
cur = connection.cursor()
1809+
cur.execute("SELECT ?", [42])
1810+
cur.fetchall()
1811+
another = connection.cursor()
1812+
another.execute("SELECT ?", [100])
1813+
another.fetchall()
1814+
1815+
verify = connection.cursor()
1816+
rows = verify.execute("SELECT query FROM system.runtime.queries WHERE user = ?", [user_name])
1817+
statements = [stmt for row in rows for stmt in row]
1818+
assert statements.count("EXECUTE IMMEDIATE 'SELECT 1'") == (1 if legacy_prepared_statements is None else 0)
1819+
1820+
17851821
def get_cursor(legacy_prepared_statements, run_trino):
17861822
_, host, port = run_trino
17871823

trino/dbapi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,13 @@ def _use_legacy_prepared_statements(self):
283283
self._create_request(),
284284
query="EXECUTE IMMEDIATE 'SELECT 1'")
285285
query.execute()
286+
value = False
286287
except Exception as e:
287288
logger.warning(
288289
"EXECUTE IMMEDIATE not available for %s:%s; defaulting to legacy prepared statements (%s)",
289290
self.host, self.port, e)
290291
value = True
291-
must_use_legacy_prepared_statements.put((self.host, self.port), value)
292+
must_use_legacy_prepared_statements.put((self.host, self.port), value)
292293
return value
293294

294295

0 commit comments

Comments
 (0)