haystak-tds-spi

It's a Go library you drop into your own application so that your app can answer SQL queries as if it were Microsoft SQL Server — letting everyday SQL tools like Excel, Power BI, and SSMS talk to whatever data you've got behind it, whether that's Mongo, Elasticsearch, OpenSearch, or whatever you build.
What it means for you
If you're deciding whether to adopt it — your analysts already have the tools (Excel, Power BI, SSMS); your data increasingly doesn't live in a SQL database. This puts a SQL Server face on the stores that don't have one, so the tools and skills you already pay for reach that data directly. Apache-2.0, one binary, nothing licensed per source — no platform to buy, no cluster to run.
If you're an analyst — or the developer wiring it up for one — if your tool connects to Microsoft SQL Server, it connects to this: same login dialog, the familiar SQL Server SQL you already write — JOINs, aggregates, pivots — over a backend (Mongo, Elasticsearch, your own store) you never have to think about. Nothing installs on anyone's machine — one Go binary listens on :1433 and every existing SQL Server client connects unchanged, because the translation lives server-side, in one place, not bolted onto every tool.
If you're putting your own store behind it — implement one Go interface (tds.Backend) plus a query path, and the library supplies the rest: T-SQL parsing, JOINs, aggregates, CTEs, and the catalog (INFORMATION_SCHEMA/sys.*). A thin backend returns whole tables and the engine evaluates the query; a thick backend pushes down to your store's native language. Writes, DDL, transactions, and auth are opt-in — advertise what you support via Caps, and the engine fails closed on the rest. A built-in conformance suite (tdstest.RunConformance) verifies that what you advertise matches what your backend actually does.
If you work at the wire — it speaks native TDS (PRELOGIN/LOGIN7, SQL_BATCH, RPC sp_executesql, TLS-in-TDS), not an ODBC/JDBC client spec — so from the client's seat there's no shim, it is a SQL Server on :1433. Pure Go, no CGO, permissive dependencies only, cross-compiles to one static binary.
Ready-to-run backends
These examples are complete, live-validated gateways — go run one and a real document store answers T-SQL on :1433 (joins, aggregates, INSERT/UPDATE/DELETE, INFORMATION_SCHEMA/sys.* and all). Each takes an optional --host <url:port> (default localhost) and --db <name> (the database/index scope; blank seeds demo data so it works immediately):
go run ./examples/elasticsearch-community # seeds + serves a local ES on :1433
sqlcmd -S 127.0.0.1,1433 -U sa -P x -C -Q "SELECT u.name, o.amount FROM users u JOIN orders o ON u.id = o.user_id"
The inferred variants need no schema — point them at a store and they sample documents to build the SQL catalog. The declared variants add the one thing inference can't recover, foreign keys, so cross-table JOINs and sys.foreign_keys light up. (examples/inmem and examples/gateway are the dependency-free reference backends.)
Each document-store example is its own Go module (its own go.mod, with a replace to the parent) so the MongoDB / Elasticsearch / OpenSearch drivers never become dependencies of the core library. Because of that they aren't published as separate pkg.go.dev pages — browse them on GitHub under examples/, each with its own README and runnable code. Only examples/inmem and examples/gateway (which share the root module) appear on pkg.go.dev.
The technical bits
A SQL Server wire-protocol (TDS) gateway, shipped as importable Go modules. It speaks TDS (the protocol SQL Server clients talk) on the front, and translates through a pluggable backend SPI to whatever store sits behind it. Any TDS client (sqlcmd, SSMS, Power BI, the go-mssqldb/.NET/JDBC drivers, MCP SQL tools) can connect, browse the catalog, and run real SQL against your backend: rich reads plus INSERT/UPDATE/DELETE and CREATE/DROP TABLE.
Pure Go, no CGO, permissive dependencies only.
Status: read and write are complete and live-validated over the wire (plaintext and TLS). That covers the full read query surface plus INSERT/UPDATE/DELETE and CREATE/DROP TABLE/DATABASE, routed to the backend's write interfaces and fail-closed when unsupported. Out of the box, the examples/ directory gives you a working MSSQL/TDS interface to MongoDB, Elasticsearch, and OpenSearch right now — run one, point sqlcmd/SSMS/Power BI at it, and query a NoSQL store as if it were SQL Server. Object Explorer is verified end to end — Databases → Tables → Columns, plus "Select Top N Rows" and ad-hoc queries — against Microsoft SQL Server Management Studio 22, alongside sqlcmd, Power BI, and the go-mssqldb driver.
Install
go get github.com/RSKGroup/haystak-tds-spi
Implement tds.Backend, then serve it with server.ListenAndServe. See Build a backend below, the runnable examples on pkg.go.dev, and the complete backends in examples/ to copy from.
Architecture
TDS client ──wire──> server ──> engine ──> your Backend (SPI)
│ │ │ implements tds.Backend (+ Scanner/QueryExecutor,
PRELOGIN/LOGIN7 dispatch parse T-SQL optionally Writer/DDL/Databaser/…)
TLS, SQL_BATCH, RPC + evaluate
server listens, runs the TDS handshake (including TLS-in-TDS), decodes SQL_BATCH and RPC sp_executesql, and streams COLMETADATA/ROW/DONE back.
internal/{tsql,engine,exec} parse a T-SQL read subset, route by capability, and evaluate it (filter/join/aggregate/set-ops/subqueries/CTEs/expressions) for thin backends.
internal/{infoschema,sysviews} answer INFORMATION_SCHEMA.* and sys.* from your declared catalog (tables, columns, types, foreign keys).
internal/extensions/ is the feature surface — views, procedures (and their procedural constructs), and scalar catalog functions — kept separate from the core engine and reached only through a Runner seam. This is where new SQL-Server capability is added.
tds is the SPI you implement.
The core (wire/tsql/exec/engine) is stable; new capability lands in internal/extensions/, one file per construct/function. See ARCHITECTURE.md for the layered map and CONTRIBUTING.md for the "where does my new file go" recipes.
Where it fits
There are already ways to put SQL in front of non-SQL data; this sits at a different point than each:
- SQL built into a single store — a search engine's or document database's own SQL feature, reachable only through that vendor's driver. This is backend-agnostic and speaks the native SQL Server wire, so existing tools connect unchanged.
- A wire shim in front of one database — a connector that fronts a single document store on a SQL wire. Here the backend is whatever you implement.
- A proprietary gateway over hundreds of connectors — closed source, licensed per source, and operated as a service. This is Apache-2.0, dependency-free, and embeds as a library.
- An enterprise data-virtualization platform — modeling, governance, caching, and operational overhead in a different deployment class.
- A distributed query engine — coordinator/worker architecture with its own protocol and drivers. This is a single Go binary that becomes the endpoint.
Why it stays this small: the whole footprint is the adapter you write plus this library, embedded inside your own service — which is what lets it ship as one binary and start instantly.
The win is a full SQL surface over a store that never had one — JOINs, aggregates, subqueries, CTEs, INFORMATION_SCHEMA/sys.*, and every SQL Server client and BI tool (Excel, Power BI, SSMS, sqlcmd, JDBC/.NET drivers) connecting with zero new drivers. The price of that reach is a translation layer between the TDS wire and your backend: you trade a slice of raw throughput for SQL accessibility — almost always a good trade for analytics, reporting, and ad-hoc query, where the bottleneck is the human, not the loop.
How small that tax is depends on the path:
- Thick backend (
QueryExecutor / Caps{FullQuery}): the query is pushed down to your store's native query language so reads run at the store's own speed — the TDS handshake and T-SQL parse are per-statement, not per-row. The overhead is marginal.
- Thin backend (
Scanner / Pushdown): the engine pulls the table(s) and evaluates WHERE/JOIN/GROUP BY in-process. Easy to stand up, but on large tables it materializes rows and won't match an index-backed query — move to QueryExecutor once a table outgrows a scan.
It is not a tuned SQL Server — no cost-based optimizer or index planner — and isn't trying to be. For analytics, reporting, catalog browsing, and ad-hoc SQL over your data, that convenience is the whole point; for hot-path OLTP, talk to your backend directly.
Run the demo gateway
go run ./examples/gateway 127.0.0.1:1433 # plaintext
HAYSTAK_TLS=1 go run ./examples/gateway # self-signed TLS
sqlcmd -S 127.0.0.1,1433 -U sa -P x -C -Q "SELECT name FROM users"
Build a backend
Implement tds.Backend plus one query path, and advertise it via Caps:
type Backend interface {
Describe(ctx) (catalog.Schema, error) // your tables/columns/PKs/FKs
Capabilities() Caps // what you support
}
The easiest way is a thin backend: implement Scanner (Scan returns whole tables) and set Caps{Pushdown:true}. Then the core engine does all the WHERE/JOIN/GROUP BY/ORDER BY/paging/subqueries/CTEs work for you. For a thick backend, implement QueryExecutor (ExecuteQuery handles a whole logical query) and set Caps{FullQuery:true} to push down to your store's native query language.
The rest is optional, advertised via Caps and detected by interface assertion:
| Interface |
For |
Caps |
Writer |
INSERT/UPDATE/DELETE |
Writable |
DDL |
CREATE/ALTER/DROP TABLE |
DDL |
DatabaseDDL |
CREATE/DROP DATABASE |
DDL |
Databaser |
more than one database (→ sys.databases, Query.Database) |
— |
TxBeginner/Tx |
transactions |
Tx |
Authenticator |
authenticate TDS logins (go/no-go) + identity |
— |
Authentication
The backend owns authentication. Implement tds.Authenticator and the gateway hands you each LOGIN7 credential set (tds.Login{Username, Password, Database, …}); return a tds.Principal to allow the login, or an error to reject it (a real 18456 login-failed reaches the client). The authenticated Principal then rides in ctx on every Scan/Insert/… call, so you authorize and audit per user with the same identity. Operators who don't want to push auth into the backend can set server.Server.Auth (for example server.StaticAuth(map)) for gateway-level auth instead; with neither set, connections are anonymous, which is the demos' default.
Then run it:
server.ListenAndServe("127.0.0.1:1433", myBackend)
// or &server.Server{Backend: myBackend, TLSConfig: cfg, ServerName: "...", Database: "..."}
examples/inmem is a complete thin reference backend.
Validate your backend
func TestConformance(t *testing.T) { tdstest.RunConformance(t, myBackend.New()) }
tds/tdstest.RunConformance checks that Caps and the implemented interfaces agree, then drives real SELECT and catalog queries through the engine.
Read surface
Single- and multi-table queries, evaluated entirely over the wire.
Query shape:
SELECT with projection, DISTINCT, TOP n [PERCENT], and aliases (AS or alias = expr)
FROM with table aliases and qualified columns
- JOINs: INNER, LEFT, RIGHT, FULL, CROSS
WHERE, GROUP BY, HAVING, ORDER BY (by column or ordinal, ASC/DESC)
OFFSET … FETCH paging
UNION, UNION ALL, INTERSECT, EXCEPT
- Subqueries:
IN (…), EXISTS, scalar = (…), derived tables FROM (SELECT …) t, and correlated forms
- CTEs (
WITH), including recursive
- No-
FROM scalar SELECT, ;-separated multi-statement batches, and RPC sp_executesql
Predicates and operators:
- Comparison
= <> < > <= >=; AND, OR, NOT, parentheses
IN (value list or subquery), BETWEEN, LIKE, IS NULL, IS NOT NULL, EXISTS
- Arithmetic
+ - * / % and string concatenation, in both SELECT and WHERE
Functions:
- Aggregate:
COUNT, SUM, AVG, MIN, MAX
- String:
LEN/DATALEN, UPPER, LOWER, LTRIM, RTRIM, TRIM, SUBSTRING, REPLACE, CONCAT
- Conditional:
ISNULL, COALESCE, NULLIF, CASE (simple and searched)
- Numeric:
ABS
- Date:
YEAR, MONTH, DAY, GETDATE, GETUTCDATE, SYSDATETIME, SYSUTCDATETIME
CAST/CONVERT to int/bigint/smallint/tinyint, float/real/decimal/numeric/money, bit,
char/varchar/nchar/nvarchar/text, plus the typed wire encodings (date, datetime,
uniqueidentifier, varbinary)
Catalog:
INFORMATION_SCHEMA: TABLES, COLUMNS, TABLE_CONSTRAINTS, KEY_COLUMN_USAGE, REFERENTIAL_CONSTRAINTS
sys.*: databases, schemas, tables, columns, types, objects, foreign_keys
Connect-time probes, so real drivers and BI tools finish their handshake:
@@VERSION, @@SPID, @@SERVERNAME, @@LANGUAGE, @@ROWCOUNT, @@ERROR, @@TRANCOUNT, @@FETCH_STATUS
DB_NAME(), SCHEMA_NAME(), SYSTEM_USER, CURRENT_USER, SESSION_USER, USER_NAME(), SUSER_SNAME(), HOST_NAME(), APP_NAME()
SERVERPROPERTY(…), DATABASEPROPERTYEX(…); SET … and USE … accepted as no-ops
Write surface
Write statements are parsed over the wire and dispatched to the backend's Writer / DDL / DatabaseDDL interfaces, each fail-closed: if the backend doesn't implement the interface, the client gets a clean error rather than a silent success.
INSERT INTO t [(cols)] VALUES (…)[, (…) …] — single or multi-row
UPDATE t SET col = val [, …] [WHERE col op val [AND …]]
DELETE FROM t [WHERE col op val [AND …]]
CREATE TABLE t (col type [, …]) — types map to the SPI kinds: int/bigint/smallint/tinyint,
bit, float/real, decimal/numeric/money, date/datetime/time, uniqueidentifier, varbinary,
anything else → varchar
ALTER TABLE t ADD col type [, …] and ALTER TABLE t DROP COLUMN col [, …]
DROP TABLE t
CREATE DATABASE name, DROP DATABASE name
The write WHERE is the simple form (col op value, joined by AND), and VALUES/SET take literals; the rich expression tree from the read path doesn't apply here.
docs/
Pinned protocol specs: MS-TDS (the wire), MC-SQLR (instance resolution), and MS-BINXML (the xml type). See docs/README.md for provenance and the licensing notice.
Documentation
Full API reference and runnable examples are on pkg.go.dev:
tds — the SPI: Backend, Scanner/QueryExecutor, Writer/DDL/DatabaseDDL/Databaser, Authenticator, Caps.
server — ListenAndServe, Server, StaticAuth.
tds/catalog — the schema model (Schema/Table/Column/ForeignKey).
tds/types — the backend-neutral type system.
tds/tdstest — the RunConformance harness.
License
Apache-2.0 — see LICENSE and NOTICE. "Haystak" is a registered trademark of RSKGroup, LLC; the license grants no rights to the name (Apache-2.0 §6).