Skip to content

Decode sql_variant values - #194

Open
michaelst wants to merge 1 commit into
elixir-ecto:masterfrom
michaelst:decode-sql-variant
Open

michaelst wants to merge 1 commit into
elixir-ecto:masterfrom
michaelst:decode-sql-variant

Conversation

@michaelst

Copy link
Copy Markdown

sql_variant columns have their COLMETADATA parsed (data_reader: :variant) but no decode_data/2 clause handles them, so any row containing one raises:

** (FunctionClauseError) no function clause matching in Tds.Types.decode_data/2

For example, SELECT SERVERPROPERTY('EngineEdition'), which DataGrip and other tools run when they connect. SESSION_CONTEXT() and sys.extended_properties.value return sql_variant too.

What changed

  • lib/tds/types.ex: decode a variant value per MS-TDS 2.2.5.5.4: a 4-byte length (0 means NULL), the base type, the prop byte count, the props, then the data. The data goes to the decoder that already handles that base type: tinyint (unsigned), smallint/int/bigint, bit, decimal/numeric, money/smallmoney, real/float, date/time/datetime2/datetimeoffset/smalldatetime/datetime, char/varchar (with their collation), nchar/nvarchar, binary/varbinary, uniqueidentifier. This replaces the # TODO Variant Types comment.
  • test/variant_test.exs: covers NULL, SERVERPROPERTY, and each base type.
  • CHANGELOG.md: an Unreleased entry.

Tested against SQL Server 2022.

SQL_VARIANT columns had their metadata parsed but no decode_data/2
clause, so any row with one raised FunctionClauseError. Decode the
value per MS-TDS (length, base type, prop bytes, props, data),
reusing the existing per-type decoders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant