Skip to content

Commit 22c74ed

Browse files
fix(cubesql) Formatting
1 parent 7b43e3d commit 22c74ed

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3174,9 +3174,10 @@ pub fn create_col_description_udf() -> ScalarUDF {
31743174

31753175
ScalarUDF::new(
31763176
"col_description",
3177-
&Signature::one_of(vec![
3178-
TypeSignature::Exact(vec![DataType::Utf8, DataType::Utf8]),
3179-
], Volatility::Immutable),
3177+
&Signature::one_of(
3178+
vec![TypeSignature::Exact(vec![DataType::Utf8, DataType::Utf8])],
3179+
Volatility::Immutable,
3180+
),
31803181
&return_type,
31813182
&fun,
31823183
)
@@ -3233,31 +3234,32 @@ pub fn create_format_udf() -> ScalarUDF {
32333234
let str_arr = downcast_string_arg!(arg, "arg", i32);
32343235
if str_arr.is_null(i) {
32353236
return Err(DataFusionError::Execution(
3236-
"NULL values cannot be formatted as identifiers".to_string(),
3237+
"NULL values cannot be formatted as identifiers"
3238+
.to_string(),
32373239
));
32383240
}
32393241
str_arr.value(i).to_string()
32403242
}
32413243
_ => {
32423244
// For other types, try to convert to string
32433245
let str_arr = cast(&arg, &DataType::Utf8)?;
3244-
let str_arr = str_arr
3245-
.as_any()
3246-
.downcast_ref::<StringArray>()
3247-
.unwrap();
3246+
let str_arr =
3247+
str_arr.as_any().downcast_ref::<StringArray>().unwrap();
32483248
if str_arr.is_null(i) {
32493249
return Err(DataFusionError::Execution(
3250-
"NULL values cannot be formatted as identifiers".to_string(),
3250+
"NULL values cannot be formatted as identifiers"
3251+
.to_string(),
32513252
));
32523253
}
32533254
str_arr.value(i).to_string()
32543255
}
32553256
};
32563257

32573258
// Quote identifier if necessary
3258-
let needs_quoting = !value.chars().all(|c| {
3259-
c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_'
3260-
}) || value.is_empty();
3259+
let needs_quoting = !value
3260+
.chars()
3261+
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
3262+
|| value.is_empty();
32613263

32623264
if needs_quoting {
32633265
result.push('"');
@@ -3296,16 +3298,12 @@ pub fn create_format_udf() -> ScalarUDF {
32963298

32973299
ScalarUDF::new(
32983300
"format",
3299-
&Signature::variadic(
3300-
vec![DataType::Utf8],
3301-
Volatility::Immutable,
3302-
),
3301+
&Signature::variadic(vec![DataType::Utf8], Volatility::Immutable),
33033302
&return_type,
33043303
&fun,
33053304
)
33063305
}
33073306

3308-
33093307
pub fn create_json_build_object_udf() -> ScalarUDF {
33103308
let fun = make_scalar_function(move |_args: &[ArrayRef]| {
33113309
// TODO: Implement

rust/cubesql/cubesql/src/compile/mod.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16262,27 +16262,52 @@ LIMIT {{ limit }}{% endif %}"#.to_string(),
1626216262
#[tokio::test]
1626316263
async fn test_format_function() -> Result<(), CubeError> {
1626416264
// Test: Basic usage with a single identifier
16265-
let result = execute_query("SELECT format('%I', 'column_name') AS formatted_identifier".to_string(), DatabaseProtocol::PostgreSQL).await?;
16265+
let result = execute_query(
16266+
"SELECT format('%I', 'column_name') AS formatted_identifier".to_string(),
16267+
DatabaseProtocol::PostgreSQL,
16268+
)
16269+
.await?;
1626616270
insta::assert_snapshot!("formatted_identifier", result);
1626716271

1626816272
// Test: Using multiple identifiers
16269-
let result = execute_query("SELECT format('%I, %I', 'table_name', 'column_name') AS formatted_identifiers".to_string(), DatabaseProtocol::PostgreSQL).await?;
16273+
let result = execute_query(
16274+
"SELECT format('%I, %I', 'table_name', 'column_name') AS formatted_identifiers"
16275+
.to_string(),
16276+
DatabaseProtocol::PostgreSQL,
16277+
)
16278+
.await?;
1627016279
insta::assert_snapshot!("formatted_identifiers", result);
1627116280

1627216281
// Test: Unsupported format specifier
16273-
let result = execute_query("SELECT format('%X', 'value') AS unsupported_specifier".to_string(), DatabaseProtocol::PostgreSQL).await;
16282+
let result = execute_query(
16283+
"SELECT format('%X', 'value') AS unsupported_specifier".to_string(),
16284+
DatabaseProtocol::PostgreSQL,
16285+
)
16286+
.await;
1627416287
assert!(result.is_err());
1627516288

1627616289
// Test: Format string ending with %
16277-
let result = execute_query("SELECT format('%', 'value') AS invalid_format".to_string(), DatabaseProtocol::PostgreSQL).await;
16290+
let result = execute_query(
16291+
"SELECT format('%', 'value') AS invalid_format".to_string(),
16292+
DatabaseProtocol::PostgreSQL,
16293+
)
16294+
.await;
1627816295
assert!(result.is_err());
1627916296

1628016297
// Test: Quoting necessary for special characters
16281-
let result = execute_query("SELECT format('%I', 'column-name') AS quoted_identifier".to_string(), DatabaseProtocol::PostgreSQL).await?;
16298+
let result = execute_query(
16299+
"SELECT format('%I', 'column-name') AS quoted_identifier".to_string(),
16300+
DatabaseProtocol::PostgreSQL,
16301+
)
16302+
.await?;
1628216303
insta::assert_snapshot!("quoted_identifier", result);
1628316304

1628416305
// Test: Quoting necessary for reserved keywords
16285-
let result = execute_query("SELECT format('%I', 'select') AS quoted_keyword".to_string(), DatabaseProtocol::PostgreSQL).await?;
16306+
let result = execute_query(
16307+
"SELECT format('%I', 'select') AS quoted_keyword".to_string(),
16308+
DatabaseProtocol::PostgreSQL,
16309+
)
16310+
.await?;
1628616311
insta::assert_snapshot!("quoted_keyword", result);
1628716312

1628816313
Ok(())

0 commit comments

Comments
 (0)