analytics-query-analyzer 0.2.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 (22) hide show
  1. analytics_query_analyzer-0.2.0/.github/workflows/publish-pypi.yml +34 -0
  2. analytics_query_analyzer-0.2.0/.github/workflows/publish-testpypi.yml +33 -0
  3. analytics_query_analyzer-0.2.0/.github/workflows/pytest.yml +27 -0
  4. analytics_query_analyzer-0.2.0/.gitignore +207 -0
  5. analytics_query_analyzer-0.2.0/LICENSE +21 -0
  6. analytics_query_analyzer-0.2.0/PKG-INFO +149 -0
  7. analytics_query_analyzer-0.2.0/README.md +135 -0
  8. analytics_query_analyzer-0.2.0/pyproject.toml +33 -0
  9. analytics_query_analyzer-0.2.0/setup.cfg +4 -0
  10. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer/__init__.py +4 -0
  11. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer/analyzer.py +54 -0
  12. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer/column_resolver.py +62 -0
  13. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer/references_analyzer.py +49 -0
  14. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer/schema_builder.py +71 -0
  15. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer/timespan_analyzer.py +596 -0
  16. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer.egg-info/PKG-INFO +149 -0
  17. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer.egg-info/SOURCES.txt +20 -0
  18. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer.egg-info/dependency_links.txt +1 -0
  19. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer.egg-info/requires.txt +4 -0
  20. analytics_query_analyzer-0.2.0/src/analytics_query_analyzer.egg-info/top_level.txt +1 -0
  21. analytics_query_analyzer-0.2.0/tests/test_analyze.py +142 -0
  22. analytics_query_analyzer-0.2.0/tests/test_analyze_timespan.py +588 -0
