1
1
import unittest
2
2
3
3
from sqlglot import exp , parse_one
4
- from sqlglot .diff import Insert , Keep , Move , Remove , Update , diff
4
+ from sqlglot .diff import Insert , Move , Remove , Update , diff
5
5
from sqlglot .expressions import Join , to_table
6
6
7
7
8
+ def diff_delta_only (source , target , matchings = None , delta_only = True , ** kwargs ):
9
+ return diff (source , target , matchings = matchings , delta_only = delta_only , ** kwargs )
10
+
11
+
8
12
class TestDiff (unittest .TestCase ):
9
13
def test_simple (self ):
10
14
self ._validate_delta_only (
11
- diff (parse_one ("SELECT a + b" ), parse_one ("SELECT a - b" )),
15
+ diff_delta_only (parse_one ("SELECT a + b" ), parse_one ("SELECT a - b" )),
12
16
[
13
17
Remove (parse_one ("a + b" )), # the Add node
14
18
Insert (parse_one ("a - b" )), # the Sub node
15
19
],
16
20
)
17
21
18
22
self ._validate_delta_only (
19
- diff (parse_one ("SELECT a, b, c" ), parse_one ("SELECT a, c" )),
23
+ diff_delta_only (parse_one ("SELECT a, b, c" ), parse_one ("SELECT a, c" )),
20
24
[
21
25
Remove (parse_one ("b" )), # the Column node
22
26
],
23
27
)
24
28
25
29
self ._validate_delta_only (
26
- diff (parse_one ("SELECT a, b" ), parse_one ("SELECT a, b, c" )),
30
+ diff_delta_only (parse_one ("SELECT a, b" ), parse_one ("SELECT a, b, c" )),
27
31
[
28
32
Insert (parse_one ("c" )), # the Column node
29
33
],
30
34
)
31
35
32
36
self ._validate_delta_only (
33
- diff (
37
+ diff_delta_only (
34
38
parse_one ("SELECT a FROM table_one" ),
35
39
parse_one ("SELECT a FROM table_two" ),
36
40
),
@@ -44,7 +48,9 @@ def test_simple(self):
44
48
45
49
def test_lambda (self ):
46
50
self ._validate_delta_only (
47
- diff (parse_one ("SELECT a, b, c, x(a -> a)" ), parse_one ("SELECT a, b, c, x(b -> b)" )),
51
+ diff_delta_only (
52
+ parse_one ("SELECT a, b, c, x(a -> a)" ), parse_one ("SELECT a, b, c, x(b -> b)" )
53
+ ),
48
54
[
49
55
Update (
50
56
exp .Lambda (this = exp .to_identifier ("a" ), expressions = [exp .to_identifier ("a" )]),
@@ -55,14 +61,16 @@ def test_lambda(self):
55
61
56
62
def test_udf (self ):
57
63
self ._validate_delta_only (
58
- diff (parse_one ('SELECT a, b, "my.udf1"()' ), parse_one ('SELECT a, b, "my.udf2"()' )),
64
+ diff_delta_only (
65
+ parse_one ('SELECT a, b, "my.udf1"()' ), parse_one ('SELECT a, b, "my.udf2"()' )
66
+ ),
59
67
[
60
68
Insert (parse_one ('"my.udf2"()' )),
61
69
Remove (parse_one ('"my.udf1"()' )),
62
70
],
63
71
)
64
72
self ._validate_delta_only (
65
- diff (
73
+ diff_delta_only (
66
74
parse_one ('SELECT a, b, "my.udf"(x, y, z)' ),
67
75
parse_one ('SELECT a, b, "my.udf"(x, y, w)' ),
68
76
),
@@ -74,28 +82,28 @@ def test_udf(self):
74
82
75
83
def test_node_position_changed (self ):
76
84
self ._validate_delta_only (
77
- diff (parse_one ("SELECT a, b, c" ), parse_one ("SELECT c, a, b" )),
85
+ diff_delta_only (parse_one ("SELECT a, b, c" ), parse_one ("SELECT c, a, b" )),
78
86
[
79
87
Move (parse_one ("c" )), # the Column node
80
88
],
81
89
)
82
90
83
91
self ._validate_delta_only (
84
- diff (parse_one ("SELECT a + b" ), parse_one ("SELECT b + a" )),
92
+ diff_delta_only (parse_one ("SELECT a + b" ), parse_one ("SELECT b + a" )),
85
93
[
86
94
Move (parse_one ("a" )), # the Column node
87
95
],
88
96
)
89
97
90
98
self ._validate_delta_only (
91
- diff (parse_one ("SELECT aaaa AND bbbb" ), parse_one ("SELECT bbbb AND aaaa" )),
99
+ diff_delta_only (parse_one ("SELECT aaaa AND bbbb" ), parse_one ("SELECT bbbb AND aaaa" )),
92
100
[
93
101
Move (parse_one ("aaaa" )), # the Column node
94
102
],
95
103
)
96
104
97
105
self ._validate_delta_only (
98
- diff (
106
+ diff_delta_only (
99
107
parse_one ("SELECT aaaa OR bbbb OR cccc" ),
100
108
parse_one ("SELECT cccc OR bbbb OR aaaa" ),
101
109
),
@@ -120,7 +128,7 @@ def test_cte(self):
120
128
"""
121
129
122
130
self ._validate_delta_only (
123
- diff (parse_one (expr_src ), parse_one (expr_tgt )),
131
+ diff_delta_only (parse_one (expr_src ), parse_one (expr_tgt )),
124
132
[
125
133
Remove (parse_one ("LOWER(c) AS c" )), # the Alias node
126
134
Remove (parse_one ("LOWER(c)" )), # the Lower node
@@ -133,8 +141,7 @@ def test_join(self):
133
141
expr_src = "SELECT a, b FROM t1 LEFT JOIN t2 ON t1.key = t2.key"
134
142
expr_tgt = "SELECT a, b FROM t1 RIGHT JOIN t2 ON t1.key = t2.key"
135
143
136
- changes = diff (parse_one (expr_src ), parse_one (expr_tgt ))
137
- changes = _delta_only (changes )
144
+ changes = diff_delta_only (parse_one (expr_src ), parse_one (expr_tgt ))
138
145
139
146
self .assertEqual (len (changes ), 2 )
140
147
self .assertTrue (isinstance (changes [0 ], Remove ))
@@ -145,10 +152,10 @@ def test_window_functions(self):
145
152
expr_src = parse_one ("SELECT ROW_NUMBER() OVER (PARTITION BY a ORDER BY b)" )
146
153
expr_tgt = parse_one ("SELECT RANK() OVER (PARTITION BY a ORDER BY b)" )
147
154
148
- self ._validate_delta_only (diff (expr_src , expr_src ), [])
155
+ self ._validate_delta_only (diff_delta_only (expr_src , expr_src ), [])
149
156
150
157
self ._validate_delta_only (
151
- diff (expr_src , expr_tgt ),
158
+ diff_delta_only (expr_src , expr_tgt ),
152
159
[
153
160
Remove (parse_one ("ROW_NUMBER()" )), # the Anonymous node
154
161
Insert (parse_one ("RANK()" )), # the Anonymous node
@@ -160,7 +167,7 @@ def test_pre_matchings(self):
160
167
expr_tgt = parse_one ("SELECT 1, 2, 3, 4" )
161
168
162
169
self ._validate_delta_only (
163
- diff (expr_src , expr_tgt ),
170
+ diff_delta_only (expr_src , expr_tgt ),
164
171
[
165
172
Remove (expr_src ),
166
173
Insert (expr_tgt ),
@@ -171,7 +178,7 @@ def test_pre_matchings(self):
171
178
)
172
179
173
180
self ._validate_delta_only (
174
- diff (expr_src , expr_tgt , matchings = [(expr_src , expr_tgt )]),
181
+ diff_delta_only (expr_src , expr_tgt , matchings = [(expr_src , expr_tgt )]),
175
182
[
176
183
Insert (exp .Literal .number (2 )),
177
184
Insert (exp .Literal .number (3 )),
@@ -180,23 +187,20 @@ def test_pre_matchings(self):
180
187
)
181
188
182
189
with self .assertRaises (ValueError ):
183
- diff (expr_src , expr_tgt , matchings = [(expr_src , expr_tgt ), (expr_src , expr_tgt )])
190
+ diff_delta_only (
191
+ expr_src , expr_tgt , matchings = [(expr_src , expr_tgt ), (expr_src , expr_tgt )]
192
+ )
184
193
185
194
def test_identifier (self ):
186
195
expr_src = parse_one ("SELECT a FROM tbl" )
187
196
expr_tgt = parse_one ("SELECT a, tbl.b from tbl" )
188
197
189
198
self ._validate_delta_only (
190
- diff (expr_src , expr_tgt ),
199
+ diff_delta_only (expr_src , expr_tgt ),
191
200
[
192
201
Insert (expression = exp .to_column ("tbl.b" )),
193
202
],
194
203
)
195
204
196
- def _validate_delta_only (self , actual_diff , expected_delta ):
197
- actual_delta = _delta_only (actual_diff )
205
+ def _validate_delta_only (self , actual_delta , expected_delta ):
198
206
self .assertEqual (set (actual_delta ), set (expected_delta ))
199
-
200
-
201
- def _delta_only (changes ):
202
- return [d for d in changes if not isinstance (d , Keep )]
0 commit comments