Skip to content

Commit

Permalink
update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
universalmind303 committed Mar 5, 2024
1 parent b13f806 commit ae5932b
Showing 1 changed file with 75 additions and 6 deletions.
81 changes: 75 additions & 6 deletions datafusion/expr/src/type_coercion/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.

use std::sync::Arc;

use crate::signature::{
ArrayFunctionSignature, FIXED_SIZE_LIST_WILDCARD, TIMEZONE_WILDCARD,
};
Expand Down Expand Up @@ -382,12 +384,20 @@ fn coerced_from<'a>(
Some(type_into.clone())
}
// should be able to coerce wildcard fixed size list to non wildcard fixed size list
FixedSizeList(_, _)
if matches!(type_from, FixedSizeList(_, FIXED_SIZE_LIST_WILDCARD))
&& list_ndims(type_from) == list_ndims(type_into) =>
{
Some(type_into.clone())
}
FixedSizeList(f_into, FIXED_SIZE_LIST_WILDCARD) => match type_from {
FixedSizeList(f_from, size_from) => {
match coerced_from(f_into.data_type(), f_from.data_type()) {
Some(data_type) if &data_type != f_into.data_type() => {
let new_field =
Arc::new(f_into.as_ref().clone().with_data_type(data_type));
Some(FixedSizeList(new_field, *size_from))
}
Some(_) => Some(FixedSizeList(f_into.clone(), *size_from)),
_ => None,
}
}
_ => None,
},

Timestamp(unit, Some(tz)) if tz.as_ref() == TIMEZONE_WILDCARD => {
match type_from {
Expand Down Expand Up @@ -419,6 +429,8 @@ fn coerced_from<'a>(
mod tests {
use std::sync::Arc;

use crate::Volatility;

use super::*;
use arrow::datatypes::{DataType, Field, TimeUnit};

Expand Down Expand Up @@ -537,6 +549,63 @@ mod tests {
let out = coerced_from(&type_into_nested, &type_from_nested);
assert_eq!(out, Some(type_into_nested));

let inner = Arc::new(Field::new("item", DataType::Int32, false));
let current_types = vec![
DataType::FixedSizeList(inner.clone(), 2), // able to coerce for any size
];
let signature = Signature::exact(
vec![DataType::FixedSizeList(
inner.clone(),
FIXED_SIZE_LIST_WILDCARD,
)],
Volatility::Stable,
);
let coerced_data_types = data_types(&current_types, &signature).unwrap();
assert_eq!(coerced_data_types, current_types);

Ok(())
}
#[test]
fn test_nested_wildcard_fixed_size_lists() -> Result<()> {
let type_into = DataType::FixedSizeList(
Arc::new(Field::new(
"item",
DataType::FixedSizeList(
Arc::new(Field::new("item", DataType::Int32, false)),
FIXED_SIZE_LIST_WILDCARD,
),
false,
)),
FIXED_SIZE_LIST_WILDCARD,
);

let type_from = DataType::FixedSizeList(
Arc::new(Field::new(
"item",
DataType::FixedSizeList(
Arc::new(Field::new("item", DataType::Int8, false)),
4,
),
false,
)),
3,
);

assert_eq!(
coerced_from(&type_into, &type_from),
Some(DataType::FixedSizeList(
Arc::new(Field::new(
"item",
DataType::FixedSizeList(
Arc::new(Field::new("item", DataType::Int32, false)),
4,
),
false,
)),
3,
))
);

Ok(())
}
}

0 comments on commit ae5932b

Please sign in to comment.