@@ -0,0 +1,34 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ jobs:
8
+ build-and-publish:
9
+ runs-on: ubuntu-latest
10
+ permissions:
11
+ contents: read
12
+ id-token: write
13
+
14
+ steps:
15
+ - name: Checkout
16
+ uses: actions/checkout@v4
17
+ with:
18
+ fetch-depth: 0
19
+
20
+ - name: Set up Python
21
+ uses: actions/setup-python@v5
22
+ with:
23
+ python-version: "3.13"
24
+
25
+ - name: Install build
26
+ run: python -m pip install --upgrade build
27
+
28
+ - name: Build
29
+ run: python -m build
30
+
31
+ - name: Publish to PyPI
32
+ uses: pypa/gh-action-pypi-publish@release/v1
33
+ with:
34
+ verbose: true
@@ -0,0 +1,33 @@
1
+ name: Publish to TestPyPI
2
+
3
+ on:
4
+ workflow_dispatch:
5
+
6
+ jobs:
7
+ build-and-publish:
8
+ runs-on: ubuntu-latest
9
+ permissions:
10
+ contents: read
11
+ id-token: write
12
+
13
+ steps:
14
+ - name: Checkout
15
+ uses: actions/checkout@v4
16
+ with:
17
+ fetch-depth: 0
18
+
19
+ - name: Set up Python
20
+ uses: actions/setup-python@v5
21
+ with:
22
+ python-version: "3.13"
23
+
24
+ - name: Install build
25
+ run: python -m pip install --upgrade build
26
+
27
+ - name: Build
28
+ run: python -m build
29
+
30
+ - name: Publish to TestPyPI
31
+ uses: pypa/gh-action-pypi-publish@release/v1
32
+ with:
33
+ repository-url: https://test.pypi.org/legacy/
@@ -0,0 +1,27 @@
1
+ name: pytest
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - "**"
7
+
8
+ jobs:
9
+ pytest:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - name: Checkout
14
+ uses: actions/checkout@v4
15
+
16
+ - name: Set up Python
17
+ uses: actions/setup-python@v5
18
+ with:
19
+ python-version: "3.13"
20
+
21
+ - name: Install dependencies
22
+ run: |
23
+ python -m pip install --upgrade pip
24
+ python -m pip install -e . pytest
25
+
26
+ - name: Run pytest
27
+ run: pytest
@@ -0,0 +1,207 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+ #poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ #pdm.lock
116
+ #pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ #pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # SageMath parsed files
135
+ *.sage.py
136
+
137
+ # Environments
138
+ .env
139
+ .envrc
140
+ .venv
141
+ env/
142
+ venv/
143
+ ENV/
144
+ env.bak/
145
+ venv.bak/
146
+
147
+ # Spyder project settings
148
+ .spyderproject
149
+ .spyproject
150
+
151
+ # Rope project settings
152
+ .ropeproject
153
+
154
+ # mkdocs documentation
155
+ /site
156
+
157
+ # mypy
158
+ .mypy_cache/
159
+ .dmypy.json
160
+ dmypy.json
161
+
162
+ # Pyre type checker
163
+ .pyre/
164
+
165
+ # pytype static type analyzer
166
+ .pytype/
167
+
168
+ # Cython debug symbols
169
+ cython_debug/
170
+
171
+ # PyCharm
172
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
175
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
176
+ #.idea/
177
+
178
+ # Abstra
179
+ # Abstra is an AI-powered process automation framework.
180
+ # Ignore directories containing user credentials, local state, and settings.
181
+ # Learn more at https://abstra.io/docs
182
+ .abstra/
183
+
184
+ # Visual Studio Code
185
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
188
+ # you could uncomment the following to ignore the entire vscode folder
189
+ # .vscode/
190
+
191
+ # Ruff stuff:
192
+ .ruff_cache/
193
+
194
+ # PyPI configuration file
195
+ .pypirc
196
+
197
+ # Cursor
198
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200
+ # refer to https://docs.cursor.com/context/ignore-files
201
+ .cursorignore
202
+ .cursorindexingignore
203
+
204
+ # Marimo
205
+ marimo/_static/
206
+ marimo/_lsp/
207
+ __marimo__/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ryo Iketani
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,149 @@
1
+ Metadata-Version: 2.4
2
+ Name: analytics-query-analyzer
3
+ Version: 0.2.0
4
+ Summary: Analytics Query Analyzer
5
+ License: MIT
6
+ Classifier: License :: OSI Approved :: MIT License
7
+ Requires-Python: >=3.13
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: sqlglot>=28
11
+ Provides-Extra: bigquery
12
+ Requires-Dist: google-cloud-bigquery; extra == "bigquery"
13
+ Dynamic: license-file
14
+
15
+ # analytics-query-analyzer
16
+
17
+ Analyze analytics SQL to extract referenced tables/columns and time bounds.
18
+
19
+ ## Install
20
+
21
+ Base install:
22
+
23
+ ```bash
24
+ pip install analytics-query-analyzer
25
+ ```
26
+
27
+ BigQuery extras (only needed for `build_schema`):
28
+
29
+ ```bash
30
+ pip install analytics-query-analyzer[bigquery]
31
+ ```
32
+
33
+ ## Support
34
+
35
+ BigQuery is currently supported and tested.
36
+
37
+ ## Schema format
38
+
39
+ Schemas follow sqlglot conventions, with nested fields represented as nested dicts.
40
+
41
+ ## Usage
42
+
43
+ ### analyze
44
+
45
+ Extract table/column references from a query.
46
+
47
+ ```python
48
+ from analytics_query_analyzer import analyze
49
+ from sqlglot import dialects
50
+
51
+ schema = {
52
+ "production": {
53
+ "shop": {
54
+ "orders": {
55
+ "id": "int64",
56
+ "ordered_at": "datetime",
57
+ "user_id": "int64",
58
+ "payment": {
59
+ "amount": "int64",
60
+ "method": "string",
61
+ },
62
+ },
63
+ },
64
+ },
65
+ }
66
+
67
+ sql = """
68
+ select
69
+ id,
70
+ user_id,
71
+ payment.amount
72
+ from
73
+ shop.orders
74
+ """
75
+
76
+ references = analyze(dialects.BigQuery, sql, schema, "production")
77
+ print(references)
78
+ # {"production.shop.orders": {"id", "user_id", "payment.amount"}}
79
+ ```
80
+
81
+ ### analyze_timespan
82
+
83
+ Extract time bounds from filters.
84
+
85
+ ```python
86
+ from analytics_query_analyzer import analyze_timespan
87
+ from sqlglot import dialects
88
+
89
+ schema = {
90
+ "production": {
91
+ "shop": {
92
+ "orders": {
93
+ "id": "int64",
94
+ "ordered_at": "datetime",
95
+ "user_id": "int64",
96
+ },
97
+ },
98
+ },
99
+ }
100
+
101
+ sql = """
102
+ select
103
+ *
104
+ from
105
+ shop.orders
106
+ where
107
+ ordered_at >= "2025-01-01"
108
+ and ordered_at < "2026-01-01"
109
+ """
110
+
111
+ timespans = analyze_timespan(dialects.BigQuery, sql, schema, "production")
112
+ print(timespans)
113
+ # {
114
+ # "production.shop.orders.ordered_at": {
115
+ # "lower": "2025-01-01",
116
+ # "upper": "2026-01-01"
117
+ # }
118
+ # }
119
+ ```
120
+
121
+ To make `current_date()` deterministic, pass a provider:
122
+
123
+ ```python
124
+ timespans = analyze_timespan(
125
+ dialects.BigQuery,
126
+ "select * from shop.orders where ordered_at >= current_date()",
127
+ schema,
128
+ "production",
129
+ current_date_provider=lambda: "2026-01-01",
130
+ )
131
+ ```
132
+
133
+ ### build_schema
134
+
135
+ Fetch a schema dictionary from BigQuery.
136
+
137
+ ```python
138
+ from analytics_query_analyzer import build_schema
139
+ from sqlglot import dialects
140
+
141
+ schema = build_schema(dialects.BigQuery, "my_project", "my_schema", "my_table")
142
+ print(schema)
143
+ ```
144
+
145
+ - Authentication uses Application Default Credentials (ADC).
146
+ - Only BigQuery is supported for `build_schema`.
147
+ - When `table` is omitted, it scans all tables in the dataset.
148
+ - When both `dataset` and `table` are omitted, it scans all datasets in the project.
149
+ - The returned `schema` can be passed directly to `analyze` and `analyze_timespan`.
@@ -0,0 +1,135 @@
1
+ # analytics-query-analyzer
2
+
3
+ Analyze analytics SQL to extract referenced tables/columns and time bounds.
4
+
5
+ ## Install
6
+
7
+ Base install:
8
+
9
+ ```bash
10
+ pip install analytics-query-analyzer
11
+ ```
12
+
13
+ BigQuery extras (only needed for `build_schema`):
14
+
15
+ ```bash
16
+ pip install analytics-query-analyzer[bigquery]
17
+ ```
18
+
19
+ ## Support
20
+
21
+ BigQuery is currently supported and tested.
22
+
23
+ ## Schema format
24
+
25
+ Schemas follow sqlglot conventions, with nested fields represented as nested dicts.
26
+
27
+ ## Usage
28
+
29
+ ### analyze
30
+
31
+ Extract table/column references from a query.
32
+
33
+ ```python
34
+ from analytics_query_analyzer import analyze
35
+ from sqlglot import dialects
36
+
37
+ schema = {
38
+ "production": {
39
+ "shop": {
40
+ "orders": {
41
+ "id": "int64",
42
+ "ordered_at": "datetime",
43
+ "user_id": "int64",
44
+ "payment": {
45
+ "amount": "int64",
46
+ "method": "string",
47
+ },
48
+ },
49
+ },
50
+ },
51
+ }
52
+
53
+ sql = """
54
+ select
55
+ id,
56
+ user_id,
57
+ payment.amount
58
+ from
59
+ shop.orders
60
+ """
61
+
62
+ references = analyze(dialects.BigQuery, sql, schema, "production")
63
+ print(references)
64
+ # {"production.shop.orders": {"id", "user_id", "payment.amount"}}
65
+ ```
66
+
67
+ ### analyze_timespan
68
+
69
+ Extract time bounds from filters.
70
+
71
+ ```python
72
+ from analytics_query_analyzer import analyze_timespan
73
+ from sqlglot import dialects
74
+
75
+ schema = {
76
+ "production": {
77
+ "shop": {
78
+ "orders": {
79
+ "id": "int64",
80
+ "ordered_at": "datetime",
81
+ "user_id": "int64",
82
+ },
83
+ },
84
+ },
85
+ }
86
+
87
+ sql = """
88
+ select
89
+ *
90
+ from
91
+ shop.orders
92
+ where
93
+ ordered_at >= "2025-01-01"
94
+ and ordered_at < "2026-01-01"
95
+ """
96
+
97
+ timespans = analyze_timespan(dialects.BigQuery, sql, schema, "production")
98
+ print(timespans)
99
+ # {
100
+ # "production.shop.orders.ordered_at": {
101
+ # "lower": "2025-01-01",
102
+ # "upper": "2026-01-01"
103
+ # }
104
+ # }
105
+ ```
106
+
107
+ To make `current_date()` deterministic, pass a provider:
108
+
109
+ ```python
110
+ timespans = analyze_timespan(
111
+ dialects.BigQuery,
112
+ "select * from shop.orders where ordered_at >= current_date()",
113
+ schema,
114
+ "production",
115
+ current_date_provider=lambda: "2026-01-01",
116
+ )
117
+ ```
118
+
119
+ ### build_schema
120
+
121
+ Fetch a schema dictionary from BigQuery.
122
+
123
+ ```python
124
+ from analytics_query_analyzer import build_schema
125
+ from sqlglot import dialects
126
+
127
+ schema = build_schema(dialects.BigQuery, "my_project", "my_schema", "my_table")
128
+ print(schema)
129
+ ```
130
+
131
+ - Authentication uses Application Default Credentials (ADC).
132
+ - Only BigQuery is supported for `build_schema`.
133
+ - When `table` is omitted, it scans all tables in the dataset.
134
+ - When both `dataset` and `table` are omitted, it scans all datasets in the project.
135
+ - The returned `schema` can be passed directly to `analyze` and `analyze_timespan`.
@@ -0,0 +1,33 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "setuptools_scm>=8", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "analytics-query-analyzer"
7
+ description = "Analytics Query Analyzer"
8
+ readme = "README.md"
9
+ requires-python = ">=3.13"
10
+ license = {text = "MIT"}
11
+ classifiers = [
12
+ "License :: OSI Approved :: MIT License",
13
+ ]
14
+ dependencies = [
15
+ "sqlglot>=28",
16
+ ]
17
+ dynamic = ["version"]
18
+
19
+ [project.optional-dependencies]
20
+ bigquery = ["google-cloud-bigquery"]
21
+
22
+ [tool.setuptools]
23
+ package-dir = {"" = "src"}
24
+
25
+ [tool.setuptools.packages.find]
26
+ where = ["src"]
27
+
28
+ [tool.setuptools_scm]
29
+ version_scheme = "post-release"
30
+ local_scheme = "no-local-version"
31
+
32
+ [tool.pytest.ini_options]
33
+ pythonpath = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,4 @@
1
+ from .analyzer import analyze, analyze_timespan
2
+ from .schema_builder import build_schema
3
+
4
+ __all__ = ["analyze", "analyze_timespan", "build_schema"]
@@ -0,0 +1,54 @@
1
+ from sqlglot import dialects, optimizer, parse_one
2
+
3
+ from .references_analyzer import ReferencesAnalyzer
4
+ from .timespan_analyzer import TimespanAnalyzer
5
+
6
+
7
+ def _resolve_dialect(
8
+ dialect: str | type[dialects.Dialect],
9
+ ) -> type[dialects.Dialect]:
10
+ if isinstance(dialect, str):
11
+ return dialects.get_or_raise(dialect.lower())
12
+ return dialect
13
+
14
+
15
+ def analyze(
16
+ dialect: str | type[dialects.Dialect],
17
+ sql: str,
18
+ schema: dict,
19
+ default_project: str,
20
+ ):
21
+ dialect = _resolve_dialect(dialect)
22
+ expression = parse_one(sql, read=dialect)
23
+
24
+ qualified = optimizer.qualify.qualify(
25
+ expression,
26
+ schema=schema,
27
+ catalog=default_project,
28
+ validate_qualify_columns=False,
29
+ )
30
+ analyzer = ReferencesAnalyzer(schema, default_project)
31
+ return analyzer.analyze(qualified)
32
+
33
+
34
+ def analyze_timespan(
35
+ dialect: str | type[dialects.Dialect],
36
+ sql: str,
37
+ schema: dict,
38
+ default_project: str,
39
+ current_date_provider=None,
40
+ ) -> dict:
41
+ dialect = _resolve_dialect(dialect)
42
+ expression = parse_one(sql, read=dialect)
43
+
44
+ qualified = optimizer.qualify.qualify(
45
+ expression,
46
+ schema=schema,
47
+ catalog=default_project,
48
+ validate_qualify_columns=False,
49
+ )
50
+ analyzer = TimespanAnalyzer(schema, default_project, current_date_provider)
51
+ return analyzer.analyze(qualified)
52
+
53
+
54
+ __all__ = ["analyze", "analyze_timespan"]