Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions rust/cubesql/cubesql/src/compile/engine/udf/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3281,6 +3281,42 @@
// %% is escaped to single %
result.push('%');
}
Some('s') => {
// Handle %s - regular string
if arg_index >= args.len() {
return Err(DataFusionError::Execution(
"Not enough arguments for format string".to_string(),
));

Check warning on line 3289 in rust/cubesql/cubesql/src/compile/engine/udf/common.rs

View check run for this annotation

Codecov / codecov/patch

rust/cubesql/cubesql/src/compile/engine/udf/common.rs#L3287-L3289

Added lines #L3287 - L3289 were not covered by tests
}

let arg = &args[arg_index];
let value = match arg.data_type() {
DataType::Utf8 => {
let str_arr = downcast_string_arg!(arg, "arg", i32);
if str_arr.is_null(i) {
// A null value is treated as an empty string
String::new()
} else {
str_arr.value(i).to_string()
}
}
_ => {
// For other types, try to convert to string
let str_arr = cast(&arg, &DataType::Utf8)?;
let str_arr =
str_arr.as_any().downcast_ref::<StringArray>().unwrap();
if str_arr.is_null(i) {

Check warning on line 3308 in rust/cubesql/cubesql/src/compile/engine/udf/common.rs

View check run for this annotation

Codecov / codecov/patch

rust/cubesql/cubesql/src/compile/engine/udf/common.rs#L3305-L3308

Added lines #L3305 - L3308 were not covered by tests
// A null value is treated as an empty string
String::new()

Check warning on line 3310 in rust/cubesql/cubesql/src/compile/engine/udf/common.rs

View check run for this annotation

Codecov / codecov/patch

rust/cubesql/cubesql/src/compile/engine/udf/common.rs#L3310

Added line #L3310 was not covered by tests
} else {
str_arr.value(i).to_string()

Check warning on line 3312 in rust/cubesql/cubesql/src/compile/engine/udf/common.rs

View check run for this annotation

Codecov / codecov/patch

rust/cubesql/cubesql/src/compile/engine/udf/common.rs#L3312

Added line #L3312 was not covered by tests
}
}
};

result.push_str(&value);
arg_index += 1;
}
Some(c) => {
return Err(DataFusionError::Execution(format!(
"Unsupported format specifier %{}",
Expand Down
24 changes: 24 additions & 0 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16214,6 +16214,30 @@ LIMIT {{ limit }}{% endif %}"#.to_string(),

#[tokio::test]
async fn test_format_function() -> Result<(), CubeError> {
// Test: Basic usage with a single string
let result = execute_query(
"SELECT format('%s', 'foo') AS formatted_string".to_string(),
DatabaseProtocol::PostgreSQL,
)
.await?;
insta::assert_snapshot!("formatted_string", result);

// Test: Basic usage with a single null string
let result = execute_query(
"SELECT format('%s', NULL) = '' AS formatted_null_string_is_empty".to_string(),
DatabaseProtocol::PostgreSQL,
)
.await?;
insta::assert_snapshot!("formatted_null_string_is_empty", result);

// Test: Basic usage with a multiple strings
let result = execute_query(
"SELECT format('%s.%s', 'foo', 'bar') AS formatted_strings".to_string(),
DatabaseProtocol::PostgreSQL,
)
.await?;
insta::assert_snapshot!("formatted_strings", result);

// Test: Basic usage with a single identifier
let result = execute_query(
"SELECT format('%I', 'column_name') AS formatted_identifier".to_string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: cubesql/src/compile/mod.rs
expression: result
---
+--------------------------------+
| formatted_null_string_is_empty |
+--------------------------------+
| true |
+--------------------------------+
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: cubesql/src/compile/mod.rs
expression: result
---
+------------------+
| formatted_string |
+------------------+
| foo |
+------------------+
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: cubesql/src/compile/mod.rs
expression: result
---
+-------------------+
| formatted_strings |
+-------------------+
| foo.bar |
+-------------------+
Loading
Loading