Skip to content

Add query_options model config for SQL Server OPTION clauses (#613).#674

Open
Benjamin-Knight wants to merge 5 commits into
dbt-msft:masterfrom
Benjamin-Knight:feat/#613-query-options
Open

Add query_options model config for SQL Server OPTION clauses (#613).#674
Benjamin-Knight wants to merge 5 commits into
dbt-msft:masterfrom
Benjamin-Knight:feat/#613-query-options

Conversation

@Benjamin-Knight
Copy link
Copy Markdown
Collaborator

@Benjamin-Knight Benjamin-Knight commented May 20, 2026

Supports a dict form (allowlisted hints; MAX_GRANT_PERCENT / MIN_GRANT_PERCENT use = syntax) and a query_options_raw escape hatch. Usable on table, incremental (delete+insert, merge, microbatch), snapshot, and unit_test materializations.
Usage on a view raises a compiler error if set.
Apply_label() is preserved as a backward-compat alias emitting only the LABEL hint.
Fixes #613

…t#613).

Supports a dict form (allowlisted hints; MAX_GRANT_PERCENT / MIN_GRANT_PERCENT
use `=` syntax) and a query_options_raw escape hatch. Honoured on table,
incremental (delete+insert, merge, microbatch), snapshot, and unit_test
materializations; view raises a compiler error if set. apply_label() is
preserved as a backward-compat alias emitting only the LABEL hint.
@Benjamin-Knight Benjamin-Knight added this to the v1.10.0 milestone May 21, 2026
Comment on lines +62 to +72
{#- Backward-compat alias for the pre-1.10 macro. Emits only the LABEL hint
and ignores query_options / query_options_raw. New adapter code should
call get_query_options() directly.

Note: this preserves non-breaking *consumption* of apply_label (user
macros calling `{{ apply_label() }}` still resolve), but does NOT
preserve non-breaking *override*: adapter macros no longer call
apply_label internally, so a project that overrides apply_label in its
own macros directory will find that override has no effect on adapter
behaviour. To customise the OPTION clause emitted by adapter macros,
override get_query_options instead. -#}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this deprecation on the changelog so we don't forget to publish it on release or check if we can avoid it as a standard execution path, this can reasonably fail with a targeted compiler error and instruct them to override get_query_options() instead.
Should we make something like this?:

  • Make get_query_options() compose with apply_label() so existing apply_label() overrides still affect generated SQL.
  • Add a test where a project overrides apply_label() and verifies table/incremental SQL still includes the custom label.
  • Document get_query_options() as the new extension point for full query-hint customization.
  • Keep apply_label() supported for at least one release cycle?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how we could keep apply label support. If anyone was using apply label overrides to apply their own options this would supersede it. The best we could do is make apply labels call the new macro internally and just print the label as it did before, that means callers of apply_label would work as before, but that is the same effect as just leaving apply_label as is, just without the duplication of code.

The risk surface is users who are overriding apply_label. If they are over riding it to do something other than applying a label because it was a useful injection point then they will have a migration issue, but I cannot see a way around that other than notifying them of the new path. I wouldn't call it a breaking change, as its a not a proper usage of the macro.

Comment thread dbt/include/sqlserver/macros/adapters/metadata.sql
Comment thread dbt/include/sqlserver/macros/adapters/metadata.sql
Comment thread tests/functional/adapter/mssql/test_query_options.py
Benjamin-Knight and others added 4 commits May 28, 2026 11:56
- Drop `| int` cast so MAX_GRANT_PERCENT / MIN_GRANT_PERCENT render decimals
  verbatim (SQL Server accepts 0.0–100.0).
- Remove PARAMETERIZATION from the allowlist — its SIMPLE/FORCED syntax
  cannot be rendered through the numeric value path. Users needing it can
  fall back to query_options_raw.
- Reject string/mapping values for query_options_raw to prevent silent
  character-by-character iteration when a non-list is passed.
- Sharpen the apply_label() deprecation comment to direct overriders to
  get_query_options() as the new extension point.
- Add CHANGELOG v1.10.0 entry covering the new configs and the apply_label
  override migration note.
- Add tests covering decimal MAX_GRANT_PERCENT, query_options_raw string
  rejection, and PARAMETERIZATION now hitting the invalid-key branch.
…query-options.

Brings in 21 commits from upstream that were missing from this branch
(view materialization improvements, multi-database catalog support,
TABLOCK on contract-enforced inserts, reserved-keyword quoting fix,
default-schema-concat flag, etc.).

Conflict in dbt/include/sqlserver/macros/adapters/catalog.sql resolved
by keeping upstream's multi-database structure while replacing the
remaining apply_label() call with get_query_options().
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.

Max Recursion config property

2 participants