-
Notifications
You must be signed in to change notification settings - Fork 151
/
test_dbapi_integration.py
1866 lines (1479 loc) · 57.1 KB
/
test_dbapi_integration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import time as t
import uuid
from datetime import date, datetime, time, timedelta, timezone
from decimal import Decimal
from typing import Tuple
try:
from zoneinfo import ZoneInfo
except ModuleNotFoundError:
from backports.zoneinfo import ZoneInfo
import pytest
import requests
from tzlocal import get_localzone_name # type: ignore
import trino
from tests.integration.conftest import trino_version
from trino import constants
from trino.dbapi import Cursor, DescribeOutput, TimeBoundLRUCache
from trino.exceptions import NotSupportedError, TrinoQueryError, TrinoUserError
from trino.transaction import IsolationLevel
@pytest.fixture
def trino_connection(run_trino):
_, host, port = run_trino
yield trino.dbapi.Connection(
host=host, port=port, user="test", source="test", max_attempts=1
)
@pytest.fixture
def trino_connection_with_transaction(run_trino):
_, host, port = run_trino
yield trino.dbapi.Connection(
host=host,
port=port,
user="test",
source="test",
max_attempts=1,
isolation_level=IsolationLevel.READ_UNCOMMITTED,
)
@pytest.fixture
def trino_connection_in_autocommit(run_trino):
_, host, port = run_trino
yield trino.dbapi.Connection(
host=host,
port=port,
user="test",
source="test",
max_attempts=1,
isolation_level=IsolationLevel.AUTOCOMMIT,
)
def test_select_query(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT * FROM system.runtime.nodes")
rows = cur.fetchall()
assert len(rows) > 0
row = rows[0]
if trino_version() == "latest":
assert row[2] is not None
else:
assert row[2] == trino_version()
columns = dict([desc[:2] for desc in cur.description])
assert columns["node_id"] == "varchar"
assert columns["http_uri"] == "varchar"
assert columns["node_version"] == "varchar"
assert columns["coordinator"] == "boolean"
assert columns["state"] == "varchar"
assert cur.query_id is not None
assert cur.query == "SELECT * FROM system.runtime.nodes"
assert cur.stats is not None
def test_select_query_result_iteration(trino_connection):
cur0 = trino_connection.cursor()
cur0.execute("SELECT custkey FROM tpch.sf1.customer LIMIT 10")
rows0 = cur0.genall()
cur1 = trino_connection.cursor()
cur1.execute("SELECT custkey FROM tpch.sf1.customer LIMIT 10")
rows1 = cur1.fetchall()
assert len(list(rows0)) == len(rows1)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_select_query_result_iteration_statement_params(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute(
"""
SELECT * FROM (
values
(1, 'one', 'a'),
(2, 'two', 'b'),
(3, 'three', 'c'),
(4, 'four', 'd'),
(5, 'five', 'e')
) x (id, name, letter)
WHERE id >= ?
""",
params=(3,) # expecting all the rows with id >= 3
)
rows = cur.fetchall()
assert len(rows) == 3
assert [3, 'three', 'c'] in rows
assert [4, 'four', 'd'] in rows
assert [5, 'five', 'e'] in rows
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_none_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute("SELECT ?", params=(None,))
rows = cur.fetchall()
assert rows[0][0] is None
assert_cursor_description(cur, trino_type="unknown")
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_string_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute("SELECT ?", params=("six'",))
rows = cur.fetchall()
assert rows[0][0] == "six'"
assert_cursor_description(cur, trino_type="varchar(4)", size=4)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_execute_many(legacy_prepared_statements, run_trino):
try:
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute("CREATE TABLE memory.default.test_execute_many (key int, value varchar)")
cur.fetchall()
operation = "INSERT INTO memory.default.test_execute_many (key, value) VALUES (?, ?)"
cur.executemany(operation, [(1, "value1")])
cur.fetchall()
cur.execute("SELECT * FROM memory.default.test_execute_many ORDER BY key")
rows = cur.fetchall()
assert len(list(rows)) == 1
assert rows[0] == [1, "value1"]
operation = "INSERT INTO memory.default.test_execute_many (key, value) VALUES (?, ?)"
cur.executemany(operation, [(2, "value2"), (3, "value3")])
cur.fetchall()
cur.execute("SELECT * FROM memory.default.test_execute_many ORDER BY key")
rows = cur.fetchall()
assert len(list(rows)) == 3
assert rows[0] == [1, "value1"]
assert rows[1] == [2, "value2"]
assert rows[2] == [3, "value3"]
finally:
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute("DROP TABLE IF EXISTS memory.default.test_execute_many")
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_execute_many_without_params(legacy_prepared_statements, run_trino):
try:
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute("CREATE TABLE memory.default.test_execute_many_without_param (value varchar)")
cur.fetchall()
with pytest.raises(TrinoUserError) as e:
cur.executemany("INSERT INTO memory.default.test_execute_many_without_param (value) VALUES (?)", [])
cur.fetchall()
assert "Incorrect number of parameters: expected 1 but found 0" in str(e.value)
finally:
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute("DROP TABLE IF EXISTS memory.default.test_execute_many_without_param")
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_execute_many_select(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
with pytest.raises(NotSupportedError) as e:
cur.executemany("SELECT ?, ?", [(1, "value1"), (2, "value2")])
assert "Query must return update type" in str(e.value)
@pytest.mark.parametrize("connection_legacy_primitive_types", [None, True, False])
@pytest.mark.parametrize("cursor_legacy_primitive_types", [None, True, False])
def test_legacy_primitive_types_with_connection_and_cursor(
connection_legacy_primitive_types,
cursor_legacy_primitive_types,
run_trino
):
_, host, port = run_trino
connection = trino.dbapi.Connection(
host=host,
port=port,
user="test",
legacy_primitive_types=connection_legacy_primitive_types,
)
cur = connection.cursor(legacy_primitive_types=cursor_legacy_primitive_types)
# If legacy_primitive_types is passed to cursor, take value from it.
# If not, take value from legacy_primitive_types passed to connection.
# If legacy_primitive_types is not passed to cursor nor connection, default to False.
if cursor_legacy_primitive_types is not None:
expected_legacy_primitive_types = cursor_legacy_primitive_types
elif connection_legacy_primitive_types is not None:
expected_legacy_primitive_types = connection_legacy_primitive_types
else:
expected_legacy_primitive_types = False
test_query = """
SELECT
DECIMAL '0.142857',
DATE '2018-01-01',
TIMESTAMP '2019-01-01 00:00:00.000+01:00',
TIMESTAMP '2019-01-01 00:00:00.000 UTC',
TIMESTAMP '2019-01-01 00:00:00.000',
TIME '00:00:00.000'
"""
# Check values which cannot be represented by Python types
if expected_legacy_primitive_types:
test_query += """
,DATE '-2001-08-22'
"""
cur.execute(test_query)
rows = cur.fetchall()
if not expected_legacy_primitive_types:
assert len(rows[0]) == 6
assert rows[0][0] == Decimal('0.142857')
assert rows[0][1] == date(2018, 1, 1)
assert rows[0][2] == datetime(2019, 1, 1, tzinfo=timezone(timedelta(hours=1)))
assert rows[0][3] == datetime(2019, 1, 1, tzinfo=ZoneInfo('UTC'))
assert rows[0][4] == datetime(2019, 1, 1)
assert rows[0][5] == time(0, 0, 0, 0)
else:
for value in rows[0]:
assert isinstance(value, str)
assert len(rows[0]) == 7
assert rows[0][0] == '0.142857'
assert rows[0][1] == '2018-01-01'
assert rows[0][2] == '2019-01-01 00:00:00.000 +01:00'
assert rows[0][3] == '2019-01-01 00:00:00.000 UTC'
assert rows[0][4] == '2019-01-01 00:00:00.000'
assert rows[0][5] == '00:00:00.000'
assert rows[0][6] == '-2001-08-22'
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_decimal_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute("SELECT ?", params=(Decimal('1112.142857'),))
rows = cur.fetchall()
assert rows[0][0] == Decimal('1112.142857')
assert_cursor_description(cur, trino_type="decimal(10, 6)", precision=10, scale=6)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_decimal_scientific_notation_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute("SELECT ?", params=(Decimal('0E-10'),))
rows = cur.fetchall()
assert rows[0][0] == Decimal('0E-10')
assert_cursor_description(cur, trino_type="decimal(10, 10)", precision=10, scale=10)
# Ensure we don't convert to floats
assert Decimal('0.1') == Decimal('1E-1') != 0.1
cur.execute("SELECT ?", params=(Decimal('1E-1'),))
rows = cur.fetchall()
assert rows[0][0] == Decimal('1E-1')
assert_cursor_description(cur, trino_type="decimal(1, 1)", precision=1, scale=1)
def test_null_decimal(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT CAST(NULL AS DECIMAL)")
rows = cur.fetchall()
assert rows[0][0] is None
assert_cursor_description(cur, trino_type="decimal(38, 0)", precision=38, scale=0)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_biggest_decimal(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = Decimal('99999999999999999999999999999999999999')
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
assert_cursor_description(cur, trino_type="decimal(38, 0)", precision=38, scale=0)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_smallest_decimal(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = Decimal('-99999999999999999999999999999999999999')
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
assert_cursor_description(cur, trino_type="decimal(38, 0)", precision=38, scale=0)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_highest_precision_decimal(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = Decimal('0.99999999999999999999999999999999999999')
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
assert_cursor_description(cur, trino_type="decimal(38, 38)", precision=38, scale=38)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_datetime_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = datetime(2020, 1, 1, 16, 43, 22, 320000)
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
assert_cursor_description(cur, trino_type="timestamp(6)", precision=6)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_datetime_with_utc_time_zone_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = datetime(2020, 1, 1, 16, 43, 22, 320000, tzinfo=ZoneInfo('UTC'))
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
assert_cursor_description(cur, trino_type="timestamp(6) with time zone", precision=6)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_datetime_with_numeric_offset_time_zone_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
tz = timezone(-timedelta(hours=5, minutes=30))
params = datetime(2020, 1, 1, 16, 43, 22, 320000, tzinfo=tz)
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
assert_cursor_description(cur, trino_type="timestamp(6) with time zone", precision=6)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_datetime_with_named_time_zone_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = datetime(2020, 1, 1, 16, 43, 22, 320000, tzinfo=ZoneInfo('America/Los_Angeles'))
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
assert_cursor_description(cur, trino_type="timestamp(6) with time zone", precision=6)
def test_datetime_with_trailing_zeros(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT TIMESTAMP '2001-08-22 03:04:05.321000'")
rows = cur.fetchall()
assert rows[0][0] == datetime.strptime("2001-08-22 03:04:05.321000", "%Y-%m-%d %H:%M:%S.%f")
assert_cursor_description(cur, trino_type="timestamp(6)", precision=6)
def test_null_datetime_with_time_zone(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT CAST(NULL AS TIMESTAMP WITH TIME ZONE)")
rows = cur.fetchall()
assert rows[0][0] is None
assert_cursor_description(cur, trino_type="timestamp(3) with time zone", precision=3)
def test_datetime_with_time_zone_numeric_offset(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT TIMESTAMP '2001-08-22 03:04:05.321 -08:00'")
rows = cur.fetchall()
assert rows[0][0] == datetime.strptime("2001-08-22 03:04:05.321 -08:00", "%Y-%m-%d %H:%M:%S.%f %z")
assert_cursor_description(cur, trino_type="timestamp(3) with time zone", precision=3)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_datetimes_with_time_zone_in_dst_gap_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
# This is a datetime that lies within a DST transition and not actually exists.
params = datetime(2021, 3, 28, 2, 30, 0, tzinfo=ZoneInfo('Europe/Brussels'))
with pytest.raises(trino.exceptions.TrinoUserError):
cur.execute("SELECT ?", params=(params,))
cur.fetchall()
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
@pytest.mark.parametrize('fold', [0, 1])
def test_doubled_datetimes(fold, legacy_prepared_statements, run_trino):
# Trino doesn't distinguish between doubled datetimes that lie within a DST transition.
# See also https://github.com/trinodb/trino/issues/5781
cur = get_cursor(legacy_prepared_statements, run_trino)
params = datetime(2002, 10, 27, 1, 30, 0, tzinfo=ZoneInfo('US/Eastern'), fold=fold)
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == datetime(2002, 10, 27, 1, 30, 0, tzinfo=ZoneInfo('US/Eastern'))
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_date_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = datetime(2020, 1, 1, 0, 0, 0).date()
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
assert_cursor_description(cur, trino_type="date")
def test_null_date(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT CAST(NULL AS DATE)")
rows = cur.fetchall()
assert rows[0][0] is None
assert_cursor_description(cur, trino_type="date")
def test_unsupported_python_dates(trino_connection):
cur = trino_connection.cursor()
# dates below python min (1-1-1) or above max date (9999-12-31) are not supported
for unsupported_date in [
'-0001-01-01',
'0000-01-01',
'10000-01-01',
'-4999999-01-01', # Trino min date
'5000000-12-31', # Trino max date
]:
with pytest.raises(trino.exceptions.TrinoDataError):
cur.execute(f"SELECT DATE '{unsupported_date}'")
cur.fetchall()
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_supported_special_dates_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
for params in (
# min python date
date(1, 1, 1),
# before julian->gregorian switch
date(1500, 1, 1),
# During julian->gregorian switch
date(1752, 9, 4),
# before epoch
date(1952, 4, 3),
date(1970, 1, 1),
date(1970, 2, 3),
# summer on northern hemisphere (possible DST)
date(2017, 7, 1),
# winter on northern hemisphere (possible DST on southern hemisphere)
date(2017, 1, 1),
# winter on southern hemisphere (possible DST on northern hemisphere)
date(2017, 12, 31),
date(1983, 4, 1),
date(1983, 10, 1),
# max python date
date(9999, 12, 31),
):
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
def test_char(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT CHAR 'trino'")
rows = cur.fetchall()
assert rows[0][0] == 'trino'
assert_cursor_description(cur, trino_type="char(5)", size=5)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_time_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = time(12, 3, 44, 333000)
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
assert_cursor_description(cur, trino_type="time(6)", precision=6)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_time_with_named_time_zone_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = time(16, 43, 22, 320000, tzinfo=ZoneInfo('Asia/Shanghai'))
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
# Asia/Shanghai
assert rows[0][0].tzinfo == timezone(timedelta(seconds=28800))
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_time_with_numeric_offset_time_zone_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
tz = timezone(-timedelta(hours=8, minutes=0))
params = time(16, 43, 22, 320000, tzinfo=tz)
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
def test_time(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT TIME '01:02:03.456'")
rows = cur.fetchall()
assert rows[0][0] == time(1, 2, 3, 456000)
assert_cursor_description(cur, trino_type="time(3)", precision=3)
def test_null_time(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT CAST(NULL AS TIME)")
rows = cur.fetchall()
assert rows[0][0] is None
assert_cursor_description(cur, trino_type="time(3)", precision=3)
def test_time_with_time_zone_negative_offset(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT TIME '01:02:03.456 -08:00'")
rows = cur.fetchall()
tz = timezone(-timedelta(hours=8, minutes=0))
assert rows[0][0] == time(1, 2, 3, 456000, tzinfo=tz)
assert_cursor_description(cur, trino_type="time(3) with time zone", precision=3)
def test_time_with_time_zone_positive_offset(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT TIME '01:02:03.456 +08:00'")
rows = cur.fetchall()
tz = timezone(timedelta(hours=8, minutes=0))
assert rows[0][0] == time(1, 2, 3, 456000, tzinfo=tz)
assert_cursor_description(cur, trino_type="time(3) with time zone", precision=3)
def test_null_date_with_time_zone(trino_connection):
cur = trino_connection.cursor()
cur.execute("SELECT CAST(NULL AS TIME WITH TIME ZONE)")
rows = cur.fetchall()
assert rows[0][0] is None
assert_cursor_description(cur, trino_type="time(3) with time zone", precision=3)
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
@pytest.mark.parametrize(
"binary_input",
[
bytearray("a", "utf-8"),
bytearray("a", "ascii"),
bytearray(b'\x00\x00\x00\x00'),
bytearray(4),
bytearray([1, 2, 3]),
],
)
def test_binary_query_param(binary_input, legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute("SELECT ?", params=(binary_input,))
rows = cur.fetchall()
assert rows[0][0] == binary_input
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_array_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
cur.execute("SELECT ?", params=([1, 2, 3],))
rows = cur.fetchall()
assert rows[0][0] == [1, 2, 3]
cur.execute("SELECT ?", params=([[1, 2, 3], [4, 5, 6]],))
rows = cur.fetchall()
assert rows[0][0] == [[1, 2, 3], [4, 5, 6]]
cur.execute("SELECT TYPEOF(?)", params=([1, 2, 3],))
rows = cur.fetchall()
assert rows[0][0] == "array(integer)"
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_array_none_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = [None, None]
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
cur.execute("SELECT TYPEOF(?)", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == "array(unknown)"
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_array_none_and_another_type_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = [None, 1]
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
cur.execute("SELECT TYPEOF(?)", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == "array(integer)"
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_array_timestamp_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = [datetime(2020, 1, 1, 0, 0, 0), datetime(2020, 1, 2, 0, 0, 0)]
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
cur.execute("SELECT TYPEOF(?)", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == "array(timestamp(6))"
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,
pytest.param(False, marks=pytest.mark.skipif(
trino_version() <= '417',
reason="EXECUTE IMMEDIATE was introduced in version 418")),
None
]
)
def test_array_timestamp_with_timezone_query_param(legacy_prepared_statements, run_trino):
cur = get_cursor(legacy_prepared_statements, run_trino)
params = [
datetime(2020, 1, 1, 0, 0, 0, tzinfo=ZoneInfo('UTC')),
datetime(2020, 1, 2, 0, 0, 0, tzinfo=ZoneInfo('UTC')),
]
cur.execute("SELECT ?", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == params
cur.execute("SELECT TYPEOF(?)", params=(params,))
rows = cur.fetchall()
assert rows[0][0] == "array(timestamp(6) with time zone)"
@pytest.mark.parametrize(
"legacy_prepared_statements",
[
True,