-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathexplain_test.py
More file actions
83 lines (68 loc) · 2.64 KB
/
explain_test.py
File metadata and controls
83 lines (68 loc) · 2.64 KB
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
"""Tests for EXPLAIN
These tests are using Python mainly because the output of EXPLAIN ANALYZE
contains timings, so the output is not deterministic.
"""
from .utils import Cursor
import pytest
import psycopg.errors
def test_explain(cur: Cursor):
cur.sql("CREATE TABLE test_table (id int, name text)")
result = cur.sql("EXPLAIN SELECT count(*) FROM test_table")
plan = "\n".join(result)
assert "UNGROUPED_AGGREGATE" in plan
assert "Total Time:" not in plan
assert "Output:" not in plan
result = cur.sql("EXPLAIN ANALYZE SELECT count(*) FROM test_table")
plan = "\n".join(result)
assert "Query Profiling Information" in plan
assert "UNGROUPED_AGGREGATE" in plan
assert "Total Time:" in plan
assert "Output:" not in plan
result = cur.sql("EXPLAIN SELECT count(*) FROM test_table where id = %s", (1,))
plan = "\n".join(result)
assert "UNGROUPED_AGGREGATE" in plan
assert "id=1" in plan
assert "Total Time:" not in plan
assert "Output:" not in plan
result = cur.sql(
"EXPLAIN ANALYZE SELECT count(*) FROM test_table where id = %s", (1,)
)
plan = "\n".join(result)
assert "UNGROUPED_AGGREGATE" in plan
assert "id=1" in plan
assert "Total Time:" in plan
assert "Output:" not in plan
result = cur.sql("EXPLAIN VERBOSE SELECT count(*) FROM test_table")
plan = "\n".join(result)
assert "UNGROUPED_AGGREGATE" in plan
assert "Total Time:" not in plan
assert "Output:" in plan
result = cur.sql("EXPLAIN (VERBOSE, ANALYZE) SELECT count(*) FROM test_table")
plan = "\n".join(result)
assert "Query Profiling Information" in plan
assert "UNGROUPED_AGGREGATE" in plan
assert "Total Time:" in plan
assert "Output:" in plan
def test_explain_ctas(cur: Cursor):
cur.sql("CREATE TEMP TABLE heap1(id) AS SELECT 1")
result = cur.sql("EXPLAIN CREATE TEMP TABLE heap2(id) AS SELECT * from heap1")
plan = "\n".join(result)
assert "POSTGRES_SCAN" in plan
assert "Total Time:" not in plan
result = cur.sql(
"EXPLAIN ANALYZE CREATE TEMP TABLE heap2(id) AS SELECT * from heap1"
)
plan = "\n".join(result)
assert "TABLE_SCAN" in plan
assert "Total Time:" in plan
result = cur.sql(
"EXPLAIN CREATE TEMP TABLE duckdb1(id) USING duckdb AS SELECT * from heap1"
)
plan = "\n".join(result)
assert "POSTGRES_SCAN" in plan
assert "Total Time:" not in plan
# EXPLAIN ANALYZE is not supported for DuckDB CTAS (yet)
with pytest.raises(psycopg.errors.FeatureNotSupported):
cur.sql(
"EXPLAIN ANALYZE CREATE TEMP TABLE duckdb2(id) USING duckdb AS SELECT * from heap1"
)