Skip to main content

duckdb-kql

CI PyPI Python versions

Run Kusto Query Language (KQL) queries on DuckDB in Python. Develop KQL queries locally, run them in unit tests, as part of CI builds, etc. without the need for network.

import duckdb_kql

con = duckdb_kql.connect()
con.sql("CREATE TABLE Logs AS SELECT * FROM 'logs.parquet'")

duckdb_kql.kql(con, """
    Logs
    | where Timestamp > ago(1d) and Level == "Error"
    | summarize Count = count() by bin(Timestamp, 1h), Component
    | sort by Timestamp asc
""")

Several statements at once — the shape Azure Data Explorer calls a database script, for setting a database up in KQL alone. Blank lines separate the statements:

duckdb_kql.script(con, """
    .set-or-replace Errors <| Logs | where Level == "Error"

    .set-or-replace ByComponent <| Errors | summarize n = count() by Component
""")

Keep that in a .kql file and the CLI runs it, so the file that sets a database up lives in the repository:

duckdb-kql script schema.kql -d logs.duckdb

See the demo notebook.

Install

APIs are split into 3 layers to control features and dependencies. Install based on needs.

Layer Install Scenario Dependencies
0 pip install duckdb-kql Translate KQL queries to SQL antlr4 only
1 pip install duckdb-kql[duckdb] Run KQL queries adds duckdb
2 pip install duckdb-kql[kusto] Run KQL queries via Kusto SDK APIs adds pandas

See Getting started.

To reduce runtime dependencies, translate KQL queries to SQL at build time using the CLI.

duckdb-kql translate -o query.sql query.kql

See Build-time translation.

KustoClient query

Run queries using APIs compatible with Kusto client SDK (azure-kusto-data).

from duckdb_kql.kusto import KustoClient
from duckdb_kql.kusto.helpers import dataframe_from_result_table

client = KustoClient(con)

response = client.execute("NetDefaultDB", """
    Requests
    | where Status >= 500
    | summarize Errors = count() by Service
    | sort by Errors desc
""")

dataframe_from_result_table(table)

See Kusto client.

Local HTTP server

Start a local KQL HTTP server using the CLI, then open https://dataexplorer.azure.com, choose 'Add connection', and enter http://127.0.0.1:31415.

duckdb-kql serve

See Kusto server.

Query validation and translation

Use validate() and to_sql() to respectively validate the KQL query and translate it to SQL.

>>> duckdb_kql.to_sql("print x = 1 + 1")
'SELECT (CAST(1 AS BIGINT) + CAST(1 AS BIGINT)) AS "x"'

>>> duckdb_kql.validate("Logs | where Level ==")
[Diagnostic(span=SourceSpan(line=1, column=21), message="mismatched input '<EOF>' ...")]

Query parameters

Declare parameters and pass values to defend against query injections.

duckdb_kql.kql(con, """
    declare query_parameters(state:string);
    StormEvents | where State == state
""", {"state": user_input})

Coverage

Correctness measured against the real KQL engine (the Kusto Emulator).

Doc-corpus cases matching ground truth 285 of 1036 (0 mismatches)
Azure Monitor's published KQL subset 115 / 119 (96%)
Tabular operators 22 / 42
Scalar functions / aggregates / binary operators 111 / 19 / 33

Supported operators: where, project, project-away, project-rename, extend, summarize, join, mv-expand, distinct, count, sort / order by, top, take / limit, union, macro-expand, lookup, parse, parse-where, render; sources print, datatable, range, and tables; plus let and declare query_parameters.

The support matrix lists every operator, function and type, supported or not, with the known limitations and Kusto discrepancies for each.

Documentation

Document Topics
Getting started Install, first query, the three layers
KQL support matrix Every operator and function, supported or not, each with its gotchas
Build-time CLI Translating .kql to .sql in CI, to avoid a runtime dependency
Local Kusto endpoint duckdb-kql serve — query a DuckDB file from the Azure Data Explorer UI
API reference Every public function and type
Kusto SDK compatibility What Layer 2 implements, no-ops, and refuses
Azure Monitor profile Coverage against a published KQL subset
docs/TRANSLATION.md Normative KQL→DuckDB mapping spec (R1–R16)
docs/create-database.md Reference notes for .create database, reconstructed from the KQL parser
docs/implementation-plan.md Architecture and milestones
docs/test-plan.md Corpus harvesting, oracle, divergence catalog
docs/kql-on-duckdb-landscape.md Survey of existing KQL-on-DuckDB work
docs/implementation-options.md Six approaches considered, with the chosen one
docs/m0-grammar-spike.md Grammar viability result
docs/frequency-scan-results.md What KQL constructs actually get used
docs/licensing.md Third-party licensing review
demo/ Notebook tour of all three layers, with outputs
CONTRIBUTING.md How to add a mapping, and when not to
SECURITY.md Reporting vulnerabilities; what is in scope
Releases What changed, and when

Design

The KQL parser is generated by ANTLR from Microsoft's KQL grammar. Translation targets DuckDB SQL as a chain of Common Table Expressions (CTEs), one per KQL operator. DuckDB handles query optimization and execution.

Development

pip install -e ".[dev]"
pytest

tools/regen_parser.sh        # regenerate the parser (maintainers; needs Java)

The acceptance suite compares against the Kusto Emulator, which runs in Docker; see docs/oracle-harness.md.

License

MIT. See LICENSE, THIRD-PARTY-NOTICES.md, and licenses/.

Trademarks

"DuckDB" is a trademark of the DuckDB Foundation. "Kusto", "Azure", and "Azure Data Explorer" are trademarks of Microsoft Corporation. This project is independent and is not affiliated with, endorsed by, or sponsored by either the DuckDB Foundation or Microsoft. Product names are used descriptively, to indicate compatibility.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

duckdb_kql-0.0.11.tar.gz (808.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

duckdb_kql-0.0.11-py3-none-any.whl (434.8 kB view details)

Uploaded Python 3

File details

Details for the file duckdb_kql-0.0.11.tar.gz.

File metadata

  • Download URL: duckdb_kql-0.0.11.tar.gz
  • Upload date:
  • Size: 808.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for duckdb_kql-0.0.11.tar.gz
Algorithm Hash digest
SHA256 585e1f3933696c21893dac6829d75f25f8af549c39f7b4a33a25fc1948bc955b
MD5 1ea20623bd5b629c2d8bc6a34a95084e
BLAKE2b-256 3d525ce773139405b0d28dcf7a898e211c1cf8f2cf4850f203c070e4ea81e1ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for duckdb_kql-0.0.11.tar.gz:

Publisher: release.yml on mmaitre314/duckdb-kql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file duckdb_kql-0.0.11-py3-none-any.whl.

File metadata

  • Download URL: duckdb_kql-0.0.11-py3-none-any.whl
  • Upload date:
  • Size: 434.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for duckdb_kql-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 ba425e39caf08accd10513247f071896fedcde1ef4ed4c82cd076473d94d7f05
MD5 199f11471219d6f76038bd74ebd87c7b
BLAKE2b-256 47764f69ef051273d677981f535f3f86329880840d102d55e3b8cce04a466f71

See more details on using hashes here.

Provenance

The following attestation bundles were made for duckdb_kql-0.0.11-py3-none-any.whl:

Publisher: release.yml on mmaitre314/duckdb-kql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.0.15

2 files

0.0.14

2 files

0.0.13

2 files

0.0.12

2 files

This release

0.0.11 This release

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page