Skip to content

Commit 2c9acb5

Browse files
committed
Add query_tags support to executemany method
- Added query_tags parameter to executemany() method - Query tags are applied to all queries in the batch - Updated example to demonstrate executemany usage with query_tags - All tests pass (122/122 client tests) Signed-off-by: Jiabin Hu <jiabin.hu@databricks.com>
1 parent c83f465 commit 2c9acb5

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

examples/query_tags_example.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,24 @@
9393
result = cursor.fetchone()
9494
print(f" Async Query Result: {result[0]}")
9595

96+
print()
97+
98+
# Example 4: executemany with query tags
99+
print("Example 4: executemany with query tags")
100+
with sql.connect(
101+
server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
102+
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
103+
access_token=os.getenv("DATABRICKS_TOKEN"),
104+
) as connection:
105+
106+
with connection.cursor() as cursor:
107+
# Execute multiple queries with the same tags
108+
cursor.executemany(
109+
"SELECT ?",
110+
[[5], [6], [7]],
111+
query_tags={"team": "data-eng", "batch": "executemany"}
112+
)
113+
result = cursor.fetchone()
114+
print(f" Executemany Query Result (last): {result[0]}")
115+
96116
print("\n=== Query Tags Example Complete ===")

src/databricks/sql/client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,12 @@ def get_async_execution_result(self):
14591459
session_id_hex=self.connection.get_session_id_hex(),
14601460
)
14611461

1462-
def executemany(self, operation, seq_of_parameters):
1462+
def executemany(
1463+
self,
1464+
operation,
1465+
seq_of_parameters,
1466+
query_tags: Optional[Dict[str, Optional[str]]] = None,
1467+
):
14631468
"""
14641469
Execute the operation once for every set of passed in parameters.
14651470
@@ -1468,10 +1473,14 @@ def executemany(self, operation, seq_of_parameters):
14681473
14691474
Only the final result set is retained.
14701475
1476+
:param query_tags: Optional dictionary of query tags to apply for all queries in this batch.
1477+
Tags are key-value pairs that can be used to identify and categorize queries.
1478+
Example: {"team": "data-eng", "application": "etl"}
1479+
14711480
:returns self
14721481
"""
14731482
for parameters in seq_of_parameters:
1474-
self.execute(operation, parameters)
1483+
self.execute(operation, parameters, query_tags=query_tags)
14751484
return self
14761485

14771486
@log_latency(StatementType.METADATA)

0 commit comments

Comments
 (0)