feat(sqlserver): part 5 add catalog introspection - #15
Merged
Conversation
Modeled on `metadata/fabric/mod.rs`, but not a copy of it. Fabric drives `sp_tables` / `sp_columns`, and two things measured against SQL Server 2022 rule that out here: - The procedures read the connection's current database only. Any other `@table_qualifier` is error 15250, "The database name component of the object qualifier must be the name of the current database". A dbt project that reaches across databases on one server is ordinary on SQL Server and impossible on Fabric, where a warehouse is its own database. - `@table_name` is a `LIKE` pattern unless `@fUsePattern = 0` is passed, so `stg_orders` also matches `stgXorders`. Underscores are everywhere in dbt model names, and the second row makes `get_relation` fail the one-row check rather than return the wrong type. `\_` does not escape it; only `@fUsePattern = 0` does. The catalog views take a three-part name, which resolves cross-database and, unlike the `USE` that v1 emits before every metadata query, leaves the connection's current database alone. That matters for a pooled connection. `sys.columns` also carries the type detail `sp_columns` drops: `TYPE_NAME` is the bare name, so `decimal(18,4)` arrives as `decimal` and `nvarchar(50)` as `nvarchar`. `compose_type_text` rebuilds the declared text from `max_length` / `precision` / `scale`, following v1's `get_columns_in_relation` -- including its halving of `max_length` for the national types, which `sys.columns` reports in bytes. `datetime` reports a scale but rejects one. `build_schemas_from_stats_sql` and `build_columns_from_get_columns` read `table_comment` and `column_comment`, as Postgres does and Fabric does not; v1's `get_catalog` returns both, and v1 supports `persist_docs`. `freshness_inner` stays `todo!()`, matching Fabric and Postgres, though v1 has `get_relation_last_modified` to port. `metadata_adapter` and `list_relations` in `adapter_impl.rs` are two of Part 7's arms, taken here because they are what makes this module reachable. Ten tests over the SQL builders and the type composer. Every query was also run against a live SQL Server 2022 from a connection attached to a different database, in the exact text the Rust emits. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
axellpadilla
force-pushed
the
part-5-sqlserver-catalog
branch
from
August 2, 2026 18:03
5cdbd82 to
5396ae6
Compare
axellpadilla
changed the base branch from
part-4-sqlserver-relation
to
sqlserver-v2-port
August 3, 2026 01:18
2 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Part 5 of the SQL Server Fusion adapter series. Part of dbt-labs#15714.
#5 says Part 5 is independent of #4. It isn't —
list_relationsandget_relationboth constructRelation::new_sqlserver, which Part 4 adds. The dependency is real, not just a stacking convenience.Not a copy of Fabric's module
The plan said "modeled on
src/metadata/fabric/mod.rs", and it is, but Fabric drivessp_tables/sp_columnsand those are the wrong tool here. Two measurements against SQL Server 2022 (16.0.4265.3), from a connection attached to a different database:1. The procedures are single-database.
Fabric never hits this — a warehouse is the database, so
@table_qualifieris always the current one. On SQL Server a project that reads a relation in another database on the same server is ordinary, and v1 supports it by emittingUSE [db];before every metadata query.2.
@table_nameis aLIKEpattern. Withprobe_tableandprobeXtableboth present:dbt model names are snake_case, so this is not an edge case. In
get_relationthe second row trips thelen() != 1check and turns a valid lookup into an error; insp_columnsit merges another table's columns into the schema.@table_name = 'probe\_table'returns zero rows — the backslash isn't an escape here. Only@fUsePattern = 0fixes it (verified: 1 row).Both apply to
metadata/fabric/mod.rsas written. I haven't touched it — I can't test whether Fabric's T-SQL surface accepts@fUsePattern, and cross-database is genuinely impossible there so only the pattern half is a live bug. Worth a separate issue by someone who can run against Fabric.What this uses instead: three-part
sys.objects/sys.schemas/sys.columns/sys.types. Resolves cross-database, matches exactly, and unlike v1'sUSEit leaves the connection's current database alone — which matters when the connection goes back to a pool. Verified:select db_name()after the query still returns the original database.Type text
sp_columns.TYPE_NAMEis the bare type name.decimal(18,4)arrives asdecimal,nvarchar(50)asnvarchar— Fabric passes that straight tomake_arrow_field, dropping precision, scale and length.sys.columnscarries them, socompose_type_textrebuilds the declared text, following v1'ssqlserver__get_columns_in_relation:charvarcharbinaryvarbinary(max_length),-1→(max)varchar(50),varchar(max)ncharnvarchar(max_length / 2)— bytes, two per charnvarchar(100)→nvarchar(50)decimalnumeric(precision,scale)decimal(18,4)datetime2datetimeoffsettime(scale)datetime2(7),time(3)datetimereports scale 3 and rejectsdatetime(3)float(24)needs no case: SQL Server stores it asreal, andfloat/realeach have exactly one precision.The narrow integer columns are cast:
max_lengthcomes backsmallintandprecision/scaletinyint, which Arrow surfaces asint16/uint8.cast(... as int)makes all threeint32so the Rust reads one array type.Comments
build_schemas_from_stats_sqlandbuild_columns_from_get_columnsreadtable_commentandcolumn_comment, following Postgres rather than Fabric, which drops both. v1'ssqlserver__get_catalogreturns them and v1 supportspersist_docs. This fixes the column contract the catalog macro has to satisfy in §5.6.Arms outside #5's checklist
metadata_adapterandlist_relationsinadapter_impl.rsare two of Part 7's arms. Taken here because they're what makes this module reachable — without them it's dead code that never runs. #7's checklist should losemetadata_adapter.Left as
todo!()freshness_innerandlist_relations_schemas_by_patterns_inner, matching both Fabric and Postgres. v1 hassqlserver__get_relation_last_modified, so freshness is a known port, not an unknown.Verification
Ten unit tests over the SQL builders and the type composer — the builders take the quoted database as a parameter, so they're testable without a connection.
Separately, every query was run against a live SQL Server 2022 in the exact text the Rust emits (dumped from the builders, executed verbatim), from a connection attached to
masterwhile readingprobe_db:list_relations— 6 relations, tables and views classified correctlyget_relation—TABLEfor a table,VIEWfor a view, 0 rows for a missing one, and exactly 1 row forprobe_tablewithprobeXtablepresentstring/int32exactly as the Rust reads themprobe "odd" db— Part 4's delimiter doubling holds inside a three-part namedbt-adapterstill has 40E0004s (Parts 6–7), socargo test -p dbt-adaptercan't run on this branch as-is. As in Part 4 I stubbed those arms locally withtodo!(), ran the suite, and reverted before committing: 891 passed / 0 failed (881 + these 10).Workspace
E0004count drops 44 → 41:adapter_impl.rs(33),sql_types.rs(5),column_builder.rs(2), anddbt-df-providersseed_io.rs(1), which no issue in the series claims.Closes #5