sqlscope-rs 0.5.0__tar.gz → 0.6.1__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: sqlscope-rs
3
- Version: 0.5.0
3
+ Version: 0.6.1
4
4
  Summary: Scope-aware SQL analysis and rewriting: row filters, CTE injection, table rewrites, column lineage.
5
5
  Project-URL: Repository, https://github.com/dcalsky/sqlscope
6
6
  License-Expression: MIT
@@ -5,7 +5,7 @@ Function Purpose
5
5
  ===================== ==========================================================
6
6
  apply_row_filter Filter every in-scope table by a predicate.
7
7
  inject_ctes Prepend CTE definitions to a query's root ``WITH``.
8
- rewrite_tables Replace table references with derived tables.
8
+ rewrite_tables Replace table references with other tables or derived tables.
9
9
  column_origins Source columns whose values reach the result (lineage).
10
10
  output_columns Names of the columns a statement outputs.
11
11
  referenced_columns Columns referenced anywhere, per table.
@@ -41,7 +41,7 @@ from ._library import (
41
41
  load,
42
42
  )
43
43
 
44
- __version__ = "0.5.0"
44
+ __version__ = "0.6.1"
45
45
 
46
46
  __all__ = [
47
47
  "Clause",
@@ -107,12 +107,15 @@ class UnionRewrite:
107
107
  class TableRewrite:
108
108
  """Replaces references to ``match_key`` (``schema.table``).
109
109
 
110
- Set exactly one of ``inline`` (``(SELECT * FROM inline)``) or ``union``.
110
+ Set exactly one of ``inline``, ``union``, or ``rename``. Optional ``columns``
111
+ selects identifier values from the inline target (empty selects all).
111
112
  """
112
113
 
113
114
  match_key: str
114
115
  inline: Optional[TableRef] = None
115
116
  union: Optional[UnionRewrite] = None
117
+ columns: Optional[Sequence[str]] = None
118
+ rename: Optional[TableRef] = None
116
119
 
117
120
 
118
121
  class Clause(str, Enum):
@@ -239,7 +242,7 @@ def rewrite_tables(
239
242
  ) -> str:
240
243
  """Replace physical table references according to ``rewrites``.
241
244
 
242
- Matched references become derived tables that keep their alias (or bare
245
+ Matched references keep their alias (or bare
243
246
  name); qualified column references are rebound onto it. Catalogs listed in
244
247
  ``strip_catalogs`` are transparent while matching. Returns the input
245
248
  unchanged when nothing matches.
@@ -248,6 +251,8 @@ def rewrite_tables(
248
251
  {
249
252
  "matchKey": rewrite.match_key,
250
253
  "inline": _table(rewrite.inline) if rewrite.inline is not None else None,
254
+ **({"columns": list(rewrite.columns)} if rewrite.columns else {}),
255
+ **({"rename": _table(rewrite.rename)} if rewrite.rename is not None else {}),
251
256
  "union": {
252
257
  "tableAlias": rewrite.union.table_alias,
253
258
  "columns": list(rewrite.union.columns),
@@ -40,6 +40,21 @@ def test_rewrite_tables():
40
40
  )
41
41
 
42
42
 
43
+ def test_project_from_injected_cte():
44
+ query = sqlscope.rewrite_tables("SELECT t.a FROM s.t", [
45
+ TableRewrite("s.t", inline=TableRef("permitted"), columns=["a", "end"])
46
+ ])
47
+ query = sqlscope.inject_ctes(query, [CteDef("permitted", "SELECT * FROM src.t")])
48
+ assert query == 'WITH permitted AS (SELECT * FROM src.t) SELECT t.a FROM (SELECT a, "end" FROM permitted) AS t'
49
+
50
+
51
+ def test_rename_table_argument():
52
+ query = sqlscope.rewrite_tables("SELECT * FROM ML.PREDICT(MODEL s.t, TABLE s.t)", [
53
+ TableRewrite("s.t", rename=TableRef("u", "v"))
54
+ ], dialect="bigquery")
55
+ assert query == "SELECT * FROM ML.PREDICT(MODEL s.t, TABLE v.u)"
56
+
57
+
43
58
  def test_rewrite_tables_requires_one_target():
44
59
  with pytest.raises(sqlscope.InvalidArgumentError):
45
60
  sqlscope.rewrite_tables("SELECT 1", [TableRewrite("s.t")])
File without changes
File without changes
File without changes