alamb commented on code in PR #26282:
URL: https://github.com/apache/datafusion/pull/26282#discussion_r4683234105
##########
datafusion/expr/src/udf.rs:
##########
@@ -1069,6 +1142,20 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send
+ Sync + Any {
fn placement(&self, _args: &[ExpressionPlacement]) -> ExpressionPlacement {
ExpressionPlacement::KeepInPlace
}
+
+ /// Returns this function's [`NestedInputAccess`] capability, if it reads
Review Comment:
👍
##########
datafusion/expr/src/udf.rs:
##########
@@ -57,6 +59,70 @@ pub struct StructFieldMapping {
pub fields: Vec<(Vec<ScalarValue>, usize)>,
}
+/// A path of struct field names from the root of an argument down to a nested
Review Comment:
For anyone else reading this PR, here is a good place to start the review
##########
datafusion/expr/src/udf.rs:
##########
@@ -57,6 +59,70 @@ pub struct StructFieldMapping {
pub fields: Vec<(Vec<ScalarValue>, usize)>,
}
+/// A path of struct field names from the root of an argument down to a nested
+/// field, for example `["a", "b"]` for `s.a.b`.
+///
+/// Each component is a literal field name, which may itself contain dots. A
+/// path that ends at a struct refers to that struct's whole subtree; a data
+/// source that cannot narrow to a subtree reads the whole argument instead.
+pub type FieldPath = Vec<String>;
+
+/// Capability of scalar functions that read only part of their nested
+/// (struct) arguments.
+///
+/// Data sources use this to decode only the parts of a nested column that a
+/// call actually reads. For example, a Parquet scan evaluating
+/// `get_field(s, 'a')` only needs the leaves under `s.a`, not every leaf of
+/// `s`. The function is still evaluated as usual, on an argument from which
+/// the unread struct fields have been removed.
+///
+/// Obtained through [`ScalarUDFImpl::as_nested_input_access`].
+pub trait NestedInputAccess {
+ /// Returns, for each argument, the struct field paths this call reads, or
+ /// `None` to read that argument whole. The result may be shorter than the
+ /// argument list; missing trailing entries also mean "read whole".
+ ///
+ /// `args` describes the call's arguments against the schema being
+ /// planned for: a table schema during planning, or one file's physical
+ /// schema when that file is opened. The answer may differ between them,
+ /// for example when nested layouts differ between files.
+ ///
+ /// # Contract
+ ///
+ /// Evaluating the call on arguments narrowed to the returned paths must
+ /// produce the same values, return field and errors as evaluating it on
+ /// the full arguments. Narrowing removes every struct field that is not
+ /// on a returned path, and keeps the validity of the struct ancestors of
+ /// the fields that remain. Paths must only traverse struct fields. A path
+ /// that is invalid for the argument type causes the whole argument to be
+ /// read.
+ fn required_fields(&self, args: &ReturnFieldArgs) ->
Vec<Option<Vec<FieldPath>>>;
+
+ /// Returns statistics of this call's output within one container of rows,
+ /// such as a Parquet row group or page, derived from statistics of the
+ /// fields it reads. Returns `None` if nothing is known.
+ ///
+ /// `inputs.num_rows` is the number of rows in the container, and
+ /// `inputs.column_statistics` holds one entry per path returned by
+ /// [`Self::required_fields`] for the same `args`: the paths of the first
+ /// argument in order, then those of the next argument, and so on. A path
+ /// that does not name a primitive field has unknown statistics.
+ ///
+ /// # Contract
+ ///
+ /// The result must be sound for every row in the container: each non-null
+ /// output value must lie within `min_value` and `max_value`, and
+ /// `null_count` must be exact when marked exact. Readers skip containers
+ /// based on it, so an unsound result silently drops rows.
+ fn output_statistics(
Review Comment:
As I understand it this is needed basically because the output statistics
for `variant_get` are computed from two input columns (leaves): the `value` and
`typed_value`. Pretty much every other function we have so far has statistics
computed from just the input alone.
I think this is ok and can't think of anything better now, but I think it
would be worth splitting
##########
datafusion/datasource-parquet/src/projection_read_plan.rs:
##########
@@ -29,18 +29,18 @@
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
-use arrow::datatypes::{DataType, Schema, SchemaRef};
+use arrow::datatypes::{DataType, FieldRef, Schema, SchemaRef};
use datafusion_functions::core::input_file_name::InputFileNameFunc;
use parquet::arrow::ProjectionMask;
use parquet::schema::types::SchemaDescriptor;
use datafusion_common::Result;
use datafusion_common::nested_struct::requires_nested_struct_cast;
use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion,
TreeNodeVisitor};
+use datafusion_expr::{FieldPath, NestedInputAccess, ReturnFieldArgs};
use datafusion_functions::core::file_row_index::FileRowIndexFunc;
-use datafusion_functions::core::getfield::GetFieldFunc;
Review Comment:
I think (effectively) removing this line is the point of the core point of
this PR -- instead of directly special casing GetFieldFunc this code is now in
terms of the more general API
##########
datafusion/datasource-parquet/src/projection_read_plan.rs:
##########
@@ -2176,4 +2154,176 @@ mod test {
"whole-root reference must select every leaf under the root"
);
}
+
+ /// A function other than `get_field` that reads fixed paths from its first
+ /// (struct) argument and reads its second argument whole.
+ #[derive(Debug, PartialEq, Eq, Hash)]
Review Comment:
I would personally prefer this as an end to end test somehow to show tht the
plumbing works all the way down into the parquet scan: registe the function,
and then use an instrumented object store to show queries using that function
do not fetch the leaf columns
I think that would test the actual usecase and ensure all the APIs are
exposed
##########
datafusion/expr/src/udf.rs:
##########
@@ -57,6 +59,70 @@ pub struct StructFieldMapping {
pub fields: Vec<(Vec<ScalarValue>, usize)>,
}
+/// A path of struct field names from the root of an argument down to a nested
+/// field, for example `["a", "b"]` for `s.a.b`.
+///
+/// Each component is a literal field name, which may itself contain dots. A
+/// path that ends at a struct refers to that struct's whole subtree; a data
+/// source that cannot narrow to a subtree reads the whole argument instead.
+pub type FieldPath = Vec<String>;
+
+/// Capability of scalar functions that read only part of their nested
+/// (struct) arguments.
+///
+/// Data sources use this to decode only the parts of a nested column that a
+/// call actually reads. For example, a Parquet scan evaluating
+/// `get_field(s, 'a')` only needs the leaves under `s.a`, not every leaf of
+/// `s`. The function is still evaluated as usual, on an argument from which
+/// the unread struct fields have been removed.
+///
+/// Obtained through [`ScalarUDFImpl::as_nested_input_access`].
+pub trait NestedInputAccess {
+ /// Returns, for each argument, the struct field paths this call reads, or
+ /// `None` to read that argument whole. The result may be shorter than the
+ /// argument list; missing trailing entries also mean "read whole".
+ ///
+ /// `args` describes the call's arguments against the schema being
+ /// planned for: a table schema during planning, or one file's physical
+ /// schema when that file is opened. The answer may differ between them,
+ /// for example when nested layouts differ between files.
+ ///
+ /// # Contract
+ ///
+ /// Evaluating the call on arguments narrowed to the returned paths must
+ /// produce the same values, return field and errors as evaluating it on
+ /// the full arguments. Narrowing removes every struct field that is not
+ /// on a returned path, and keeps the validity of the struct ancestors of
+ /// the fields that remain. Paths must only traverse struct fields. A path
+ /// that is invalid for the argument type causes the whole argument to be
+ /// read.
+ fn required_fields(&self, args: &ReturnFieldArgs) ->
Vec<Option<Vec<FieldPath>>>;
+
+ /// Returns statistics of this call's output within one container of rows,
+ /// such as a Parquet row group or page, derived from statistics of the
+ /// fields it reads. Returns `None` if nothing is known.
+ ///
+ /// `inputs.num_rows` is the number of rows in the container, and
+ /// `inputs.column_statistics` holds one entry per path returned by
+ /// [`Self::required_fields`] for the same `args`: the paths of the first
+ /// argument in order, then those of the next argument, and so on. A path
+ /// that does not name a primitive field has unknown statistics.
+ ///
+ /// # Contract
+ ///
+ /// The result must be sound for every row in the container: each non-null
+ /// output value must lie within `min_value` and `max_value`, and
+ /// `null_count` must be exact when marked exact. Readers skip containers
+ /// based on it, so an unsound result silently drops rows.
+ fn output_statistics(
Review Comment:
For anyone else following along, getting output statistics is important to
be able to skip fetching a `value` leaf for shredded columns -- see
https://github.com/datafusion-contrib/datafusion-variant/issues/199#issuecomment-7134488412
for the gory details.
##########
datafusion/expr/src/udf.rs:
##########
@@ -57,6 +59,70 @@ pub struct StructFieldMapping {
pub fields: Vec<(Vec<ScalarValue>, usize)>,
}
+/// A path of struct field names from the root of an argument down to a nested
+/// field, for example `["a", "b"]` for `s.a.b`.
+///
+/// Each component is a literal field name, which may itself contain dots. A
+/// path that ends at a struct refers to that struct's whole subtree; a data
+/// source that cannot narrow to a subtree reads the whole argument instead.
+pub type FieldPath = Vec<String>;
Review Comment:
I think it might be better to avoid the need for owned strings here -- so
either return Vec<&str> or maybe Vec<Cow<str>> perhaps
Although in retrospect maybe it would be better to make this an enum/struct
we can evolve and make explicit -- for example
```rust
enum StructAccess {
Whole, // the entire struct
Subfields(Vec<...>), // a single sub path
}
```
That would also allow us to add other access types if needed
##########
datafusion/expr/src/udf.rs:
##########
@@ -57,6 +59,70 @@ pub struct StructFieldMapping {
pub fields: Vec<(Vec<ScalarValue>, usize)>,
}
+/// A path of struct field names from the root of an argument down to a nested
+/// field, for example `["a", "b"]` for `s.a.b`.
+///
+/// Each component is a literal field name, which may itself contain dots. A
+/// path that ends at a struct refers to that struct's whole subtree; a data
+/// source that cannot narrow to a subtree reads the whole argument instead.
+pub type FieldPath = Vec<String>;
+
+/// Capability of scalar functions that read only part of their nested
+/// (struct) arguments.
+///
+/// Data sources use this to decode only the parts of a nested column that a
+/// call actually reads. For example, a Parquet scan evaluating
+/// `get_field(s, 'a')` only needs the leaves under `s.a`, not every leaf of
+/// `s`. The function is still evaluated as usual, on an argument from which
+/// the unread struct fields have been removed.
+///
+/// Obtained through [`ScalarUDFImpl::as_nested_input_access`].
+pub trait NestedInputAccess {
Review Comment:
While this `NestedInputAccess` is a nice way to keep replaced methods
together, it means that all wrappers would need to forward the trait methods on
through
This PR does it for `AliasedScalarUDFImpl` but other wrappers (e.g. stuff
under ffi and user wrappers) may not.
I think it would be cleaner just to add new default methods (that return
None/empty vec) to `ScalarUdfImpl` would be better.
##########
datafusion/datasource-parquet/src/projection_read_plan.rs:
##########
@@ -2176,4 +2154,176 @@ mod test {
"whole-root reference must select every leaf under the root"
);
}
+
+ /// A function other than `get_field` that reads fixed paths from its first
+ /// (struct) argument and reads its second argument whole.
+ #[derive(Debug, PartialEq, Eq, Hash)]
Review Comment:
Also, I think we should test the case where the shredding / results are
different across files (.e.g one file has a path shredded and one does not)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]