Skip to content

Commit f6d40bc

Browse files
authored
fix(query): view query should not cover the origin query (#18272)
Run `CREATE VIEW c AS SELECT * FROM hits.hits LIMIT ` query the query_history: ```sql SELECT query_start_time, query_duration_ms, query_id, query_text FROM system_history.query_history; ``` In main : │ 2025-06-30 04:54:06.178345 │ 0 │ 475d40329f8946fd9cc2310f79de643f │ SELECT * FROM hits.hits LIMIT 1 │ In pr: │ 2025-06-30 04:59:20.485635 │ 82 │ 04319b72fff94aac8b6c4f72fae01414 │ CREATE VIEW c AS SELECT * FROM hits.hits LIMIT 1 │
1 parent beca0a1 commit f6d40bc

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/query/service/src/sessions/query_ctx_shared.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,10 @@ impl QueryContextShared {
631631
}
632632

633633
pub fn attach_query_str(&self, kind: QueryKind, query: String) {
634+
// `create view as view_query` the view_query should not cover create view
635+
if !self.get_query_str().is_empty() {
636+
return;
637+
}
634638
{
635639
let mut running_query = self.running_query.write();
636640
*running_query = Some(short_sql(
@@ -649,14 +653,18 @@ impl QueryContextShared {
649653

650654
pub fn attach_query_hash(&self, text_hash: String, parameterized_hash: String) {
651655
{
652-
let mut running_query_hash = self.running_query_text_hash.write();
653-
*running_query_hash = Some(text_hash);
656+
if self.get_query_text_hash().is_empty() {
657+
let mut running_query_hash = self.running_query_text_hash.write();
658+
*running_query_hash = Some(text_hash);
659+
}
654660
}
655661

656662
{
657-
let mut running_query_parameterized_hash =
658-
self.running_query_parameterized_hash.write();
659-
*running_query_parameterized_hash = Some(parameterized_hash);
663+
if self.get_query_parameterized_hash().is_empty() {
664+
let mut running_query_parameterized_hash =
665+
self.running_query_parameterized_hash.write();
666+
*running_query_parameterized_hash = Some(parameterized_hash);
667+
}
660668
}
661669
}
662670

tests/logging/check_logs_table.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function check_query_log() {
1515
full_sql_query+=" query_id = '$query_id'"
1616
fi
1717

18+
echo $full_sql_query
1819
response=$(curl -s -u root: -XPOST "http://localhost:8000/v1/query" \
1920
-H 'Content-Type: application/json' \
2021
-d "{\"sql\": \"$full_sql_query\"}")
@@ -39,6 +40,9 @@ response=$(curl -s -u root: -XPOST "http://localhost:8000/v1/query" -H 'Content-
3940
response=$(curl -s -u root: -XPOST "http://localhost:8000/v1/query" -H 'Content-Type: application/json' -d '{"sql": "create table t (a INT)"}')
4041
create_query_id=$(echo $response | jq -r '.id')
4142
echo "Create Query ID: $create_query_id"
43+
response=$(curl -s -u root: -XPOST "http://localhost:8000/v1/query" -H 'Content-Type: application/json' -d '{"sql": "create view v as select a from t"}')
44+
create_view_query_id=$(echo $response | jq -r '.id')
45+
echo "Create VIEW Query ID: $create_view_query_id"
4246
response=$(curl -s -u root: -XPOST "http://localhost:8000/v1/query" -H 'Content-Type: application/json' -d '{"sql": "insert into t values (1),(2),(3)"}')
4347
insert_query_id=$(echo $response | jq -r '.id')
4448
echo "Insert Query ID: $insert_query_id"
@@ -63,8 +67,6 @@ check_query_log "basic-3" "$query_id" "SELECT count(*) FROM system_history.log_h
6367
# Test 4
6468
check_query_log "basic-4" "$query_id" "SELECT count(*) FROM system_history.query_history WHERE" "1"
6569

66-
# Test 5
67-
check_query_log "basic-5" "$select_query_id" "SELECT count(*) FROM system_history.log_history WHERE target = 'databend::log::access' and" "1"
6870

6971
# Test 6
7072
check_query_log "basic-6" "$select_query_id" "SELECT count(*) FROM system_history.access_history WHERE" "1"
@@ -78,7 +80,8 @@ check_query_log "basic-8" "$insert_query_id" "SELECT objects_modified[0]['object
7880
# Test 9
7981
check_query_log "basic-9" "$select_query_id" "SELECT base_objects_accessed[0]['object_name'] FROM system_history.access_history WHERE" "default.default.t"
8082

81-
83+
# Test 10
84+
check_query_log "basic-10" $create_view_query_id "select query_text from system_history.query_history where" "CREATE VIEW v AS SELECT a FROM t"
8285

8386
# Check timezone, regression test for https://github.com/databendlabs/databend/pull/18059
8487
check_query_log "t-1" "$select_query_id" "settings (timezone='Asia/Shanghai') SELECT DATE_DIFF(hour, timestamp, now()) FROM system_history.log_history WHERE target = 'databend::log::profile' and" "0"

0 commit comments

Comments
 (0)