kqlbridge 0.1.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.
- kqlbridge-0.1.0/LICENSE +93 -0
- kqlbridge-0.1.0/PKG-INFO +267 -0
- kqlbridge-0.1.0/README.md +145 -0
- kqlbridge-0.1.0/pyproject.toml +56 -0
- kqlbridge-0.1.0/setup.cfg +4 -0
- kqlbridge-0.1.0/src/kqlbridge/__init__.py +115 -0
- kqlbridge-0.1.0/src/kqlbridge/ast_nodes.py +317 -0
- kqlbridge-0.1.0/src/kqlbridge/generators/__init__.py +4 -0
- kqlbridge-0.1.0/src/kqlbridge/generators/spark_sql.py +433 -0
- kqlbridge-0.1.0/src/kqlbridge/generators/tsql.py +33 -0
- kqlbridge-0.1.0/src/kqlbridge/grammar/kql.lark +188 -0
- kqlbridge-0.1.0/src/kqlbridge/parser.py +468 -0
- kqlbridge-0.1.0/src/kqlbridge/semantic.py +134 -0
- kqlbridge-0.1.0/src/kqlbridge.egg-info/PKG-INFO +267 -0
- kqlbridge-0.1.0/src/kqlbridge.egg-info/SOURCES.txt +18 -0
- kqlbridge-0.1.0/src/kqlbridge.egg-info/dependency_links.txt +1 -0
- kqlbridge-0.1.0/src/kqlbridge.egg-info/entry_points.txt +2 -0
- kqlbridge-0.1.0/src/kqlbridge.egg-info/requires.txt +6 -0
- kqlbridge-0.1.0/src/kqlbridge.egg-info/top_level.txt +1 -0
- kqlbridge-0.1.0/tests/test_operators.py +328 -0
kqlbridge-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
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.
|
|
18
|
+
|
|
19
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
20
|
+
exercising permissions granted by this License.
|
|
21
|
+
|
|
22
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
23
|
+
including but not limited to software source code, documentation
|
|
24
|
+
source, and configuration files.
|
|
25
|
+
|
|
26
|
+
"Object" form shall mean any form resulting from mechanical
|
|
27
|
+
transformation or translation of a Source form, including but
|
|
28
|
+
not limited to compiled object code, generated documentation,
|
|
29
|
+
and conversions to other media types.
|
|
30
|
+
|
|
31
|
+
"Work" shall mean the work of authorship made available under
|
|
32
|
+
the License.
|
|
33
|
+
|
|
34
|
+
"Derivative Works" shall mean any work that is based on the Work.
|
|
35
|
+
|
|
36
|
+
"Contribution" shall mean any work of authorship submitted to the Licensor.
|
|
37
|
+
|
|
38
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
39
|
+
whom a Contribution has been received by the Licensor.
|
|
40
|
+
|
|
41
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
42
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
43
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
44
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
45
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
46
|
+
Work and such Derivative Works in Source or Object form.
|
|
47
|
+
|
|
48
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
49
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
50
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
51
|
+
patent license to make, have made, use, offer to sell, sell,
|
|
52
|
+
import, and otherwise transfer the Work.
|
|
53
|
+
|
|
54
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
55
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
56
|
+
modifications, and in Source or Object form, provided that You
|
|
57
|
+
meet the following conditions:
|
|
58
|
+
|
|
59
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
60
|
+
Works a copy of this License; and
|
|
61
|
+
|
|
62
|
+
(b) You must cause any modified files to carry prominent notices
|
|
63
|
+
stating that You changed the files; and
|
|
64
|
+
|
|
65
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
66
|
+
that You distribute, all copyright, patent, trademark, and
|
|
67
|
+
attribution notices from the Source form of the Work; and
|
|
68
|
+
|
|
69
|
+
(d) If the Work includes a "NOTICE" text file, include a readable
|
|
70
|
+
copy of the attribution notices contained within such NOTICE file.
|
|
71
|
+
|
|
72
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
73
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
74
|
+
shall be under the terms and conditions of this License, without any
|
|
75
|
+
additional terms or conditions.
|
|
76
|
+
|
|
77
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
78
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
79
|
+
|
|
80
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
81
|
+
agreed to in writing, Licensor provides the Work on an "AS IS"
|
|
82
|
+
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
83
|
+
|
|
84
|
+
8. Limitation of Liability. In no event and under no legal theory
|
|
85
|
+
shall any Contributor be liable for any damages arising as a result
|
|
86
|
+
of this License or out of the use or inability to use the Work.
|
|
87
|
+
|
|
88
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
89
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
90
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
91
|
+
or other liability obligations consistent of this License.
|
|
92
|
+
|
|
93
|
+
Copyright 2026 Navakanth — KQLBridge Contributors
|
kqlbridge-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kqlbridge
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: KQL → Spark SQL / T-SQL transpiler for Microsoft Fabric and Databricks
|
|
5
|
+
License: Apache License
|
|
6
|
+
Version 2.0, January 2004
|
|
7
|
+
http://www.apache.org/licenses/
|
|
8
|
+
|
|
9
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
10
|
+
|
|
11
|
+
1. Definitions.
|
|
12
|
+
|
|
13
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
14
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
15
|
+
|
|
16
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
17
|
+
the copyright owner that is granting the License.
|
|
18
|
+
|
|
19
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
20
|
+
other entities that control, are controlled by, or are under common
|
|
21
|
+
control with that 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 made available under
|
|
36
|
+
the License.
|
|
37
|
+
|
|
38
|
+
"Derivative Works" shall mean any work that is based on the Work.
|
|
39
|
+
|
|
40
|
+
"Contribution" shall mean any work of authorship submitted to the Licensor.
|
|
41
|
+
|
|
42
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
43
|
+
whom a Contribution has been received by the Licensor.
|
|
44
|
+
|
|
45
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
46
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
47
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
48
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
49
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
50
|
+
Work and such Derivative Works in Source or Object form.
|
|
51
|
+
|
|
52
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
53
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
54
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
55
|
+
patent license to make, have made, use, offer to sell, sell,
|
|
56
|
+
import, and otherwise transfer the Work.
|
|
57
|
+
|
|
58
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
59
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
60
|
+
modifications, and in Source or Object form, provided that You
|
|
61
|
+
meet the following conditions:
|
|
62
|
+
|
|
63
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
64
|
+
Works a copy of this License; and
|
|
65
|
+
|
|
66
|
+
(b) You must cause any modified files to carry prominent notices
|
|
67
|
+
stating that You changed the files; and
|
|
68
|
+
|
|
69
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
70
|
+
that You distribute, all copyright, patent, trademark, and
|
|
71
|
+
attribution notices from the Source form of the Work; and
|
|
72
|
+
|
|
73
|
+
(d) If the Work includes a "NOTICE" text file, include a readable
|
|
74
|
+
copy of the attribution notices contained within such NOTICE file.
|
|
75
|
+
|
|
76
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
77
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
78
|
+
shall be under the terms and conditions of this License, without any
|
|
79
|
+
additional terms or conditions.
|
|
80
|
+
|
|
81
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
82
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
83
|
+
|
|
84
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
85
|
+
agreed to in writing, Licensor provides the Work on an "AS IS"
|
|
86
|
+
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
87
|
+
|
|
88
|
+
8. Limitation of Liability. In no event and under no legal theory
|
|
89
|
+
shall any Contributor be liable for any damages arising as a result
|
|
90
|
+
of this License or out of the use or inability to use the Work.
|
|
91
|
+
|
|
92
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
93
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
94
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
95
|
+
or other liability obligations consistent of this License.
|
|
96
|
+
|
|
97
|
+
Copyright 2026 Navakanth — KQLBridge Contributors
|
|
98
|
+
|
|
99
|
+
Project-URL: Homepage, https://github.com/navakanth1984/kqlbridge
|
|
100
|
+
Project-URL: Repository, https://github.com/navakanth1984/kqlbridge
|
|
101
|
+
Project-URL: Issues, https://github.com/navakanth1984/kqlbridge/issues
|
|
102
|
+
Keywords: kql,kusto,spark,databricks,fabric,transpiler,sql
|
|
103
|
+
Classifier: Development Status :: 3 - Alpha
|
|
104
|
+
Classifier: Intended Audience :: Developers
|
|
105
|
+
Classifier: Intended Audience :: Science/Research
|
|
106
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
107
|
+
Classifier: Programming Language :: Python :: 3
|
|
108
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
109
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
110
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
111
|
+
Classifier: Topic :: Database
|
|
112
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
113
|
+
Requires-Python: >=3.10
|
|
114
|
+
Description-Content-Type: text/markdown
|
|
115
|
+
License-File: LICENSE
|
|
116
|
+
Requires-Dist: lark>=1.1.9
|
|
117
|
+
Requires-Dist: sqlglot>=25.0.0
|
|
118
|
+
Provides-Extra: dev
|
|
119
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
120
|
+
Requires-Dist: pytest-cov>=5.0; extra == "dev"
|
|
121
|
+
Dynamic: license-file
|
|
122
|
+
|
|
123
|
+
# KQLBridge
|
|
124
|
+
|
|
125
|
+
**KQL → Spark SQL / T-SQL transpiler for Microsoft Fabric and Databricks**
|
|
126
|
+
|
|
127
|
+
[](https://badge.fury.io/py/kqlbridge)
|
|
128
|
+
[](tests/eval/prepare.py)
|
|
129
|
+
[](LICENSE)
|
|
130
|
+
[](pyproject.toml)
|
|
131
|
+
|
|
132
|
+
KQLBridge lets data engineers write queries in **Kusto Query Language (KQL)** and compile them to **Spark SQL** (Databricks, Microsoft Fabric Spark notebooks) or **T-SQL** (Fabric SQL Warehouse, Synapse Analytics).
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
from kqlbridge import translate
|
|
136
|
+
|
|
137
|
+
kql = "AppLogs | where Level == 'Error' | summarize count() by ServiceName"
|
|
138
|
+
sql = translate(kql, target="spark")
|
|
139
|
+
# → SELECT ServiceName, COUNT(*) FROM AppLogs WHERE Level = 'Error' GROUP BY ServiceName
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## Why KQLBridge?
|
|
145
|
+
|
|
146
|
+
Microsoft Fabric runs two query engines side by side: **Eventhouse** (KQL) and **Spark notebooks / SQL Warehouse** (Spark SQL / T-SQL). Teams that wrote years of KQL analytics can't simply copy-paste those queries into a Spark cell. KQLBridge automates the translation.
|
|
147
|
+
|
|
148
|
+
**Use cases:**
|
|
149
|
+
- Migrate ADX / Eventhouse KQL workloads to Databricks or Fabric Spark
|
|
150
|
+
- Build routing agents that pick the right engine per query at runtime
|
|
151
|
+
- Teach polyglot data engineering — learn one language, compile to all targets
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## Installation
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
pip install kqlbridge
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**Requires**: Python 3.10+, no external services, no API keys.
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## Quick Start
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
from kqlbridge import translate, detect_operators, is_supported
|
|
169
|
+
|
|
170
|
+
# Translate KQL to Spark SQL
|
|
171
|
+
sql = translate(
|
|
172
|
+
"AppLogs | where TimeGenerated > ago(24h) | project Message, Level",
|
|
173
|
+
target="spark"
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
# Check which operators are used
|
|
177
|
+
ops = detect_operators("AppLogs | where Level == 'Error' | summarize count() by Host")
|
|
178
|
+
# → ['where', 'summarize']
|
|
179
|
+
|
|
180
|
+
# Check if a query is fully translatable
|
|
181
|
+
if is_supported("AppLogs | make-series count() on TimeGenerated"):
|
|
182
|
+
sql = translate(...)
|
|
183
|
+
else:
|
|
184
|
+
print("Unsupported operators — keep in KQL engine")
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## Supported Operators (v0.1)
|
|
190
|
+
|
|
191
|
+
| KQL Operator | Spark SQL Output | Status |
|
|
192
|
+
|---|---|---|
|
|
193
|
+
| `where` | `WHERE clause` | ✅ v0.1 |
|
|
194
|
+
| `project` | `SELECT columns` | ✅ v0.1 |
|
|
195
|
+
| `summarize count()` | `SELECT COUNT(*) GROUP BY` | ✅ v0.1 |
|
|
196
|
+
| `summarize sum/avg/min/max` | Aggregation functions | ✅ v0.1 |
|
|
197
|
+
| `bin(col, 1h)` | `DATE_TRUNC('hour', col)` | ✅ v0.1 |
|
|
198
|
+
| `ago(1h)` | `CURRENT_TIMESTAMP - INTERVAL '1 hours'` | ✅ v0.1 |
|
|
199
|
+
| `extend` | `SELECT *, expr AS alias` | ✅ v0.1 |
|
|
200
|
+
| `order by` / `sort by` | `ORDER BY col ASC/DESC` | ✅ v0.1 |
|
|
201
|
+
| `take` / `limit` | `LIMIT n` | ✅ v0.1 |
|
|
202
|
+
| `distinct` | `SELECT DISTINCT` | ✅ v0.1 |
|
|
203
|
+
| `join` (inner) | `INNER JOIN ON key` | ✅ v0.1 |
|
|
204
|
+
| `union` | `UNION ALL` | ✅ v0.1 |
|
|
205
|
+
| `let` variables | CTEs (`WITH … AS`) | ✅ v0.1 |
|
|
206
|
+
| `count()` | `COUNT(*)` scalar | ✅ v0.1 |
|
|
207
|
+
|
|
208
|
+
See [supported_operators.md](docs/supported_operators.md) for full reference.
|
|
209
|
+
See [unsupported_operators.md](docs/unsupported_operators.md) for operators with no SQL equivalent.
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Eval Score
|
|
214
|
+
|
|
215
|
+
KQLBridge measures accuracy against a **locked 100-query benchmark** (70 standard, 20 edge-case, 10 adversarial). The eval script is the single source of truth — it is never modified by the agent loop.
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
python tests/eval/prepare.py
|
|
219
|
+
# SCORE: 85.0% (85/100)
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## Architecture
|
|
225
|
+
|
|
226
|
+
```
|
|
227
|
+
KQL input
|
|
228
|
+
→ Lark lexer (LOCKED grammar: kql.lark)
|
|
229
|
+
→ Parser (MODIFIABLE: parser.py)
|
|
230
|
+
→ AST nodes (LOCKED: ast_nodes.py)
|
|
231
|
+
→ Semantic check (LOCKED: semantic.py)
|
|
232
|
+
→ Generator (MODIFIABLE: generators/spark_sql.py)
|
|
233
|
+
→ Spark SQL / T-SQL output
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**Locked files** (oracle, grammar, types) are never touched by the agentic build loop.
|
|
237
|
+
**Modifiable files** (parser, generators) are where improvements happen.
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
## Contributing
|
|
242
|
+
|
|
243
|
+
All contributions must include a corresponding test case in `tests/eval/benchmark.json`.
|
|
244
|
+
PRs that do not include a new test case will not be merged.
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
git clone https://github.com/navakanth1984/kqlbridge
|
|
248
|
+
cd kqlbridge
|
|
249
|
+
pip install -e ".[dev]"
|
|
250
|
+
python tests/eval/prepare.py # baseline score
|
|
251
|
+
pytest tests/ # unit tests
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for full guidelines.
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
## Companion Projects
|
|
259
|
+
|
|
260
|
+
- **PipeQL** — Pipe-first SQL syntax that compiles to the same Spark SQL target (v0.2 roadmap)
|
|
261
|
+
- **DE-Context Kit** — Routing agent + CDLC skill packages using KQLBridge under the hood
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
## License
|
|
266
|
+
|
|
267
|
+
Apache 2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# KQLBridge
|
|
2
|
+
|
|
3
|
+
**KQL → Spark SQL / T-SQL transpiler for Microsoft Fabric and Databricks**
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/py/kqlbridge)
|
|
6
|
+
[](tests/eval/prepare.py)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
[](pyproject.toml)
|
|
9
|
+
|
|
10
|
+
KQLBridge lets data engineers write queries in **Kusto Query Language (KQL)** and compile them to **Spark SQL** (Databricks, Microsoft Fabric Spark notebooks) or **T-SQL** (Fabric SQL Warehouse, Synapse Analytics).
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
from kqlbridge import translate
|
|
14
|
+
|
|
15
|
+
kql = "AppLogs | where Level == 'Error' | summarize count() by ServiceName"
|
|
16
|
+
sql = translate(kql, target="spark")
|
|
17
|
+
# → SELECT ServiceName, COUNT(*) FROM AppLogs WHERE Level = 'Error' GROUP BY ServiceName
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Why KQLBridge?
|
|
23
|
+
|
|
24
|
+
Microsoft Fabric runs two query engines side by side: **Eventhouse** (KQL) and **Spark notebooks / SQL Warehouse** (Spark SQL / T-SQL). Teams that wrote years of KQL analytics can't simply copy-paste those queries into a Spark cell. KQLBridge automates the translation.
|
|
25
|
+
|
|
26
|
+
**Use cases:**
|
|
27
|
+
- Migrate ADX / Eventhouse KQL workloads to Databricks or Fabric Spark
|
|
28
|
+
- Build routing agents that pick the right engine per query at runtime
|
|
29
|
+
- Teach polyglot data engineering — learn one language, compile to all targets
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install kqlbridge
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**Requires**: Python 3.10+, no external services, no API keys.
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Quick Start
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from kqlbridge import translate, detect_operators, is_supported
|
|
47
|
+
|
|
48
|
+
# Translate KQL to Spark SQL
|
|
49
|
+
sql = translate(
|
|
50
|
+
"AppLogs | where TimeGenerated > ago(24h) | project Message, Level",
|
|
51
|
+
target="spark"
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
# Check which operators are used
|
|
55
|
+
ops = detect_operators("AppLogs | where Level == 'Error' | summarize count() by Host")
|
|
56
|
+
# → ['where', 'summarize']
|
|
57
|
+
|
|
58
|
+
# Check if a query is fully translatable
|
|
59
|
+
if is_supported("AppLogs | make-series count() on TimeGenerated"):
|
|
60
|
+
sql = translate(...)
|
|
61
|
+
else:
|
|
62
|
+
print("Unsupported operators — keep in KQL engine")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Supported Operators (v0.1)
|
|
68
|
+
|
|
69
|
+
| KQL Operator | Spark SQL Output | Status |
|
|
70
|
+
|---|---|---|
|
|
71
|
+
| `where` | `WHERE clause` | ✅ v0.1 |
|
|
72
|
+
| `project` | `SELECT columns` | ✅ v0.1 |
|
|
73
|
+
| `summarize count()` | `SELECT COUNT(*) GROUP BY` | ✅ v0.1 |
|
|
74
|
+
| `summarize sum/avg/min/max` | Aggregation functions | ✅ v0.1 |
|
|
75
|
+
| `bin(col, 1h)` | `DATE_TRUNC('hour', col)` | ✅ v0.1 |
|
|
76
|
+
| `ago(1h)` | `CURRENT_TIMESTAMP - INTERVAL '1 hours'` | ✅ v0.1 |
|
|
77
|
+
| `extend` | `SELECT *, expr AS alias` | ✅ v0.1 |
|
|
78
|
+
| `order by` / `sort by` | `ORDER BY col ASC/DESC` | ✅ v0.1 |
|
|
79
|
+
| `take` / `limit` | `LIMIT n` | ✅ v0.1 |
|
|
80
|
+
| `distinct` | `SELECT DISTINCT` | ✅ v0.1 |
|
|
81
|
+
| `join` (inner) | `INNER JOIN ON key` | ✅ v0.1 |
|
|
82
|
+
| `union` | `UNION ALL` | ✅ v0.1 |
|
|
83
|
+
| `let` variables | CTEs (`WITH … AS`) | ✅ v0.1 |
|
|
84
|
+
| `count()` | `COUNT(*)` scalar | ✅ v0.1 |
|
|
85
|
+
|
|
86
|
+
See [supported_operators.md](docs/supported_operators.md) for full reference.
|
|
87
|
+
See [unsupported_operators.md](docs/unsupported_operators.md) for operators with no SQL equivalent.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Eval Score
|
|
92
|
+
|
|
93
|
+
KQLBridge measures accuracy against a **locked 100-query benchmark** (70 standard, 20 edge-case, 10 adversarial). The eval script is the single source of truth — it is never modified by the agent loop.
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
python tests/eval/prepare.py
|
|
97
|
+
# SCORE: 85.0% (85/100)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Architecture
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
KQL input
|
|
106
|
+
→ Lark lexer (LOCKED grammar: kql.lark)
|
|
107
|
+
→ Parser (MODIFIABLE: parser.py)
|
|
108
|
+
→ AST nodes (LOCKED: ast_nodes.py)
|
|
109
|
+
→ Semantic check (LOCKED: semantic.py)
|
|
110
|
+
→ Generator (MODIFIABLE: generators/spark_sql.py)
|
|
111
|
+
→ Spark SQL / T-SQL output
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Locked files** (oracle, grammar, types) are never touched by the agentic build loop.
|
|
115
|
+
**Modifiable files** (parser, generators) are where improvements happen.
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## Contributing
|
|
120
|
+
|
|
121
|
+
All contributions must include a corresponding test case in `tests/eval/benchmark.json`.
|
|
122
|
+
PRs that do not include a new test case will not be merged.
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
git clone https://github.com/navakanth1984/kqlbridge
|
|
126
|
+
cd kqlbridge
|
|
127
|
+
pip install -e ".[dev]"
|
|
128
|
+
python tests/eval/prepare.py # baseline score
|
|
129
|
+
pytest tests/ # unit tests
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for full guidelines.
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Companion Projects
|
|
137
|
+
|
|
138
|
+
- **PipeQL** — Pipe-first SQL syntax that compiles to the same Spark SQL target (v0.2 roadmap)
|
|
139
|
+
- **DE-Context Kit** — Routing agent + CDLC skill packages using KQLBridge under the hood
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
Apache 2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "kqlbridge"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "KQL → Spark SQL / T-SQL transpiler for Microsoft Fabric and Databricks"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
keywords = ["kql", "kusto", "spark", "databricks", "fabric", "transpiler", "sql"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Intended Audience :: Science/Research",
|
|
17
|
+
"License :: OSI Approved :: Apache Software License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Topic :: Database",
|
|
23
|
+
"Topic :: Software Development :: Compilers",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"lark>=1.1.9",
|
|
27
|
+
"sqlglot>=25.0.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
dev = [
|
|
32
|
+
"pytest>=8.0",
|
|
33
|
+
"pytest-cov>=5.0",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/navakanth1984/kqlbridge"
|
|
38
|
+
Repository = "https://github.com/navakanth1984/kqlbridge"
|
|
39
|
+
Issues = "https://github.com/navakanth1984/kqlbridge/issues"
|
|
40
|
+
|
|
41
|
+
[project.scripts]
|
|
42
|
+
kqlbridge = "kqlbridge.cli:main"
|
|
43
|
+
|
|
44
|
+
[tool.setuptools.packages.find]
|
|
45
|
+
where = ["src"]
|
|
46
|
+
|
|
47
|
+
[tool.setuptools.package-data]
|
|
48
|
+
"kqlbridge" = ["grammar/*.lark"]
|
|
49
|
+
|
|
50
|
+
[tool.pytest.ini_options]
|
|
51
|
+
testpaths = ["tests"]
|
|
52
|
+
addopts = "-v --tb=short"
|
|
53
|
+
|
|
54
|
+
[tool.coverage.run]
|
|
55
|
+
source = ["src/kqlbridge"]
|
|
56
|
+
omit = ["tests/eval/*"] # Never instrument the locked oracle
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""
|
|
2
|
+
kqlbridge — KQL to Spark SQL / T-SQL transpiler
|
|
3
|
+
================================================
|
|
4
|
+
Public API:
|
|
5
|
+
|
|
6
|
+
translate(kql, target="spark") → str
|
|
7
|
+
detect_operators(kql) → list[str]
|
|
8
|
+
is_supported(kql) → bool
|
|
9
|
+
check(kql) → SemanticResult
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
from typing import Literal
|
|
14
|
+
|
|
15
|
+
from .parser import parse
|
|
16
|
+
from .semantic import check as _semantic_check, SemanticResult
|
|
17
|
+
from .generators.spark_sql import SparkSQLGenerator
|
|
18
|
+
from .generators.tsql import TSQLGenerator
|
|
19
|
+
|
|
20
|
+
__version__ = "0.1.0"
|
|
21
|
+
__all__ = ["translate", "detect_operators", "is_supported", "check", "__version__"]
|
|
22
|
+
|
|
23
|
+
_SPARK_GEN = SparkSQLGenerator()
|
|
24
|
+
_TSQL_GEN = TSQLGenerator()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def translate(
|
|
28
|
+
kql: str,
|
|
29
|
+
target: Literal["spark", "tsql"] = "spark",
|
|
30
|
+
) -> str:
|
|
31
|
+
"""
|
|
32
|
+
Translate a KQL query string to the target SQL dialect.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
kql: KQL query string
|
|
36
|
+
target: "spark" (default) or "tsql"
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
SQL string in the target dialect
|
|
40
|
+
|
|
41
|
+
Raises:
|
|
42
|
+
lark.exceptions.UnexpectedInput: on KQL parse error
|
|
43
|
+
NotImplementedError: if target generator is not implemented
|
|
44
|
+
ValueError: if query contains unsupported operators (check first)
|
|
45
|
+
"""
|
|
46
|
+
query = parse(kql)
|
|
47
|
+
if target == "spark":
|
|
48
|
+
return _SPARK_GEN.generate(query)
|
|
49
|
+
if target == "tsql":
|
|
50
|
+
return _TSQL_GEN.generate(query)
|
|
51
|
+
raise ValueError(f"Unknown target: {target!r}. Use 'spark' or 'tsql'.")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def detect_operators(kql: str) -> list[str]:
|
|
55
|
+
"""
|
|
56
|
+
Return the list of KQL operators used in a query string.
|
|
57
|
+
|
|
58
|
+
Useful for routing agents that need to decide which engine to use.
|
|
59
|
+
|
|
60
|
+
Example:
|
|
61
|
+
detect_operators("T | where x == 1 | summarize count() by y")
|
|
62
|
+
→ ['where', 'summarize']
|
|
63
|
+
"""
|
|
64
|
+
from .ast_nodes import (
|
|
65
|
+
WhereOp, ProjectOp, SummarizeOp, OrderOp, TakeOp,
|
|
66
|
+
DistinctOp, ExtendOp, JoinOp, UnionOp, CountOp,
|
|
67
|
+
)
|
|
68
|
+
_OP_NAMES = {
|
|
69
|
+
WhereOp: "where",
|
|
70
|
+
ProjectOp: "project",
|
|
71
|
+
SummarizeOp: "summarize",
|
|
72
|
+
OrderOp: "order by",
|
|
73
|
+
TakeOp: "take",
|
|
74
|
+
DistinctOp: "distinct",
|
|
75
|
+
ExtendOp: "extend",
|
|
76
|
+
JoinOp: "join",
|
|
77
|
+
UnionOp: "union",
|
|
78
|
+
CountOp: "count",
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
try:
|
|
82
|
+
query = parse(kql)
|
|
83
|
+
return [_OP_NAMES[type(op)] for op in query.pipes
|
|
84
|
+
if type(op) in _OP_NAMES]
|
|
85
|
+
except Exception:
|
|
86
|
+
return []
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def is_supported(kql: str) -> bool:
|
|
90
|
+
"""
|
|
91
|
+
Return True if the query can be fully translated to Spark SQL.
|
|
92
|
+
|
|
93
|
+
Queries with unsupported operators (make-series, render, etc.)
|
|
94
|
+
return False — the caller should keep those in the native KQL engine.
|
|
95
|
+
"""
|
|
96
|
+
try:
|
|
97
|
+
query = parse(kql)
|
|
98
|
+
result = _semantic_check(query)
|
|
99
|
+
return result.is_supported
|
|
100
|
+
except Exception:
|
|
101
|
+
return False
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def check(kql: str) -> SemanticResult:
|
|
105
|
+
"""
|
|
106
|
+
Parse and semantically validate a KQL query.
|
|
107
|
+
|
|
108
|
+
Returns a SemanticResult with:
|
|
109
|
+
- is_valid: True if AST is structurally sound
|
|
110
|
+
- is_supported: True if query can be fully translated
|
|
111
|
+
- warnings: Non-blocking issues
|
|
112
|
+
- errors: Blocking issues (unsupported operators, ambiguities)
|
|
113
|
+
"""
|
|
114
|
+
query = parse(kql)
|
|
115
|
+
return _semantic_check(query)
|