From ac12ad171e2b2097c6b06c4e2181e835ae931348 Mon Sep 17 00:00:00 2001 From: Bohu Date: Sun, 19 May 2024 11:09:40 +0800 Subject: [PATCH] chore: short_sql from 10KB to 30KB (#15575) --- src/query/service/src/sessions/query_ctx_shared.rs | 9 ++++++--- src/query/service/tests/it/sessions/query_ctx.rs | 12 ++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/query/service/src/sessions/query_ctx_shared.rs b/src/query/service/src/sessions/query_ctx_shared.rs index 6d4bc457db026..cc55c410ffd13 100644 --- a/src/query/service/src/sessions/query_ctx_shared.rs +++ b/src/query/service/src/sessions/query_ctx_shared.rs @@ -582,14 +582,17 @@ impl Drop for QueryContextShared { pub fn short_sql(sql: String) -> String { use unicode_segmentation::UnicodeSegmentation; + const MAX_LENGTH: usize = 30 * 1024; // 30KB + let query = sql.trim_start(); - if query.as_bytes().len() > 10240 && query.as_bytes()[..6].eq_ignore_ascii_case(b"INSERT") { - // keep first 10KB (10,240 bytes) + if query.as_bytes().len() > MAX_LENGTH && query.as_bytes()[..6].eq_ignore_ascii_case(b"INSERT") + { + // keep first 30KB let mut result = Vec::new(); let mut bytes_taken = 0; for grapheme in query.graphemes(true) { let grapheme_bytes = grapheme.as_bytes(); - if bytes_taken + grapheme_bytes.len() <= 10240 { + if bytes_taken + grapheme_bytes.len() <= MAX_LENGTH { result.extend_from_slice(grapheme_bytes); bytes_taken += grapheme_bytes.len(); } else { diff --git a/src/query/service/tests/it/sessions/query_ctx.rs b/src/query/service/tests/it/sessions/query_ctx.rs index 039f7c86ef454..e5a1fc615ab39 100644 --- a/src/query/service/tests/it/sessions/query_ctx.rs +++ b/src/query/service/tests/it/sessions/query_ctx.rs @@ -69,19 +69,19 @@ async fn test_get_storage_accessor_fs() -> Result<()> { #[test] fn test_short_sql() { - // Test case 1: SQL query shorter than 10KB + // Test case 1: SQL query shorter than 30KB let sql1 = "SELECT * FROM users WHERE id = 1;".to_string(); assert_eq!(short_sql(sql1.clone()), sql1); - // Test case 2: SQL query longer than 10KB and starts with "INSERT" + // Test case 2: SQL query longer than 30KB and starts with "INSERT" let long_sql = "INSERT INTO users (id, name, email) VALUES ".to_string() - + &"(1, 'John Doe', 'john@example.com'), ".repeat(1000); - let expected_result = long_sql.as_bytes()[..10240].to_vec(); + + &"(1, 'John Doe', 'john@example.com'), ".repeat(1500); // Adjusted for 30KB + let expected_result = long_sql.as_bytes()[..30 * 1024].to_vec(); let expected_result = String::from_utf8(expected_result).unwrap() + "..."; assert_eq!(short_sql(long_sql), expected_result); - // Test case 3: SQL query longer than 10KB but does not start with "INSERT" - let long_sql = "SELECT * FROM users WHERE ".to_string() + &"id = 1 OR ".repeat(1000); + // Test case 3: SQL query longer than 30KB but does not start with "INSERT" + let long_sql = "SELECT * FROM users WHERE ".to_string() + &"id = 1 OR ".repeat(1500); // Adjusted for 30KB assert_eq!(short_sql(long_sql.clone()), long_sql); // Test case 4: Empty SQL query