Skip to content

Commit 6f1a9ba

Browse files
authored
cubesql-engine (chore): remove duplicates (#8321)
* cubesql-engine (chore): remove duplicates Removed 2nd identical coercion function and renamed the only one into more general
1 parent 667e95b commit 6f1a9ba

File tree

2 files changed

+22
-44
lines changed

2 files changed

+22
-44
lines changed

rust/cubesql/cubesql/src/compile/engine/df/coerce.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,7 @@ pub fn numerical_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<Da
4747
}
4848
}
4949

50-
pub fn if_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
51-
// same type => all good
52-
if lhs_type == rhs_type {
53-
return Some(lhs_type.clone());
54-
}
55-
56-
let hack_ty = match (lhs_type, rhs_type) {
57-
(_, DataType::Null) => Some(lhs_type.clone()),
58-
(DataType::Null, _) => Some(rhs_type.clone()),
59-
//
60-
(DataType::Utf8, DataType::UInt64) => Some(DataType::Utf8),
61-
(DataType::Utf8, DataType::Int64) => Some(DataType::Utf8),
62-
//
63-
(DataType::UInt64, DataType::Utf8) => Some(DataType::Utf8),
64-
(DataType::Int64, DataType::Utf8) => Some(DataType::Utf8),
65-
//
66-
_ => None,
67-
};
68-
69-
hack_ty.or_else(|| numerical_coercion(lhs_type, rhs_type))
70-
}
71-
72-
pub fn least_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
50+
pub fn common_type_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
7351
// same type => all good
7452
if lhs_type == rhs_type {
7553
return Some(lhs_type.clone());

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ use sha1_smol::Sha1;
4040

4141
use crate::{
4242
compile::engine::{
43-
df::{
44-
coerce::{if_coercion, least_coercion},
45-
columar::if_then_else,
46-
},
43+
df::{coerce::common_type_coercion, columar::if_then_else},
4744
udf::utils::*,
4845
},
4946
sql::SessionState,
@@ -425,13 +422,14 @@ pub fn create_if_udf() -> ScalarUDF {
425422
let left = &args[1];
426423
let right = &args[2];
427424

428-
let return_type = if_coercion(left.data_type(), right.data_type()).ok_or_else(|| {
429-
DataFusionError::Execution(format!(
430-
"Positive and negative results must be the same type, actual: [{}, {}]",
431-
left.data_type(),
432-
right.data_type(),
433-
))
434-
})?;
425+
let return_type =
426+
common_type_coercion(left.data_type(), right.data_type()).ok_or_else(|| {
427+
DataFusionError::Execution(format!(
428+
"Positive and negative results must be the same type, actual: [{}, {}]",
429+
left.data_type(),
430+
right.data_type(),
431+
))
432+
})?;
435433

436434
let cond_array = match condition.data_type() {
437435
// // Arrow doesnt support UTF8 -> Boolean cast
@@ -468,7 +466,7 @@ pub fn create_if_udf() -> ScalarUDF {
468466
let return_type: ReturnTypeFunction = Arc::new(move |types| {
469467
assert!(types.len() == 3);
470468

471-
let base_type = if_coercion(&types[1], &types[2]).ok_or_else(|| {
469+
let base_type = common_type_coercion(&types[1], &types[2]).ok_or_else(|| {
472470
DataFusionError::Execution(format!(
473471
"Positive and negative results must be the same type, actual: [{}, {}]",
474472
&types[1], &types[2],
@@ -486,21 +484,23 @@ pub fn create_if_udf() -> ScalarUDF {
486484
)
487485
}
488486

489-
// LEAST() function in MySQL is used to find smallest values from given arguments respectively. If any given value is NULL, it return NULLs. Otherwise it returns the smallest value.
487+
// LEAST() function in MySQL is used to find the smallest values from given arguments respectively.
488+
// If any given value is NULL, it returns NULLs. Otherwise, it returns the smallest value.
490489
pub fn create_least_udf() -> ScalarUDF {
491490
let fun = make_scalar_function(move |args: &[ArrayRef]| {
492491
assert!(args.len() == 2);
493492

494493
let left = &args[0];
495494
let right = &args[1];
496495

497-
let base_type = least_coercion(&left.data_type(), &right.data_type()).ok_or_else(|| {
498-
DataFusionError::Execution(format!(
499-
"Unable to coercion types, actual: [{}, {}]",
500-
&left.data_type(),
501-
&right.data_type(),
502-
))
503-
})?;
496+
let base_type =
497+
common_type_coercion(&left.data_type(), &right.data_type()).ok_or_else(|| {
498+
DataFusionError::Execution(format!(
499+
"Unable to coercion types, actual: [{}, {}]",
500+
&left.data_type(),
501+
&right.data_type(),
502+
))
503+
})?;
504504

505505
let result = if left.is_null(0) {
506506
cast(&left, &base_type)?
@@ -548,7 +548,7 @@ pub fn create_least_udf() -> ScalarUDF {
548548
return Ok(Arc::new(DataType::Null));
549549
}
550550

551-
let base_type = least_coercion(&types[0], &types[1]).ok_or_else(|| {
551+
let base_type = common_type_coercion(&types[0], &types[1]).ok_or_else(|| {
552552
DataFusionError::Execution(format!(
553553
"Unable to coercion types, actual: [{}, {}]",
554554
&types[0], &types[1],

0 commit comments

Comments
 (0)