-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Various schema corrections (#18474)
- Loading branch information
Showing
9 changed files
with
301 additions
and
173 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use arrow::legacy::error::PolarsResult; | ||
use polars_utils::arena::{Arena, Node}; | ||
use polars_utils::format_pl_smallstr; | ||
|
||
use super::*; | ||
use crate::dsl::{Expr, FunctionExpr}; | ||
use crate::plans::AExpr; | ||
use crate::prelude::FunctionOptions; | ||
|
||
pub(super) fn convert_functions( | ||
input: Vec<Expr>, | ||
function: FunctionExpr, | ||
options: FunctionOptions, | ||
arena: &mut Arena<AExpr>, | ||
state: &mut ConversionContext, | ||
) -> PolarsResult<Node> { | ||
match function { | ||
// This can be created by col(*).is_null() on empty dataframes. | ||
FunctionExpr::Boolean(BooleanFunction::AllHorizontal | BooleanFunction::AnyHorizontal) | ||
if input.is_empty() => | ||
{ | ||
return to_aexpr_impl(lit(true), arena, state); | ||
}, | ||
// Convert to binary expression as the optimizer understands those. | ||
// Don't exceed 128 expressions as we might stackoverflow. | ||
FunctionExpr::Boolean(BooleanFunction::AllHorizontal) => { | ||
if input.len() < 128 { | ||
let expr = input | ||
.into_iter() | ||
.reduce(|l, r| l.logical_and(r)) | ||
.unwrap() | ||
.cast(DataType::Boolean); | ||
return to_aexpr_impl(expr, arena, state); | ||
} | ||
}, | ||
FunctionExpr::Boolean(BooleanFunction::AnyHorizontal) => { | ||
if input.len() < 128 { | ||
let expr = input | ||
.into_iter() | ||
.reduce(|l, r| l.logical_or(r)) | ||
.unwrap() | ||
.cast(DataType::Boolean); | ||
return to_aexpr_impl(expr, arena, state); | ||
} | ||
}, | ||
_ => {}, | ||
} | ||
|
||
let e = to_expr_irs(input, arena)?; | ||
|
||
if state.output_name.is_none() { | ||
// Handles special case functions like `struct.field`. | ||
if let Some(name) = function.output_name() { | ||
state.output_name = name | ||
} else { | ||
set_function_output_name(&e, state, || format_pl_smallstr!("{}", &function)); | ||
} | ||
} | ||
|
||
let ae_function = AExpr::Function { | ||
input: e, | ||
function, | ||
options, | ||
}; | ||
Ok(arena.add(ae_function)) | ||
} |
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
83 changes: 83 additions & 0 deletions
83
crates/polars-plan/src/plans/conversion/type_coercion/functions.rs
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use either::Either; | ||
|
||
use super::*; | ||
|
||
pub(super) fn get_function_dtypes( | ||
input: &[ExprIR], | ||
expr_arena: &Arena<AExpr>, | ||
input_schema: &Schema, | ||
function: &FunctionExpr, | ||
mut options: FunctionOptions, | ||
) -> PolarsResult<Either<Vec<DataType>, AExpr>> { | ||
let mut early_return = move || { | ||
// Next iteration this will not hit anymore as options is updated. | ||
options.cast_to_supertypes = None; | ||
Ok(Either::Right(AExpr::Function { | ||
function: function.clone(), | ||
input: input.to_vec(), | ||
options, | ||
})) | ||
}; | ||
|
||
let mut dtypes = Vec::with_capacity(input.len()); | ||
let mut first = true; | ||
for e in input { | ||
let Some((_, dtype)) = get_aexpr_and_type(expr_arena, e.node(), input_schema) else { | ||
return early_return(); | ||
}; | ||
|
||
if first { | ||
check_namespace(function, &dtype)?; | ||
first = false; | ||
} | ||
// Ignore Unknown in the inputs. | ||
// We will raise if we cannot find the supertype later. | ||
match dtype { | ||
DataType::Unknown(UnknownKind::Any) => { | ||
return early_return(); | ||
}, | ||
_ => dtypes.push(dtype), | ||
} | ||
} | ||
|
||
if dtypes.iter().all_equal() { | ||
return early_return(); | ||
} | ||
Ok(Either::Left(dtypes)) | ||
} | ||
|
||
// `str` namespace belongs to `String` | ||
// `cat` namespace belongs to `Categorical` etc. | ||
fn check_namespace(function: &FunctionExpr, first_dtype: &DataType) -> PolarsResult<()> { | ||
match function { | ||
#[cfg(feature = "strings")] | ||
FunctionExpr::StringExpr(_) => { | ||
polars_ensure!(first_dtype == &DataType::String, InvalidOperation: "expected String type, got: {}", first_dtype) | ||
}, | ||
FunctionExpr::BinaryExpr(_) => { | ||
polars_ensure!(first_dtype == &DataType::Binary, InvalidOperation: "expected Binary type, got: {}", first_dtype) | ||
}, | ||
#[cfg(feature = "temporal")] | ||
FunctionExpr::TemporalExpr(_) => { | ||
polars_ensure!(first_dtype.is_temporal(), InvalidOperation: "expected Date(time)/Duration type, got: {}", first_dtype) | ||
}, | ||
FunctionExpr::ListExpr(_) => { | ||
polars_ensure!(matches!(first_dtype, DataType::List(_)), InvalidOperation: "expected List type, got: {}", first_dtype) | ||
}, | ||
#[cfg(feature = "dtype-array")] | ||
FunctionExpr::ArrayExpr(_) => { | ||
polars_ensure!(matches!(first_dtype, DataType::Array(_, _)), InvalidOperation: "expected Array type, got: {}", first_dtype) | ||
}, | ||
#[cfg(feature = "dtype-struct")] | ||
FunctionExpr::StructExpr(_) => { | ||
polars_ensure!(matches!(first_dtype, DataType::Struct(_)), InvalidOperation: "expected Struct type, got: {}", first_dtype) | ||
}, | ||
#[cfg(feature = "dtype-categorical")] | ||
FunctionExpr::Categorical(_) => { | ||
polars_ensure!(matches!(first_dtype, DataType::Categorical(_, _)), InvalidOperation: "expected Struct type, got: {}", first_dtype) | ||
}, | ||
_ => {}, | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.