Skip to content

Conversation

@grahamplata
Copy link
Member

@grahamplata grahamplata commented Nov 5, 2025

Support query specific attributes for ClickHouse. This enables end users to enrich queries and leverage our internal routing to our row_filters.

ClickHouse Docs: custom settings

Example with ClickHouse SQL and Yaml

-- models/metrics.yaml
type: metrics_view
model: metrics

# ...snip

query_attributes:
  prefix_partner_id: '{{ .user.partner_id }}'

Relates to: #8120 Custom user attributes support

After the above is merged I can leverage the security context for {{ .user.specific }} attributes

Checklist:

  • Covered by tests
  • Ran it and it works as intended
  • Reviewed the diff before requesting a review
  • Checked for unhandled edge cases
  • Linked the issues it closes
  • Checked if the docs need to be updated. If so, create a separate Linear DOCS issue
  • Intend to cherry-pick into the release branch
  • I'm proud of this work!
@grahamplata grahamplata force-pushed the gplata/plat-246-ch-templating branch from c9a30c5 to 958c7f7 Compare November 12, 2025 20:07
@grahamplata
Copy link
Member Author

Updated test instance to support sql_ prefix for query attribute testing Docs: custom settings

@grahamplata grahamplata marked this pull request as ready for review November 13, 2025 15:26
@grahamplata grahamplata self-assigned this Nov 13, 2025
@grahamplata grahamplata force-pushed the gplata/plat-246-ch-templating branch from 453ed05 to ad8f4cd Compare November 18, 2025 23:46
@grahamplata grahamplata force-pushed the gplata/plat-246-ch-templating branch from 4718a62 to 638585b Compare November 21, 2025 18:17
Comment on lines 133 to 167
if c.supportSettings {
if c.config.QuerySettingsOverride != "" {
stmt.Query += "\n SETTINGS " + c.config.QuerySettingsOverride
} else {
stmt.Query += "\n SETTINGS cast_keep_nullable = 1, join_use_nulls = 1, session_timezone = 'UTC', prefer_global_in_and_join = 1, insert_distributed_sync = 1"
if c.config.QuerySettings != "" {
stmt.Query += ", " + c.config.QuerySettings
settings := map[string]any{
"cast_keep_nullable": 1,
"insert_distributed_sync": 1,
"prefer_global_in_and_join": 1,
"session_timezone": "UTC",
"join_use_nulls": 1,
}

if c.supportSettings {
if len(stmt.QueryAttributes) > 0 {
for k, v := range stmt.QueryAttributes {
settings[k] = v
}
}

if c.config.QuerySettingsOverride != "" {
stmt.Query += "\n SETTINGS " + c.config.QuerySettingsOverride
} else {
stmt.Query += "\n SETTINGS cast_keep_nullable = 1, join_use_nulls = 1, session_timezone = 'UTC', prefer_global_in_and_join = 1, insert_distributed_sync = 1"
if c.config.QuerySettings != "" {
stmt.Query += ", " + c.config.QuerySettings
}
}
}

ctx = clickhouse.Context(ctx, clickhouse.WithSettings(settings))
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. It checks c.supportSettings after it has already been checked?
  2. This no longer correctly honors QuerySettingsOverride, which is supposed to override the default query settings. If QuerySettingsOverride != "", it should not add the default query settings through clickhouse.WithSettings.
  3. Now that the default query settings are passed using clickhouse.WithSettings, it should not also add them to the SQL query (as currently happens on line 160)
Copy link
Contributor

Choose a reason for hiding this comment

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

@grahamplata After your most recent push, it appears to no longer honor c.config.QuerySettings at all. And it also doesn't address bullet 2 – it still doesn't clear the built-in default settings when QuerySettingsOverride is set.

@begelundmuller begelundmuller merged commit d6f8566 into main Dec 11, 2025
14 checks passed
@begelundmuller begelundmuller deleted the gplata/plat-246-ch-templating branch December 11, 2025 18:14
k-anshul pushed a commit that referenced this pull request Dec 18, 2025
* add query attributes to MetricsViewSpec

* add support for query attributes in OLAP driver and executor

* add ClickHouse driver and executor tests

* protos

* add user claims handling in query attribute resolution

* implement query attribute validation and add corresponding tests

* output the additional query attributes in the query logging

* add tests for query attributes

* query attribute handling in ClickHouse queries

* handle sql prefix

* add custom_settings_prefixes to test clickhouse instance

* test_ prefix

* log query configuration for drivers

* support for user attributes in executor

* regen protos

* self

* update tests

* review

* update regex

* query attributes

* use context

* ctx

* dont parse

* verify settings get added to query

* review and tests

* review

* Review

* Tests

* Review

---------

Co-authored-by: Benjamin Egelund-Müller <b@egelund-muller.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants