analytics-query-analyzer 0.4.0__tar.gz → 0.5.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.5.0}/PKG-INFO +32 -9
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/README.md +31 -8
- analytics_query_analyzer-0.5.0/src/analytics_query_analyzer/__init__.py +4 -0
- analytics_query_analyzer-0.5.0/src/analytics_query_analyzer/analyzer.py +90 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.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.5.0/src/analytics_query_analyzer/timebounds_analyzer.py +41 -32
- analytics_query_analyzer-0.5.0/src/analytics_query_analyzer/types.py +13 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer.egg-info/PKG-INFO +32 -9
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer.egg-info/SOURCES.txt +4 -3
- analytics_query_analyzer-0.4.0/tests/test_analyze_timespan_bigquery.py → analytics_query_analyzer-0.5.0/tests/test_analyze_with_timebounds_bigquery.py +887 -57
- analytics_query_analyzer-0.5.0/tests/test_analyze_with_timebounds_redshift.py +1799 -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_redshift.py +0 -576
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/.github/workflows/publish-pypi.yml +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/.github/workflows/publish-testpypi.yml +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/.github/workflows/pytest.yml +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/.gitignore +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/LICENSE +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/pyproject.toml +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/setup.cfg +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer/column_resolver.py +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer/schema_builder.py +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer.egg-info/dependency_links.txt +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer.egg-info/requires.txt +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer.egg-info/top_level.txt +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.0}/tests/test_analyze_bigquery.py +0 -0
- {analytics_query_analyzer-0.4.0 → analytics_query_analyzer-0.5.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.5.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,12 @@ 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(
|
|
33
|
+
_stringify_results(self._analyze_internal(expression))
|
|
34
|
+
)
|
|
32
35
|
|
|
33
|
-
def _analyze_internal(self, expression: exp.Expression) ->
|
|
36
|
+
def _analyze_internal(self, expression: exp.Expression) -> TimeboundsResults:
|
|
34
37
|
if isinstance(expression, exp.Union):
|
|
35
38
|
with_expr = expression.args.get("with")
|
|
36
39
|
left = expression.this.copy()
|
|
@@ -43,8 +46,8 @@ class TimespanAnalyzer:
|
|
|
43
46
|
return self.merge_bounds_union(left_bounds, right_bounds)
|
|
44
47
|
return self.analyze_scopes(expression)
|
|
45
48
|
|
|
46
|
-
def analyze_scopes(self, expression: exp.Expression) ->
|
|
47
|
-
scoped_results:
|
|
49
|
+
def analyze_scopes(self, expression: exp.Expression) -> TimeboundsResults:
|
|
50
|
+
scoped_results: TimeboundsResults = {}
|
|
48
51
|
for scope in traverse_scope(expression):
|
|
49
52
|
conditions = []
|
|
50
53
|
where = scope.expression.args.get("where")
|
|
@@ -81,7 +84,9 @@ class TimespanAnalyzer:
|
|
|
81
84
|
|
|
82
85
|
return scoped_results
|
|
83
86
|
|
|
84
|
-
def bounds_for_condition(
|
|
87
|
+
def bounds_for_condition(
|
|
88
|
+
self, condition: exp.Expression, scope
|
|
89
|
+
) -> TimeboundsResults:
|
|
85
90
|
if isinstance(condition, exp.Paren):
|
|
86
91
|
return self.bounds_for_condition(condition.this, scope)
|
|
87
92
|
if isinstance(condition, exp.Not):
|
|
@@ -133,7 +138,7 @@ class TimespanAnalyzer:
|
|
|
133
138
|
|
|
134
139
|
def bounds_for_comparison(
|
|
135
140
|
self, comparison: exp.Expression, scope
|
|
136
|
-
) ->
|
|
141
|
+
) -> TimeboundsResults:
|
|
137
142
|
if isinstance(comparison, exp.Between):
|
|
138
143
|
column = self.extract_column(comparison.this)
|
|
139
144
|
low = self.extract_literal(comparison.args.get("low"))
|
|
@@ -270,7 +275,9 @@ class TimespanAnalyzer:
|
|
|
270
275
|
return columns[0]
|
|
271
276
|
return None
|
|
272
277
|
|
|
273
|
-
def extract_literal(self, expr: exp.Expression) -> date | None:
|
|
278
|
+
def extract_literal(self, expr: exp.Expression | None) -> date | None:
|
|
279
|
+
if expr is None:
|
|
280
|
+
return None
|
|
274
281
|
if isinstance(expr, exp.Literal):
|
|
275
282
|
if expr.is_string:
|
|
276
283
|
return _normalize_iso_date(expr.this)
|
|
@@ -532,9 +539,9 @@ class TimespanAnalyzer:
|
|
|
532
539
|
|
|
533
540
|
@staticmethod
|
|
534
541
|
def merge_bounds_and(
|
|
535
|
-
left:
|
|
536
|
-
) ->
|
|
537
|
-
merged:
|
|
542
|
+
left: TimeboundsResults, right: TimeboundsResults
|
|
543
|
+
) -> TimeboundsResults:
|
|
544
|
+
merged: TimeboundsResults = {}
|
|
538
545
|
for key in set(left) | set(right):
|
|
539
546
|
if key in left and key in right:
|
|
540
547
|
lower_left = left[key]["lower"]
|
|
@@ -562,9 +569,9 @@ class TimespanAnalyzer:
|
|
|
562
569
|
|
|
563
570
|
@staticmethod
|
|
564
571
|
def merge_bounds_or(
|
|
565
|
-
left:
|
|
566
|
-
) ->
|
|
567
|
-
merged:
|
|
572
|
+
left: TimeboundsResults, right: TimeboundsResults
|
|
573
|
+
) -> TimeboundsResults:
|
|
574
|
+
merged: TimeboundsResults = {}
|
|
568
575
|
for key in set(left) & set(right):
|
|
569
576
|
lower_left = left[key]["lower"]
|
|
570
577
|
lower_right = right[key]["lower"]
|
|
@@ -585,13 +592,13 @@ class TimespanAnalyzer:
|
|
|
585
592
|
|
|
586
593
|
@staticmethod
|
|
587
594
|
def merge_bounds_union(
|
|
588
|
-
left:
|
|
589
|
-
) ->
|
|
595
|
+
left: TimeboundsResults, right: TimeboundsResults
|
|
596
|
+
) -> TimeboundsResults:
|
|
590
597
|
if not left:
|
|
591
598
|
return right
|
|
592
599
|
if not right:
|
|
593
600
|
return left
|
|
594
|
-
merged:
|
|
601
|
+
merged: TimeboundsResults = {}
|
|
595
602
|
for key in set(left) | set(right):
|
|
596
603
|
if key not in left:
|
|
597
604
|
merged[key] = right[key]
|
|
@@ -624,7 +631,9 @@ def _normalize_iso_date(value: str) -> date | None:
|
|
|
624
631
|
return None
|
|
625
632
|
|
|
626
633
|
|
|
627
|
-
def _stringify_results(
|
|
634
|
+
def _stringify_results(
|
|
635
|
+
results: TimeboundsResults,
|
|
636
|
+
) -> dict[str, dict[str, str | None]]:
|
|
628
637
|
output: dict[str, dict[str, str | None]] = {}
|
|
629
638
|
for key, bounds in results.items():
|
|
630
639
|
output[key] = {
|
|
@@ -634,21 +643,21 @@ def _stringify_results(results: TimespanResults) -> dict[str, dict[str, str | No
|
|
|
634
643
|
return output
|
|
635
644
|
|
|
636
645
|
|
|
637
|
-
def
|
|
646
|
+
def _flatten_timebounds(
|
|
638
647
|
results: dict[str, dict[str, str | None]],
|
|
639
|
-
) -> list[
|
|
640
|
-
rows: list[
|
|
648
|
+
) -> list[TimeboundsRow]:
|
|
649
|
+
rows: list[TimeboundsRow] = []
|
|
641
650
|
for full_path, bounds in results.items():
|
|
642
651
|
database, schema, table, column = full_path.split(".", 3)
|
|
643
652
|
rows.append(
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
653
|
+
TimeboundsRow(
|
|
654
|
+
database=database,
|
|
655
|
+
schema=schema,
|
|
656
|
+
table=table,
|
|
657
|
+
column=column,
|
|
658
|
+
lower=bounds.get("lower"),
|
|
659
|
+
upper=bounds.get("upper"),
|
|
660
|
+
)
|
|
652
661
|
)
|
|
653
662
|
rows.sort(
|
|
654
663
|
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.5.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
|