Skip to content

Commit

Permalink
chore: reduce the short_sql to 128bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
BohuTANG committed May 23, 2024
1 parent b80bb5d commit 48321cc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/query/service/src/sessions/query_ctx_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,12 +582,11 @@ impl Drop for QueryContextShared {

pub fn short_sql(sql: String) -> String {
use unicode_segmentation::UnicodeSegmentation;
const MAX_LENGTH: usize = 30 * 1024; // 30KB
const MAX_LENGTH: usize = 128;

let query = sql.trim_start();
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) {
Expand Down
12 changes: 6 additions & 6 deletions src/query/service/tests/it/sessions/query_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ async fn test_get_storage_accessor_fs() -> Result<()> {

#[test]
fn test_short_sql() {
// Test case 1: SQL query shorter than 30KB
// Test case 1: SQL query shorter than 128 bytes
let sql1 = "SELECT * FROM users WHERE id = 1;".to_string();
assert_eq!(short_sql(sql1.clone()), sql1);

// Test case 2: SQL query longer than 30KB and starts with "INSERT"
// Test case 2: SQL query longer than 128 bytes and starts with "INSERT"
let long_sql = "INSERT INTO users (id, name, email) VALUES ".to_string()
+ &"(1, 'John Doe', 'john@example.com'), ".repeat(1500); // Adjusted for 30KB
let expected_result = long_sql.as_bytes()[..30 * 1024].to_vec();
+ &"(1, 'John Doe', 'john@example.com'), ".repeat(5); // Adjusted for 128 bytes
let expected_result = long_sql.as_bytes()[..128].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 30KB but does not start with "INSERT"
let long_sql = "SELECT * FROM users WHERE ".to_string() + &"id = 1 OR ".repeat(1500); // Adjusted for 30KB
// Test case 3: SQL query longer than 128 bytes but does not start with "INSERT"
let long_sql = "SELECT * FROM users WHERE ".to_string() + &"id = 1 OR ".repeat(20); // Adjusted for 128 bytes
assert_eq!(short_sql(long_sql.clone()), long_sql);

// Test case 4: Empty SQL query
Expand Down

0 comments on commit 48321cc

Please sign in to comment.