-
Notifications
You must be signed in to change notification settings - Fork 7
DEV-1520: add SQL Server (T-SQL) datasource support #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ZmeiGorynych
merged 12 commits into
main
from
egor/dev-1520-add-sqlserver-datasource-support
Jun 3, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f3e2998
DEV-1520: add SQL Server (T-SQL) datasource support
ZmeiGorynych 4b50b05
DEV-1520: fix Dockerfile (drop redundant pyodbc pip install) and rege…
ZmeiGorynych 220c253
DEV-1520: address process-reviews findings (Group 2 + Group 3)
ZmeiGorynych bd7862f
DEV-1520: fix credential URL-encoding and BIT type mapping
ZmeiGorynych 8076e1c
DEV-1520: fix non-idempotent CREATE DATABASE and T-SQL type-probe LIMIT
ZmeiGorynych e3c1be2
DEV-1520: fix sqlserver example and rowversion type mapping
ZmeiGorynych b182cfa
DEV-1520: fix assert guard, ODBC type map, and TrustServerCertificate…
ZmeiGorynych 64d0abb
DEV-1520: fix S125 false positives, split percentile error messages, …
ZmeiGorynych 1ad4e1e
DEV-1520: add QUARTER test, add SQL Server 2022 docs warning, remove …
ZmeiGorynych c5c7d33
DEV-1520: add test coverage for SQL Server extension ODBC type codes
ZmeiGorynych 5f9a272
DEV-1520: fix stale comment — MySQL and T-SQL use variance-decomposit…
ZmeiGorynych 01c474a
DEV-1520: use ingest_datasource_idempotent in sqlserver example start.sh
ZmeiGorynych File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # SQL Server Example | ||
|
|
||
| This example uses **SQL Server 2022** (`mcr.microsoft.com/mssql/server:2022-latest`). | ||
|
|
||
| ## Important: SQL Server 2022 required | ||
|
|
||
| `DATETRUNC` was introduced in SQL Server 2022. Earlier versions (2019 and older) do not have | ||
| this function and will error on time-dimension queries. The Docker image tag | ||
| `mcr.microsoft.com/mssql/server:2022-latest` is the only supported tag for this example. | ||
|
|
||
| ## ODBC driver dependency | ||
|
|
||
| The seed and SLayer containers use a custom `Dockerfile` (in this directory) that installs | ||
| `msodbcsql18` via the Microsoft apt repository. The driver version is pinned to 18 because | ||
| pyodbc's connection string includes `ODBC+Driver+18+for+SQL+Server`. | ||
|
|
||
| ## Running | ||
|
|
||
| ```bash | ||
| cd examples/sqlserver | ||
| docker compose up -d | ||
| python verify.py | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| FROM python:3.14-slim-bookworm | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Install msodbcsql18 driver (OS-level dependency for pyodbc) | ||
| RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
| curl \ | ||
| gnupg \ | ||
| unixodbc-dev \ | ||
| && curl -fsSL https://packages.microsoft.com/keys/microsoft.asc \ | ||
| | gpg --dearmor -o /usr/share/keyrings/microsoft-prod.gpg \ | ||
| && curl -fsSL https://packages.microsoft.com/config/debian/12/prod.list \ | ||
| > /etc/apt/sources.list.d/mssql-release.list \ | ||
| && apt-get update \ | ||
| && ACCEPT_EULA=Y apt-get install -y --no-install-recommends msodbcsql18 \ | ||
| && rm -rf /var/lib/apt/lists/* | ||
|
|
||
| # Install Python dependencies | ||
| COPY pyproject.toml poetry.lock README.md LICENSE ./ | ||
| RUN pip install --no-cache-dir poetry && \ | ||
| poetry config virtualenvs.create false && \ | ||
| poetry install -E all --no-root --no-interaction --no-ansi && \ | ||
| pip uninstall -y poetry | ||
|
|
||
| # Copy application code and install project | ||
| COPY slayer/ slayer/ | ||
| RUN pip install --no-deps . && \ | ||
| useradd --create-home slayer | ||
| USER slayer | ||
|
|
||
| ENV SLAYER_STORAGE=/data | ||
| EXPOSE 5143 | ||
|
|
||
| CMD ["slayer", "serve", "--host", "0.0.0.0", "--port", "5143", "--storage", "/data"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # SLayer + SQL Server Example | ||
|
|
||
| Runs SLayer against a SQL Server 2022 database using Docker Compose. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Docker and Docker Compose | ||
| - Python 3.11+ | ||
|
|
||
| ## Quick start | ||
|
|
||
| ```bash | ||
| cd examples/sqlserver | ||
| docker compose up -d | ||
| # Wait ~30 s for SQL Server to be ready and the seed to complete, then: | ||
| python verify.py | ||
| ``` | ||
|
|
||
| ## What it does | ||
|
|
||
| 1. Starts a SQL Server 2022 container | ||
| 2. Creates the `slayer_demo` database | ||
| 3. Seeds it with the shared e-commerce dataset (regions, customers, products, orders) | ||
| 4. Starts a SLayer API server on port 5143 | ||
|
|
||
| ## Notes | ||
|
|
||
| - SQL Server 2022 is required — `DATETRUNC` (used for time-dimension truncation) was added in 2022. | ||
| - `median` and `percentile` are not supported on T-SQL; SLayer raises `NotImplementedError` for those. | ||
| - `corr`, `covar_samp`, and `covar_pop` use a variance-decomposition formula (no native T-SQL equivalent). | ||
| - The `Dockerfile` in this directory extends the standard SLayer image with `msodbcsql18` (Microsoft ODBC Driver 18). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| services: | ||
| sqlserver: | ||
| image: mcr.microsoft.com/mssql/server:2022-latest | ||
| environment: | ||
| ACCEPT_EULA: "Y" | ||
| MSSQL_SA_PASSWORD: "YourStrong@Passw0rd" | ||
| MSSQL_PID: "Developer" | ||
| ports: | ||
| - "1433:1433" | ||
| healthcheck: | ||
| test: | ||
| - "CMD-SHELL" | ||
| - > | ||
| /opt/mssql-tools18/bin/sqlcmd | ||
| -S localhost -U sa -P 'YourStrong@Passw0rd' | ||
| -Q 'SELECT 1' -No || exit 1 | ||
| interval: 5s | ||
| timeout: 10s | ||
| retries: 30 | ||
| start_period: 30s | ||
|
|
||
| createdb: | ||
| image: mcr.microsoft.com/mssql/server:2022-latest | ||
| command: > | ||
| /opt/mssql-tools18/bin/sqlcmd | ||
| -S sqlserver -U sa -P 'YourStrong@Passw0rd' | ||
| -Q "IF DB_ID(N'slayer_demo') IS NULL CREATE DATABASE slayer_demo;" -No | ||
| depends_on: | ||
| sqlserver: | ||
| condition: service_healthy | ||
|
|
||
| seed: | ||
| build: | ||
| context: ../.. | ||
| dockerfile: examples/sqlserver/Dockerfile | ||
| command: > | ||
| python /examples/seed.py | ||
| "mssql+pyodbc://sa:YourStrong%40Passw0rd@sqlserver:1433/slayer_demo?driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes" | ||
| volumes: | ||
| - ../seed.py:/examples/seed.py:ro | ||
| depends_on: | ||
| createdb: | ||
| condition: service_completed_successfully | ||
|
|
||
| slayer: | ||
| build: | ||
| context: ../.. | ||
| dockerfile: examples/sqlserver/Dockerfile | ||
| command: sh /examples/start.sh | ||
| ports: | ||
| - "5143:5143" | ||
| volumes: | ||
| - ./start.sh:/examples/start.sh:ro | ||
| - ./slayer_data:/data | ||
| depends_on: | ||
| seed: | ||
| condition: service_completed_successfully |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| * | ||
| !.gitignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #!/bin/sh | ||
| # Ingest models from SQL Server and start the SLayer API server. | ||
|
|
||
| python -c " | ||
| from slayer.async_utils import run_sync | ||
| from slayer.core.models import DatasourceConfig | ||
| from slayer.engine.ingestion import ingest_datasource_idempotent | ||
| from slayer.storage.yaml_storage import YAMLStorage | ||
|
|
||
| storage = YAMLStorage(base_dir='/data') | ||
| ds = DatasourceConfig( | ||
| name='demo', type='mssql', | ||
| host='sqlserver', port=1433, | ||
| database='slayer_demo', username='sa', password='YourStrong@Passw0rd', | ||
| ) | ||
| run_sync(storage.save_datasource(ds)) | ||
| result = run_sync(ingest_datasource_idempotent(datasource=ds, storage=storage)) | ||
| print(f'Ingested {len(result.additions)} models') | ||
| " | ||
|
|
||
| exec slayer serve --host 0.0.0.0 --port 5143 --storage /data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """Verification script for the SQL Server Docker example. | ||
|
|
||
| Run after `docker compose up -d`: | ||
| python examples/sqlserver/verify.py | ||
|
|
||
| SQL Server 2022 supports STDEV/STDEVP/VAR/VARP natively; corr/covar_samp/ | ||
| covar_pop use a variance-decomposition formula (no native function on T-SQL). | ||
| median/percentile are not supported on T-SQL and raise NotImplementedError. | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) | ||
| from verify_common import ( | ||
| run_common_checks, | ||
| check_rollup, | ||
| check_stddev_var, | ||
| check_corr_covar, | ||
| check_column_types, | ||
| summary, | ||
| ) | ||
|
|
||
| if __name__ == "__main__": | ||
| models = run_common_checks() | ||
| check_rollup(expect_rollup=True) | ||
|
|
||
| check_column_types( | ||
| model_name="orders", | ||
| expected_types={ | ||
| "id": "INT", | ||
| "customer_id": "INT", | ||
| "product_id": "INT", | ||
| "quantity": "INT", | ||
| "status": "TEXT", | ||
| "created_at": "TIMESTAMP", | ||
| }, | ||
| ) | ||
| check_column_types( | ||
| model_name="products", | ||
| expected_types={ | ||
| "id": "INT", | ||
| "name": "TEXT", | ||
| "category": "TEXT", | ||
| "price": "DOUBLE", | ||
| }, | ||
| ) | ||
|
|
||
| # T-SQL uses STDEV/STDEVP/VAR/VARP (not stddev_samp etc.) — verified via | ||
| # the SQL generator; the API response is the same regardless of dialect. | ||
| check_stddev_var() | ||
|
|
||
| # corr/covar_samp/covar_pop via variance-decomposition formula. | ||
| check_corr_covar() | ||
|
|
||
| summary() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.