Skip to content

Commit 76eadbc

Browse files
authored
feat: return timezone in HTTP handler. (#18936)
* feat: return timezone in HTTP handler. * fix flaky test.
1 parent a75e78d commit 76eadbc

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/query/service/src/servers/http/v1/http_query_handlers.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::collections::BTreeMap;
1516
use std::collections::HashMap;
1617
use std::sync::Arc;
1718

@@ -160,6 +161,11 @@ pub struct QueryResponse {
160161
pub data: Arc<BlocksSerializer>,
161162
pub affect: Option<QueryAffect>,
162163
pub result_timeout_secs: Option<u64>,
164+
// settings also used by driver, may be set in session or global
165+
// only include timezone for now
166+
// only is_some in the initial response
167+
#[serde(skip_serializing_if = "Option::is_none")]
168+
pub settings: Option<BTreeMap<String, String>>,
163169

164170
pub stats: QueryStats,
165171

@@ -193,6 +199,7 @@ impl QueryResponse {
193199
}: HttpQueryResponseInternal,
194200
is_final: bool,
195201
body_format: BodyFormat,
202+
settings: Option<BTreeMap<String, String>>,
196203
) -> Response {
197204
let (data, next_uri) = if is_final {
198205
(Arc::new(BlocksSerializer::empty()), None)
@@ -257,6 +264,7 @@ impl QueryResponse {
257264
error: error.map(QueryError::from_error_code),
258265
has_result_set,
259266
result_timeout_secs: Some(result_timeout_secs),
267+
settings,
260268
};
261269

262270
match body_format {
@@ -389,6 +397,7 @@ async fn query_final_handler(
389397
response,
390398
true,
391399
body_format,
400+
None,
392401
))
393402
}
394403
None => Err(query_id_not_found(&query_id, &ctx.node_id)),
@@ -519,6 +528,7 @@ async fn query_page_handler(
519528
resp,
520529
false,
521530
body_format,
531+
None,
522532
))
523533
}
524534
}
@@ -577,6 +587,14 @@ pub(crate) async fn query_handler(
577587
Ok(req.fail_to_start_sql(err).into_response())
578588
}
579589
Ok(mut query) => {
590+
let settings = query.execute_state.lock().get_session_state().settings;
591+
let tz = settings.get_timezone().unwrap();
592+
let driver_settings = if tz == "UTC" {
593+
None
594+
} else {
595+
Some(BTreeMap::from_iter([("timezone".to_string(), tz)]))
596+
};
597+
580598
if let Err(err) = query.start_query(sql.clone()).await {
581599
let err = err.display_with_sql(&sql);
582600
error!("[HTTP-QUERY] Failed to start SQL query, error: {:?}", err);
@@ -612,10 +630,14 @@ pub(crate) async fn query_handler(
612630
query
613631
.update_expire_time(false, resp.is_data_drained())
614632
.await;
615-
Ok(
616-
QueryResponse::from_internal(query.id.to_string(), resp, false, body_format)
617-
.into_response(),
633+
Ok(QueryResponse::from_internal(
634+
query.id.to_string(),
635+
resp,
636+
false,
637+
body_format,
638+
driver_settings,
618639
)
640+
.into_response())
619641
}
620642
}
621643
};

src/query/service/src/servers/http/v1/query/http_query.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl HttpQueryRequest {
128128
error: Some(QueryError::from_error_code(err)),
129129
has_result_set: None,
130130
result_timeout_secs: None,
131+
settings: None,
131132
})
132133
}
133134

@@ -325,6 +326,7 @@ pub struct HttpSessionConf {
325326
pub role: Option<String>,
326327
#[serde(skip_serializing_if = "Option::is_none")]
327328
pub secondary_roles: Option<Vec<String>>,
329+
// session level settings
328330
#[serde(skip_serializing_if = "Option::is_none")]
329331
pub settings: Option<BTreeMap<String, String>>,
330332
#[serde(skip_serializing_if = "Option::is_none")]
@@ -569,7 +571,7 @@ pub struct HttpQuery {
569571
// cache only
570572
pub(crate) result_timeout_secs: u64,
571573

572-
execute_state: Arc<Mutex<Executor>>,
574+
pub(crate) execute_state: Arc<Mutex<Executor>>,
573575

574576
// client states
575577
client_state: Arc<Mutex<ClientState>>,

tests/nox/suites/1_stateful/09_http_handler/test_09_0014_query_lifecycle.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
auth = ("root", "")
88
STICKY_HEADER = "X-DATABEND-STICKY-NODE"
99

10-
max_threads = 32
10+
timezone = 'Asia/Shanghai'
1111

1212
def patch_json(resp):
1313
if resp.get("stats", {}).get("running_time_ms"):
@@ -19,7 +19,7 @@ def do_query(query, timeout=10, pagination=None, port=8000, patch=True):
1919
session = {
2020
"settings": {
2121
"http_handler_result_timeout_secs": f"{timeout}",
22-
"max_threads": f"{max_threads}"
22+
"timezone": f"{timezone}"
2323
}
2424
}
2525

@@ -118,7 +118,7 @@ def test_query_lifecycle_finalized(rows):
118118
"max_rows_in_buffer": max_rows_per_page,
119119
}
120120

121-
resp0 = do_query(f"select * from numbers({rows})", timeout=timeout, pagination=pagination)
121+
resp0 = do_query(f"select number from numbers({rows}) order by number", timeout=timeout, pagination=pagination)
122122

123123
query_id = resp0.get("id")
124124
node_id = resp0.get("node_id")
@@ -148,14 +148,15 @@ def test_query_lifecycle_finalized(rows):
148148
"final_uri": f"/v1/query/{query_id}/final",
149149
"next_uri": f"/v1/query/{query_id}/page/1",
150150
"kill_uri": f"/v1/query/{query_id}/kill",
151+
'settings': {'timezone': 'Asia/Shanghai'},
151152
'session': {'catalog': 'default',
152153
'database': 'default',
153154
'internal': sessions_internal,
154155
'need_keep_alive': False,
155156
'need_sticky': False,
156157
'role': 'account_admin',
157158
'settings': {'http_handler_result_timeout_secs': f'{timeout}',
158-
'max_threads': f"{max_threads}"},
159+
'timezone': f"{timezone}"},
159160
'txn_state': 'AutoCommit'}
160161
}
161162

@@ -184,6 +185,7 @@ def test_query_lifecycle_finalized(rows):
184185

185186
# not return session since nothing changed
186187
exp["session"] = None
188+
del exp["settings"] # only in the first resp
187189
exp["state"] = "Succeeded"
188190
if rows == 8:
189191
exp["next_uri"] = f"/v1/query/{query_id}/page/2"

0 commit comments

Comments
 (0)