analytics-query-analyzer 0.4.0__tar.gz → 0.6.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.
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/PKG-INFO +32 -9
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/README.md +31 -8
- analytics_query_analyzer-0.6.0/src/analytics_query_analyzer/__init__.py +4 -0
- analytics_query_analyzer-0.6.0/src/analytics_query_analyzer/analyzer.py +90 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/src/analytics_query_analyzer/references_analyzer.py +13 -10
- analytics_query_analyzer-0.4.0/src/analytics_query_analyzer/timespan_analyzer.py → analytics_query_analyzer-0.6.0/src/analytics_query_analyzer/timebounds_analyzer.py +37 -42
- analytics_query_analyzer-0.6.0/src/analytics_query_analyzer/types.py +14 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/src/analytics_query_analyzer.egg-info/PKG-INFO +32 -9
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/src/analytics_query_analyzer.egg-info/SOURCES.txt +4 -3
- analytics_query_analyzer-0.6.0/tests/test_analyze_with_timebounds_bigquery.py +1737 -0
- analytics_query_analyzer-0.6.0/tests/test_analyze_with_timebounds_redshift.py +1801 -0
- analytics_query_analyzer-0.4.0/src/analytics_query_analyzer/__init__.py +0 -4
- analytics_query_analyzer-0.4.0/src/analytics_query_analyzer/analyzer.py +0 -54
- analytics_query_analyzer-0.4.0/tests/test_analyze_timespan_bigquery.py +0 -905
- analytics_query_analyzer-0.4.0/tests/test_analyze_timespan_redshift.py +0 -576
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/.github/workflows/publish-pypi.yml +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/.github/workflows/publish-testpypi.yml +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/.github/workflows/pytest.yml +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/.gitignore +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/LICENSE +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/pyproject.toml +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/setup.cfg +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/src/analytics_query_analyzer/column_resolver.py +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/src/analytics_query_analyzer/schema_builder.py +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/src/analytics_query_analyzer.egg-info/dependency_links.txt +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/src/analytics_query_analyzer.egg-info/requires.txt +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/src/analytics_query_analyzer.egg-info/top_level.txt +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/tests/test_analyze_bigquery.py +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.6.0}/tests/test_analyze_redshift.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: analytics-query-analyzer
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Analytics Query Analyzer
|
|
5
5
|
License: MIT
|
|
6
6
|
Classifier: License :: OSI Approved :: MIT License
|
|
@@ -103,12 +103,12 @@ print(references)
|
|
|
103
103
|
# ]
|
|
104
104
|
```
|
|
105
105
|
|
|
106
|
-
###
|
|
106
|
+
### analyze with time bounds
|
|
107
107
|
|
|
108
|
-
Extract time bounds from filters
|
|
108
|
+
Extract time bounds from filters by enabling `with_timebounds`.
|
|
109
109
|
|
|
110
110
|
```python
|
|
111
|
-
from analytics_query_analyzer import
|
|
111
|
+
from analytics_query_analyzer import analyze
|
|
112
112
|
from sqlglot import dialects
|
|
113
113
|
|
|
114
114
|
schema = {
|
|
@@ -133,28 +133,51 @@ where
|
|
|
133
133
|
and ordered_at < "2026-01-01"
|
|
134
134
|
"""
|
|
135
135
|
|
|
136
|
-
|
|
137
|
-
|
|
136
|
+
timebounds = analyze(
|
|
137
|
+
dialects.BigQuery,
|
|
138
|
+
sql,
|
|
139
|
+
schema,
|
|
140
|
+
"production",
|
|
141
|
+
with_timebounds=True,
|
|
142
|
+
)
|
|
143
|
+
print(timebounds)
|
|
138
144
|
# [
|
|
139
145
|
# {
|
|
140
146
|
# "database": "production",
|
|
141
147
|
# "schema": "shop",
|
|
142
148
|
# "table": "orders",
|
|
149
|
+
# "column": "id",
|
|
150
|
+
# "lower": None,
|
|
151
|
+
# "upper": None,
|
|
152
|
+
# },
|
|
153
|
+
# {
|
|
154
|
+
# "database": "production",
|
|
155
|
+
# "schema": "shop",
|
|
156
|
+
# "table": "orders",
|
|
143
157
|
# "column": "ordered_at",
|
|
144
158
|
# "lower": "2025-01-01",
|
|
145
159
|
# "upper": "2026-01-01",
|
|
146
|
-
# }
|
|
160
|
+
# },
|
|
161
|
+
# {
|
|
162
|
+
# "database": "production",
|
|
163
|
+
# "schema": "shop",
|
|
164
|
+
# "table": "orders",
|
|
165
|
+
# "column": "user_id",
|
|
166
|
+
# "lower": None,
|
|
167
|
+
# "upper": None,
|
|
168
|
+
# },
|
|
147
169
|
# ]
|
|
148
170
|
```
|
|
149
171
|
|
|
150
172
|
To make `current_date()` deterministic, pass a provider:
|
|
151
173
|
|
|
152
174
|
```python
|
|
153
|
-
|
|
175
|
+
timebounds = analyze(
|
|
154
176
|
dialects.BigQuery,
|
|
155
177
|
"select * from shop.orders where ordered_at >= current_date()",
|
|
156
178
|
schema,
|
|
157
179
|
"production",
|
|
180
|
+
with_timebounds=True,
|
|
158
181
|
current_date_provider=lambda: "2026-01-01",
|
|
159
182
|
)
|
|
160
183
|
```
|
|
@@ -174,7 +197,7 @@ print(schema)
|
|
|
174
197
|
- Authentication uses Application Default Credentials (ADC).
|
|
175
198
|
- When `table` is omitted, it scans all tables in the dataset.
|
|
176
199
|
- When both `dataset` and `table` are omitted, it scans all datasets in the project.
|
|
177
|
-
- The returned `schema` can be passed directly to `analyze
|
|
200
|
+
- The returned `schema` can be passed directly to `analyze`.
|
|
178
201
|
|
|
179
202
|
Fetching from Redshift is also supported:
|
|
180
203
|
|
|
@@ -87,12 +87,12 @@ print(references)
|
|
|
87
87
|
# ]
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
-
###
|
|
90
|
+
### analyze with time bounds
|
|
91
91
|
|
|
92
|
-
Extract time bounds from filters
|
|
92
|
+
Extract time bounds from filters by enabling `with_timebounds`.
|
|
93
93
|
|
|
94
94
|
```python
|
|
95
|
-
from analytics_query_analyzer import
|
|
95
|
+
from analytics_query_analyzer import analyze
|
|
96
96
|
from sqlglot import dialects
|
|
97
97
|
|
|
98
98
|
schema = {
|
|
@@ -117,28 +117,51 @@ where
|
|
|
117
117
|
and ordered_at < "2026-01-01"
|
|
118
118
|
"""
|
|
119
119
|
|
|
120
|
-
|
|
121
|
-
|
|
120
|
+
timebounds = analyze(
|
|
121
|
+
dialects.BigQuery,
|
|
122
|
+
sql,
|
|
123
|
+
schema,
|
|
124
|
+
"production",
|
|
125
|
+
with_timebounds=True,
|
|
126
|
+
)
|
|
127
|
+
print(timebounds)
|
|
122
128
|
# [
|
|
123
129
|
# {
|
|
124
130
|
# "database": "production",
|
|
125
131
|
# "schema": "shop",
|
|
126
132
|
# "table": "orders",
|
|
133
|
+
# "column": "id",
|
|
134
|
+
# "lower": None,
|
|
135
|
+
# "upper": None,
|
|
136
|
+
# },
|
|
137
|
+
# {
|
|
138
|
+
# "database": "production",
|
|
139
|
+
# "schema": "shop",
|
|
140
|
+
# "table": "orders",
|
|
127
141
|
# "column": "ordered_at",
|
|
128
142
|
# "lower": "2025-01-01",
|
|
129
143
|
# "upper": "2026-01-01",
|
|
130
|
-
# }
|
|
144
|
+
# },
|
|
145
|
+
# {
|
|
146
|
+
# "database": "production",
|
|
147
|
+
# "schema": "shop",
|
|
148
|
+
# "table": "orders",
|
|
149
|
+
# "column": "user_id",
|
|
150
|
+
# "lower": None,
|
|
151
|
+
# "upper": None,
|
|
152
|
+
# },
|
|
131
153
|
# ]
|
|
132
154
|
```
|
|
133
155
|
|
|
134
156
|
To make `current_date()` deterministic, pass a provider:
|
|
135
157
|
|
|
136
158
|
```python
|
|
137
|
-
|
|
159
|
+
timebounds = analyze(
|
|
138
160
|
dialects.BigQuery,
|
|
139
161
|
"select * from shop.orders where ordered_at >= current_date()",
|
|
140
162
|
schema,
|
|
141
163
|
"production",
|
|
164
|
+
with_timebounds=True,
|
|
142
165
|
current_date_provider=lambda: "2026-01-01",
|
|
143
166
|
)
|
|
144
167
|
```
|
|
@@ -158,7 +181,7 @@ print(schema)
|
|
|
158
181
|
- Authentication uses Application Default Credentials (ADC).
|
|
159
182
|
- When `table` is omitted, it scans all tables in the dataset.
|
|
160
183
|
- When both `dataset` and `table` are omitted, it scans all datasets in the project.
|
|
161
|
-
- The returned `schema` can be passed directly to `analyze
|
|
184
|
+
- The returned `schema` can be passed directly to `analyze`.
|
|
162
185
|
|
|
163
186
|
Fetching from Redshift is also supported:
|
|
164
187
|
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
from typing import Literal, overload
|
|
2
|
+
|
|
3
|
+
from sqlglot import dialects, optimizer, parse_one
|
|
4
|
+
|
|
5
|
+
from .references_analyzer import ReferencesAnalyzer
|
|
6
|
+
from .timebounds_analyzer import TimeboundsAnalyzer
|
|
7
|
+
from .types import ReferenceRow, TimeboundsRow
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _resolve_dialect(
|
|
11
|
+
dialect: str | type[dialects.Dialect],
|
|
12
|
+
) -> type[dialects.Dialect]:
|
|
13
|
+
if isinstance(dialect, str):
|
|
14
|
+
return dialects.get_or_raise(dialect.lower())
|
|
15
|
+
return dialect
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@overload
|
|
19
|
+
def analyze(
|
|
20
|
+
dialect: str | type[dialects.Dialect],
|
|
21
|
+
sql: str,
|
|
22
|
+
schema: dict,
|
|
23
|
+
default_catalog: str,
|
|
24
|
+
with_timebounds: Literal[False] = False,
|
|
25
|
+
current_date_provider=None,
|
|
26
|
+
) -> list[ReferenceRow]: ...
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@overload
|
|
30
|
+
def analyze(
|
|
31
|
+
dialect: str | type[dialects.Dialect],
|
|
32
|
+
sql: str,
|
|
33
|
+
schema: dict,
|
|
34
|
+
default_catalog: str,
|
|
35
|
+
with_timebounds: Literal[True],
|
|
36
|
+
current_date_provider=None,
|
|
37
|
+
) -> list[TimeboundsRow]: ...
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def analyze(
|
|
41
|
+
dialect: str | type[dialects.Dialect],
|
|
42
|
+
sql: str,
|
|
43
|
+
schema: dict,
|
|
44
|
+
default_catalog: str,
|
|
45
|
+
with_timebounds: bool = False,
|
|
46
|
+
current_date_provider=None,
|
|
47
|
+
) -> list[ReferenceRow] | list[TimeboundsRow]:
|
|
48
|
+
dialect = _resolve_dialect(dialect)
|
|
49
|
+
expression = parse_one(sql, read=dialect)
|
|
50
|
+
|
|
51
|
+
qualified = optimizer.qualify.qualify(
|
|
52
|
+
expression,
|
|
53
|
+
schema=schema,
|
|
54
|
+
catalog=default_catalog,
|
|
55
|
+
validate_qualify_columns=False,
|
|
56
|
+
)
|
|
57
|
+
analyzer = ReferencesAnalyzer(schema, default_catalog)
|
|
58
|
+
references: list[ReferenceRow] = analyzer.analyze(qualified)
|
|
59
|
+
if not with_timebounds:
|
|
60
|
+
return references
|
|
61
|
+
return _merge_timebounds(
|
|
62
|
+
references,
|
|
63
|
+
TimeboundsAnalyzer(schema, default_catalog, current_date_provider).analyze(
|
|
64
|
+
qualified
|
|
65
|
+
),
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _merge_timebounds(
|
|
70
|
+
references: list[ReferenceRow],
|
|
71
|
+
timebounds: list[TimeboundsRow],
|
|
72
|
+
) -> list[TimeboundsRow]:
|
|
73
|
+
merged: dict[tuple[str, str, str, str], TimeboundsRow] = {}
|
|
74
|
+
for row in references:
|
|
75
|
+
key = (row["database"], row["schema"], row["table"], row["column"])
|
|
76
|
+
merged[key] = {**row, "lower": None, "upper": None}
|
|
77
|
+
for row in timebounds:
|
|
78
|
+
key = (row["database"], row["schema"], row["table"], row["column"])
|
|
79
|
+
if key in merged:
|
|
80
|
+
merged[key]["lower"] = row.get("lower")
|
|
81
|
+
merged[key]["upper"] = row.get("upper")
|
|
82
|
+
else:
|
|
83
|
+
merged[key] = row
|
|
84
|
+
return [
|
|
85
|
+
merged[key]
|
|
86
|
+
for key in sorted(merged.keys(), key=lambda k: (k[0], k[1], k[2], k[3]))
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
__all__ = ["analyze"]
|
|
@@ -2,6 +2,7 @@ from sqlglot import exp
|
|
|
2
2
|
from sqlglot.optimizer.scope import traverse_scope
|
|
3
3
|
|
|
4
4
|
from .column_resolver import resolve_column_path
|
|
5
|
+
from .types import ReferenceRow
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
class ReferencesAnalyzer:
|
|
@@ -9,7 +10,7 @@ class ReferencesAnalyzer:
|
|
|
9
10
|
self.schema = schema
|
|
10
11
|
self.default_catalog = default_catalog
|
|
11
12
|
|
|
12
|
-
def analyze(self, expression: exp.Expression) -> list[
|
|
13
|
+
def analyze(self, expression: exp.Expression) -> list[ReferenceRow]:
|
|
13
14
|
references: dict[str, set[str]] = {}
|
|
14
15
|
|
|
15
16
|
for scope in traverse_scope(expression):
|
|
@@ -36,20 +37,22 @@ def _add(references: dict[str, set[str]], table: str, column: str):
|
|
|
36
37
|
references[table] = {column}
|
|
37
38
|
|
|
38
39
|
|
|
39
|
-
def _flatten_references(references: dict[str, set[str]]) -> list[
|
|
40
|
-
rows: list[
|
|
40
|
+
def _flatten_references(references: dict[str, set[str]]) -> list[ReferenceRow]:
|
|
41
|
+
rows: list[ReferenceRow] = []
|
|
41
42
|
for table_path, columns in references.items():
|
|
42
43
|
database, schema, table = table_path.split(".", 2)
|
|
43
44
|
for column in columns:
|
|
44
45
|
rows.append(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
ReferenceRow(
|
|
47
|
+
database=database,
|
|
48
|
+
schema=schema,
|
|
49
|
+
table=table,
|
|
50
|
+
column=column,
|
|
51
|
+
)
|
|
51
52
|
)
|
|
52
|
-
rows.sort(
|
|
53
|
+
rows.sort(
|
|
54
|
+
key=lambda row: (row["database"], row["schema"], row["table"], row["column"])
|
|
55
|
+
)
|
|
53
56
|
return rows
|
|
54
57
|
|
|
55
58
|
|
|
@@ -6,17 +6,18 @@ from sqlglot import exp
|
|
|
6
6
|
from sqlglot.optimizer.scope import traverse_scope
|
|
7
7
|
|
|
8
8
|
from .column_resolver import resolve_column_path
|
|
9
|
+
from .types import TimeboundsRow
|
|
9
10
|
|
|
10
11
|
|
|
11
|
-
class
|
|
12
|
+
class TimeboundsBounds(TypedDict):
|
|
12
13
|
lower: date | None
|
|
13
14
|
upper: date | None
|
|
14
15
|
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
TimeboundsResults: TypeAlias = dict[str, TimeboundsBounds]
|
|
17
18
|
|
|
18
19
|
|
|
19
|
-
class
|
|
20
|
+
class TimeboundsAnalyzer:
|
|
20
21
|
def __init__(
|
|
21
22
|
self,
|
|
22
23
|
schema: dict,
|
|
@@ -27,10 +28,10 @@ class TimespanAnalyzer:
|
|
|
27
28
|
self.default_catalog = default_catalog
|
|
28
29
|
self.current_date_provider = current_date_provider or date.today
|
|
29
30
|
|
|
30
|
-
def analyze(self, expression: exp.Expression) -> list[
|
|
31
|
-
return
|
|
31
|
+
def analyze(self, expression: exp.Expression) -> list[TimeboundsRow]:
|
|
32
|
+
return _flatten_timebounds(self._analyze_internal(expression))
|
|
32
33
|
|
|
33
|
-
def _analyze_internal(self, expression: exp.Expression) ->
|
|
34
|
+
def _analyze_internal(self, expression: exp.Expression) -> TimeboundsResults:
|
|
34
35
|
if isinstance(expression, exp.Union):
|
|
35
36
|
with_expr = expression.args.get("with")
|
|
36
37
|
left = expression.this.copy()
|
|
@@ -43,8 +44,8 @@ class TimespanAnalyzer:
|
|
|
43
44
|
return self.merge_bounds_union(left_bounds, right_bounds)
|
|
44
45
|
return self.analyze_scopes(expression)
|
|
45
46
|
|
|
46
|
-
def analyze_scopes(self, expression: exp.Expression) ->
|
|
47
|
-
scoped_results:
|
|
47
|
+
def analyze_scopes(self, expression: exp.Expression) -> TimeboundsResults:
|
|
48
|
+
scoped_results: TimeboundsResults = {}
|
|
48
49
|
for scope in traverse_scope(expression):
|
|
49
50
|
conditions = []
|
|
50
51
|
where = scope.expression.args.get("where")
|
|
@@ -81,7 +82,9 @@ class TimespanAnalyzer:
|
|
|
81
82
|
|
|
82
83
|
return scoped_results
|
|
83
84
|
|
|
84
|
-
def bounds_for_condition(
|
|
85
|
+
def bounds_for_condition(
|
|
86
|
+
self, condition: exp.Expression, scope
|
|
87
|
+
) -> TimeboundsResults:
|
|
85
88
|
if isinstance(condition, exp.Paren):
|
|
86
89
|
return self.bounds_for_condition(condition.this, scope)
|
|
87
90
|
if isinstance(condition, exp.Not):
|
|
@@ -133,7 +136,7 @@ class TimespanAnalyzer:
|
|
|
133
136
|
|
|
134
137
|
def bounds_for_comparison(
|
|
135
138
|
self, comparison: exp.Expression, scope
|
|
136
|
-
) ->
|
|
139
|
+
) -> TimeboundsResults:
|
|
137
140
|
if isinstance(comparison, exp.Between):
|
|
138
141
|
column = self.extract_column(comparison.this)
|
|
139
142
|
low = self.extract_literal(comparison.args.get("low"))
|
|
@@ -270,7 +273,9 @@ class TimespanAnalyzer:
|
|
|
270
273
|
return columns[0]
|
|
271
274
|
return None
|
|
272
275
|
|
|
273
|
-
def extract_literal(self, expr: exp.Expression) -> date | None:
|
|
276
|
+
def extract_literal(self, expr: exp.Expression | None) -> date | None:
|
|
277
|
+
if expr is None:
|
|
278
|
+
return None
|
|
274
279
|
if isinstance(expr, exp.Literal):
|
|
275
280
|
if expr.is_string:
|
|
276
281
|
return _normalize_iso_date(expr.this)
|
|
@@ -532,9 +537,9 @@ class TimespanAnalyzer:
|
|
|
532
537
|
|
|
533
538
|
@staticmethod
|
|
534
539
|
def merge_bounds_and(
|
|
535
|
-
left:
|
|
536
|
-
) ->
|
|
537
|
-
merged:
|
|
540
|
+
left: TimeboundsResults, right: TimeboundsResults
|
|
541
|
+
) -> TimeboundsResults:
|
|
542
|
+
merged: TimeboundsResults = {}
|
|
538
543
|
for key in set(left) | set(right):
|
|
539
544
|
if key in left and key in right:
|
|
540
545
|
lower_left = left[key]["lower"]
|
|
@@ -562,9 +567,9 @@ class TimespanAnalyzer:
|
|
|
562
567
|
|
|
563
568
|
@staticmethod
|
|
564
569
|
def merge_bounds_or(
|
|
565
|
-
left:
|
|
566
|
-
) ->
|
|
567
|
-
merged:
|
|
570
|
+
left: TimeboundsResults, right: TimeboundsResults
|
|
571
|
+
) -> TimeboundsResults:
|
|
572
|
+
merged: TimeboundsResults = {}
|
|
568
573
|
for key in set(left) & set(right):
|
|
569
574
|
lower_left = left[key]["lower"]
|
|
570
575
|
lower_right = right[key]["lower"]
|
|
@@ -585,13 +590,13 @@ class TimespanAnalyzer:
|
|
|
585
590
|
|
|
586
591
|
@staticmethod
|
|
587
592
|
def merge_bounds_union(
|
|
588
|
-
left:
|
|
589
|
-
) ->
|
|
593
|
+
left: TimeboundsResults, right: TimeboundsResults
|
|
594
|
+
) -> TimeboundsResults:
|
|
590
595
|
if not left:
|
|
591
596
|
return right
|
|
592
597
|
if not right:
|
|
593
598
|
return left
|
|
594
|
-
merged:
|
|
599
|
+
merged: TimeboundsResults = {}
|
|
595
600
|
for key in set(left) | set(right):
|
|
596
601
|
if key not in left:
|
|
597
602
|
merged[key] = right[key]
|
|
@@ -624,31 +629,21 @@ def _normalize_iso_date(value: str) -> date | None:
|
|
|
624
629
|
return None
|
|
625
630
|
|
|
626
631
|
|
|
627
|
-
def
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
"lower": bounds["lower"].isoformat() if bounds["lower"] else None,
|
|
632
|
-
"upper": bounds["upper"].isoformat() if bounds["upper"] else None,
|
|
633
|
-
}
|
|
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]] = []
|
|
632
|
+
def _flatten_timebounds(
|
|
633
|
+
results: TimeboundsResults,
|
|
634
|
+
) -> list[TimeboundsRow]:
|
|
635
|
+
rows: list[TimeboundsRow] = []
|
|
641
636
|
for full_path, bounds in results.items():
|
|
642
637
|
database, schema, table, column = full_path.split(".", 3)
|
|
643
638
|
rows.append(
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
639
|
+
TimeboundsRow(
|
|
640
|
+
database=database,
|
|
641
|
+
schema=schema,
|
|
642
|
+
table=table,
|
|
643
|
+
column=column,
|
|
644
|
+
lower=bounds.get("lower"),
|
|
645
|
+
upper=bounds.get("upper"),
|
|
646
|
+
)
|
|
652
647
|
)
|
|
653
648
|
rows.sort(
|
|
654
649
|
key=lambda row: (
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: analytics-query-analyzer
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Analytics Query Analyzer
|
|
5
5
|
License: MIT
|
|
6
6
|
Classifier: License :: OSI Approved :: MIT License
|
|
@@ -103,12 +103,12 @@ print(references)
|
|
|
103
103
|
# ]
|
|
104
104
|
```
|
|
105
105
|
|
|
106
|
-
###
|
|
106
|
+
### analyze with time bounds
|
|
107
107
|
|
|
108
|
-
Extract time bounds from filters
|
|
108
|
+
Extract time bounds from filters by enabling `with_timebounds`.
|
|
109
109
|
|
|
110
110
|
```python
|
|
111
|
-
from analytics_query_analyzer import
|
|
111
|
+
from analytics_query_analyzer import analyze
|
|
112
112
|
from sqlglot import dialects
|
|
113
113
|
|
|
114
114
|
schema = {
|
|
@@ -133,28 +133,51 @@ where
|
|
|
133
133
|
and ordered_at < "2026-01-01"
|
|
134
134
|
"""
|
|
135
135
|
|
|
136
|
-
|
|
137
|
-
|
|
136
|
+
timebounds = analyze(
|
|
137
|
+
dialects.BigQuery,
|
|
138
|
+
sql,
|
|
139
|
+
schema,
|
|
140
|
+
"production",
|
|
141
|
+
with_timebounds=True,
|
|
142
|
+
)
|
|
143
|
+
print(timebounds)
|
|
138
144
|
# [
|
|
139
145
|
# {
|
|
140
146
|
# "database": "production",
|
|
141
147
|
# "schema": "shop",
|
|
142
148
|
# "table": "orders",
|
|
149
|
+
# "column": "id",
|
|
150
|
+
# "lower": None,
|
|
151
|
+
# "upper": None,
|
|
152
|
+
# },
|
|
153
|
+
# {
|
|
154
|
+
# "database": "production",
|
|
155
|
+
# "schema": "shop",
|
|
156
|
+
# "table": "orders",
|
|
143
157
|
# "column": "ordered_at",
|
|
144
158
|
# "lower": "2025-01-01",
|
|
145
159
|
# "upper": "2026-01-01",
|
|
146
|
-
# }
|
|
160
|
+
# },
|
|
161
|
+
# {
|
|
162
|
+
# "database": "production",
|
|
163
|
+
# "schema": "shop",
|
|
164
|
+
# "table": "orders",
|
|
165
|
+
# "column": "user_id",
|
|
166
|
+
# "lower": None,
|
|
167
|
+
# "upper": None,
|
|
168
|
+
# },
|
|
147
169
|
# ]
|
|
148
170
|
```
|
|
149
171
|
|
|
150
172
|
To make `current_date()` deterministic, pass a provider:
|
|
151
173
|
|
|
152
174
|
```python
|
|
153
|
-
|
|
175
|
+
timebounds = analyze(
|
|
154
176
|
dialects.BigQuery,
|
|
155
177
|
"select * from shop.orders where ordered_at >= current_date()",
|
|
156
178
|
schema,
|
|
157
179
|
"production",
|
|
180
|
+
with_timebounds=True,
|
|
158
181
|
current_date_provider=lambda: "2026-01-01",
|
|
159
182
|
)
|
|
160
183
|
```
|
|
@@ -174,7 +197,7 @@ print(schema)
|
|
|
174
197
|
- Authentication uses Application Default Credentials (ADC).
|
|
175
198
|
- When `table` is omitted, it scans all tables in the dataset.
|
|
176
199
|
- When both `dataset` and `table` are omitted, it scans all datasets in the project.
|
|
177
|
-
- The returned `schema` can be passed directly to `analyze
|
|
200
|
+
- The returned `schema` can be passed directly to `analyze`.
|
|
178
201
|
|
|
179
202
|
Fetching from Redshift is also supported:
|
|
180
203
|
|
|
@@ -10,7 +10,8 @@ src/analytics_query_analyzer/analyzer.py
|
|
|
10
10
|
src/analytics_query_analyzer/column_resolver.py
|
|
11
11
|
src/analytics_query_analyzer/references_analyzer.py
|
|
12
12
|
src/analytics_query_analyzer/schema_builder.py
|
|
13
|
-
src/analytics_query_analyzer/
|
|
13
|
+
src/analytics_query_analyzer/timebounds_analyzer.py
|
|
14
|
+
src/analytics_query_analyzer/types.py
|
|
14
15
|
src/analytics_query_analyzer.egg-info/PKG-INFO
|
|
15
16
|
src/analytics_query_analyzer.egg-info/SOURCES.txt
|
|
16
17
|
src/analytics_query_analyzer.egg-info/dependency_links.txt
|
|
@@ -18,5 +19,5 @@ src/analytics_query_analyzer.egg-info/requires.txt
|
|
|
18
19
|
src/analytics_query_analyzer.egg-info/top_level.txt
|
|
19
20
|
tests/test_analyze_bigquery.py
|
|
20
21
|
tests/test_analyze_redshift.py
|
|
21
|
-
tests/
|
|
22
|
-
tests/
|
|
22
|
+
tests/test_analyze_with_timebounds_bigquery.py
|
|
23
|
+
tests/test_analyze_with_timebounds_redshift.py
|