Skip to content

Commit 1dc80fb

Browse files
damian3031hashhar
authored andcommitted
Make Python types the default and add legacy_primitive_types config
This commit makes the behaviour of experimental_python_types enabled to be the default. It introduces a new connection parameter legacy_primitive_types which can be enabled to restore the old default behaviour of type mapping.
1 parent d7ed5e1 commit 1dc80fb

File tree

8 files changed

+107
-104
lines changed

8 files changed

+107
-104
lines changed

README.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ engine = create_engine(
116116
connect_args={
117117
"session_properties": {'query_max_run_time': '1d'},
118118
"client_tags": ["tag1", "tag2"],
119-
"experimental_python_types": True,
120119
"roles": {"catalog1": "role1"},
121120
}
122121
)
@@ -126,16 +125,14 @@ engine = create_engine(
126125
'trino://user@localhost:8080/system?'
127126
'session_properties={"query_max_run_time": "1d"}'
128127
'&client_tags=["tag1", "tag2"]'
129-
'&experimental_python_types=true'
130128
'&roles={"catalog1": "role1"}'
131129
)
132130

133131
# or using the URL factory method
134132
engine = create_engine(URL(
135133
host="localhost",
136134
port=8080,
137-
client_tags=["tag1", "tag2"],
138-
experimental_python_types=True
135+
client_tags=["tag1", "tag2"]
139136
))
140137
```
141138

@@ -440,35 +437,33 @@ The transaction is created when the first SQL statement is executed.
440437
exits the *with* context and the queries succeed, otherwise
441438
`trino.dbapi.Connection.rollback()` will be called.
442439

443-
## Improved Python types
440+
## Legacy Primitive types
444441

445-
If you enable the flag `experimental_python_types`, the client will convert the results of the query to the
442+
By default, the client will convert the results of the query to the
446443
corresponding Python types. For example, if the query returns a `DECIMAL` column, the result will be a `Decimal` object.
444+
If you want to disable this behaviour, set flag `legacy_primitive_types` to `True`.
447445

448446
Limitations of the Python types are described in the
449447
[Python types documentation](https://docs.python.org/3/library/datatypes.html). These limitations will generate an
450-
exception `trino.exceptions.DataError` if the query returns a value that cannot be converted to the corresponding Python
448+
exception `trino.exceptions.TrinoDataError` if the query returns a value that cannot be converted to the corresponding Python
451449
type.
452450

453451
```python
454452
import trino
455-
import pytz
456-
from datetime import datetime
457453

458454
conn = trino.dbapi.connect(
459-
experimental_python_types=True,
455+
legacy_primitive_types=True,
460456
...
461457
)
462458

463459
cur = conn.cursor()
464-
465-
params = datetime(2020, 1, 1, 16, 43, 22, 320000, tzinfo=pytz.timezone('America/Los_Angeles'))
466-
467-
cur.execute("SELECT ?", params=(params,))
460+
# Negative DATE cannot be represented with Python types
461+
# legacy_primitive_types needs to be enabled
462+
cur.execute("SELECT DATE '-2001-08-22'")
468463
rows = cur.fetchall()
469464

470-
assert rows[0][0] == params
471-
assert cur.description[0][1] == "timestamp with time zone"
465+
assert rows[0][0] == "-2001-08-22"
466+
assert cur.description[0][1] == "date"
472467
```
473468

474469
# Need help?

0 commit comments

Comments
 (0)