How to set query_tags in system.query.history

hobrob_ex
New Contributor III

I've been trying to see if there's a way to have metadata included in query logging in a similar way to query banding in Teradata, and I've come across the field query_tags in system.query.history. However, I can't find any reference to it in the Databricks documentation that explains how to populate it, I've only found mention of it in what looks like some third-party DBT documentation here:- Query Tags and Context | databricks/dbt-databricks | DeepWiki

Presumably if this is a Databricks feature leveraged by DBT, it must be running some SQL to set query tag values. Does anyone know what that syntax is?

K_Anudeep
Databricks Employee
Databricks Employee

Hello @hobrob_ex ,

query_tags is a Databricks feature, but (as of the current docs) it’s not set via a SQL statement like Teradata query banding / Snowflake ALTER SESSION

A per the docs, you need to pass it via session configuration

from databricks import sql

with sql.connect(
    server_hostname="...",
    http_path="...",
    access_token="...",
    session_configuration={"query_tags": "team:engineering,dashboard:abc123,env:prod"}
) as conn:
    with conn.cursor() as cur:
        cur.execute("SELECT 1")

Doc link:https://docs.databricks.com/aws/en/dev-tools/python-sql-connector#query-tags

 

Anudeep

View solution in original post

K_Anudeep
Databricks Employee
Databricks Employee

Hello @hobrob_ex ! Good Day!

We now have the querytags feature available in Public Preview. Please find the docs here: https://docs.databricks.com/aws/en/sql/user/queries/query-tags

Anudeep

hobrob_ex
New Contributor III

Hey Anudeep, Good day to you too!

That's great news, setting the tags via SQL is just what I need! Out of interest, was that already in the pipeline or was it implemented as a response to my query?