Skip to content

Commit f070d2b

Browse files
dtsongclaude
andauthored
chore: remove deprecated Compiler.compile() wrapper and unused Param class (#17)
* chore: remove deprecated Compiler.compile() wrapper and unused Param class Remove the deprecated compile() method on Compiler — all callers now use dialect.compile() directly. Also remove the Param class, its render method, and the cv_params context variable, as nothing in the codebase ever instantiates Param. Closes #7, closes #10 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: remove dead params parameter from BaseDialect.compile() The params argument was left behind after removing the Param/cv_params system — it was silently accepted but never used. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7ac6101 commit f070d2b

File tree

4 files changed

+70
-83
lines changed

4 files changed

+70
-83
lines changed

data_diff/databases/base.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import abc
2-
import contextvars
32
import decimal
43
import functools
54
import logging
@@ -65,7 +64,6 @@
6564
InsertToTable,
6665
IsDistinctFrom,
6766
Join,
68-
Param,
6967
Random,
7068
Root,
7169
TableAlias,
@@ -81,7 +79,6 @@
8179
from data_diff.utils import ArithString, ArithUUID, is_uuid, join_iter, safezip
8280

8381
logger = logging.getLogger("database")
84-
cv_params = contextvars.ContextVar("params")
8582

8683

8784
class CompileError(Exception):
@@ -115,10 +112,6 @@ class Compiler(AbstractCompiler):
115112
def dialect(self) -> "BaseDialect":
116113
return self.database.dialect
117114

118-
# TODO: DEPRECATED: Remove once the dialect is used directly in all places.
119-
def compile(self, elem, params=None) -> str:
120-
return self.dialect.compile(self, elem, params)
121-
122115
def new_unique_name(self, prefix="tmp") -> str:
123116
self._counter[0] += 1
124117
return f"{prefix}{self._counter[0]}"
@@ -221,10 +214,7 @@ def parse_table_name(self, name: str) -> DbPath:
221214
"Parse the given table name into a DbPath"
222215
return parse_table_name(name)
223216

224-
def compile(self, compiler: Compiler, elem, params=None) -> str:
225-
if params:
226-
cv_params.set(params)
227-
217+
def compile(self, compiler: Compiler, elem) -> str:
228218
if compiler.root and isinstance(elem, Compilable) and not isinstance(elem, Root):
229219
from data_diff.queries.ast_classes import Select
230220

@@ -268,8 +258,6 @@ def render_compilable(self, c: Compiler, elem: Compilable) -> str:
268258
return self.render_cte(c, elem)
269259
elif isinstance(elem, Commit):
270260
return self.render_commit(c, elem)
271-
elif isinstance(elem, Param):
272-
return self.render_param(c, elem)
273261
elif isinstance(elem, NormalizeAsString):
274262
return self.render_normalizeasstring(c, elem)
275263
elif isinstance(elem, ApplyFuncAndNormalizeAsString):
@@ -369,10 +357,6 @@ def render_cte(self, parent_c: Compiler, elem: Cte) -> str:
369357
def render_commit(self, c: Compiler, elem: Commit) -> str:
370358
return "COMMIT" if not c.database.is_autocommit else SKIP
371359

372-
def render_param(self, c: Compiler, elem: Param) -> str:
373-
params = cv_params.get()
374-
return self._compile(c, params[elem.name])
375-
376360
def render_normalizeasstring(self, c: Compiler, elem: NormalizeAsString) -> str:
377361
expr = self.compile(c, elem.expr)
378362
return self.normalize_value_by_type(expr, elem.expr_type or elem.expr.type)

data_diff/queries/ast_classes.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _dfs_values(self):
3838
if k == "source_table":
3939
# Skip data-sources, we're only interested in data-parameters
4040
continue
41-
if not isinstance(vs, (list, tuple)):
41+
if not isinstance(vs, list | tuple):
4242
vs = [vs]
4343
for v in vs:
4444
if isinstance(v, ExprNode):
@@ -689,7 +689,7 @@ def __getattr__(self, name):
689689
return _ResolveColumn(name)
690690

691691
def __getitem__(self, name):
692-
if isinstance(name, (list, tuple)):
692+
if isinstance(name, list | tuple):
693693
return [_ResolveColumn(n) for n in name]
694694
return _ResolveColumn(name)
695695

@@ -794,10 +794,3 @@ def returning(self, *exprs) -> Self:
794794
@attrs.define(frozen=True, eq=False)
795795
class Commit(Statement):
796796
"""Generate a COMMIT statement, if we're in the middle of a transaction, or in auto-commit. Otherwise SKIP."""
797-
798-
799-
@attrs.define(frozen=True, eq=False)
800-
class Param(ExprNode, ITable): # TODO: Unused?
801-
"""A value placeholder, to be specified at compilation time using the `cv_params` context variable."""
802-
803-
name: str

tests/test_query.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ def test_basic(self):
108108

109109
t = table("point")
110110
t2 = t.select(x=this.x + 1, y=t["y"] + this.x)
111-
assert c.compile(t2) == "SELECT (x + 1) AS x, (y + x) AS y FROM point"
111+
assert c.dialect.compile(c, t2) == "SELECT (x + 1) AS x, (y + x) AS y FROM point"
112112

113113
t = table("point").where(this.x == 1, this.y == 2)
114-
assert c.compile(t) == "SELECT * FROM point WHERE (x = 1) AND (y = 2)"
114+
assert c.dialect.compile(c, t) == "SELECT * FROM point WHERE (x = 1) AND (y = 2)"
115115

116116
t = table("person").where(this.name == "Albert")
117-
self.assertEqual(c.compile(t), "SELECT * FROM person WHERE (name = 'Albert')")
117+
self.assertEqual(c.dialect.compile(c, t), "SELECT * FROM person WHERE (name = 'Albert')")
118118

119119
def test_outerjoin(self):
120120
c = Compiler(MockDatabase())
@@ -127,7 +127,8 @@ def test_outerjoin(self):
127127
j = outerjoin(a, b).on(a[k] == b[k] for k in keys)
128128

129129
self.assertEqual(
130-
c.compile(j), "SELECT * FROM a tmp1 FULL OUTER JOIN b tmp2 ON (tmp1.x = tmp2.x) AND (tmp1.y = tmp2.y)"
130+
c.dialect.compile(c, j),
131+
"SELECT * FROM a tmp1 FULL OUTER JOIN b tmp2 ON (tmp1.x = tmp2.x) AND (tmp1.y = tmp2.y)",
131132
)
132133

133134
def test_schema(self):
@@ -137,7 +138,7 @@ def test_schema(self):
137138
# test table
138139
t = table("a", schema=CaseInsensitiveDict(schema))
139140
q = t.select(this.Id, t["COMMENT"])
140-
assert c.compile(q) == "SELECT id, comment FROM a"
141+
assert c.dialect.compile(c, q) == "SELECT id, comment FROM a"
141142

142143
t = table("a", schema=CaseSensitiveDict(schema))
143144
self.assertRaises(KeyError, t.__getitem__, "Id")
@@ -174,67 +175,67 @@ def test_cte(self):
174175
t3 = t2.select(this.x)
175176

176177
expected = "WITH tmp1 AS (SELECT x FROM a) SELECT x FROM tmp1"
177-
assert normalize_spaces(c.compile(t3)) == expected
178+
assert normalize_spaces(c.dialect.compile(c, t3)) == expected
178179

179180
# nested cte
180181
c = Compiler(MockDatabase())
181182
t4 = cte(t3).select(this.x)
182183

183184
expected = "WITH tmp1 AS (SELECT x FROM a), tmp2 AS (SELECT x FROM tmp1) SELECT x FROM tmp2"
184-
assert normalize_spaces(c.compile(t4)) == expected
185+
assert normalize_spaces(c.dialect.compile(c, t4)) == expected
185186

186187
# parameterized cte
187188
c = Compiler(MockDatabase())
188189
t2 = cte(t.select(this.x), params=["y"])
189190
t3 = t2.select(this.y)
190191

191192
expected = "WITH tmp1(y) AS (SELECT x FROM a) SELECT y FROM tmp1"
192-
assert normalize_spaces(c.compile(t3)) == expected
193+
assert normalize_spaces(c.dialect.compile(c, t3)) == expected
193194

194195
def test_funcs(self):
195196
c = Compiler(MockDatabase())
196197
t = table("a")
197198

198-
q = c.compile(t.order_by(Random()).limit(10))
199+
q = c.dialect.compile(c, t.order_by(Random()).limit(10))
199200
self.assertEqual(q, "SELECT * FROM (SELECT * FROM a ORDER BY random()) AS LIMITED_SELECT LIMIT 10")
200201

201-
q = c.compile(t.select(coalesce(this.a, this.b)))
202+
q = c.dialect.compile(c, t.select(coalesce(this.a, this.b)))
202203
self.assertEqual(q, "SELECT COALESCE(a, b) FROM a")
203204

204205
def test_select_distinct(self):
205206
c = Compiler(MockDatabase())
206207
t = table("a")
207208

208-
q = c.compile(t.select(this.b, distinct=True))
209+
q = c.dialect.compile(c, t.select(this.b, distinct=True))
209210
assert q == "SELECT DISTINCT b FROM a"
210211

211212
# selects merge
212-
q = c.compile(t.where(this.b > 10).select(this.b, distinct=True))
213+
q = c.dialect.compile(c, t.where(this.b > 10).select(this.b, distinct=True))
213214
self.assertEqual(q, "SELECT DISTINCT b FROM a WHERE (b > 10)")
214215

215216
# selects stay apart
216-
q = c.compile(t.limit(10).select(this.b, distinct=True))
217+
q = c.dialect.compile(c, t.limit(10).select(this.b, distinct=True))
217218
self.assertEqual(q, "SELECT DISTINCT b FROM (SELECT * FROM (SELECT * FROM a) AS LIMITED_SELECT LIMIT 10) tmp1")
218219

219-
q = c.compile(t.select(this.b, distinct=True).select(distinct=False))
220+
q = c.dialect.compile(c, t.select(this.b, distinct=True).select(distinct=False))
220221
self.assertEqual(q, "SELECT * FROM (SELECT DISTINCT b FROM a) tmp2")
221222

222223
def test_select_with_optimizer_hints(self):
223224
c = Compiler(MockDatabase())
224225
t = table("a")
225226

226-
q = c.compile(t.select(this.b, optimizer_hints="PARALLEL(a 16)"))
227+
q = c.dialect.compile(c, t.select(this.b, optimizer_hints="PARALLEL(a 16)"))
227228
assert q == "SELECT /*+ PARALLEL(a 16) */ b FROM a"
228229

229-
q = c.compile(t.where(this.b > 10).select(this.b, optimizer_hints="PARALLEL(a 16)"))
230+
q = c.dialect.compile(c, t.where(this.b > 10).select(this.b, optimizer_hints="PARALLEL(a 16)"))
230231
self.assertEqual(q, "SELECT /*+ PARALLEL(a 16) */ b FROM a WHERE (b > 10)")
231232

232-
q = c.compile(t.limit(10).select(this.b, optimizer_hints="PARALLEL(a 16)"))
233+
q = c.dialect.compile(c, t.limit(10).select(this.b, optimizer_hints="PARALLEL(a 16)"))
233234
self.assertEqual(
234235
q, "SELECT /*+ PARALLEL(a 16) */ b FROM (SELECT * FROM (SELECT * FROM a) AS LIMITED_SELECT LIMIT 10) tmp1"
235236
)
236237

237-
q = c.compile(t.select(this.a).group_by(this.b).agg(this.c).select(optimizer_hints="PARALLEL(a 16)"))
238+
q = c.dialect.compile(c, t.select(this.a).group_by(this.b).agg(this.c).select(optimizer_hints="PARALLEL(a 16)"))
238239
self.assertEqual(
239240
q, "SELECT /*+ PARALLEL(a 16) */ * FROM (SELECT b, c FROM (SELECT a FROM a) tmp2 GROUP BY 1) tmp3"
240241
)
@@ -244,85 +245,86 @@ def test_table_ops(self):
244245
a = table("a").select(this.x)
245246
b = table("b").select(this.y)
246247

247-
q = c.compile(a.union(b))
248+
q = c.dialect.compile(c, a.union(b))
248249
assert q == "SELECT x FROM a UNION SELECT y FROM b"
249250

250-
q = c.compile(a.union_all(b))
251+
q = c.dialect.compile(c, a.union_all(b))
251252
assert q == "SELECT x FROM a UNION ALL SELECT y FROM b"
252253

253-
q = c.compile(a.minus(b))
254+
q = c.dialect.compile(c, a.minus(b))
254255
assert q == "SELECT x FROM a EXCEPT SELECT y FROM b"
255256

256-
q = c.compile(a.intersect(b))
257+
q = c.dialect.compile(c, a.intersect(b))
257258
assert q == "SELECT x FROM a INTERSECT SELECT y FROM b"
258259

259260
def test_ops(self):
260261
c = Compiler(MockDatabase())
261262
t = table("a")
262263

263-
q = c.compile(t.select(this.b + this.c))
264+
q = c.dialect.compile(c, t.select(this.b + this.c))
264265
self.assertEqual(q, "SELECT (b + c) FROM a")
265266

266-
q = c.compile(t.select(this.b.like(this.c)))
267+
q = c.dialect.compile(c, t.select(this.b.like(this.c)))
267268
self.assertEqual(q, "SELECT (b LIKE c) FROM a")
268269

269-
q = c.compile(t.select(-this.b.sum()))
270+
q = c.dialect.compile(c, t.select(-this.b.sum()))
270271
self.assertEqual(q, "SELECT (-SUM(b)) FROM a")
271272

272273
def test_group_by(self):
273274
c = Compiler(MockDatabase())
274275
t = table("a")
275276

276-
q = c.compile(t.group_by(this.b).agg(this.c))
277+
q = c.dialect.compile(c, t.group_by(this.b).agg(this.c))
277278
self.assertEqual(q, "SELECT b, c FROM a GROUP BY 1")
278279

279-
q = c.compile(t.where(this.b > 1).group_by(this.b).agg(this.c))
280+
q = c.dialect.compile(c, t.where(this.b > 1).group_by(this.b).agg(this.c))
280281
self.assertEqual(q, "SELECT b, c FROM a WHERE (b > 1) GROUP BY 1")
281282

282-
self.assertRaises(CompileError, c.compile, t.select(this.b).group_by(this.b))
283+
self.assertRaises(CompileError, c.dialect.compile, c, t.select(this.b).group_by(this.b))
283284

284-
q = c.compile(t.select(this.b).group_by(this.b).agg())
285+
q = c.dialect.compile(c, t.select(this.b).group_by(this.b).agg())
285286
self.assertEqual(q, "SELECT b FROM (SELECT b FROM a) tmp1 GROUP BY 1")
286287

287-
q = c.compile(t.group_by(this.b, this.c).agg(this.d, this.e))
288+
q = c.dialect.compile(c, t.group_by(this.b, this.c).agg(this.d, this.e))
288289
self.assertEqual(q, "SELECT b, c, d, e FROM a GROUP BY 1, 2")
289290

290291
# Having
291-
q = c.compile(t.group_by(this.b).agg(this.c).having(this.b > 1))
292+
q = c.dialect.compile(c, t.group_by(this.b).agg(this.c).having(this.b > 1))
292293
self.assertEqual(q, "SELECT b, c FROM a GROUP BY 1 HAVING (b > 1)")
293294

294-
q = c.compile(t.group_by(this.b).having(this.b > 1).agg(this.c))
295+
q = c.dialect.compile(c, t.group_by(this.b).having(this.b > 1).agg(this.c))
295296
self.assertEqual(q, "SELECT b, c FROM a GROUP BY 1 HAVING (b > 1)")
296297

297-
q = c.compile(t.select(this.b).group_by(this.b).agg().having(this.b > 1))
298+
q = c.dialect.compile(c, t.select(this.b).group_by(this.b).agg().having(this.b > 1))
298299
self.assertEqual(q, "SELECT b FROM (SELECT b FROM a) tmp2 GROUP BY 1 HAVING (b > 1)")
299300

300301
# Having sum
301-
q = c.compile(t.group_by(this.b).agg(this.c, this.d).having(this.b.sum() > 1))
302+
q = c.dialect.compile(c, t.group_by(this.b).agg(this.c, this.d).having(this.b.sum() > 1))
302303
self.assertEqual(q, "SELECT b, c, d FROM a GROUP BY 1 HAVING (SUM(b) > 1)")
303304

304305
# Select interaction
305-
q = c.compile(t.select(this.a).group_by(this.b).agg(this.c).select(this.c + 1))
306+
q = c.dialect.compile(c, t.select(this.a).group_by(this.b).agg(this.c).select(this.c + 1))
306307
self.assertEqual(q, "SELECT (c + 1) FROM (SELECT b, c FROM (SELECT a FROM a) tmp3 GROUP BY 1) tmp4")
307308

308309
def test_case_when(self):
309310
c = Compiler(MockDatabase())
310311
t = table("a")
311312

312-
q = c.compile(t.select(when(this.b).then(this.c)))
313+
q = c.dialect.compile(c, t.select(when(this.b).then(this.c)))
313314
self.assertEqual(q, "SELECT CASE WHEN b THEN c END FROM a")
314315

315-
q = c.compile(t.select(when(this.b).then(this.c).else_(this.d)))
316+
q = c.dialect.compile(c, t.select(when(this.b).then(this.c).else_(this.d)))
316317
self.assertEqual(q, "SELECT CASE WHEN b THEN c ELSE d END FROM a")
317318

318-
q = c.compile(
319+
q = c.dialect.compile(
320+
c,
319321
t.select(
320322
when(this.type == "text")
321323
.then(this.text)
322324
.when(this.type == "number")
323325
.then(this.number)
324326
.else_("unknown type")
325-
)
327+
),
326328
)
327329
self.assertEqual(
328330
q,
@@ -333,13 +335,13 @@ def test_code(self):
333335
c = Compiler(MockDatabase())
334336
t = table("a")
335337

336-
q = c.compile(t.select(this.b, code("<x>")).where(code("<y>")))
338+
q = c.dialect.compile(c, t.select(this.b, code("<x>")).where(code("<y>")))
337339
self.assertEqual(q, "SELECT b, <x> FROM a WHERE <y>")
338340

339341
def tablesample(t, size):
340342
return code("{t} TABLESAMPLE BERNOULLI ({size})", t=t, size=size)
341343

342344
nonzero = table("points").where(this.x > 0, this.y > 0)
343345

344-
q = c.compile(tablesample(nonzero, 10))
346+
q = c.dialect.compile(c, tablesample(nonzero, 10))
345347
self.assertEqual(q, "SELECT * FROM points WHERE (x > 0) AND (y > 0) TABLESAMPLE BERNOULLI (10)")

0 commit comments

Comments
 (0)