analytics-query-analyzer 0.7.0__tar.gz → 0.7.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.
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/PKG-INFO +1 -1
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer/column_resolver.py +45 -6
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer/references_analyzer.py +47 -1
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer/timebounds_analyzer.py +4 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer.egg-info/PKG-INFO +1 -1
- analytics_query_analyzer-0.7.1/src/analytics_query_analyzer.egg-info/scm_version.json +8 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_analyze_bigquery.py +63 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_timebounds_safety.py +24 -0
- analytics_query_analyzer-0.7.0/src/analytics_query_analyzer.egg-info/scm_version.json +0 -8
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/.github/dependabot.yml +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/.github/workflows/publish-pypi.yml +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/.github/workflows/publish-testpypi.yml +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/.github/workflows/pytest.yml +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/.gitignore +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/LICENSE +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/README.md +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/pyproject.toml +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/setup.cfg +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer/__init__.py +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer/analyzer.py +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer/schema_builder.py +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer/types.py +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer.egg-info/SOURCES.txt +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer.egg-info/dependency_links.txt +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer.egg-info/requires.txt +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer.egg-info/scm_file_list.json +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/src/analytics_query_analyzer.egg-info/top_level.txt +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_analyze_redshift.py +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_analyze_with_timebounds_bigquery.py +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_analyze_with_timebounds_redshift.py +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_api_validation.py +0 -0
- {analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_bigquery_special_tables.py +0 -0
|
@@ -2,6 +2,26 @@ from sqlglot import exp
|
|
|
2
2
|
from sqlglot.optimizer.scope import Scope
|
|
3
3
|
|
|
4
4
|
|
|
5
|
+
def _fields_after(column: exp.Column) -> list[str]:
|
|
6
|
+
fields = []
|
|
7
|
+
node = column
|
|
8
|
+
while node.parent is not None:
|
|
9
|
+
parent = node.parent
|
|
10
|
+
if isinstance(parent, exp.Dot) and parent.this is node:
|
|
11
|
+
fields.append(parent.expression.name)
|
|
12
|
+
elif (
|
|
13
|
+
isinstance(parent, exp.Bracket)
|
|
14
|
+
and parent.this is node
|
|
15
|
+
and isinstance(node.type, exp.DataType)
|
|
16
|
+
and node.type.this == exp.DataType.Type.ARRAY
|
|
17
|
+
):
|
|
18
|
+
pass
|
|
19
|
+
else:
|
|
20
|
+
break
|
|
21
|
+
node = parent
|
|
22
|
+
return fields
|
|
23
|
+
|
|
24
|
+
|
|
5
25
|
def resolve_column_path(
|
|
6
26
|
column: exp.Column,
|
|
7
27
|
scope,
|
|
@@ -12,6 +32,22 @@ def resolve_column_path(
|
|
|
12
32
|
table_alias = column.table
|
|
13
33
|
source = scope.sources.get(table_alias)
|
|
14
34
|
if isinstance(source, Scope):
|
|
35
|
+
fields = _fields_after(column)
|
|
36
|
+
if isinstance(source.expression, exp.Unnest):
|
|
37
|
+
expressions = source.expression.expressions
|
|
38
|
+
nested_column = (
|
|
39
|
+
column_extractor(expressions[0]) if len(expressions) == 1 else None
|
|
40
|
+
)
|
|
41
|
+
if not nested_column:
|
|
42
|
+
return None
|
|
43
|
+
resolved = resolve_column_path(
|
|
44
|
+
nested_column, source, default_catalog, schema, column_extractor
|
|
45
|
+
)
|
|
46
|
+
if not resolved:
|
|
47
|
+
return None
|
|
48
|
+
full_path, column_path = resolved
|
|
49
|
+
suffix = ".".join(fields)
|
|
50
|
+
return (f"{full_path}.{suffix}", f"{column_path}.{suffix}") if suffix else resolved
|
|
15
51
|
select_expression = None
|
|
16
52
|
if source.expression:
|
|
17
53
|
named = source.expression.named_selects
|
|
@@ -21,12 +57,19 @@ def resolve_column_path(
|
|
|
21
57
|
return None
|
|
22
58
|
if isinstance(select_expression, exp.Alias):
|
|
23
59
|
select_expression = select_expression.this
|
|
60
|
+
if fields and not isinstance(select_expression, exp.Column):
|
|
61
|
+
return None
|
|
24
62
|
nested_column = column_extractor(select_expression)
|
|
25
63
|
if not nested_column:
|
|
26
64
|
return None
|
|
27
|
-
|
|
65
|
+
resolved = resolve_column_path(
|
|
28
66
|
nested_column, source, default_catalog, schema, column_extractor
|
|
29
67
|
)
|
|
68
|
+
if not resolved or not fields:
|
|
69
|
+
return resolved
|
|
70
|
+
full_path, column_path = resolved
|
|
71
|
+
suffix = ".".join(fields)
|
|
72
|
+
return f"{full_path}.{suffix}", f"{column_path}.{suffix}"
|
|
30
73
|
if not isinstance(source, exp.Table):
|
|
31
74
|
return None
|
|
32
75
|
|
|
@@ -46,11 +89,7 @@ def resolve_column_path(
|
|
|
46
89
|
if not dataset:
|
|
47
90
|
return None
|
|
48
91
|
|
|
49
|
-
fields =
|
|
50
|
-
parent = column.parent
|
|
51
|
-
while isinstance(parent, exp.Dot):
|
|
52
|
-
fields.append(parent.expression.name)
|
|
53
|
-
parent = parent.parent
|
|
92
|
+
fields = _fields_after(column)
|
|
54
93
|
|
|
55
94
|
if column.parts[0].name == column.table:
|
|
56
95
|
column_full_path = ".".join(p.name for p in column.parts[1:])
|
|
@@ -14,7 +14,34 @@ class ReferencesAnalyzer:
|
|
|
14
14
|
references: dict[str, set[str]] = {}
|
|
15
15
|
tables: set[str] = set()
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
scopes = list(traverse_scope(expression))
|
|
18
|
+
intermediate: dict[str, set[str]] = {}
|
|
19
|
+
explicit: dict[str, set[str]] = {}
|
|
20
|
+
# Only narrow passthroughs in simple queries. In other query shapes,
|
|
21
|
+
# projections can affect results even if only a field is read later
|
|
22
|
+
# (for example, DISTINCT, grouping, ordering, and scalar subqueries).
|
|
23
|
+
may_narrow = not any(
|
|
24
|
+
expression.find(kind)
|
|
25
|
+
for kind in (
|
|
26
|
+
exp.SetOperation,
|
|
27
|
+
exp.Distinct,
|
|
28
|
+
exp.Group,
|
|
29
|
+
exp.Having,
|
|
30
|
+
exp.Order,
|
|
31
|
+
exp.Limit,
|
|
32
|
+
exp.Qualify,
|
|
33
|
+
exp.Subquery,
|
|
34
|
+
exp.AggFunc,
|
|
35
|
+
exp.Window,
|
|
36
|
+
exp.Array,
|
|
37
|
+
exp.Struct,
|
|
38
|
+
)
|
|
39
|
+
) and all(
|
|
40
|
+
isinstance(join.this, exp.Unnest) for join in expression.find_all(exp.Join)
|
|
41
|
+
) and all(
|
|
42
|
+
not select.args.get("kind") for select in expression.find_all(exp.Select)
|
|
43
|
+
)
|
|
44
|
+
for scope in scopes:
|
|
18
45
|
for _, source in scope.selected_sources.values():
|
|
19
46
|
if not isinstance(source, exp.Table):
|
|
20
47
|
continue
|
|
@@ -43,6 +70,25 @@ class ReferencesAnalyzer:
|
|
|
43
70
|
full_path, column_path = resolved
|
|
44
71
|
table_path = ".".join(full_path.split(".", 3)[:3])
|
|
45
72
|
_add(references, table_path, column_path)
|
|
73
|
+
is_projection = (
|
|
74
|
+
isinstance(column.parent, exp.Alias)
|
|
75
|
+
and column.parent.parent is scope.expression
|
|
76
|
+
)
|
|
77
|
+
is_intermediate = may_narrow and (
|
|
78
|
+
(
|
|
79
|
+
scope is not scopes[-1]
|
|
80
|
+
and is_projection
|
|
81
|
+
)
|
|
82
|
+
or column.find_ancestor(exp.Unnest) is not None
|
|
83
|
+
)
|
|
84
|
+
_add(intermediate if is_intermediate else explicit, table_path, column_path)
|
|
85
|
+
|
|
86
|
+
for table_path, columns in intermediate.items():
|
|
87
|
+
for column in columns - explicit.get(table_path, set()):
|
|
88
|
+
if any(
|
|
89
|
+
other.startswith(column + ".") for other in references[table_path]
|
|
90
|
+
):
|
|
91
|
+
references[table_path].discard(column)
|
|
46
92
|
|
|
47
93
|
rows = _flatten_references(references)
|
|
48
94
|
for table_path in tables - references.keys():
|
|
@@ -40,6 +40,10 @@ class TimeboundsAnalyzer:
|
|
|
40
40
|
return self.analyze_scopes(expression)
|
|
41
41
|
|
|
42
42
|
def analyze_scopes(self, expression: exp.Expression) -> TimeboundsResults:
|
|
43
|
+
# Recursive iterations can transform values, so predicates on recursive
|
|
44
|
+
# outputs do not necessarily constrain the original table reads.
|
|
45
|
+
if any(with_.args.get("recursive") for with_ in expression.find_all(exp.With)):
|
|
46
|
+
return {}
|
|
43
47
|
scopes = list(traverse_scope(expression))
|
|
44
48
|
scope_bounds = {}
|
|
45
49
|
reads = []
|
{analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_analyze_bigquery.py
RENAMED
|
@@ -246,3 +246,66 @@ def _sorted_rows(rows: list[dict[str, str]]) -> list[dict[str, str]]:
|
|
|
246
246
|
rows,
|
|
247
247
|
key=lambda row: (row["database"], row["schema"], row["table"], row["column"]),
|
|
248
248
|
)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
@pytest.mark.parametrize(
|
|
252
|
+
("sql", "table", "columns"),
|
|
253
|
+
[
|
|
254
|
+
("with i as (select brand from shop.items) select brand.name from i", "items", {"brand.name"}),
|
|
255
|
+
("with i as (select brand as b from shop.items), j as (select b as c from i) select c.name from j", "items", {"brand.name"}),
|
|
256
|
+
("select it.item_id from shop.orders o, unnest(o.items) it where it.amount > 0", "orders", {"items.item_id", "items.amount"}),
|
|
257
|
+
("with x as (select it from shop.orders, unnest(items) it) select it.amount from x", "orders", {"items.amount"}),
|
|
258
|
+
],
|
|
259
|
+
)
|
|
260
|
+
def test_nested_field_lineage(sql, table, columns):
|
|
261
|
+
rows = analyze(dialects.BigQuery, sql, schema, "production")
|
|
262
|
+
assert {(row["table"], row["column"]) for row in rows} == {
|
|
263
|
+
(table, col) for col in columns
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def test_struct_whole_value_and_field_are_both_references():
|
|
268
|
+
sql = "with i as (select brand from shop.items) select brand, brand.name from i"
|
|
269
|
+
rows = analyze(dialects.BigQuery, sql, schema, "production")
|
|
270
|
+
assert {row["column"] for row in rows} == {"brand", "brand.name"}
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
@pytest.mark.parametrize("sql", [
|
|
274
|
+
"""select brand from shop.items union all
|
|
275
|
+
select brand from shop.items where brand.name = 'x'""",
|
|
276
|
+
"""with a as (select brand from shop.items union all
|
|
277
|
+
select brand from shop.items where brand.name = 'x') select brand from a""",
|
|
278
|
+
])
|
|
279
|
+
def test_union_preserves_whole_struct_reference(sql):
|
|
280
|
+
rows = analyze(dialects.BigQuery, sql, schema, "production")
|
|
281
|
+
assert {row["column"] for row in rows} == {"brand", "brand.name"}
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def test_distinct_preserves_whole_struct_reference():
|
|
285
|
+
sql = "with i as (select distinct brand from shop.items) select brand.name from i"
|
|
286
|
+
rows = analyze(dialects.BigQuery, sql, schema, "production")
|
|
287
|
+
assert {row["column"] for row in rows} == {"brand", "brand.name"}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
@pytest.mark.parametrize("sql", [
|
|
291
|
+
"select array(select brand from shop.items), (select brand.name from shop.items limit 1)",
|
|
292
|
+
"with i as (select brand from shop.items group by all) select brand.name from i",
|
|
293
|
+
"with i as (select brand from shop.items order by to_json_string(brand) limit 1) select brand.name from i",
|
|
294
|
+
])
|
|
295
|
+
def test_preserves_whole_struct_when_query_uses_it(sql):
|
|
296
|
+
rows = analyze(dialects.BigQuery, sql, schema, "production")
|
|
297
|
+
assert {row["column"] for row in rows} == {"brand", "brand.name"}
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
@pytest.mark.parametrize("index", ["offset(0)", "ordinal(1)", "safe_offset(0)", "safe_ordinal(1)"])
|
|
301
|
+
@pytest.mark.parametrize("prefix", ["", "with a as (select items from shop.orders) "])
|
|
302
|
+
def test_array_element_field_reference(index, prefix):
|
|
303
|
+
source = "a" if prefix else "shop.orders"
|
|
304
|
+
rows = analyze(dialects.BigQuery, f"{prefix}select items[{index}].amount from {source}", schema, "production")
|
|
305
|
+
assert {row["column"] for row in rows} == {"items.amount"}
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def test_array_element_whole_and_field_references():
|
|
309
|
+
sql = "select items[offset(0)], items[offset(user_id)].amount from shop.orders"
|
|
310
|
+
rows = analyze(dialects.BigQuery, sql, schema, "production")
|
|
311
|
+
assert {row["column"] for row in rows} == {"items", "items.amount", "user_id"}
|
{analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_timebounds_safety.py
RENAMED
|
@@ -8,6 +8,30 @@ from analytics_query_analyzer import analyze
|
|
|
8
8
|
SCHEMA = {"p": {"shop": {"orders": {"ordered_at": "date"}}}}
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
def test_recursive_cte_timebounds_are_conservative():
|
|
12
|
+
sql = """with recursive r as (
|
|
13
|
+
select ordered_at from shop.orders
|
|
14
|
+
union all select date_add(ordered_at, interval 1 day) from r
|
|
15
|
+
where ordered_at < '2026-01-10'
|
|
16
|
+
) select ordered_at from r"""
|
|
17
|
+
rows = analyze(dialects.BigQuery, sql, SCHEMA, "p", with_timebounds=True)
|
|
18
|
+
assert rows == [
|
|
19
|
+
{"database": "p", "schema": "shop", "table": "orders", "column": "ordered_at", "lower": None, "upper": None}
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_recursive_cte_without_time_column_does_not_raise():
|
|
24
|
+
sql = """with recursive r as (
|
|
25
|
+
select id from shop.users
|
|
26
|
+
union all select id + 1 from r where id < 10
|
|
27
|
+
) select id from r"""
|
|
28
|
+
schema = {"p": {"shop": {"users": {"id": "int64"}}}}
|
|
29
|
+
rows = analyze(dialects.BigQuery, sql, schema, "p", with_timebounds=True)
|
|
30
|
+
assert rows == [
|
|
31
|
+
{"database": "p", "schema": "shop", "table": "users", "column": "id", "lower": None, "upper": None}
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
|
|
11
35
|
@pytest.mark.parametrize(
|
|
12
36
|
("condition", "expected"),
|
|
13
37
|
[
|
|
File without changes
|
{analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/.github/workflows/publish-pypi.yml
RENAMED
|
File without changes
|
|
File without changes
|
{analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/.github/workflows/pytest.yml
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_analyze_redshift.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{analytics_query_analyzer-0.7.0 → analytics_query_analyzer-0.7.1}/tests/test_api_validation.py
RENAMED
|
File without changes
|
|
File without changes
|