kql-language-tools
Rust bindings to Microsoft's Kusto.Language library for KQL (Kusto Query Language) validation, completion, and syntax classification.
Overview
This crate provides native Rust access to the official KQL parser and language services via .NET AOT compilation. It enables:
- Syntax Validation - Check KQL queries for syntax errors
- Schema Validation - Validate queries against a database schema (tables, columns, functions)
- Completions - Get intellisense suggestions at cursor position
- Classification - Get syntax highlighting spans for queries
Prerequisites
Required Dependencies by Platform
macOS
# Install .NET SDK via Homebrew
# Or download from Microsoft
# https://dotnet.microsoft.com/download/dotnet/8.0
# Xcode Command Line Tools (for C compiler)
Required:
- .NET 8.0+ SDK
- Xcode Command Line Tools (provides clang)
Linux (Ubuntu/Debian)
# Install .NET SDK
# Option 1: Package manager (Ubuntu 22.04+)
# Option 2: Microsoft packages
# Install build essentials (C compiler)
Required:
- .NET 8.0+ SDK
- GCC or Clang (build-essential package)
Linux (Fedora/RHEL)
# Install .NET SDK
# Install C compiler
Required:
- .NET 8.0+ SDK
- GCC
Windows
# Option 1: Download installer from Microsoft
# https://dotnet.microsoft.com/download/dotnet/8.0
# Option 2: Using winget
winget install Microsoft.DotNet.SDK.8
# Option 3: Using Chocolatey
choco install dotnet-sdk
Required:
- .NET 8.0+ SDK
- Visual Studio Build Tools or Visual Studio with C++ workload
- Download from: https://visualstudio.microsoft.com/visual-cpp-build-tools/
Verifying Installation
# Check .NET SDK version
# Should output 8.0.x or higher
# Check C compiler (macOS/Linux)
# Check C compiler (Windows - in Developer Command Prompt)
Architecture
┌─────────────────────────────────────────────────────────────┐
│ Rust Application │
│ │
│ KqlValidator::new()? │
│ .validate_syntax("T | where x > 1")? │
│ .get_completions("T | ", 4, Some(&schema))? │
│ .get_classifications("T | take 10")? │
└──────────────────────────┬──────────────────────────────────┘
│ C ABI
┌──────────────────────────▼──────────────────────────────────┐
│ .NET Native Library │
│ (KqlLanguageFfiNE.dylib/.so/.dll) │
│ │
│ Microsoft.Azure.Kusto.Language │
│ - KustoCode.ParseAndAnalyze() │
│ - CodeScript.GetCompletionItems() │
│ - Syntax tree classification │
└─────────────────────────────────────────────────────────────┘
Installation
Add to your Cargo.toml:
[]
= "0.1"
Note: The native library is built automatically by cargo build if not present (requires .NET SDK and C compiler).
API Reference
KqlValidator
The main entry point for all language services.
use ;
// Create a validator instance (loads native library)
let validator = new?;
Syntax Validation
Check a query for syntax errors without schema awareness:
let result = validator.validate_syntax?;
if result.is_valid else
Schema Validation
Validate queries against a known schema:
let schema = new
.table;
let result = validator.validate_with_schema?;
// Will report error: "UnknownColumn" not found
assert!;
Completions (Intellisense)
Get completion suggestions at a cursor position:
// Without schema - returns keywords and operators
let completions = validator.get_completions?;
for item in &completions.items
// With schema - includes table and column names
let completions = validator.get_completions?;
// Returns: TimeGenerated, Account, Computer, plus functions...
CompletionItem fields:
label- Display textkind-Keyword,Function,Table,Column, etc.insert_text- Text to insert (if different from label)detail- Brief description or signaturesort_order- Priority (lower = higher priority)edit_start- Character position where replacement starts
Classification (Syntax Highlighting)
Get classified spans for syntax highlighting:
let result = validator.get_classifications?;
for span in &result.spans
// 0..13: Identifier (SecurityEvent)
// 14..15: QueryOperator (|)
// 16..21: QueryOperator (where)
// 22..23: Identifier (x)
// 24..25: ScalarOperator (>)
// 26..27: Literal (1)
ClassificationKind variants:
PlainText,Comment,Punctuation,DirectiveLiteral,StringLiteral,Type,IdentifierColumn,Table,Database,ClusterScalarFunction,AggregateFunctionKeyword,Operator,Variable,ParameterQueryOperator,ScalarOperator
Types
ValidationResult
Diagnostic
Schema
let schema = new
.with_database
.table
.function;
Building the Native Library
Automatic Build (Recommended)
The native library is built automatically when you run cargo build:
The build script will:
- Check if the native library already exists
- If not, detect if .NET SDK is available
- Automatically run the build script to compile the native library
- Provide helpful instructions if .NET SDK is not installed
Prerequisites
- .NET 8.0 SDK or later
- Platform-specific C compiler (Xcode on macOS, gcc on Linux, MSVC on Windows)
Manual Build (Optional)
If you prefer to build manually or need cross-platform builds:
# Build for current platform
# Build for specific platform
# Build all platforms (requires cross-compilation setup)
Supported platforms:
osx-arm64- macOS Apple Siliconosx-x64- macOS Intellinux-x64- Linux x86_64linux-arm64- Linux ARM64win-x64- Windows x86_64win-arm64- Windows ARM64
Output
The build produces:
dotnet/native/{rid}/
├── KqlLanguageFfiNE.dylib # Native entry point (or .so/.dll)
├── KqlLanguageFfi.dll # Managed assembly
├── Kusto.Language.dll # Kusto parser
└── KqlLanguageFfi.runtimeconfig.json
Testing
Run Tests
The library auto-detects DOTNET_ROOT on most systems (including Homebrew installations):
# Run all tests (including integration tests that require native library)
# Run with output visible
# Run specific test
Manual DOTNET_ROOT (if auto-detection fails)
If tests fail with runtime errors, manually set DOTNET_ROOT:
# macOS with Homebrew
# Or find your .NET installation
Test Coverage
| Test | Description |
|---|---|
test_validate_syntax_valid |
Valid query returns no errors |
test_validate_syntax_invalid |
Invalid query returns errors |
test_validate_with_schema |
Schema-aware validation passes |
test_validate_with_schema_unknown_column |
Unknown column detected |
test_get_classifications |
Syntax spans returned |
test_get_completions_after_pipe |
Operators suggested after | |
test_get_completions_with_schema |
Columns suggested with schema |
Library Loading
The native library is searched in this order:
KQL_LANGUAGE_TOOLS_PATHenvironment variable (file or directory)- Same directory as the executable
dotnet/native/{rid}/relative to the crate- Current working directory
To override:
C API Contract
For consumers building their own bindings, the C ABI functions are:
// Lifecycle
int32_t ;
void ;
// Validation
int32_t ;
int32_t ;
// Completions
int32_t ;
// Classification
int32_t ;
// Error retrieval
int32_t ;
Return codes:
> 0- Success, value is JSON length written to output0- Success, empty/valid result-1- Buffer too small-2- Parse error in input-3- Internal error
Platform Support
| Platform | Build | Test | Status |
|---|---|---|---|
| macOS ARM64 | Yes | Yes | Verified |
| macOS x64 | Yes | - | Untested |
| Linux x64 | Yes | - | Untested |
| Linux ARM64 | Yes | - | Untested |
| Windows x64 | Yes | - | Untested |
| Windows ARM64 | Yes | - | Untested |
License
MIT OR Apache-2.0
The Kusto.Language library is provided by Microsoft under its own license terms.