jupyter-duckdb 1.2.0.1__py3-none-any.whl → 1.4.111__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- duckdb_kernel/db/Connection.py +3 -0
- duckdb_kernel/db/Table.py +8 -0
- duckdb_kernel/db/implementation/duckdb/Connection.py +27 -13
- duckdb_kernel/db/implementation/postgres/Connection.py +27 -12
- duckdb_kernel/db/implementation/sqlite/Connection.py +9 -3
- duckdb_kernel/kernel.py +407 -200
- duckdb_kernel/magics/MagicCommand.py +34 -10
- duckdb_kernel/magics/MagicCommandCallback.py +11 -7
- duckdb_kernel/magics/MagicCommandHandler.py +58 -9
- duckdb_kernel/magics/MagicState.py +11 -0
- duckdb_kernel/magics/__init__.py +1 -0
- duckdb_kernel/parser/DCParser.py +17 -7
- duckdb_kernel/parser/LogicParser.py +6 -6
- duckdb_kernel/parser/ParserError.py +18 -0
- duckdb_kernel/parser/RAParser.py +29 -21
- duckdb_kernel/parser/__init__.py +1 -0
- duckdb_kernel/parser/elements/DCOperand.py +7 -4
- duckdb_kernel/parser/elements/LogicElement.py +0 -2
- duckdb_kernel/parser/elements/RAElement.py +4 -1
- duckdb_kernel/parser/elements/RARelationReference.py +86 -0
- duckdb_kernel/parser/elements/RAUnaryOperator.py +6 -0
- duckdb_kernel/parser/elements/__init__.py +2 -1
- duckdb_kernel/parser/elements/binary/And.py +1 -1
- duckdb_kernel/parser/elements/binary/ConditionalSet.py +37 -10
- duckdb_kernel/parser/elements/binary/Cross.py +2 -2
- duckdb_kernel/parser/elements/binary/Difference.py +1 -1
- duckdb_kernel/parser/elements/binary/Divide.py +1 -1
- duckdb_kernel/parser/elements/binary/Division.py +0 -4
- duckdb_kernel/parser/elements/binary/FullOuterJoin.py +40 -0
- duckdb_kernel/parser/elements/binary/Join.py +4 -1
- duckdb_kernel/parser/elements/binary/LeftOuterJoin.py +27 -0
- duckdb_kernel/parser/elements/binary/LeftSemiJoin.py +27 -0
- duckdb_kernel/parser/elements/binary/RightOuterJoin.py +27 -0
- duckdb_kernel/parser/elements/binary/RightSemiJoin.py +27 -0
- duckdb_kernel/parser/elements/binary/__init__.py +21 -6
- duckdb_kernel/parser/elements/unary/AttributeRename.py +39 -0
- duckdb_kernel/parser/elements/unary/Projection.py +1 -1
- duckdb_kernel/parser/elements/unary/Rename.py +68 -14
- duckdb_kernel/parser/elements/unary/__init__.py +2 -0
- duckdb_kernel/parser/tokenizer/Token.py +24 -3
- duckdb_kernel/parser/util/QuerySplitter.py +87 -0
- duckdb_kernel/parser/util/RenamableColumnList.py +10 -2
- duckdb_kernel/tests/__init__.py +76 -0
- duckdb_kernel/tests/test_dc.py +483 -0
- duckdb_kernel/tests/test_ra.py +1966 -0
- duckdb_kernel/tests/test_result_comparison.py +173 -0
- duckdb_kernel/tests/test_sql.py +48 -0
- duckdb_kernel/util/ResultSetComparator.py +22 -4
- duckdb_kernel/util/SQL.py +6 -0
- duckdb_kernel/util/TestError.py +4 -0
- duckdb_kernel/visualization/Plotly.py +144 -0
- duckdb_kernel/visualization/RATreeDrawer.py +34 -2
- duckdb_kernel/visualization/__init__.py +1 -0
- duckdb_kernel/visualization/lib/__init__.py +53 -0
- duckdb_kernel/visualization/lib/plotly-3.0.1.min.js +3879 -0
- duckdb_kernel/visualization/lib/ra.css +3 -0
- duckdb_kernel/visualization/lib/ra.js +55 -0
- {jupyter_duckdb-1.2.0.1.dist-info → jupyter_duckdb-1.4.111.dist-info}/METADATA +53 -19
- jupyter_duckdb-1.4.111.dist-info/RECORD +104 -0
- {jupyter_duckdb-1.2.0.1.dist-info → jupyter_duckdb-1.4.111.dist-info}/WHEEL +1 -1
- jupyter_duckdb-1.2.0.1.dist-info/RECORD +0 -82
- {jupyter_duckdb-1.2.0.1.dist-info → jupyter_duckdb-1.4.111.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from typing import Iterator
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def split_queries(query: str, split_at: str | None = ';', remove_comments: bool = False) -> Iterator[str]:
|
|
5
|
+
quotes = '\'"`'
|
|
6
|
+
|
|
7
|
+
escaped = False
|
|
8
|
+
in_quotes = None
|
|
9
|
+
in_singleline_comment = False
|
|
10
|
+
in_multiline_comment = False
|
|
11
|
+
|
|
12
|
+
previous = None
|
|
13
|
+
current_query = []
|
|
14
|
+
|
|
15
|
+
for symbol in query:
|
|
16
|
+
keep_symbol = True
|
|
17
|
+
|
|
18
|
+
# escaped symbol
|
|
19
|
+
if escaped:
|
|
20
|
+
escaped = False
|
|
21
|
+
|
|
22
|
+
# backslash (escape)
|
|
23
|
+
elif symbol == '\\':
|
|
24
|
+
escaped = True
|
|
25
|
+
|
|
26
|
+
# if in quotes
|
|
27
|
+
elif in_quotes is not None:
|
|
28
|
+
if symbol == in_quotes:
|
|
29
|
+
in_quotes = False
|
|
30
|
+
|
|
31
|
+
# if in single line comment
|
|
32
|
+
elif in_singleline_comment:
|
|
33
|
+
if symbol == '\n':
|
|
34
|
+
in_singleline_comment = False
|
|
35
|
+
elif remove_comments:
|
|
36
|
+
keep_symbol = False
|
|
37
|
+
|
|
38
|
+
# if in multiline comment
|
|
39
|
+
elif in_multiline_comment:
|
|
40
|
+
if previous == '*' and symbol == '/':
|
|
41
|
+
in_multiline_comment = False
|
|
42
|
+
|
|
43
|
+
if remove_comments:
|
|
44
|
+
keep_symbol = False
|
|
45
|
+
|
|
46
|
+
# start of quotes
|
|
47
|
+
elif symbol in quotes:
|
|
48
|
+
in_quotes = symbol
|
|
49
|
+
|
|
50
|
+
# start of single line comment
|
|
51
|
+
elif previous == '-' and symbol == '-':
|
|
52
|
+
in_singleline_comment = True
|
|
53
|
+
|
|
54
|
+
if remove_comments:
|
|
55
|
+
keep_symbol = False
|
|
56
|
+
current_query.pop()
|
|
57
|
+
|
|
58
|
+
# start of multiline comment
|
|
59
|
+
elif previous == '/' and symbol == '*':
|
|
60
|
+
in_multiline_comment = True
|
|
61
|
+
|
|
62
|
+
if remove_comments:
|
|
63
|
+
keep_symbol = False
|
|
64
|
+
current_query.pop()
|
|
65
|
+
|
|
66
|
+
# semicolon
|
|
67
|
+
elif split_at is not None and symbol == split_at:
|
|
68
|
+
yield ''.join(current_query)
|
|
69
|
+
|
|
70
|
+
current_query = []
|
|
71
|
+
keep_symbol = False
|
|
72
|
+
|
|
73
|
+
# store symbol
|
|
74
|
+
if keep_symbol:
|
|
75
|
+
current_query.append(symbol)
|
|
76
|
+
|
|
77
|
+
previous = symbol
|
|
78
|
+
|
|
79
|
+
# yield remaining symbols
|
|
80
|
+
yield ''.join(current_query)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def get_last_query(query: str, split_at: str | None = ';', remove_comments: bool = False) -> str:
|
|
84
|
+
for query in split_queries(query, split_at, remove_comments):
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
return query
|
|
@@ -73,18 +73,26 @@ class RenamableColumnList(list[RenamableColumn]):
|
|
|
73
73
|
|
|
74
74
|
return RenamableColumnList(cols.values())
|
|
75
75
|
|
|
76
|
-
def intersect(self, other: 'RenamableColumnList') \
|
|
76
|
+
def intersect(self, other: 'RenamableColumnList', prefer_right: bool = False) \
|
|
77
77
|
-> Tuple[List[Tuple[RenamableColumn, RenamableColumn]], 'RenamableColumnList']:
|
|
78
78
|
self_cols: Dict[str, RenamableColumn] = {col.name: col for col in self}
|
|
79
79
|
other_cols: Dict[str, RenamableColumn] = {col.name: col for col in other}
|
|
80
80
|
|
|
81
|
+
replacements: Dict[RenamableColumn, RenamableColumn] = {}
|
|
81
82
|
intersection: List[Tuple[RenamableColumn, RenamableColumn]] = []
|
|
83
|
+
|
|
82
84
|
for name in tuple(self_cols.keys()):
|
|
83
85
|
if name in other_cols:
|
|
86
|
+
if prefer_right:
|
|
87
|
+
replacements[self_cols[name]] = other_cols[name]
|
|
88
|
+
|
|
84
89
|
intersection.append((self_cols[name], other_cols[name]))
|
|
85
90
|
del other_cols[name]
|
|
86
91
|
|
|
87
92
|
# if len(intersection) == 0:
|
|
88
93
|
# raise AssertionError('no common attributes found for join')
|
|
89
94
|
|
|
90
|
-
return intersection, RenamableColumnList(
|
|
95
|
+
return intersection, RenamableColumnList(
|
|
96
|
+
(replacements.get(x, x) for x in self_cols.values()),
|
|
97
|
+
other_cols.values()
|
|
98
|
+
)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from typing import Dict, List, Tuple
|
|
3
|
+
|
|
4
|
+
from duckdb_kernel.db import Connection as DB, Table
|
|
5
|
+
from duckdb_kernel.db.error import EmptyResultError
|
|
6
|
+
from duckdb_kernel.parser.elements import RAElement
|
|
7
|
+
from duckdb_kernel.parser.elements.binary import ConditionalSet
|
|
8
|
+
|
|
9
|
+
with open('examples/tables.sql', 'r', encoding='utf-8') as file:
|
|
10
|
+
EXAMPLE_STMTS = [stmt
|
|
11
|
+
for stmt in file.read().split(';')
|
|
12
|
+
if stmt.strip()]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Connection:
|
|
16
|
+
def __enter__(self):
|
|
17
|
+
db_type = os.environ.get('DB_TYPE')
|
|
18
|
+
if db_type == 'postgres':
|
|
19
|
+
host = os.environ.get('POSTGRES_HOST', 'localhost')
|
|
20
|
+
port = int(os.environ.get('POSTGRES_PORT', 5432))
|
|
21
|
+
username = os.environ.get('POSTGRES_USER', 'postgres')
|
|
22
|
+
password = os.environ.get('POSTGRES_PASSWORD', 'postgres')
|
|
23
|
+
|
|
24
|
+
from duckdb_kernel.db.implementation.postgres import Connection as PostgreSQL
|
|
25
|
+
self.con: DB = PostgreSQL(host, port, username, password, None)
|
|
26
|
+
elif db_type == 'sqlite':
|
|
27
|
+
from duckdb_kernel.db.implementation.sqlite import Connection as SQLite
|
|
28
|
+
self.con: DB = SQLite(':memory:')
|
|
29
|
+
else:
|
|
30
|
+
from duckdb_kernel.db.implementation.duckdb import Connection as DuckDB
|
|
31
|
+
self.con: DB = DuckDB(':memory:')
|
|
32
|
+
|
|
33
|
+
for stmt in EXAMPLE_STMTS:
|
|
34
|
+
try:
|
|
35
|
+
self.con.execute(stmt)
|
|
36
|
+
except EmptyResultError:
|
|
37
|
+
pass
|
|
38
|
+
|
|
39
|
+
return self
|
|
40
|
+
|
|
41
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
42
|
+
self.con.close()
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def tables(self) -> Dict[str, Table]:
|
|
46
|
+
return self.con.analyze()
|
|
47
|
+
|
|
48
|
+
def execute_sql_return_cols(self, query: str) -> Tuple[List, List]:
|
|
49
|
+
return self.con.execute(query)
|
|
50
|
+
|
|
51
|
+
def execute_sql(self, query: str) -> List:
|
|
52
|
+
_, rows = self.execute_sql_return_cols(query)
|
|
53
|
+
return rows
|
|
54
|
+
|
|
55
|
+
def execute_ra_return_cols(self, root: RAElement) -> Tuple[List, List]:
|
|
56
|
+
sql = root.to_sql_with_renamed_columns(self.tables)
|
|
57
|
+
cols, rows = self.execute_sql_return_cols(sql)
|
|
58
|
+
|
|
59
|
+
return cols, rows # sorted(rows, key=lambda t: tuple(-1 if x is None else x for x in t))
|
|
60
|
+
|
|
61
|
+
def execute_ra(self, root: RAElement) -> List:
|
|
62
|
+
_, rows = self.execute_ra_return_cols(root)
|
|
63
|
+
return rows
|
|
64
|
+
|
|
65
|
+
def execute_dc_return_cols(self, root: ConditionalSet) -> Tuple[List, List]:
|
|
66
|
+
sql, cnm = root.to_sql_with_renamed_columns(self.tables)
|
|
67
|
+
cols, rows = self.execute_sql_return_cols(sql)
|
|
68
|
+
|
|
69
|
+
return [cnm.get(c, c) for c in cols], rows # sorted(rows)
|
|
70
|
+
|
|
71
|
+
def execute_dc(self, root: ConditionalSet) -> List:
|
|
72
|
+
_, rows = self.execute_dc_return_cols(root)
|
|
73
|
+
return rows
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
__all__ = ['Connection']
|
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
from duckdb_kernel.parser import DCParser
|
|
2
|
+
from . import Connection
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def test_case_insensitivity():
|
|
6
|
+
for query in (
|
|
7
|
+
'{ username | users(id, username) }',
|
|
8
|
+
'{ username | Users(id, username) }',
|
|
9
|
+
'{ username | USERS(id, username) }',
|
|
10
|
+
'{ username | uSers(id, username) }',
|
|
11
|
+
):
|
|
12
|
+
root = DCParser.parse_query(query)
|
|
13
|
+
|
|
14
|
+
# execute to test case insensitivity
|
|
15
|
+
with Connection() as con:
|
|
16
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
17
|
+
|
|
18
|
+
assert [c.lower() for c in cols] == [
|
|
19
|
+
'username'
|
|
20
|
+
]
|
|
21
|
+
assert rows == [
|
|
22
|
+
('Alice',),
|
|
23
|
+
('Bob',),
|
|
24
|
+
('Charlie',)
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
for query in (
|
|
28
|
+
'{ username | users(id, username) }',
|
|
29
|
+
'{ Username | users(id, username) }',
|
|
30
|
+
'{ USERNAME | users(id, username) }',
|
|
31
|
+
'{ userName | users(id, username) }',
|
|
32
|
+
):
|
|
33
|
+
root = DCParser.parse_query(query)
|
|
34
|
+
|
|
35
|
+
# execute to test case insensitivity
|
|
36
|
+
with Connection() as con:
|
|
37
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
38
|
+
|
|
39
|
+
assert [c.lower() for c in cols] == [
|
|
40
|
+
'username'
|
|
41
|
+
]
|
|
42
|
+
assert rows == [
|
|
43
|
+
('Alice',),
|
|
44
|
+
('Bob',),
|
|
45
|
+
('Charlie',)
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_comments():
|
|
50
|
+
for query in (
|
|
51
|
+
'{ username | users(id, username) } -- cmmnt',
|
|
52
|
+
'{ username | users(id, username) } /* cmmnt */',
|
|
53
|
+
'{ username | /* cmmnt */ users(id, username) }',
|
|
54
|
+
):
|
|
55
|
+
root = DCParser.parse_query(query)
|
|
56
|
+
|
|
57
|
+
# execute to test case insensitivity
|
|
58
|
+
with Connection() as con:
|
|
59
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
60
|
+
|
|
61
|
+
assert [c.lower() for c in cols] == [
|
|
62
|
+
'username'
|
|
63
|
+
]
|
|
64
|
+
assert rows == [
|
|
65
|
+
('Alice',),
|
|
66
|
+
('Bob',),
|
|
67
|
+
('Charlie',)
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
for query in (
|
|
71
|
+
'-- comment',
|
|
72
|
+
'/* comment */'
|
|
73
|
+
):
|
|
74
|
+
root = DCParser.parse_query(query)
|
|
75
|
+
assert root is None
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_simple_queries():
|
|
79
|
+
with Connection() as con:
|
|
80
|
+
for query in [
|
|
81
|
+
'{ id | Users(id, _) }',
|
|
82
|
+
'{ id | Users (id, _) }',
|
|
83
|
+
'{ id | Users id,_ }',
|
|
84
|
+
'{id|Users(id,_)}',
|
|
85
|
+
'id | Users (id ,_)',
|
|
86
|
+
]:
|
|
87
|
+
root = DCParser.parse_query(query)
|
|
88
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
89
|
+
|
|
90
|
+
assert [c.lower() for c in cols] == [
|
|
91
|
+
'id'
|
|
92
|
+
]
|
|
93
|
+
assert rows == [
|
|
94
|
+
(1,),
|
|
95
|
+
(2,),
|
|
96
|
+
(3,)
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
for query in [
|
|
100
|
+
'{ id, name | Users(id, name) }',
|
|
101
|
+
'{ id,name | Users(id, name) }'
|
|
102
|
+
]:
|
|
103
|
+
root = DCParser.parse_query(query)
|
|
104
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
105
|
+
|
|
106
|
+
assert [c.lower() for c in cols] == [
|
|
107
|
+
'id',
|
|
108
|
+
'name'
|
|
109
|
+
]
|
|
110
|
+
assert rows == [
|
|
111
|
+
(1, 'Alice'),
|
|
112
|
+
(2, 'Bob'),
|
|
113
|
+
(3, 'Charlie')
|
|
114
|
+
]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def test_asterisk_projection():
|
|
118
|
+
with Connection() as con:
|
|
119
|
+
root = DCParser.parse_query('{ * | Users(id, _) }')
|
|
120
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
121
|
+
|
|
122
|
+
assert [c.lower() for c in cols] == [
|
|
123
|
+
'id'
|
|
124
|
+
]
|
|
125
|
+
assert rows == [
|
|
126
|
+
(1,),
|
|
127
|
+
(2,),
|
|
128
|
+
(3,)
|
|
129
|
+
]
|
|
130
|
+
|
|
131
|
+
root = DCParser.parse_query('{ * | Users(id, name) }')
|
|
132
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
133
|
+
|
|
134
|
+
assert [c.lower() for c in cols] == [
|
|
135
|
+
'id',
|
|
136
|
+
'name'
|
|
137
|
+
]
|
|
138
|
+
assert rows == [
|
|
139
|
+
(1, 'Alice'),
|
|
140
|
+
(2, 'Bob'),
|
|
141
|
+
(3, 'Charlie')
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def test_conditions():
|
|
146
|
+
with Connection() as con:
|
|
147
|
+
for query in [
|
|
148
|
+
'{ name | Users(id, name) ∧ id > 1 }',
|
|
149
|
+
'{ name | Users(id, name) ∧ id ≠ 1 }',
|
|
150
|
+
'{ name | Users(id, name) ∧ (id = 2 ∨ id = 3) }'
|
|
151
|
+
]:
|
|
152
|
+
root = DCParser.parse_query(query)
|
|
153
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
154
|
+
|
|
155
|
+
assert [c.lower() for c in cols] == [
|
|
156
|
+
'name'
|
|
157
|
+
]
|
|
158
|
+
assert rows == [
|
|
159
|
+
('Bob',),
|
|
160
|
+
('Charlie',)
|
|
161
|
+
]
|
|
162
|
+
|
|
163
|
+
for query in [
|
|
164
|
+
'{ id | Users(id, name) ∧ name = "Bob" }',
|
|
165
|
+
'{ id | Users(id, name) ∧ name > "B" ∧ name < "C" }',
|
|
166
|
+
]:
|
|
167
|
+
root = DCParser.parse_query(query)
|
|
168
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
169
|
+
|
|
170
|
+
assert [c.lower() for c in cols] == [
|
|
171
|
+
'id'
|
|
172
|
+
]
|
|
173
|
+
assert rows == [
|
|
174
|
+
(2,)
|
|
175
|
+
]
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def test_shortcut_conditions():
|
|
179
|
+
with Connection() as con:
|
|
180
|
+
# single shortcut conditions
|
|
181
|
+
for query in [
|
|
182
|
+
'{ name | Users(1, name) }'
|
|
183
|
+
]:
|
|
184
|
+
root = DCParser.parse_query(query)
|
|
185
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
186
|
+
|
|
187
|
+
assert [c.lower() for c in cols] == [
|
|
188
|
+
'name'
|
|
189
|
+
]
|
|
190
|
+
assert rows == [
|
|
191
|
+
('Alice',)
|
|
192
|
+
]
|
|
193
|
+
|
|
194
|
+
for query in [
|
|
195
|
+
'{ season_name | Seasons(1, 1, season_name) }'
|
|
196
|
+
]:
|
|
197
|
+
root = DCParser.parse_query(query)
|
|
198
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
199
|
+
|
|
200
|
+
assert [c.lower() for c in cols] == [
|
|
201
|
+
'season_name'
|
|
202
|
+
]
|
|
203
|
+
assert rows == [
|
|
204
|
+
('Show 1 / Season 1',)
|
|
205
|
+
]
|
|
206
|
+
|
|
207
|
+
# multiple shortcut conditions
|
|
208
|
+
for query in [
|
|
209
|
+
'{ sname, ename | Seasons(snum, 2, sname) ∧ Episodes(enum, snum, 2, ename) }'
|
|
210
|
+
]:
|
|
211
|
+
root = DCParser.parse_query(query)
|
|
212
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
213
|
+
|
|
214
|
+
assert [c.lower() for c in cols] == [
|
|
215
|
+
'sname',
|
|
216
|
+
'ename'
|
|
217
|
+
]
|
|
218
|
+
assert rows == [
|
|
219
|
+
('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 1'),
|
|
220
|
+
('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 2'),
|
|
221
|
+
('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 3'),
|
|
222
|
+
('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 1'),
|
|
223
|
+
('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 2'),
|
|
224
|
+
('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 3'),
|
|
225
|
+
('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 4')
|
|
226
|
+
]
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def test_joins():
|
|
230
|
+
with Connection() as con:
|
|
231
|
+
# with one attribute
|
|
232
|
+
for query in [
|
|
233
|
+
'{ sename | Shows(shid, shname) ∧ Seasons(senum, shid, sename) }'
|
|
234
|
+
]:
|
|
235
|
+
root = DCParser.parse_query(query)
|
|
236
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
237
|
+
|
|
238
|
+
assert [c.lower() for c in cols] == [
|
|
239
|
+
'sename'
|
|
240
|
+
]
|
|
241
|
+
assert rows == [
|
|
242
|
+
('Show 1 / Season 1',),
|
|
243
|
+
('Show 1 / Season 2',),
|
|
244
|
+
('Show 2 / Season 1',),
|
|
245
|
+
('Show 2 / Season 2',)
|
|
246
|
+
]
|
|
247
|
+
|
|
248
|
+
for query in [
|
|
249
|
+
'{ sename | Shows(shid, shname) ∧ Seasons(senum, shid, sename) ∧ shname = "Show 1" }',
|
|
250
|
+
'{ sename | Seasons(senum, shid, sename) ∧ Shows(shid, "Show 1") }'
|
|
251
|
+
]:
|
|
252
|
+
root = DCParser.parse_query(query)
|
|
253
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
254
|
+
|
|
255
|
+
assert [c.lower() for c in cols] == [
|
|
256
|
+
'sename'
|
|
257
|
+
]
|
|
258
|
+
assert rows == [
|
|
259
|
+
('Show 1 / Season 1',),
|
|
260
|
+
('Show 1 / Season 2',)
|
|
261
|
+
]
|
|
262
|
+
|
|
263
|
+
# with multiple attributes
|
|
264
|
+
for query in [
|
|
265
|
+
'{ sname, ename | Seasons(snum, shid, sname) ∧ Episodes(enum, snum, shid, ename) ∧ shid = 2 }'
|
|
266
|
+
]:
|
|
267
|
+
root = DCParser.parse_query(query)
|
|
268
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
269
|
+
|
|
270
|
+
assert [c.lower() for c in cols] == [
|
|
271
|
+
'sname',
|
|
272
|
+
'ename'
|
|
273
|
+
]
|
|
274
|
+
assert rows == [
|
|
275
|
+
('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 1'),
|
|
276
|
+
('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 2'),
|
|
277
|
+
('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 3'),
|
|
278
|
+
('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 1'),
|
|
279
|
+
('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 2'),
|
|
280
|
+
('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 3'),
|
|
281
|
+
('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 4')
|
|
282
|
+
]
|
|
283
|
+
|
|
284
|
+
# join three relations
|
|
285
|
+
for query in [
|
|
286
|
+
'{ s2,c5 | Shows(s1,s2) ∧ Episodes(e1,e2,s1,e4) ∧ Characters(c1,e1,c3,s1,c5) ∧ s1=2 ∧ e4="Show 2 / Season 1 / Episode 2" }'
|
|
287
|
+
]:
|
|
288
|
+
root = DCParser.parse_query(query)
|
|
289
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
290
|
+
|
|
291
|
+
assert [c.lower() for c in cols] == [
|
|
292
|
+
's2',
|
|
293
|
+
'c5'
|
|
294
|
+
]
|
|
295
|
+
assert rows == [
|
|
296
|
+
('Show 2', 'Actor F')
|
|
297
|
+
]
|
|
298
|
+
|
|
299
|
+
# cross join
|
|
300
|
+
root = DCParser.parse_query('{ sename | Shows(shid1, shname) ∧ Seasons(senum, shid2, sename) ∧ shid1 = shid2 }')
|
|
301
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
302
|
+
|
|
303
|
+
assert [c.lower() for c in cols] == [
|
|
304
|
+
'sename'
|
|
305
|
+
]
|
|
306
|
+
assert rows == [
|
|
307
|
+
('Show 1 / Season 1',),
|
|
308
|
+
('Show 1 / Season 2',),
|
|
309
|
+
('Show 2 / Season 1',),
|
|
310
|
+
('Show 2 / Season 2',)
|
|
311
|
+
]
|
|
312
|
+
|
|
313
|
+
for query in [
|
|
314
|
+
'{ s2,c5 | Shows(sa1,s2) ∧ Episodes(e1,e2,sb1,e4) ∧ Characters(c1,e1,c3,sb1,c5) ∧ sa1=2 ∧ sa1 = sb1 ∧ e4="Show 2 / Season 1 / Episode 2" }',
|
|
315
|
+
'{ s2,c5 | Shows(sa1,s2) ∧ Episodes(e1,e2,sb1,e4) ∧ Characters(c1,e1,c3,sc1,c5) ∧ sa1=2 ∧ sa1 = sb1 ∧ sb1 = sc1 ∧ e4="Show 2 / Season 1 / Episode 2" }'
|
|
316
|
+
]:
|
|
317
|
+
root = DCParser.parse_query(query)
|
|
318
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
319
|
+
|
|
320
|
+
assert [c.lower() for c in cols] == [
|
|
321
|
+
's2',
|
|
322
|
+
'c5'
|
|
323
|
+
]
|
|
324
|
+
assert rows == [
|
|
325
|
+
('Show 2', 'Actor F')
|
|
326
|
+
]
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def test_disjunction_joins():
|
|
330
|
+
with Connection() as con:
|
|
331
|
+
for query in [
|
|
332
|
+
"{ enum, snum, sid | Episodes(enum, snum, sid, ename) ∧ (Characters('Character B', enum, snum, sid, _) ∨ Characters('Character D', enum, snum, sid, _)) }",
|
|
333
|
+
"{ enum, snum, sid | Episodes(enum, snum, sid, ename) ∧ (Characters(cname1, enum, snum, sid, _) ∧ cname1 = 'Character B' ∨ Characters(cname2, enum, snum, sid, _) ∧ cname2 = 'Character D') }",
|
|
334
|
+
]:
|
|
335
|
+
root = DCParser.parse_query(query)
|
|
336
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
337
|
+
|
|
338
|
+
assert [c.lower() for c in cols] == [
|
|
339
|
+
'enum',
|
|
340
|
+
'snum',
|
|
341
|
+
'sid'
|
|
342
|
+
]
|
|
343
|
+
assert rows == [
|
|
344
|
+
(1, 1, 1),
|
|
345
|
+
(2, 1, 1)
|
|
346
|
+
]
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def test_cross_join():
|
|
350
|
+
with Connection() as con:
|
|
351
|
+
for query in [
|
|
352
|
+
"{ cname1, cname2 | Characters(cname1, _, _, 2, _) ∧ Characters(cname2, _, _, 2, _) }",
|
|
353
|
+
]:
|
|
354
|
+
root = DCParser.parse_query(query)
|
|
355
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
356
|
+
|
|
357
|
+
assert [c.lower() for c in cols] == [
|
|
358
|
+
'cname1',
|
|
359
|
+
'cname2'
|
|
360
|
+
]
|
|
361
|
+
assert rows == [
|
|
362
|
+
('Character E', 'Character E'),
|
|
363
|
+
('Character E', 'Character F'),
|
|
364
|
+
('Character F', 'Character E'),
|
|
365
|
+
('Character F', 'Character F'),
|
|
366
|
+
]
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def test_underscores():
|
|
370
|
+
with Connection() as con:
|
|
371
|
+
# distinct underscores
|
|
372
|
+
for query in [
|
|
373
|
+
'{ ename | Seasons(snum, shid, sname) ∧ Episodes(_, snum, shid, ename) ∧ shid = 2 }',
|
|
374
|
+
'{ ename | Seasons(snum, shid, sname) ∧ Episodes(enum, _, shid, ename) ∧ shid = 2 }',
|
|
375
|
+
'{ ename | Seasons(snum, shid, sname) ∧ Episodes(__, snum, shid, ename) ∧ shid = 2 }',
|
|
376
|
+
'{ ename | Seasons(snum, shid, sname) ∧ Episodes(_, __, shid, ename) ∧ shid = 2 }'
|
|
377
|
+
]:
|
|
378
|
+
root = DCParser.parse_query(query)
|
|
379
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
380
|
+
|
|
381
|
+
assert [c.lower() for c in cols] == [
|
|
382
|
+
'ename'
|
|
383
|
+
]
|
|
384
|
+
assert rows == [
|
|
385
|
+
('Show 2 / Season 1 / Episode 1',),
|
|
386
|
+
('Show 2 / Season 1 / Episode 2',),
|
|
387
|
+
('Show 2 / Season 1 / Episode 3',),
|
|
388
|
+
('Show 2 / Season 2 / Episode 1',),
|
|
389
|
+
('Show 2 / Season 2 / Episode 2',),
|
|
390
|
+
('Show 2 / Season 2 / Episode 3',),
|
|
391
|
+
('Show 2 / Season 2 / Episode 4',)
|
|
392
|
+
]
|
|
393
|
+
|
|
394
|
+
# reused underscores in a single relation
|
|
395
|
+
for query in [
|
|
396
|
+
'{ ename | Seasons(snum, shid, sname) ∧ Episodes(_, _, shid, ename) ∧ shid = 2 }'
|
|
397
|
+
]:
|
|
398
|
+
root = DCParser.parse_query(query)
|
|
399
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
400
|
+
|
|
401
|
+
assert [c.lower() for c in cols] == [
|
|
402
|
+
'ename'
|
|
403
|
+
]
|
|
404
|
+
assert rows == [
|
|
405
|
+
('Show 2 / Season 1 / Episode 1',),
|
|
406
|
+
('Show 2 / Season 1 / Episode 2',),
|
|
407
|
+
('Show 2 / Season 1 / Episode 3',),
|
|
408
|
+
('Show 2 / Season 2 / Episode 1',),
|
|
409
|
+
('Show 2 / Season 2 / Episode 2',),
|
|
410
|
+
('Show 2 / Season 2 / Episode 3',),
|
|
411
|
+
('Show 2 / Season 2 / Episode 4',)
|
|
412
|
+
]
|
|
413
|
+
|
|
414
|
+
# reused underscores in two different relations
|
|
415
|
+
for query in [
|
|
416
|
+
'{ ename | Seasons(_, shid, _) ∧ Episodes(_, _, shid, ename) ∧ shid = 2 }',
|
|
417
|
+
'{ ename | Seasons(_, shid, __) ∧ Episodes(_, __, shid, ename) ∧ shid = 2 }'
|
|
418
|
+
]:
|
|
419
|
+
root = DCParser.parse_query(query)
|
|
420
|
+
cols, rows = con.execute_dc_return_cols(root)
|
|
421
|
+
|
|
422
|
+
assert [c.lower() for c in cols] == [
|
|
423
|
+
'ename'
|
|
424
|
+
]
|
|
425
|
+
assert rows == [
|
|
426
|
+
('Show 2 / Season 1 / Episode 1',),
|
|
427
|
+
('Show 2 / Season 1 / Episode 2',),
|
|
428
|
+
('Show 2 / Season 1 / Episode 3',),
|
|
429
|
+
('Show 2 / Season 2 / Episode 1',),
|
|
430
|
+
('Show 2 / Season 2 / Episode 2',),
|
|
431
|
+
('Show 2 / Season 2 / Episode 3',),
|
|
432
|
+
('Show 2 / Season 2 / Episode 4',)
|
|
433
|
+
]
|
|
434
|
+
|
|
435
|
+
def test_anonymous_column_names():
|
|
436
|
+
with Connection() as con:
|
|
437
|
+
for query in [
|
|
438
|
+
'{ * | Episodes(_, _, 2, ename) }',
|
|
439
|
+
]:
|
|
440
|
+
root = DCParser.parse_query(query)
|
|
441
|
+
cols, _ = con.execute_dc_return_cols(root)
|
|
442
|
+
|
|
443
|
+
assert cols == ['2', 'ename']
|
|
444
|
+
|
|
445
|
+
for query in [
|
|
446
|
+
"{ * | Episodes(_, _, '2', ename) }",
|
|
447
|
+
]:
|
|
448
|
+
root = DCParser.parse_query(query)
|
|
449
|
+
cols, _ = con.execute_dc_return_cols(root)
|
|
450
|
+
|
|
451
|
+
assert cols == ["'2'", 'ename']
|
|
452
|
+
|
|
453
|
+
for query in [
|
|
454
|
+
"{ * | Episodes(_, _, sid, 'ename') }",
|
|
455
|
+
]:
|
|
456
|
+
root = DCParser.parse_query(query)
|
|
457
|
+
cols, _ = con.execute_dc_return_cols(root)
|
|
458
|
+
|
|
459
|
+
assert cols == ['sid', "'ename'"]
|
|
460
|
+
|
|
461
|
+
for query in [
|
|
462
|
+
'{ * | Episodes(_, 1, 2, ename) }',
|
|
463
|
+
]:
|
|
464
|
+
root = DCParser.parse_query(query)
|
|
465
|
+
cols, _ = con.execute_dc_return_cols(root)
|
|
466
|
+
|
|
467
|
+
assert cols == ['1', '2', 'ename']
|
|
468
|
+
|
|
469
|
+
for query in [
|
|
470
|
+
'{ * | Episodes(_, 2, 2, ename) }',
|
|
471
|
+
]:
|
|
472
|
+
root = DCParser.parse_query(query)
|
|
473
|
+
cols, _ = con.execute_dc_return_cols(root)
|
|
474
|
+
|
|
475
|
+
assert cols == ['2', '2', 'ename']
|
|
476
|
+
|
|
477
|
+
for query in [
|
|
478
|
+
'{ * | Episodes(_, _, sid, ename) ∧ sid = 2 }',
|
|
479
|
+
]:
|
|
480
|
+
root = DCParser.parse_query(query)
|
|
481
|
+
cols, _ = con.execute_dc_return_cols(root)
|
|
482
|
+
|
|
483
|
+
assert cols == ['sid', 'ename']
|