querygate-spark 0.4.3__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.
- querygate_spark-0.4.3/.gitignore +11 -0
- querygate_spark-0.4.3/CHANGELOG.md +63 -0
- querygate_spark-0.4.3/CITATION.cff +13 -0
- querygate_spark-0.4.3/LICENSE +200 -0
- querygate_spark-0.4.3/PKG-INFO +210 -0
- querygate_spark-0.4.3/README.md +178 -0
- querygate_spark-0.4.3/pyproject.toml +66 -0
- querygate_spark-0.4.3/src/querygate_spark/__init__.py +13 -0
- querygate_spark-0.4.3/src/querygate_spark/cli.py +173 -0
- querygate_spark-0.4.3/src/querygate_spark/config.py +122 -0
- querygate_spark-0.4.3/src/querygate_spark/core.py +209 -0
- querygate_spark-0.4.3/src/querygate_spark/dotenv.py +140 -0
- querygate_spark-0.4.3/src/querygate_spark/llm.py +104 -0
- querygate_spark-0.4.3/src/querygate_spark/parser.py +31 -0
- querygate_spark-0.4.3/src/querygate_spark/prompts/__init__.py +45 -0
- querygate_spark-0.4.3/src/querygate_spark/prompts/error_interpreter.py +85 -0
- querygate_spark-0.4.3/src/querygate_spark/prompts/model_validator.py +1038 -0
- querygate_spark-0.4.3/src/querygate_spark/redact.py +49 -0
- querygate_spark-0.4.3/src/querygate_spark/result.py +49 -0
- querygate_spark-0.4.3/src/querygate_spark/rules.py +647 -0
- querygate_spark-0.4.3/tests/conftest.py +62 -0
- querygate_spark-0.4.3/tests/test_agent_config.py +34 -0
- querygate_spark-0.4.3/tests/test_api_rules.py +28 -0
- querygate_spark-0.4.3/tests/test_cli.py +52 -0
- querygate_spark-0.4.3/tests/test_config.py +56 -0
- querygate_spark-0.4.3/tests/test_dotenv.py +126 -0
- querygate_spark-0.4.3/tests/test_pipeline_llm.py +82 -0
- querygate_spark-0.4.3/tests/test_prompt_templating.py +44 -0
- querygate_spark-0.4.3/tests/test_rules.py +202 -0
- querygate_spark-0.4.3/tests/test_semantic_rules.py +106 -0
- querygate_spark-0.4.3/tests/test_syntax_validator.py +18 -0
- querygate_spark-0.4.3/tests/test_verbose.py +119 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `querygate-spark`.
|
|
4
|
+
|
|
5
|
+
## 0.4.3 (2026-11-29)
|
|
6
|
+
|
|
7
|
+
README fixes for the PyPI page; no code changes.
|
|
8
|
+
|
|
9
|
+
- Fixed a broken inline code span in the ".env file" section (a literal line break where `\n` was meant).
|
|
10
|
+
- Links to `CITATION.cff`, `LICENSE` and `benchmark/LICENSE-DATA` are now absolute GitHub URLs, so they work on
|
|
11
|
+
PyPI.
|
|
12
|
+
- The Python example now defines `sql` before using it, and notes that `latency_ms` has an `llm` entry only when
|
|
13
|
+
the LLM was called.
|
|
14
|
+
|
|
15
|
+
## 0.4.2 (2026-11-29)
|
|
16
|
+
|
|
17
|
+
- **Verbose mode.** `querygate check -v` prints a per-stage trace to stderr:
|
|
18
|
+
- config: the .env file and loaded key names, the resolved mode and why, the model, which variable supplied the
|
|
19
|
+
API key, api_base (scheme and host only)
|
|
20
|
+
- input normalization
|
|
21
|
+
- parser result and timing
|
|
22
|
+
- each rule finding, and whether it short-circuited the LLM
|
|
23
|
+
- the LLM call: which prompt, model, verdict, summary or warning, token usage, JSON fallback used, errors
|
|
24
|
+
- merge and dedup decisions
|
|
25
|
+
- final verdict and exit code
|
|
26
|
+
`-vv` adds the raw LLM response (truncated to 2,000 chars). `--json` stdout stays clean.
|
|
27
|
+
- `Result.trace`: the same per-stage records, always populated, from the Python API.
|
|
28
|
+
- **Redaction safeguard.** Known secret values (the API key in use, and values of environment or .env variables
|
|
29
|
+
whose names contain KEY, TOKEN, SECRET, PASSWORD, CREDENTIAL or AUTH) are replaced with `[REDACTED]` in the trace,
|
|
30
|
+
the raw response, messages and the JSON output.
|
|
31
|
+
|
|
32
|
+
## 0.4.1 (2026-11-29)
|
|
33
|
+
|
|
34
|
+
- **.env support** with no new dependency. QueryGate reads `./.env` from the current working directory (no
|
|
35
|
+
search of parent directories) once per process, on the first `validate()` call or CLI run. It fills in only
|
|
36
|
+
variables that are not already set: kwargs and CLI flags win over real environment variables, which win over
|
|
37
|
+
`.env`.
|
|
38
|
+
- Explicit file: `QUERYGATE_ENV_FILE` or `querygate check --env-file PATH` (must exist).
|
|
39
|
+
- Opt out: `QUERYGATE_DOTENV=0`.
|
|
40
|
+
- Built-in parser: `KEY=VALUE`, `export`, single and double quotes (`\n` escapes in double quotes), `#`
|
|
41
|
+
comments. Malformed lines are skipped. Values are never logged.
|
|
42
|
+
- The sdist excludes `.env` files explicitly.
|
|
43
|
+
|
|
44
|
+
## 0.4.0 (2026-11-28)
|
|
45
|
+
|
|
46
|
+
First PyPI package (published to TestPyPI only).
|
|
47
|
+
|
|
48
|
+
- Library `querygate_spark` with
|
|
49
|
+
`validate(sql, *, model=None, mode=None, api_key=None, api_base=None, timeout=None)`, returning a `Result`
|
|
50
|
+
(verdict valid / warning / invalid / error, messages with their source, stage, latencies, model, raw response,
|
|
51
|
+
notice).
|
|
52
|
+
- Pipeline: SQLGlot parser, then deterministic rules (no schema, no network), then an optional LLM. A certain
|
|
53
|
+
rule finding skips the LLM call. Rule warnings and the LLM's warning are merged.
|
|
54
|
+
- LLM calls through LiteLLM (any provider, including Ollama), replacing the Google ADK runtime. The prompts are
|
|
55
|
+
byte-identical to the benchmarked v0.4 prompts. Temperature 0, JSON output where supported, robust JSON
|
|
56
|
+
extraction otherwise. The ADK runtime remains as an optional example in the repository.
|
|
57
|
+
- Configuration through `QUERYGATE_MODEL`, `QUERYGATE_MODE` (auto / rules / llm), `QUERYGATE_API_KEY`,
|
|
58
|
+
`QUERYGATE_API_BASE`, `QUERYGATE_TIMEOUT`, or kwargs / CLI flags. Auto mode falls back to rules-only with a
|
|
59
|
+
notice when no model, key or litellm is available.
|
|
60
|
+
- CLI `querygate check FILE... | -` with `--model`, `--mode`, `--api-base`, `--timeout`, `--json`. Exit codes 0
|
|
61
|
+
valid, 1 warning, 2 invalid, 3 error.
|
|
62
|
+
- Input normalization (whitespace and trailing `;`), matching the benchmark harness.
|
|
63
|
+
- Core dependency: sqlglot only. Extra `llm`: litellm.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If you use this software or benchmark, please cite it as below."
|
|
3
|
+
title: "QueryGate: pre-execution validation for Spark SQL, with a reproducible benchmark"
|
|
4
|
+
type: software
|
|
5
|
+
authors:
|
|
6
|
+
- family-names: Chaurasia
|
|
7
|
+
given-names: Vishal
|
|
8
|
+
repository-code: "https://github.com/vchaurasia95/querygate-spark-sql"
|
|
9
|
+
license: Apache-2.0
|
|
10
|
+
version: 0.4.3
|
|
11
|
+
date-released: 2026-11-29
|
|
12
|
+
keywords: [Spark SQL, query validation, static analysis, LLM, benchmark]
|
|
13
|
+
doi: 10.5281/zenodo.24633580
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 Vishal Chaurasia
|
|
190
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
191
|
+
you may not use this file except in compliance with the License.
|
|
192
|
+
You may obtain a copy of the License at
|
|
193
|
+
|
|
194
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
195
|
+
|
|
196
|
+
Unless required by applicable law or agreed to in writing, software
|
|
197
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
198
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
199
|
+
See the License for the specific language governing permissions and
|
|
200
|
+
limitations under the License.
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: querygate-spark
|
|
3
|
+
Version: 0.4.3
|
|
4
|
+
Summary: Check Spark SQL before it runs: a SQLGlot parser, deterministic rules for runtime and wrong-answer hazards, and an optional LLM reviewer (any provider via LiteLLM).
|
|
5
|
+
Project-URL: Homepage, https://github.com/vchaurasia95/querygate-spark-sql
|
|
6
|
+
Project-URL: Repository, https://github.com/vchaurasia95/querygate-spark-sql
|
|
7
|
+
Project-URL: Issues, https://github.com/vchaurasia95/querygate-spark-sql/issues
|
|
8
|
+
Project-URL: DOI, https://doi.org/10.5281/zenodo.24633580
|
|
9
|
+
Author: Vishal Chaurasia
|
|
10
|
+
License-Expression: Apache-2.0
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: linter,llm,spark,spark-sql,sql,sqlglot,static-analysis,validation
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Database
|
|
24
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Requires-Dist: sqlglot<31,>=30.0
|
|
27
|
+
Provides-Extra: llm
|
|
28
|
+
Requires-Dist: litellm>=1.40; extra == 'llm'
|
|
29
|
+
Provides-Extra: test
|
|
30
|
+
Requires-Dist: pytest>=7; extra == 'test'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# QueryGate for Spark SQL
|
|
34
|
+
|
|
35
|
+
[](https://doi.org/10.5281/zenodo.24633580)
|
|
36
|
+
|
|
37
|
+
Check Spark SQL **before it runs**. QueryGate combines three layers:
|
|
38
|
+
|
|
39
|
+
1. **Parser**: SQLGlot (Spark dialect) catches grammar errors.
|
|
40
|
+
2. **Deterministic rules**: no schema, no network, about 1 ms. They catch errors that only show up at runtime
|
|
41
|
+
(invalid regex and datetime patterns, literal value errors, `element_at(..., 0)`), window functions in
|
|
42
|
+
WHERE, missing GROUP BY, and text-certain wrong-answer hazards (`= NULL`, comma joins without a predicate,
|
|
43
|
+
LIMIT without ORDER BY, a LEFT JOIN undone by a WHERE filter, NOT IN over a nullable subquery). They warn
|
|
44
|
+
about runtime risks (casts and divisions that may fail under ANSI or silently return NULL).
|
|
45
|
+
3. **Optional LLM reviewer**, through [LiteLLM](https://docs.litellm.ai/) (any provider): explains errors in
|
|
46
|
+
plain language and flags likely wrong-answer bugs. A certain rule finding skips the LLM call.
|
|
47
|
+
|
|
48
|
+
Each query gets a verdict: **valid**, **warning** (it runs, but may fail or return a misleading result) or
|
|
49
|
+
**invalid**. Each verdict comes with short messages that say which part of the query is affected and why.
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install querygate-spark # parser + rules only (no network, no API key)
|
|
55
|
+
pip install "querygate-spark[llm]" # adds the LLM reviewer (LiteLLM)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Python 3.10+.
|
|
59
|
+
|
|
60
|
+
## Quickstart
|
|
61
|
+
|
|
62
|
+
### Command line
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
querygate check query.sql # one or more files
|
|
66
|
+
cat query.sql | querygate check - # stdin
|
|
67
|
+
querygate check --mode rules query.sql # offline, deterministic
|
|
68
|
+
querygate check --json query.sql # machine-readable
|
|
69
|
+
querygate check --model openai/gpt-4o-mini query.sql
|
|
70
|
+
querygate check -v query.sql # trace each stage on stderr (-vv: raw LLM response too)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Exit codes (the worst over all inputs): `0` valid, `1` warning, `2` invalid, `3` error. That makes it usable as a
|
|
74
|
+
CI or pre-commit gate.
|
|
75
|
+
|
|
76
|
+
```text
|
|
77
|
+
$ querygate check --mode rules q.sql
|
|
78
|
+
q.sql: INVALID (stage: rules, mode: rules)
|
|
79
|
+
- [rules] The SELECT list mixes aggregate functions with name, which is not aggregated, and the query has no
|
|
80
|
+
GROUP BY. Spark needs every non-aggregated column to appear in GROUP BY.
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Python
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from querygate_spark import validate
|
|
87
|
+
|
|
88
|
+
sql = "SELECT total / divisor AS ratio FROM payments"
|
|
89
|
+
r = validate(sql)
|
|
90
|
+
r.verdict # "warning"
|
|
91
|
+
r.messages # [Message(source="rules", severity="warning", text="'/ divisor' fails under ANSI ...")]
|
|
92
|
+
r.stage # "rules", "parser", "model_validator" or "error_interpreter"
|
|
93
|
+
r.latency_ms # {"parser": ..., "rules": ..., "total": ...}, plus "llm": ... only when the LLM was called
|
|
94
|
+
r.to_dict()
|
|
95
|
+
|
|
96
|
+
validate(sql, mode="rules") # never calls an LLM
|
|
97
|
+
validate(sql, model="anthropic/claude-sonnet-4-5") # any LiteLLM model string
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Configuration
|
|
101
|
+
|
|
102
|
+
Keyword arguments to `validate()` and CLI flags override environment variables.
|
|
103
|
+
|
|
104
|
+
| Environment variable | CLI flag | Meaning |
|
|
105
|
+
|---|---|---|
|
|
106
|
+
| `QUERYGATE_MODEL` | `--model` | LiteLLM model string. Default `gemini/gemini-2.5-flash` |
|
|
107
|
+
| `QUERYGATE_MODE` | `--mode` | `auto` (default): use the LLM if a model and key are available, otherwise rules-only with a notice. `rules`: never call an LLM. `llm`: require the LLM (error if unavailable) |
|
|
108
|
+
| `QUERYGATE_API_KEY` | | API key passed to LiteLLM (overrides provider variables) |
|
|
109
|
+
| `QUERYGATE_API_BASE` | `--api-base` | Base URL for self-hosted or Ollama endpoints |
|
|
110
|
+
| `QUERYGATE_TIMEOUT` | `--timeout` | LLM request timeout in seconds (default 60) |
|
|
111
|
+
| `QUERYGATE_ENV_FILE` | `--env-file` | Explicit `.env` file to load (default: `./.env` if present) |
|
|
112
|
+
| `QUERYGATE_DOTENV` | | Set to `0` / `false` / `no` to disable `.env` loading |
|
|
113
|
+
| `GEMINI_API_KEY` / `GOOGLE_API_KEY`, `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, ... | | Provider keys, read by LiteLLM from its standard variables |
|
|
114
|
+
|
|
115
|
+
### .env file
|
|
116
|
+
|
|
117
|
+
QueryGate reads a `.env` file from the **current working directory**, if one exists. It does not search parent
|
|
118
|
+
directories. This needs no extra dependency.
|
|
119
|
+
|
|
120
|
+
```dotenv
|
|
121
|
+
# .env (never commit this file)
|
|
122
|
+
QUERYGATE_MODEL=gemini/gemini-2.5-flash
|
|
123
|
+
GEMINI_API_KEY=your-key-here
|
|
124
|
+
# or: QUERYGATE_MODEL=openai/gpt-4o-mini and OPENAI_API_KEY=...
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
- **Precedence:** keyword arguments and CLI flags win over real environment variables, which win over `.env`
|
|
128
|
+
values. A `.env` file only fills in variables that are not already set.
|
|
129
|
+
- **Another file:** `QUERYGATE_ENV_FILE=/path/to/file` or `querygate check --env-file PATH ...`. An explicit file must
|
|
130
|
+
exist. A missing default `./.env` is silently ignored.
|
|
131
|
+
- **Opt out:** set `QUERYGATE_DOTENV=0` (or `false` / `no`).
|
|
132
|
+
- **Format:** `KEY=VALUE`, optional `export`, single or double quotes (`\n` escapes in double quotes), `#` comments. Values are never logged.
|
|
133
|
+
- Keep `.env` out of version control: add it to `.gitignore`.
|
|
134
|
+
|
|
135
|
+
LLM calls use temperature 0 and request JSON output where the provider supports it. Otherwise QueryGate extracts
|
|
136
|
+
the JSON from the text reply. If an LLM call fails in `auto` mode, the rules-only result is returned with a notice.
|
|
137
|
+
|
|
138
|
+
### Verbose output
|
|
139
|
+
|
|
140
|
+
`querygate check -v` prints a trace of every stage to **stderr**, so `--json` output on stdout stays valid JSON.
|
|
141
|
+
`-vv` also prints the raw LLM response (truncated to 2,000 characters). From Python, every `Result` carries the same
|
|
142
|
+
information in `result.trace` (a list of per-stage records).
|
|
143
|
+
|
|
144
|
+
```text
|
|
145
|
+
$ querygate check -v --model gemini/gemini-2.5-flash orders.sql
|
|
146
|
+
[querygate] orders.sql
|
|
147
|
+
config: .env: /work/project/.env (keys: GEMINI_API_KEY, QUERYGATE_MODEL)
|
|
148
|
+
mode: auto -> llm (LLM available)
|
|
149
|
+
model: gemini/gemini-2.5-flash; API key: found via GEMINI_API_KEY (from .env); timeout: 60.0s
|
|
150
|
+
input: 132 -> 131 chars (whitespace / trailing ';' removed)
|
|
151
|
+
parser: parsed (0.41 ms)
|
|
152
|
+
rules: no findings (0.62 ms)
|
|
153
|
+
llm: called model validator on gemini/gemini-2.5-flash (2310 ms)
|
|
154
|
+
tokens: prompt 9120, completion 96, total 9216
|
|
155
|
+
response_format: json_object; JSON: parsed directly
|
|
156
|
+
verdict: warning
|
|
157
|
+
summary: Query is syntactically valid Spark SQL.
|
|
158
|
+
warning: SUM(o.amount) after joining order_items counts each order once per item ...
|
|
159
|
+
merge: kept llm:warning
|
|
160
|
+
final: warning (exit code 1; decided by model_validator)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
The trace never contains secret values. It shows **which variable** supplied the API key, never the key itself,
|
|
164
|
+
and `api_base` is reduced to scheme and host. As a safeguard, any known secret value is scrubbed (`[REDACTED]`) from
|
|
165
|
+
the verbose output, the raw response and the JSON output. Known secrets are the key in use, plus values of
|
|
166
|
+
environment or `.env` variables whose names contain KEY, TOKEN, SECRET, PASSWORD, CREDENTIAL or AUTH.
|
|
167
|
+
|
|
168
|
+
### Providers
|
|
169
|
+
|
|
170
|
+
Any model [LiteLLM supports](https://docs.litellm.ai/docs/providers), for example:
|
|
171
|
+
|
|
172
|
+
| Provider | Model string | Key |
|
|
173
|
+
|---|---|---|
|
|
174
|
+
| Google Gemini | `gemini/gemini-2.5-flash` | `GEMINI_API_KEY` or `GOOGLE_API_KEY` |
|
|
175
|
+
| OpenAI | `openai/gpt-4o-mini` | `OPENAI_API_KEY` |
|
|
176
|
+
| Anthropic | `anthropic/claude-sonnet-4-5` | `ANTHROPIC_API_KEY` |
|
|
177
|
+
| Ollama (local) | `ollama/llama3.1` | none; set `QUERYGATE_API_BASE=http://localhost:11434` |
|
|
178
|
+
|
|
179
|
+
## Accuracy and models
|
|
180
|
+
|
|
181
|
+
The published benchmark results ([`benchmark/results/report.md`](https://github.com/vchaurasia95/querygate-spark-sql/blob/master/benchmark/results/report.md),
|
|
182
|
+
DOI above) were measured with **gemini-2.5-flash** against Apache Spark 4.0.1 ground truth. Quality varies by
|
|
183
|
+
model: smaller or local models may miss more problems or produce more false warnings. The rules layer gives the
|
|
184
|
+
same results with any model, or with none. The benchmark report also compares QueryGate with external
|
|
185
|
+
baselines, including the same model with a plain prompt.
|
|
186
|
+
|
|
187
|
+
## Privacy
|
|
188
|
+
|
|
189
|
+
In `rules` mode nothing leaves your machine. In `llm` and `auto` mode, **the SQL text is sent to the model
|
|
190
|
+
provider you configure** (or to your own endpoint with `QUERYGATE_API_BASE`). QueryGate sends no data
|
|
191
|
+
itself and has no telemetry. Use `--mode rules` (or `QUERYGATE_MODE=rules`) for queries that must not leave your
|
|
192
|
+
environment.
|
|
193
|
+
|
|
194
|
+
## Repository layout
|
|
195
|
+
|
|
196
|
+
```
|
|
197
|
+
src/querygate_spark/ the library (published to PyPI)
|
|
198
|
+
tests/ unit tests (no network)
|
|
199
|
+
benchmark/ queries, Spark ground truth, harness and results (not shipped in the package)
|
|
200
|
+
examples/adk_agent/ the original Google ADK runtime, kept as an optional example
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Citation
|
|
204
|
+
|
|
205
|
+
If you use QueryGate or its benchmark, please cite it via the DOI above (see [CITATION.cff](https://github.com/vchaurasia95/querygate-spark-sql/blob/master/CITATION.cff)).
|
|
206
|
+
|
|
207
|
+
## License
|
|
208
|
+
|
|
209
|
+
Apache License 2.0 (see [LICENSE](https://github.com/vchaurasia95/querygate-spark-sql/blob/master/LICENSE)). Benchmark queries and results under `benchmark/` are licensed CC BY 4.0
|
|
210
|
+
(see [benchmark/LICENSE-DATA](https://github.com/vchaurasia95/querygate-spark-sql/blob/master/benchmark/LICENSE-DATA)).
|