analytics-query-analyzer 0.3.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.3.0 → analytics_query_analyzer-0.5.0}/PKG-INFO +60 -14
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/README.md +59 -13
- 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.3.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer/references_analyzer.py +22 -2
- analytics_query_analyzer-0.3.0/src/analytics_query_analyzer/timespan_analyzer.py → analytics_query_analyzer-0.5.0/src/analytics_query_analyzer/timebounds_analyzer.py +57 -21
- analytics_query_analyzer-0.5.0/src/analytics_query_analyzer/types.py +13 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer.egg-info/PKG-INFO +60 -14
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer.egg-info/SOURCES.txt +4 -3
- analytics_query_analyzer-0.5.0/tests/test_analyze_bigquery.py +247 -0
- analytics_query_analyzer-0.5.0/tests/test_analyze_redshift.py +253 -0
- analytics_query_analyzer-0.5.0/tests/test_analyze_with_timebounds_bigquery.py +1735 -0
- analytics_query_analyzer-0.5.0/tests/test_analyze_with_timebounds_redshift.py +1799 -0
- analytics_query_analyzer-0.3.0/src/analytics_query_analyzer/__init__.py +0 -4
- analytics_query_analyzer-0.3.0/src/analytics_query_analyzer/analyzer.py +0 -54
- analytics_query_analyzer-0.3.0/tests/test_analyze_bigquery.py +0 -142
- analytics_query_analyzer-0.3.0/tests/test_analyze_redshift.py +0 -142
- analytics_query_analyzer-0.3.0/tests/test_analyze_timespan_bigquery.py +0 -588
- analytics_query_analyzer-0.3.0/tests/test_analyze_timespan_redshift.py +0 -608
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/.github/workflows/publish-pypi.yml +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/.github/workflows/publish-testpypi.yml +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/.github/workflows/pytest.yml +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/.gitignore +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/LICENSE +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/pyproject.toml +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/setup.cfg +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer/column_resolver.py +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer/schema_builder.py +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer.egg-info/dependency_links.txt +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.0}/src/analytics_query_analyzer.egg-info/requires.txt +0 -0
- {analytics_query_analyzer-0.3.0 → analytics_query_analyzer-0.5.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
|
+
Version: 0.5.0
|
|
4
4
|
Summary: Analytics Query Analyzer
|
|
5
5
|
License: MIT
|
|
6
6
|
Classifier: License :: OSI Approved :: MIT License
|
|
@@ -81,15 +81,34 @@ from
|
|
|
81
81
|
|
|
82
82
|
references = analyze(dialects.BigQuery, sql, schema, "production")
|
|
83
83
|
print(references)
|
|
84
|
-
#
|
|
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 with time bounds
|
|
88
107
|
|
|
89
|
-
Extract time bounds from filters
|
|
108
|
+
Extract time bounds from filters by enabling `with_timebounds`.
|
|
90
109
|
|
|
91
110
|
```python
|
|
92
|
-
from analytics_query_analyzer import
|
|
111
|
+
from analytics_query_analyzer import analyze
|
|
93
112
|
from sqlglot import dialects
|
|
94
113
|
|
|
95
114
|
schema = {
|
|
@@ -114,24 +133,51 @@ where
|
|
|
114
133
|
and ordered_at < "2026-01-01"
|
|
115
134
|
"""
|
|
116
135
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
136
|
+
timebounds = analyze(
|
|
137
|
+
dialects.BigQuery,
|
|
138
|
+
sql,
|
|
139
|
+
schema,
|
|
140
|
+
"production",
|
|
141
|
+
with_timebounds=True,
|
|
142
|
+
)
|
|
143
|
+
print(timebounds)
|
|
144
|
+
# [
|
|
145
|
+
# {
|
|
146
|
+
# "database": "production",
|
|
147
|
+
# "schema": "shop",
|
|
148
|
+
# "table": "orders",
|
|
149
|
+
# "column": "id",
|
|
150
|
+
# "lower": None,
|
|
151
|
+
# "upper": None,
|
|
152
|
+
# },
|
|
153
|
+
# {
|
|
154
|
+
# "database": "production",
|
|
155
|
+
# "schema": "shop",
|
|
156
|
+
# "table": "orders",
|
|
157
|
+
# "column": "ordered_at",
|
|
121
158
|
# "lower": "2025-01-01",
|
|
122
|
-
# "upper": "2026-01-01"
|
|
123
|
-
# }
|
|
124
|
-
#
|
|
159
|
+
# "upper": "2026-01-01",
|
|
160
|
+
# },
|
|
161
|
+
# {
|
|
162
|
+
# "database": "production",
|
|
163
|
+
# "schema": "shop",
|
|
164
|
+
# "table": "orders",
|
|
165
|
+
# "column": "user_id",
|
|
166
|
+
# "lower": None,
|
|
167
|
+
# "upper": None,
|
|
168
|
+
# },
|
|
169
|
+
# ]
|
|
125
170
|
```
|
|
126
171
|
|
|
127
172
|
To make `current_date()` deterministic, pass a provider:
|
|
128
173
|
|
|
129
174
|
```python
|
|
130
|
-
|
|
175
|
+
timebounds = analyze(
|
|
131
176
|
dialects.BigQuery,
|
|
132
177
|
"select * from shop.orders where ordered_at >= current_date()",
|
|
133
178
|
schema,
|
|
134
179
|
"production",
|
|
180
|
+
with_timebounds=True,
|
|
135
181
|
current_date_provider=lambda: "2026-01-01",
|
|
136
182
|
)
|
|
137
183
|
```
|
|
@@ -151,7 +197,7 @@ print(schema)
|
|
|
151
197
|
- Authentication uses Application Default Credentials (ADC).
|
|
152
198
|
- When `table` is omitted, it scans all tables in the dataset.
|
|
153
199
|
- When both `dataset` and `table` are omitted, it scans all datasets in the project.
|
|
154
|
-
- The returned `schema` can be passed directly to `analyze
|
|
200
|
+
- The returned `schema` can be passed directly to `analyze`.
|
|
155
201
|
|
|
156
202
|
Fetching from Redshift is also supported:
|
|
157
203
|
|
|
@@ -65,15 +65,34 @@ from
|
|
|
65
65
|
|
|
66
66
|
references = analyze(dialects.BigQuery, sql, schema, "production")
|
|
67
67
|
print(references)
|
|
68
|
-
#
|
|
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 with time bounds
|
|
72
91
|
|
|
73
|
-
Extract time bounds from filters
|
|
92
|
+
Extract time bounds from filters by enabling `with_timebounds`.
|
|
74
93
|
|
|
75
94
|
```python
|
|
76
|
-
from analytics_query_analyzer import
|
|
95
|
+
from analytics_query_analyzer import analyze
|
|
77
96
|
from sqlglot import dialects
|
|
78
97
|
|
|
79
98
|
schema = {
|
|
@@ -98,24 +117,51 @@ where
|
|
|
98
117
|
and ordered_at < "2026-01-01"
|
|
99
118
|
"""
|
|
100
119
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
120
|
+
timebounds = analyze(
|
|
121
|
+
dialects.BigQuery,
|
|
122
|
+
sql,
|
|
123
|
+
schema,
|
|
124
|
+
"production",
|
|
125
|
+
with_timebounds=True,
|
|
126
|
+
)
|
|
127
|
+
print(timebounds)
|
|
128
|
+
# [
|
|
129
|
+
# {
|
|
130
|
+
# "database": "production",
|
|
131
|
+
# "schema": "shop",
|
|
132
|
+
# "table": "orders",
|
|
133
|
+
# "column": "id",
|
|
134
|
+
# "lower": None,
|
|
135
|
+
# "upper": None,
|
|
136
|
+
# },
|
|
137
|
+
# {
|
|
138
|
+
# "database": "production",
|
|
139
|
+
# "schema": "shop",
|
|
140
|
+
# "table": "orders",
|
|
141
|
+
# "column": "ordered_at",
|
|
105
142
|
# "lower": "2025-01-01",
|
|
106
|
-
# "upper": "2026-01-01"
|
|
107
|
-
# }
|
|
108
|
-
#
|
|
143
|
+
# "upper": "2026-01-01",
|
|
144
|
+
# },
|
|
145
|
+
# {
|
|
146
|
+
# "database": "production",
|
|
147
|
+
# "schema": "shop",
|
|
148
|
+
# "table": "orders",
|
|
149
|
+
# "column": "user_id",
|
|
150
|
+
# "lower": None,
|
|
151
|
+
# "upper": None,
|
|
152
|
+
# },
|
|
153
|
+
# ]
|
|
109
154
|
```
|
|
110
155
|
|
|
111
156
|
To make `current_date()` deterministic, pass a provider:
|
|
112
157
|
|
|
113
158
|
```python
|
|
114
|
-
|
|
159
|
+
timebounds = analyze(
|
|
115
160
|
dialects.BigQuery,
|
|
116
161
|
"select * from shop.orders where ordered_at >= current_date()",
|
|
117
162
|
schema,
|
|
118
163
|
"production",
|
|
164
|
+
with_timebounds=True,
|
|
119
165
|
current_date_provider=lambda: "2026-01-01",
|
|
120
166
|
)
|
|
121
167
|
```
|
|
@@ -135,7 +181,7 @@ print(schema)
|
|
|
135
181
|
- Authentication uses Application Default Credentials (ADC).
|
|
136
182
|
- When `table` is omitted, it scans all tables in the dataset.
|
|
137
183
|
- When both `dataset` and `table` are omitted, it scans all datasets in the project.
|
|
138
|
-
- The returned `schema` can be passed directly to `analyze
|
|
184
|
+
- The returned `schema` can be passed directly to `analyze`.
|
|
139
185
|
|
|
140
186
|
Fetching from Redshift is also supported:
|
|
141
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) ->
|
|
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):
|
|
@@ -27,7 +28,7 @@ class ReferencesAnalyzer:
|
|
|
27
28
|
table_path = ".".join(full_path.split(".", 3)[:3])
|
|
28
29
|
_add(references, table_path, column_path)
|
|
29
30
|
|
|
30
|
-
return references
|
|
31
|
+
return _flatten_references(references)
|
|
31
32
|
|
|
32
33
|
def _add(references: dict[str, set[str]], table: str, column: str):
|
|
33
34
|
if table in references:
|
|
@@ -36,6 +37,25 @@ def _add(references: dict[str, set[str]], table: str, column: str):
|
|
|
36
37
|
references[table] = {column}
|
|
37
38
|
|
|
38
39
|
|
|
40
|
+
def _flatten_references(references: dict[str, set[str]]) -> list[ReferenceRow]:
|
|
41
|
+
rows: list[ReferenceRow] = []
|
|
42
|
+
for table_path, columns in references.items():
|
|
43
|
+
database, schema, table = table_path.split(".", 2)
|
|
44
|
+
for column in columns:
|
|
45
|
+
rows.append(
|
|
46
|
+
ReferenceRow(
|
|
47
|
+
database=database,
|
|
48
|
+
schema=schema,
|
|
49
|
+
table=table,
|
|
50
|
+
column=column,
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
rows.sort(
|
|
54
|
+
key=lambda row: (row["database"], row["schema"], row["table"], row["column"])
|
|
55
|
+
)
|
|
56
|
+
return rows
|
|
57
|
+
|
|
58
|
+
|
|
39
59
|
def _extract_column(expr: exp.Expression) -> exp.Column | None:
|
|
40
60
|
if isinstance(expr, exp.Column):
|
|
41
61
|
return expr
|
|
@@ -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) ->
|
|
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] = {
|
|
@@ -632,3 +641,30 @@ def _stringify_results(results: TimespanResults) -> dict[str, dict[str, str | No
|
|
|
632
641
|
"upper": bounds["upper"].isoformat() if bounds["upper"] else None,
|
|
633
642
|
}
|
|
634
643
|
return output
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
def _flatten_timebounds(
|
|
647
|
+
results: dict[str, dict[str, str | None]],
|
|
648
|
+
) -> list[TimeboundsRow]:
|
|
649
|
+
rows: list[TimeboundsRow] = []
|
|
650
|
+
for full_path, bounds in results.items():
|
|
651
|
+
database, schema, table, column = full_path.split(".", 3)
|
|
652
|
+
rows.append(
|
|
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
|
+
)
|
|
661
|
+
)
|
|
662
|
+
rows.sort(
|
|
663
|
+
key=lambda row: (
|
|
664
|
+
row["database"] or "",
|
|
665
|
+
row["schema"] or "",
|
|
666
|
+
row["table"] or "",
|
|
667
|
+
row["column"] or "",
|
|
668
|
+
)
|
|
669
|
+
)
|
|
670
|
+
return rows
|
|
@@ -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
|
|
@@ -81,15 +81,34 @@ from
|
|
|
81
81
|
|
|
82
82
|
references = analyze(dialects.BigQuery, sql, schema, "production")
|
|
83
83
|
print(references)
|
|
84
|
-
#
|
|
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 with time bounds
|
|
88
107
|
|
|
89
|
-
Extract time bounds from filters
|
|
108
|
+
Extract time bounds from filters by enabling `with_timebounds`.
|
|
90
109
|
|
|
91
110
|
```python
|
|
92
|
-
from analytics_query_analyzer import
|
|
111
|
+
from analytics_query_analyzer import analyze
|
|
93
112
|
from sqlglot import dialects
|
|
94
113
|
|
|
95
114
|
schema = {
|
|
@@ -114,24 +133,51 @@ where
|
|
|
114
133
|
and ordered_at < "2026-01-01"
|
|
115
134
|
"""
|
|
116
135
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
136
|
+
timebounds = analyze(
|
|
137
|
+
dialects.BigQuery,
|
|
138
|
+
sql,
|
|
139
|
+
schema,
|
|
140
|
+
"production",
|
|
141
|
+
with_timebounds=True,
|
|
142
|
+
)
|
|
143
|
+
print(timebounds)
|
|
144
|
+
# [
|
|
145
|
+
# {
|
|
146
|
+
# "database": "production",
|
|
147
|
+
# "schema": "shop",
|
|
148
|
+
# "table": "orders",
|
|
149
|
+
# "column": "id",
|
|
150
|
+
# "lower": None,
|
|
151
|
+
# "upper": None,
|
|
152
|
+
# },
|
|
153
|
+
# {
|
|
154
|
+
# "database": "production",
|
|
155
|
+
# "schema": "shop",
|
|
156
|
+
# "table": "orders",
|
|
157
|
+
# "column": "ordered_at",
|
|
121
158
|
# "lower": "2025-01-01",
|
|
122
|
-
# "upper": "2026-01-01"
|
|
123
|
-
# }
|
|
124
|
-
#
|
|
159
|
+
# "upper": "2026-01-01",
|
|
160
|
+
# },
|
|
161
|
+
# {
|
|
162
|
+
# "database": "production",
|
|
163
|
+
# "schema": "shop",
|
|
164
|
+
# "table": "orders",
|
|
165
|
+
# "column": "user_id",
|
|
166
|
+
# "lower": None,
|
|
167
|
+
# "upper": None,
|
|
168
|
+
# },
|
|
169
|
+
# ]
|
|
125
170
|
```
|
|
126
171
|
|
|
127
172
|
To make `current_date()` deterministic, pass a provider:
|
|
128
173
|
|
|
129
174
|
```python
|
|
130
|
-
|
|
175
|
+
timebounds = analyze(
|
|
131
176
|
dialects.BigQuery,
|
|
132
177
|
"select * from shop.orders where ordered_at >= current_date()",
|
|
133
178
|
schema,
|
|
134
179
|
"production",
|
|
180
|
+
with_timebounds=True,
|
|
135
181
|
current_date_provider=lambda: "2026-01-01",
|
|
136
182
|
)
|
|
137
183
|
```
|
|
@@ -151,7 +197,7 @@ print(schema)
|
|
|
151
197
|
- Authentication uses Application Default Credentials (ADC).
|
|
152
198
|
- When `table` is omitted, it scans all tables in the dataset.
|
|
153
199
|
- When both `dataset` and `table` are omitted, it scans all datasets in the project.
|
|
154
|
-
- The returned `schema` can be passed directly to `analyze
|
|
200
|
+
- The returned `schema` can be passed directly to `analyze`.
|
|
155
201
|
|
|
156
202
|
Fetching from Redshift is also supported:
|
|
157
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
|