-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: spill out short_sql to common/base/src/string.rs (#15633)
- Loading branch information
Showing
12 changed files
with
83 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
|
||
use databend_common_base::base::*; | ||
use databend_common_exception::Result; | ||
use unicode_segmentation::UnicodeSegmentation; | ||
|
||
#[test] | ||
fn test_progress() -> Result<()> { | ||
|
@@ -107,3 +108,47 @@ fn test_mask_connection_info() { | |
|
||
assert_eq!(expect, actual); | ||
} | ||
|
||
#[test] | ||
fn test_short_sql() { | ||
// Test case 1: SQL query shorter than 128 characters | ||
let sql1 = "SELECT * FROM users WHERE id = 1;".to_string(); | ||
assert_eq!(short_sql(sql1.clone()), sql1); | ||
|
||
// Test case 2: SQL query longer than 128 characters and starts with "INSERT" | ||
let long_sql_insert = "INSERT INTO users (id, name, email) VALUES ".to_string() | ||
+ &"(1, 'John Doe', '[email protected]'), ".repeat(5); // Make sure this creates a string longer than 128 characters | ||
let expected_length_insert = long_sql_insert.graphemes(true).count().saturating_sub(128); | ||
let expected_result_insert = { | ||
let truncated: String = long_sql_insert.graphemes(true).take(128).collect(); | ||
truncated + &format!("...[{} more characters]", expected_length_insert) | ||
}; | ||
assert_eq!(short_sql(long_sql_insert.clone()), expected_result_insert); | ||
|
||
// Test case 3: SQL query longer than 128 characters but does not start with "INSERT" | ||
let long_sql_update = | ||
"UPDATE users SET name = 'John' WHERE id = 1;".to_string() + &"id = 1 OR ".repeat(20); // Make sure this creates a string longer than 128 characters | ||
assert_eq!(short_sql(long_sql_update.clone()), long_sql_update); | ||
|
||
// Test case 4: Empty SQL query | ||
let empty_sql = "".to_string(); | ||
assert_eq!(short_sql(empty_sql.clone()), empty_sql); | ||
|
||
// Test case 5: SQL query with leading whitespace | ||
let sql_with_whitespace = | ||
" INSERT INTO users (id, name, email) VALUES (1, 'John Doe', '[email protected]');" | ||
.to_string(); | ||
let trimmed_sql = sql_with_whitespace.trim_start().to_string(); | ||
assert_eq!(short_sql(sql_with_whitespace.clone()), trimmed_sql); | ||
|
||
// Test case 6: SQL query with multiple emojis to test truncation at an emoji point | ||
let emoji_sql = "INSERT INTO users (id, name) VALUES (1, 'John Doe 😊😊😊😊😊😊😊😊😊😊');" | ||
.to_string() | ||
+ &" more text to exceed 128 characters ".repeat(3); | ||
let expected_emoji_result = { | ||
let truncated: String = emoji_sql.graphemes(true).take(128).collect(); | ||
let remaining_length = emoji_sql.graphemes(true).count().saturating_sub(128); | ||
truncated + &format!("...[{} more characters]", remaining_length) | ||
}; | ||
assert_eq!(short_sql(emoji_sql.clone()), expected_emoji_result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ use databend_common_exception::Result; | |
use databend_common_meta_app::storage::StorageFsConfig; | ||
use databend_common_meta_app::storage::StorageParams; | ||
use databend_common_meta_app::storage::StorageS3Config; | ||
use databend_query::sessions::short_sql; | ||
use databend_query::sessions::TableContext; | ||
use databend_query::test_kits::ConfigBuilder; | ||
use databend_query::test_kits::TestFixture; | ||
|
@@ -66,29 +65,3 @@ async fn test_get_storage_accessor_fs() -> Result<()> { | |
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_short_sql() { | ||
// 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 128 bytes and starts with "INSERT" | ||
let long_sql = "INSERT INTO users (id, name, email) VALUES ".to_string() | ||
+ &"(1, 'John Doe', '[email protected]'), ".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 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 | ||
let empty_sql = "".to_string(); | ||
assert_eq!(short_sql(empty_sql.clone()), empty_sql); | ||
|
||
// Test case 5: SQL query with leading whitespace | ||
let sql_with_whitespace = " SELECT * FROM users;".to_string(); | ||
assert_eq!(short_sql(sql_with_whitespace.clone()), sql_with_whitespace); | ||
} |