-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Various schema corrections #18474
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will try this in a follow up.