Skip to content

Commit bcab1df

Browse files
authored
query tags integration (#663)
* query tags integration Signed-off-by: Sreekanth Vadigi <[email protected]> * query tags with sea flow Signed-off-by: Sreekanth Vadigi <[email protected]> * optmized tests Signed-off-by: Sreekanth Vadigi <[email protected]> --------- Signed-off-by: Sreekanth Vadigi <[email protected]>
1 parent 048fae1 commit bcab1df

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

examples/query_tags_example.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import databricks.sql as sql
3+
4+
"""
5+
This example demonstrates how to use Query Tags.
6+
7+
Query Tags are key-value pairs that can be attached to SQL executions and will appear
8+
in the system.query.history table for analytical purposes.
9+
10+
Format: "key1:value1,key2:value2,key3:value3"
11+
"""
12+
13+
print("=== Query Tags Example ===\n")
14+
15+
with sql.connect(
16+
server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
17+
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
18+
access_token=os.getenv("DATABRICKS_TOKEN"),
19+
session_configuration={
20+
'QUERY_TAGS': 'team:engineering,test:query-tags',
21+
'ansi_mode': False
22+
}
23+
) as connection:
24+
25+
with connection.cursor() as cursor:
26+
cursor.execute("SELECT 1")
27+
result = cursor.fetchone()
28+
print(f" Result: {result[0]}")
29+
30+
print("\n=== Query Tags Example Complete ===")

src/databricks/sql/backend/sea/utils/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"STATEMENT_TIMEOUT": "0",
1616
"TIMEZONE": "UTC",
1717
"USE_CACHED_RESULT": "true",
18+
"QUERY_TAGS": "",
1819
}
1920

2021

tests/e2e/test_driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ def test_timestamps_arrow(self):
901901
)
902902
def test_multi_timestamps_arrow(self, extra_params):
903903
with self.cursor(
904-
{"session_configuration": {"ansi_mode": False}, **extra_params}
904+
{"session_configuration": {"ansi_mode": False, "query_tags": "test:multi-timestamps,driver:python"}, **extra_params}
905905
) as cursor:
906906
query, expected = self.multi_query()
907907
expected = [

tests/unit/test_sea_backend.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def test_session_management(self, sea_client, mock_http_client, thrift_session_i
185185
session_config = {
186186
"ANSI_MODE": "FALSE", # Supported parameter
187187
"STATEMENT_TIMEOUT": "3600", # Supported parameter
188+
"QUERY_TAGS": "team:marketing,dashboard:abc123", # Supported parameter
188189
"unsupported_param": "value", # Unsupported parameter
189190
}
190191
catalog = "test_catalog"
@@ -196,6 +197,7 @@ def test_session_management(self, sea_client, mock_http_client, thrift_session_i
196197
"session_confs": {
197198
"ansi_mode": "FALSE",
198199
"statement_timeout": "3600",
200+
"query_tags": "team:marketing,dashboard:abc123",
199201
},
200202
"catalog": catalog,
201203
"schema": schema,
@@ -641,6 +643,7 @@ def test_filter_session_configuration(self):
641643
"TIMEZONE": "UTC",
642644
"enable_photon": False,
643645
"MAX_FILE_PARTITION_BYTES": 128.5,
646+
"QUERY_TAGS": "team:engineering,project:data-pipeline",
644647
"unsupported_param": "value",
645648
"ANOTHER_UNSUPPORTED": 42,
646649
}
@@ -663,6 +666,7 @@ def test_filter_session_configuration(self):
663666
"timezone": "UTC", # string -> "UTC", key lowercased
664667
"enable_photon": "False", # boolean False -> "False", key lowercased
665668
"max_file_partition_bytes": "128.5", # float -> "128.5", key lowercased
669+
"query_tags": "team:engineering,project:data-pipeline",
666670
}
667671

668672
assert result == expected_result
@@ -683,12 +687,14 @@ def test_filter_session_configuration(self):
683687
"ansi_mode": "false", # lowercase key
684688
"STATEMENT_TIMEOUT": 7200, # uppercase key
685689
"TiMeZoNe": "America/New_York", # mixed case key
690+
"QueRy_TaGs": "team:marketing,test:case-insensitive",
686691
}
687692
result = _filter_session_configuration(case_insensitive_config)
688693
expected_case_result = {
689694
"ansi_mode": "false",
690695
"statement_timeout": "7200",
691696
"timezone": "America/New_York",
697+
"query_tags": "team:marketing,test:case-insensitive",
692698
}
693699
assert result == expected_case_result
694700

tests/unit/test_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_socket_timeout_passthrough(self, mock_client_class):
155155

156156
@patch("%s.session.ThriftDatabricksClient" % PACKAGE_NAME)
157157
def test_configuration_passthrough(self, mock_client_class):
158-
mock_session_config = Mock()
158+
mock_session_config = {"ANSI_MODE": "FALSE", "QUERY_TAGS": "team:engineering,project:data-pipeline"}
159159
databricks.sql.connect(
160160
session_configuration=mock_session_config, **self.DUMMY_CONNECTION_ARGS
161161
)

0 commit comments

Comments
 (0)