analytics-query-analyzer 0.3.0__tar.gz → 0.4.0__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.
Files changed (28) hide show
  1. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/PKG-INFO +29 -6
  2. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/README.md +28 -5
  3. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer/references_analyzer.py +19 -2
  4. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer/timespan_analyzer.py +29 -2
  5. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer.egg-info/PKG-INFO +29 -6
  6. analytics_query_analyzer-0.4.0/tests/test_analyze_bigquery.py +247 -0
  7. analytics_query_analyzer-0.4.0/tests/test_analyze_redshift.py +253 -0
  8. analytics_query_analyzer-0.4.0/tests/test_analyze_timespan_bigquery.py +905 -0
  9. analytics_query_analyzer-0.4.0/tests/test_analyze_timespan_redshift.py +576 -0
  10. analytics_query_analyzer-0.3.0/tests/test_analyze_bigquery.py +0 -142
  11. analytics_query_analyzer-0.3.0/tests/test_analyze_redshift.py +0 -142
  12. analytics_query_analyzer-0.3.0/tests/test_analyze_timespan_bigquery.py +0 -588
  13. analytics_query_analyzer-0.3.0/tests/test_analyze_timespan_redshift.py +0 -608
  14. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/.github/workflows/publish-pypi.yml +0 -0
  15. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/.github/workflows/publish-testpypi.yml +0 -0
  16. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/.github/workflows/pytest.yml +0 -0
  17. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/.gitignore +0 -0
  18. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/LICENSE +0 -0
  19. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/pyproject.toml +0 -0
  20. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/setup.cfg +0 -0
  21. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer/__init__.py +0 -0
  22. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer/analyzer.py +0 -0
  23. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer/column_resolver.py +0 -0
  24. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer/schema_builder.py +0 -0
  25. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer.egg-info/SOURCES.txt +0 -0
  26. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer.egg-info/dependency_links.txt +0 -0
  27. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer.egg-info/requires.txt +0 -0
  28. {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.4.0}/src/analytics_query_analyzer.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: analytics-query-analyzer
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Analytics Query Analyzer
5
5
  License: MIT
6
6
  Classifier: License :: OSI Approved :: MIT License
@@ -81,7 +81,26 @@ from
81
81
 
82
82
  references = analyze(dialects.BigQuery, sql, schema, "production")
83
83
  print(references)
84
- # {"production.shop.orders": {"id", "user_id", "payment.amount"}}
84
+ # [
85
+ # {
86
+ # "database": "production",
87
+ # "schema": "shop",
88
+ # "table": "orders",
89
+ # "column": "id",
90
+ # },
91
+ # {
92
+ # "database": "production",
93
+ # "schema": "shop",
94
+ # "table": "orders",
95
+ # "column": "payment.amount",
96
+ # },
97
+ # {
98
+ # "database": "production",
99
+ # "schema": "shop",
100
+ # "table": "orders",
101
+ # "column": "user_id",
102
+ # },
103
+ # ]
85
104
  ```
86
105
 
87
106
  ### analyze_timespan
@@ -116,12 +135,16 @@ where
116
135
 
117
136
  timespans = analyze_timespan(dialects.BigQuery, sql, schema, "production")
118
137
  print(timespans)
119
- # {
120
- # "production.shop.orders.ordered_at": {
138
+ # [
139
+ # {
140
+ # "database": "production",
141
+ # "schema": "shop",
142
+ # "table": "orders",
143
+ # "column": "ordered_at",
121
144
  # "lower": "2025-01-01",
122
- # "upper": "2026-01-01"
145
+ # "upper": "2026-01-01",
123
146
  # }
124
- # }
147
+ # ]
125
148
  ```
126
149
 
127
150
  To make `current_date()` deterministic, pass a provider:
@@ -65,7 +65,26 @@ from
65
65
 
66
66
  references = analyze(dialects.BigQuery, sql, schema, "production")
67
67
  print(references)
