You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "duongcongtoai (via GitHub)" <gi...@apache.org> on 2024/07/20 19:54:11 UTC
[PR] Fix query referencing the same unnest expr in expr tree [datafusion]
duongcongtoai opened a new pull request, #11577:
URL: https://github.com/apache/datafusion/pull/11577
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #11198 .
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#issuecomment-2373691899
I think we can handle the follow ons / comments above as a follow on PR. Let's merge this one to keep things flowing.
@duongcongtoai let me know if you plan to work on the suggestions.
Thanks again. 🚀 onwards
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1774864131
##########
datafusion/sql/src/utils.rs:
##########
@@ -309,6 +315,269 @@ pub(crate) fn transform_bottom_unnests(
.collect::<Vec<_>>())
}
+/*
+This is only usedful when used with transform down up
+A full example of how the transformation works:
+ */
+struct RecursiveUnnestRewriter<'a> {
+ input_schema: &'a DFSchemaRef,
+ root_expr: &'a Expr,
+ // useful to detect which child expr is a part of/ not a part of unnest operation
+ top_most_unnest: Option<Unnest>,
+ consecutive_unnest: Vec<Option<Unnest>>,
+ inner_projection_exprs: &'a mut Vec<Expr>,
+ columns_unnestings: &'a mut Vec<(Column, ColumnUnnestType)>,
+ transformed_root_exprs: Option<Vec<Expr>>,
+}
+impl<'a> RecursiveUnnestRewriter<'a> {
+ /// This struct stores the history of expr
+ /// during its tree-traversal with a notation of
+ /// \[None,**Unnest(exprA)**,**Unnest(exprB)**,None,None\]
+ /// then this function will returns \[**Unnest(exprA)**,**Unnest(exprB)**\]
+ ///
+ /// The first item will be the inner most expr
+ fn get_latest_consecutive_unnest(&self) -> Vec<Unnest> {
+ self.consecutive_unnest
+ .iter()
+ .rev()
+ .skip_while(|item| item.is_none())
+ .take_while(|item| item.is_some())
+ .to_owned()
+ .cloned()
+ .map(|item| item.unwrap())
+ .collect()
+ }
+
+ fn transform(
+ &mut self,
+ level: usize,
+ alias_name: String,
+ expr_in_unnest: &Expr,
+ struct_allowed: bool,
+ ) -> Result<Vec<Expr>> {
+ let inner_expr_name = expr_in_unnest.schema_name().to_string();
+
+ // Full context, we are trying to plan the execution as InnerProjection->Unnest->OuterProjection
+ // inside unnest execution, each column inside the inner projection
+ // will be transformed into new columns. Thus we need to keep track of these placeholding column names
+ // let placeholder_name = unnest_expr.display_name()?;
+ let placeholder_name = format!("unnest_placeholder({})", inner_expr_name);
+ let post_unnest_name =
+ format!("unnest_placeholder({},depth={})", inner_expr_name, level);
+ // This is due to the fact that unnest transformation should keep the original
+ // column name as is, to comply with group by and order by
+ // let post_unnest_alias = print_unnest(&inner_expr_name, level);
+ let placeholder_column = Column::from_name(placeholder_name.clone());
+
+ let (data_type, _) = expr_in_unnest.data_type_and_nullable(self.input_schema)?;
+
+ match data_type {
+ DataType::Struct(inner_fields) => {
+ if !struct_allowed {
+ return internal_err!("unnest on struct can only be applied at the root level of select expression");
+ }
+ push_projection_dedupl(
+ self.inner_projection_exprs,
+ expr_in_unnest.clone().alias(placeholder_name.clone()),
+ );
+ self.columns_unnestings.push((
+ Column::from_name(placeholder_name.clone()),
+ ColumnUnnestType::Struct,
+ ));
+ Ok(
+ get_struct_unnested_columns(&placeholder_name, &inner_fields)
+ .into_iter()
+ .map(Expr::Column)
+ .collect(),
+ )
+ }
+ DataType::List(_)
+ | DataType::FixedSizeList(_, _)
+ | DataType::LargeList(_) => {
+ push_projection_dedupl(
+ self.inner_projection_exprs,
+ expr_in_unnest.clone().alias(placeholder_name.clone()),
+ );
+
+ // let post_unnest_column = Column::from_name(post_unnest_name);
Review Comment:
seems like leftover
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #11577:
URL: https://github.com/apache/datafusion/pull/11577
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1774868527
##########
datafusion/sql/src/utils.rs:
##########
@@ -309,6 +315,269 @@ pub(crate) fn transform_bottom_unnests(
.collect::<Vec<_>>())
}
+/*
+This is only usedful when used with transform down up
+A full example of how the transformation works:
+ */
+struct RecursiveUnnestRewriter<'a> {
+ input_schema: &'a DFSchemaRef,
+ root_expr: &'a Expr,
+ // useful to detect which child expr is a part of/ not a part of unnest operation
+ top_most_unnest: Option<Unnest>,
+ consecutive_unnest: Vec<Option<Unnest>>,
+ inner_projection_exprs: &'a mut Vec<Expr>,
+ columns_unnestings: &'a mut Vec<(Column, ColumnUnnestType)>,
+ transformed_root_exprs: Option<Vec<Expr>>,
+}
+impl<'a> RecursiveUnnestRewriter<'a> {
+ /// This struct stores the history of expr
+ /// during its tree-traversal with a notation of
+ /// \[None,**Unnest(exprA)**,**Unnest(exprB)**,None,None\]
+ /// then this function will returns \[**Unnest(exprA)**,**Unnest(exprB)**\]
+ ///
+ /// The first item will be the inner most expr
+ fn get_latest_consecutive_unnest(&self) -> Vec<Unnest> {
+ self.consecutive_unnest
+ .iter()
+ .rev()
+ .skip_while(|item| item.is_none())
+ .take_while(|item| item.is_some())
+ .to_owned()
+ .cloned()
+ .map(|item| item.unwrap())
+ .collect()
+ }
+
+ fn transform(
+ &mut self,
+ level: usize,
+ alias_name: String,
+ expr_in_unnest: &Expr,
+ struct_allowed: bool,
+ ) -> Result<Vec<Expr>> {
+ let inner_expr_name = expr_in_unnest.schema_name().to_string();
+
+ // Full context, we are trying to plan the execution as InnerProjection->Unnest->OuterProjection
+ // inside unnest execution, each column inside the inner projection
+ // will be transformed into new columns. Thus we need to keep track of these placeholding column names
+ // let placeholder_name = unnest_expr.display_name()?;
Review Comment:
seems like leftover
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "duongcongtoai (via GitHub)" <gi...@apache.org>.
duongcongtoai commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1749176137
##########
datafusion/sqllogictest/test_files/unnest.slt:
##########
@@ -165,6 +172,25 @@ select unnest(column1), column1 from unnest_table;
6 [6]
12 [12]
+
+query II
+select unnest([1,2,3]) + unnest([1,2,3]), unnest([1,2,3]) + unnest([4,5]);
Review Comment:
Fix https://github.com/apache/datafusion/issues/11689
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix query referencing the same unnest expr in expr tree [datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1720870065
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -2921,17 +2922,53 @@ pub enum Partitioning {
DistributeBy(Vec<Expr>),
}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum ColumnUnnestType {
+ List(Vec<ColumnUnnestList>),
+ // for struct, there can only be one unnest performed on one column at a time
+ Struct,
+ // Infer the unnest type based on column schema
+ // If column is a list column, the unnest depth will be 1
+ // This value is to support sugar syntax of old api (unnest(columns1,columns2))
Review Comment:
I think we didn't support multi expression in unnest (?
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "duongcongtoai (via GitHub)" <gi...@apache.org>.
duongcongtoai commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1770524828
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -3049,17 +3051,54 @@ pub enum Partitioning {
DistributeBy(Vec<Expr>),
}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum ColumnUnnestType {
+ List(Vec<ColumnUnnestList>),
+ // for struct, there can only be one unnest performed on one column at a time
+ Struct,
+ // Infer the unnest type based on column schema
+ // If column is a list column, the unnest depth will be 1
+ // This value is to support sugar syntax of old api (unnest(columns1,columns2))
+ Inferred,
+}
+
+impl fmt::Display for ColumnUnnestType {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ ColumnUnnestType::List(lists) => {
+ let list_strs: Vec<String> =
+ lists.iter().map(|list| list.to_string()).collect();
+ write!(f, "List([{}])", list_strs.join(", "))
+ }
+ ColumnUnnestType::Struct => write!(f, "Struct"),
+ ColumnUnnestType::Inferred => write!(f, "Inferred"),
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Review Comment:
Thank you, comment addressed
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -3049,17 +3051,54 @@ pub enum Partitioning {
DistributeBy(Vec<Expr>),
}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum ColumnUnnestType {
+ List(Vec<ColumnUnnestList>),
Review Comment:
comment addressed
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "toaiduongdh (via GitHub)" <gi...@apache.org>.
toaiduongdh commented on PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#issuecomment-2373831576
Yes thank you for the reviews, i'll create a follow up Pr
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1771937668
##########
datafusion/expr/src/logical_plan/builder.rs:
##########
@@ -1184,6 +1186,26 @@ impl LogicalPlanBuilder {
self,
columns: Vec<Column>,
options: UnnestOptions,
+ ) -> Result<Self> {
+ unnest_with_options(
+ Arc::unwrap_or_clone(self.plan),
+ columns
+ .into_iter()
+ .map(|c| (c, ColumnUnnestType::Inferred))
+ .collect(),
+ options,
+ )
+ .map(Self::new)
+ }
+
+ /// Unnest the given columns with the given [`UnnestOptions`]
+ /// if one column is a list type, it can be recursively and simultaneously
+ /// unnested into the desired recursion levels
+ /// e.g select unnest(list_col,depth=1), unnest(list_col,depth=2)
+ pub fn unnest_columns_recursive_with_options(
Review Comment:
An alternate API would be to add the `ColumnUnnestType` as a field on `UnnestOptions`
(for that matter, maybe we should just add `columns: Vec<(Column, ColumnUnnestType)>,` as a field 🤔
##########
datafusion/expr/src/logical_plan/builder.rs:
##########
@@ -1561,7 +1583,61 @@ impl TableSource for LogicalTableSource {
/// Create a [`LogicalPlan::Unnest`] plan
pub fn unnest(input: LogicalPlan, columns: Vec<Column>) -> Result<LogicalPlan> {
- unnest_with_options(input, columns, UnnestOptions::default())
+ let unnestings = columns
+ .into_iter()
+ .map(|c| (c, ColumnUnnestType::Inferred))
+ .collect();
+ unnest_with_options(input, unnestings, UnnestOptions::default())
+}
+
+pub fn get_unnested_list_datatype_recursive(
Review Comment:
If this needs to be `pub` i think it should have some doc strings
##########
datafusion/sql/src/utils.rs:
##########
@@ -553,17 +901,25 @@ mod tests {
// Only the inner most/ bottom most unnest is transformed
assert_eq!(
transformed_exprs,
- vec![unnest(col("UNNEST(struct_col[matrix])"))]
+ vec![col("unnest_placeholder(struct_col[matrix],depth=2)")
+ .alias("UNNEST(UNNEST(struct_col[matrix]))")]
);
- assert_eq!(
- unnest_placeholder_columns,
- vec!["UNNEST(struct_col[matrix])"]
+ // TODO: add a test case where
Review Comment:
Is this still something you mean to do?
##########
datafusion/expr/src/logical_plan/builder.rs:
##########
@@ -1561,7 +1583,61 @@ impl TableSource for LogicalTableSource {
/// Create a [`LogicalPlan::Unnest`] plan
pub fn unnest(input: LogicalPlan, columns: Vec<Column>) -> Result<LogicalPlan> {
- unnest_with_options(input, columns, UnnestOptions::default())
+ let unnestings = columns
+ .into_iter()
+ .map(|c| (c, ColumnUnnestType::Inferred))
+ .collect();
+ unnest_with_options(input, unnestings, UnnestOptions::default())
+}
+
+pub fn get_unnested_list_datatype_recursive(
+ data_type: &DataType,
+ depth: usize,
+) -> Result<DataType> {
+ match data_type {
+ DataType::List(field)
+ | DataType::FixedSizeList(field, _)
+ | DataType::LargeList(field) => {
+ if depth == 1 {
+ return Ok(field.data_type().clone());
+ }
+ return get_unnested_list_datatype_recursive(field.data_type(), depth - 1);
+ }
+ _ => {}
+ };
+
+ internal_err!("trying to unnest on invalid data type {:?}", data_type)
+}
+
+/// Infer the unnest type based on the data type:
+/// - list type: infer to unnest(list(col, depth=1))
+/// - struct type: infer to unnest(struct)
+fn infer_unnest_type(
+ col_name: &String,
+ data_type: &DataType,
+) -> Result<ColumnUnnestType> {
+ match data_type {
+ DataType::List(_) | DataType::FixedSizeList(_, _) | DataType::LargeList(_) => {
+ Ok(ColumnUnnestType::List(vec![ColumnUnnestList {
+ output_column: Column::from_name(col_name),
+ depth: 1,
+ }]))
+ }
+ DataType::Struct(_) => Ok(ColumnUnnestType::Struct),
+ _ => {
+ internal_err!("trying to unnest on invalid data type {:?}", data_type)
+ }
+ }
+}
+
+pub fn get_struct_unnested_columns(
Review Comment:
likewise here
##########
datafusion/physical-plan/src/unnest.rs:
##########
@@ -765,6 +994,139 @@ mod tests {
Ok(())
}
+ #[test]
+ fn test_build_batch_list_arr_recursive() -> datafusion_common::Result<()> {
+ // col1 | col2
+ // [[1,2,3],null,[4,5]] | ['a','b']
+ // [[7,8,9,10], null, [11,12,13]] | ['c','d']
+ // null | ['e']
+ let list_arr1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
+ Some(vec![Some(1), Some(2), Some(3)]),
+ None,
+ Some(vec![Some(4), Some(5)]),
+ Some(vec![Some(7), Some(8), Some(9), Some(10)]),
+ None,
+ Some(vec![Some(11), Some(12), Some(13)]),
+ ]);
+
+ let list_arr1_ref = Arc::new(list_arr1) as ArrayRef;
+ let offsets = OffsetBuffer::from_lengths([3, 3, 0]);
+ let mut nulls = BooleanBufferBuilder::new(3);
+ nulls.append(true);
+ nulls.append(true);
+ nulls.append(false);
+ // list<list<int32>>
+ let col1_field = Field::new_list_field(
+ DataType::List(Arc::new(Field::new_list_field(
+ list_arr1_ref.data_type().to_owned(),
+ true,
+ ))),
+ true,
+ );
+ let col1 = ListArray::new(
+ Arc::new(Field::new_list_field(
+ list_arr1_ref.data_type().to_owned(),
+ true,
+ )),
+ offsets,
+ list_arr1_ref,
+ Some(NullBuffer::new(nulls.finish())),
+ );
+
+ let list_arr2 = StringArray::from(vec![
+ Some("a"),
+ Some("b"),
+ Some("c"),
+ Some("d"),
+ Some("e"),
+ ]);
+
+ let offsets = OffsetBuffer::from_lengths([2, 2, 1]);
+ let mut nulls = BooleanBufferBuilder::new(3);
+ nulls.append_n(3, true);
+ let col2_field = Field::new(
+ "col2",
+ DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true))),
+ true,
+ );
+ let col2 = GenericListArray::<i32>::new(
+ Arc::new(Field::new_list_field(DataType::Utf8, true)),
+ OffsetBuffer::new(offsets.into()),
+ Arc::new(list_arr2),
+ Some(NullBuffer::new(nulls.finish())),
+ );
+ // convert col1 and col2 to a record batch
+ let schema = Arc::new(Schema::new(vec![col1_field, col2_field]));
+ let out_schema = Arc::new(Schema::new(vec![
+ Field::new(
+ "col1_unnest_placeholder_depth_1",
+ DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
+ true,
+ ),
+ Field::new("col1_unnest_placeholder_depth_2", DataType::Int32, true),
+ Field::new("col2_unnest_placeholder_depth_1", DataType::Utf8, true),
+ ]));
+ let batch = RecordBatch::try_new(
+ Arc::clone(&schema),
+ vec![Arc::new(col1) as ArrayRef, Arc::new(col2) as ArrayRef],
+ )
+ .unwrap();
+ let list_type_columns = vec![
+ ListUnnest {
+ index_in_input_schema: 0,
+ depth: 1,
+ },
+ ListUnnest {
+ index_in_input_schema: 0,
+ depth: 2,
+ },
+ ListUnnest {
+ index_in_input_schema: 1,
+ depth: 1,
+ },
+ ];
+ let ret = build_batch(
+ &batch,
+ &out_schema,
+ list_type_columns.as_ref(),
+ &HashSet::default(),
+ &UnnestOptions {
+ preserve_nulls: true,
+ },
+ )?;
+ let actual =
Review Comment:
for the future, you an use `assert_batches_eq` for this kind of comparison
##########
datafusion/sqllogictest/test_files/unnest.slt:
##########
@@ -165,6 +165,34 @@ select unnest(column1), column1 from unnest_table;
6 [6]
12 [12]
Review Comment:
Are there any `.slt` tests for unnesting structures? these tests seem to be on arrays (Lists) rather than Structs
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "duongcongtoai (via GitHub)" <gi...@apache.org>.
duongcongtoai commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1720902957
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -2921,17 +2922,53 @@ pub enum Partitioning {
DistributeBy(Vec<Expr>),
}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum ColumnUnnestType {
+ List(Vec<ColumnUnnestList>),
+ // for struct, there can only be one unnest performed on one column at a time
+ Struct,
+ // Infer the unnest type based on column schema
+ // If column is a list column, the unnest depth will be 1
+ // This value is to support sugar syntax of old api (unnest(columns1,columns2))
Review Comment:
tks, it is a typo
and my implementation is hard to read right now, so reviewing it can be exausting :pray:
I'm still refactoring/simplifying to make it more readable
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "duongcongtoai (via GitHub)" <gi...@apache.org>.
duongcongtoai commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1759694901
##########
datafusion/sqllogictest/test_files/unnest.slt:
##########
@@ -165,6 +165,34 @@ select unnest(column1), column1 from unnest_table;
6 [6]
12 [12]
+# unnest at different level at the same time
+query II
+select unnest([1,2,3]), unnest(unnest([[1,2,3]]));
Review Comment:
fix https://github.com/apache/datafusion/issues/11689
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "duongcongtoai (via GitHub)" <gi...@apache.org>.
duongcongtoai commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1761654336
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -2921,17 +2922,53 @@ pub enum Partitioning {
DistributeBy(Vec<Expr>),
}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum ColumnUnnestType {
+ List(Vec<ColumnUnnestList>),
+ // for struct, there can only be one unnest performed on one column at a time
+ Struct,
+ // Infer the unnest type based on column schema
+ // If column is a list column, the unnest depth will be 1
+ // This value is to support sugar syntax of old api (unnest(columns1,columns2))
Review Comment:
i refactor and added more documentation, please help me take a look
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1769535649
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -3049,17 +3051,54 @@ pub enum Partitioning {
DistributeBy(Vec<Expr>),
}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum ColumnUnnestType {
+ List(Vec<ColumnUnnestList>),
+ // for struct, there can only be one unnest performed on one column at a time
+ Struct,
+ // Infer the unnest type based on column schema
+ // If column is a list column, the unnest depth will be 1
+ // This value is to support sugar syntax of old api (unnest(columns1,columns2))
+ Inferred,
+}
+
+impl fmt::Display for ColumnUnnestType {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ ColumnUnnestType::List(lists) => {
+ let list_strs: Vec<String> =
+ lists.iter().map(|list| list.to_string()).collect();
+ write!(f, "List([{}])", list_strs.join(", "))
+ }
+ ColumnUnnestType::Struct => write!(f, "Struct"),
+ ColumnUnnestType::Inferred => write!(f, "Inferred"),
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Review Comment:
Can we please add documentation to this struct about what it means (what computation it represents)? In the context of this PR it is clear, but once we merge I think the context will be lost
##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -3049,17 +3051,54 @@ pub enum Partitioning {
DistributeBy(Vec<Expr>),
}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum ColumnUnnestType {
+ List(Vec<ColumnUnnestList>),
Review Comment:
Can you please add documentation to `List` about what it is (ideally with an example of when it is used and how it is different to `Inferred`?
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Refactor to support recursive unnest in physical plan [datafusion]
Posted by "duongcongtoai (via GitHub)" <gi...@apache.org>.
duongcongtoai commented on code in PR #11577:
URL: https://github.com/apache/datafusion/pull/11577#discussion_r1749176016
##########
datafusion/sqllogictest/test_files/unnest.slt:
##########
@@ -497,10 +523,45 @@ select unnest(column1) from (select * from (values([1,2,3]), ([4,5,6])) limit 1
6
## FIXME: https://github.com/apache/datafusion/issues/11198
-query error DataFusion error: Error during planning: Projections require unique expression names but the expression "UNNEST\(Column\(Column \{ relation: Some\(Bare \{ table: "unnest_table" \}\), name: "column1" \}\)\)" at position 0 and "UNNEST\(Column\(Column \{ relation: Some\(Bare \{ table: "unnest_table" \}\), name: "column1" \}\)\)" at position 1 have the same name. Consider aliasing \("AS"\) one of them.
+query error DataFusion error: Error during planning: Projections require unique expression names but the expression "UNNEST\(unnest_table.column1\)" at position 0 and "UNNEST\(unnest_table.column1\)" at position 1 have the same name. Consider aliasing \("AS"\) one of them.
select unnest(column1), unnest(column1) from unnest_table;
+## the same unnest expr is referened multiple times (unnest is the bottom-most expr)
Review Comment:
Fix: https://github.com/apache/datafusion/issues/11198
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org