skuldshao commented on code in PR #18179:
URL: https://github.com/apache/druid/pull/18179#discussion_r2241381695


##########
docs/querying/set-query-context.md:
##########
@@ -0,0 +1,245 @@
+---
+id: set-query-context
+title: "Set query context"
+sidebar_label: "Set query context"
+description: 
+  "Learn how to configure the query context
+  to customize query execution behavior and optimize performance."
+---
+
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+  
+
+The query context gives you fine-grained control over how Apache Druid 
executes your individual queries. While the default settings in Druid work well 
for most queries, you can set the query context to handle specific requirements 
and optimize performance.
+
+Common use cases for the query context include:
+- Override default timeouts for long-running queries or complex aggregations.
+- Debug query performance by disabling caching during testing.
+- Configure SQL-specific behaviors like time zones for accurate time-based 
analysis.
+- Set priorities to ensure critical queries get computational resources first.
+- Adjust memory limits for queries that process large datasets.
+
+The way you set the query context depends on how you submit the query to 
Druid, whether using the web console or API.
+It also depends on whether your query is Druid SQL or a JSON-based native 
query.
+This guide shows you how to set the query context for each application.
+
+Before you begin, identify which context parameters you need to configure in 
order to establish your query context as query context carriers. For available 
parameters and their descriptions, see [Query context 
reference](query-context-reference.md).
+
+## Web console
+
+You can configure query context parameters is via the [Web 
console](../operations/web-console.md). In the web console, you can set up 
context parameters for both Druid SQL and native queries.
+
+The following steps show you how to set the query context using the web 
console:
+
+1. In the web console, select **Query** from the top-level navigation.
+
+   ![Query view](../assets/set-query-context-query-view.png)
+
+1. Click the **Engine** selector next to the **Run** button to choose the 
appropriate query type. In most cases, you can leave the engine as `Auto` to 
let Druid choose the best engine for you.
+
+   ![Engine selection](../assets/set-query-context-select-engine.png)
+
+2. Enter the query you want to run.
+
+   ![Adding query](../assets/set-query-context-insert-query.png)
+
+3. In the menu for the engine selector, click **Edit query context**.
+
+   ![Opening context 
dialog](../assets/set-query-context-open-context-dialog.png)
+
+4. In the **Edit query context** dialog, add your context parameters as JSON 
key-value pairs and then click **Save**. For example, you can insert the 
following context parameters:
+
+   ```json
+   {
+     "timeout": 300000,
+     "useCache": false
+   }
+   ```
+
+   The web console validates the JSON object containing the query context 
parameters and highlights any syntax errors.
+
+   ![Setting the context 
parameters](../assets/set-query-context-set-context-parameters.png)
+
+6. Click **Run** to execute your query with the specified context parameters.
+
+   ![Running the query](../assets/set-query-context-run-the-query.png)
+
+
+For more information about the Query view in the web console, see 
[Query](../operations/web-console.md#query).
+
+## Druid SQL
+
+When using Druid SQL programmatically—such as in applications, automated 
scripts, or database tools—you can set the query context through various 
methods depending on how you submit your queries.
+
+### HTTP API
+
+When using the HTTP API, you include query context parameters in the `context` 
object of your JSON request. For more information on how to format Druid SQL 
API requests and handle responses, see [Druid SQL 
API](../api-reference/sql-api.md).
+
+The following example sets the `sqlTimeZone` parameter:
+
+   ```json
+   {
+     "query" : "SELECT COUNT(*) FROM data_source WHERE foo = 'bar' AND __time 
> TIMESTAMP '2000-01-01 00:00:00'",
+     "context" : {
+       "sqlTimeZone" : "America/Los_Angeles"
+     }
+   }
+   ```
+
+Druid executes your query using the specified context parameters and return 
the results.
+
+You can set multiple context parameters in a single request:
+
+```json
+{
+  "query" : "SELECT COUNT(*) FROM data_source WHERE foo = 'bar'",
+  "context" : {
+    "timeout" : 30000,
+    "useCache" : false,
+    "sqlTimeZone" : "America/Los_Angeles"
+  }
+}
+```
+
+
+### JDBC driver API
+
+You can connect to Druid over JDBC and issue Druid SQL queries using this 
[Druid SQL JDBC driver API](../api-reference/sql-jdbc.md).
+This approach is useful when integrating Druid with BI tools or Java 
applications.
+When connecting to Druid through JDBC, you set query context parameters a JDBC 
connection properties object.
+
+For example, you can set query context parameters when creating your JDBC 
connection:
+
+```java
+String url = 
"jdbc:avatica:remote:url=http://localhost:8082/druid/v2/sql/avatica/";;
+
+// Set any query context parameters you need here.
+Properties connectionProperties = new Properties();
+connectionProperties.setProperty("sqlTimeZone", "America/Los_Angeles");
+connectionProperties.setProperty("useCache", "false");
+
+try (Connection connection = DriverManager.getConnection(url, 
connectionProperties)) {
+  // create and execute statements, process result sets, etc
+}
+```
+
+
+### SET statements
+
+You can use the SET command to specify SQL query context parameters that 
modify the behavior of a Druid SQL query. Druid accepts one or more SET 
statements before the main SQL query. The SET command works in the both web 
console and the Druid SQL HTTP API.
+
+In the web console, you can write your SET statements followed by your query 
directly. For example, 
+
+```sql
+SET useApproximateTopN = false;
+SET sqlTimeZone = 'America/Los_Angeles';
+SET timeout = 90000;
+SELECT some_column, COUNT(*) 
+FROM druid.foo 
+WHERE other_column = 'foo' 
+GROUP BY 1 
+ORDER BY 2 DESC
+```
+
+You can also include your SET statements as part of the query string in your 
HTTP API call. For example,
+
+```bash
+curl -X POST 'http://localhost:8888/druid/v2/sql' \
+  -H 'Content-Type: application/json' \
+  -d '{
+    "query": "SET useApproximateTopN = false; SET sqlTimeZone = 
'\''America/Los_Angeles'\''; SET timeout = 90000; SELECT some_column, COUNT(*) 
FROM druid.foo WHERE other_column = '\''foo'\'' GROUP BY 1 ORDER BY 2 DESC"
+  }'
+```
+
+You can also combine SET statements with the `context` field. If you include 
both, the parameter value in SET takes precedence:
+
+```json
+{
+  "query": "SET timeout = 90000; SELECT COUNT(*) FROM data_source",
+  "context": {
+    "timeout": 30000,  // This will be overridden by SET
+    "priority": 100    // This will still apply
+  }
+}
+```
+
+For more details on how to use the SET command in your SQL query, see 
[SET](sql.md#set).
+
+:::info
+You cannot use SET statements in JDBC connections.
+:::
+
+
+## Native queries
+
+For native queries, you can include query context parameters in a JSON object 
named `context` within your query structure or through [Web 
Console](./set-query-context.md#web-console).
+
+The following example shows a native query that sets the query ID to 
`only_query_id_test`:
+
+```json
+{
+  "queryType": "timeseries",
+  "dataSource": "wikipedia",
+  "granularity": "day",
+  "descending": true,
+  "filter": {
+    "type": "and",
+    "fields": [
+      { "type": "selector", "dimension": "countryName", "value": "Australia" },
+      { "type": "selector", "dimension": "isAnonymous", "value": "true" }
+    ]
+  },
+  "aggregations": [
+    { "type": "count", "name": "row_count" }
+  ],
+  "intervals": ["2015-09-12T00:00:00.000/2015-09-13T00:00:00.000"],
+  "context": {
+    "queryId": "only_query_id_test"
+  }
+}
+```
+
+For more information about native queries, see [Native queries](querying.md).
+
+
+## Query Context Precedence
+
+You can set the query context using various methods. For a given parameter, 
Druid determines the value to use based on the following order of precedence, 
from lowest to highest:
+
+1. **Built-in hard-coded defaults** — these are the system’s default values 
used if you don’t specify anything else.

Review Comment:
   ```suggestion
   1. **Built-in hard-coded defaults**: these are the system’s default values 
used if you don’t specify anything else.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org
For additional commands, e-mail: commits-h...@druid.apache.org

Reply via email to