68
- # {"production.shop.orders": {"id", "user_id", "payment.amount"}}
68
+ # [
69
+ # {
70
+ # "database": "production",
71
+ # "schema": "shop",
72
+ # "table": "orders",
73
+ # "column": "id",
74
+ # },
75
+ # {
76
+ # "database": "production",
77
+ # "schema": "shop",
78
+ # "table": "orders",
79
+ # "column": "payment.amount",
80
+ # },
81
+ # {
82
+ # "database": "production",
83
+ # "schema": "shop",
84
+ # "table": "orders",
85
+ # "column": "user_id",
86
+ # },
87
+ # ]
69
88
  ```
70
89
 
71
90
  ### analyze_timespan
@@ -100,12 +119,16 @@ where
100
119
 
101
120
  timespans = analyze_timespan(dialects.BigQuery, sql, schema, "production")
102
121
  print(timespans)
103
- # {
104
- # "production.shop.orders.ordered_at": {
122
+ # [
123
+ # {
124
+ # "database": "production",
125
+ # "schema": "shop",
126
+ # "table": "orders",
127
+ # "column": "ordered_at",
105
128
  # "lower": "2025-01-01",
106
- # "upper": "2026-01-01"
129
+ # "upper": "2026-01-01",
107
130
  # }
108
- # }
131
+ # ]
109
132
  ```
110
133
 
111
134
  To make `current_date()` deterministic, pass a provider:
@@ -9,7 +9,7 @@ class ReferencesAnalyzer:
9
9
  self.schema = schema
10
10
  self.default_catalog = default_catalog
11
11
 
12
- def analyze(self, expression: exp.Expression) -> dict:
12
+ def analyze(self, expression: exp.Expression) -> list[dict[str, str]]:
13
13
  references: dict[str, set[str]] = {}
14
14
 
15
15
  for scope in traverse_scope(expression):
@@ -27,7 +27,7 @@ class ReferencesAnalyzer:
27
27
  table_path = ".".join(full_path.split(".", 3)[:3])
28
28
  _add(references, table_path, column_path)
29
29
 
30
- return references
30
+ return _flatten_references(references)
31
31
 
32
32
  def _add(references: dict[str, set[str]], table: str, column: str):
33
33
  if table in references:
@@ -36,6 +36,23 @@ def _add(references: dict[str, set[str]], table: str, column: str):
36
36
  references[table] = {column}
37
37
 
38
38
 
39
+ def _flatten_references(references: dict[str, set[str]]) -> list[dict[str, str]]:
40
+ rows: list[dict[str, str]] = []
41
+ for table_path, columns in references.items():
42
+ database, schema, table = table_path.split(".", 2)
43
+ for column in columns:
44
+ rows.append(
45
+ {
46
+ "database": database,
47
+ "schema": schema,
48
+ "table": table,
49
+ "column": column,
50
+ }
51
+ )
52
+ rows.sort(key=lambda row: (row["database"], row["schema"], row["table"], row["column"]))
53
+ return rows
54
+
55
+
39
56
  def _extract_column(expr: exp.Expression) -> exp.Column | None:
40
57
  if isinstance(expr, exp.Column):
41
58
  return expr
@@ -27,8 +27,8 @@ class TimespanAnalyzer:
27
27
  self.default_catalog = default_catalog
28
28
  self.current_date_provider = current_date_provider or date.today
29
29
 
30
- def analyze(self, expression: exp.Expression) -> dict[str, dict[str, str | None]]:
31
- return _stringify_results(self._analyze_internal(expression))
30
+ def analyze(self, expression: exp.Expression) -> list[dict[str, str | None]]:
31
+ return _flatten_timespans(_stringify_results(self._analyze_internal(expression)))
32
32
 
33
33
  def _analyze_internal(self, expression: exp.Expression) -> TimespanResults:
34
34
  if isinstance(expression, exp.Union):
@@ -632,3 +632,30 @@ def _stringify_results(results: TimespanResults) -> dict[str, dict[str, str | No
632
632
  "upper": bounds["upper"].isoformat() if bounds["upper"] else None,
633
633
  }
634
634
  return output
635
+
636
+
637
+ def _flatten_timespans(
638
+ results: dict[str, dict[str, str | None]],
639
+ ) -> list[dict[str, str | None]]:
640
+ rows: list[dict[str, str | None]] = []
641
+ for full_path, bounds in results.items():
642
+ database, schema, table, column = full_path.split(".", 3)
643
+ rows.append(
644
+ {
645
+ "database": database,
646
+ "schema": schema,
647
+ "table": table,
648
+ "column": column,
649
+ "lower": bounds.get("lower"),
650
+ "upper": bounds.get("upper"),
651
+ }
652
+ )
653
+ rows.sort(
654
+ key=lambda row: (
655
+ row["database"] or "",
656
+ row["schema"] or "",
657
+ row["table"] or "",
658
+ row["column"] or "",
659
+ )
660
+ )
661
+ return rows
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: analytics-query-analyzer
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Analytics Query Analyzer
5
5
  License: MIT
6
6
  Classifier: License :: OSI Approved :: MIT License
@@ -81,7 +81,26 @@ from
81
81
 
82
82
  references = analyze(dialects.BigQuery, sql, schema, "production")
83
83
  print(references)
84
- # {"production.shop.orders": {"id", "user_id", "payment.amount"}}
84
+ # [
85
+ # {
86
+ # "database": "production",
87
+ # "schema": "shop",
88
+ # "table": "orders",
89
+ # "column": "id",
90
+ # },
91
+ # {
92
+ # "database": "production",
93
+ # "schema": "shop",
94
+ # "table": "orders",
95
+ # "column": "payment.amount",
96
+ # },
97
+ # {
98
+ # "database": "production",
99
+ # "schema": "shop",
100
+ # "table": "orders",
101
+ # "column": "user_id",
102
+ # },
103
+ # ]
85
104
  ```
86
105
 
87
106
  ### analyze_timespan
@@ -116,12 +135,16 @@ where
116
135
 
117
136
  timespans = analyze_timespan(dialects.BigQuery, sql, schema, "production")
118
137
  print(timespans)
119
- # {
120
- # "production.shop.orders.ordered_at": {
138
+ # [
139
+ # {
140
+ # "database": "production",
141
+ # "schema": "shop",
142
+ # "table": "orders",
143
+ # "column": "ordered_at",
121
144
  # "lower": "2025-01-01",
122
- # "upper": "2026-01-01"
145
+ # "upper": "2026-01-01",
123
146
  # }
124
- # }
147
+ # ]
125
148
  ```
126
149
 
127
150
  To make `current_date()` deterministic, pass a provider:
@@ -0,0 +1,247 @@
1
+ import pytest
2
+ from sqlglot import dialects
3
+
4
+ from analytics_query_analyzer.analyzer import analyze
5
+
6
+
7
+ schema = {
8
+ "production": {
9
+ "shop": {
10
+ "orders": {
11
+ "id": "int64",
12
+ "ordered_at": "datetime",
13
+ "user_id": "int64",
14
+ "payment_amount": "int64",
15
+ "payment_method": "string",
16
+ "items": "array<struct<item_id int64, amount int64>>",
17
+ },
18
+ "users": {"id": "int64", "name": "string"},
19
+ "items": {
20
+ "id": "int64",
21
+ "name": "string",
22
+ "brand": {"category": "string", "name": "string"},
23
+ },
24
+ }
25
+ },
26
+ "development": {
27
+ "shop": {
28
+ "users": {"id": "int64", "name": "string"},
29
+ }
30
+ },
31
+ }
32
+
33
+
34
+ test_cases = [
35
+ {"name": "not referencing a table", "sql": "select 1", "expected": []},
36
+ {
37
+ "name": "referencing a table but not columns",
38
+ "sql": "select count(1) from shop.orders",
39
+ "expected": [],
40
+ },
41
+ {
42
+ "name": "simple column reference",
43
+ "sql": "select user_id from shop.orders",
44
+ "expected": [
45
+ {
46
+ "database": "production",
47
+ "schema": "shop",
48
+ "table": "orders",
49
+ "column": "user_id",
50
+ }
51
+ ],
52
+ },
53
+ {
54
+ "name": "qualifying a table with a project",
55
+ "sql": "select user_id from production.shop.orders",
56
+ "expected": [
57
+ {
58
+ "database": "production",
59
+ "schema": "shop",
60
+ "table": "orders",
61
+ "column": "user_id",
62
+ }
63
+ ],
64
+ },
65
+ {
66
+ "name": "qualifying a table with a non-default project",
67
+ "sql": "select id from development.shop.users",
68
+ "expected": [
69
+ {
70
+ "database": "development",
71
+ "schema": "shop",
72
+ "table": "users",
73
+ "column": "id",
74
+ }
75
+ ],
76
+ },
77
+ {
78
+ "name": "referencing a column in a where clause",
79
+ "sql": "select count(1) from shop.orders where ordered_at >= '2026-01-01'",
80
+ "expected": [
81
+ {
82
+ "database": "production",
83
+ "schema": "shop",
84
+ "table": "orders",
85
+ "column": "ordered_at",
86
+ }
87
+ ],
88
+ },
89
+ {
90
+ "name": "referencing a column in a join",
91
+ "sql": "select count(1) from shop.orders join shop.users on orders.user_id = users.id",
92
+ "expected": [
93
+ {
94
+ "database": "production",
95
+ "schema": "shop",
96
+ "table": "orders",
97
+ "column": "user_id",
98
+ },
99
+ {
100
+ "database": "production",
101
+ "schema": "shop",
102
+ "table": "users",
103
+ "column": "id",
104
+ },
105
+ ],
106
+ },
107
+ {
108
+ "name": "referencing a column in an ORDER BY clause",
109
+ "sql": "select user_id from shop.orders order by payment_amount desc",
110
+ "expected": [
111
+ {
112
+ "database": "production",
113
+ "schema": "shop",
114
+ "table": "orders",
115
+ "column": "payment_amount",
116
+ },
117
+ {
118
+ "database": "production",
119
+ "schema": "shop",
120
+ "table": "orders",
121
+ "column": "user_id",
122
+ },
123
+ ],
124
+ },
125
+ {
126
+ "name": "wildcard pattern",
127
+ "sql": "select * from shop.users",
128
+ "expected": [
129
+ {
130
+ "database": "production",
131
+ "schema": "shop",
132
+ "table": "users",
133
+ "column": column,
134
+ }
135
+ for column in schema["production"]["shop"]["users"].keys()
136
+ ],
137
+ },
138
+ {
139
+ "name": "using a wildcard with COUNT",
140
+ "sql": "select count(*) from shop.users",
141
+ "expected": [],
142
+ },
143
+ {
144
+ "name": "SELECT EXCEPT pattern",
145
+ "sql": "select * except (name) from shop.users",
146
+ "expected": [
147
+ {
148
+ "database": "production",
149
+ "schema": "shop",
150
+ "table": "users",
151
+ "column": "id",
152
+ }
153
+ ],
154
+ },
155
+ {
156
+ "name": "CTE pattern",
157
+ "sql": """
158
+ with amount_by_method as (
159
+ select
160
+ payment_method,
161
+ sum(payment_amount) as total_amount
162
+ from
163
+ shop.orders
164
+ group by
165
+ 1
166
+ )
167
+ select
168
+ *
169
+ from
170
+ amount_by_method
171
+ """,
172
+ "expected": [
173
+ {
174
+ "database": "production",
175
+ "schema": "shop",
176
+ "table": "orders",
177
+ "column": "payment_amount",
178
+ },
179
+ {
180
+ "database": "production",
181
+ "schema": "shop",
182
+ "table": "orders",
183
+ "column": "payment_method",
184
+ },
185
+ ],
186
+ },
187
+ {
188
+ "name": "referencing a field of a struct",
189
+ "sql": "select brand.category from shop.items",
190
+ "expected": [
191
+ {
192
+ "database": "production",
193
+ "schema": "shop",
194
+ "table": "items",
195
+ "column": "brand.category",
196
+ }
197
+ ],
198
+ },
199
+ {
200
+ "name": "referencing fields of a struct with a wildcard",
201
+ "sql": "select brand.* from shop.items",
202
+ "expected": [
203
+ {
204
+ "database": "production",
205
+ "schema": "shop",
206
+ "table": "items",
207
+ "column": "brand.*",
208
+ }
209
+ ],
210
+ },
211
+ {
212
+ "name": "referencing a field in an unnested struct",
213
+ "sql": """
214
+ select
215
+ count(1)
216
+ from
217
+ shop.orders
218
+ where
219
+ exists (select 1 from unnest(items) where amount > 1)
220
+ """,
221
+ "expected": [
222
+ {
223
+ "database": "production",
224
+ "schema": "shop",
225
+ "table": "orders",
226
+ "column": "items",
227
+ }
228
+ ],
229
+ },
230
+ ]
231
+
232
+
233
+ @pytest.mark.parametrize(
234
+ ("sql", "expected"),
235
+ [(case["sql"], case["expected"]) for case in test_cases],
236
+ ids=[case["name"] for case in test_cases],
237
+ )
238
+ def test_analyze_case(sql, expected):
239
+ result = analyze(dialects.BigQuery, sql, schema, "production")
240
+ assert _sorted_rows(result) == _sorted_rows(expected)
241
+
242
+
243
+ def _sorted_rows(rows: list[dict[str, str]]) -> list[dict[str, str]]:
244
+ return sorted(
245
+ rows,
246
+ key=lambda row: (row["database"], row["schema"], row["table"], row["column"]),
247
+ )