You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "yyy1000 (via GitHub)" <gi...@apache.org> on 2024/05/28 04:36:26 UTC
[PR] Support LogicalPlan::Distinct in unparser [datafusion]
yyy1000 opened a new pull request, #10690:
URL: https://github.com/apache/datafusion/pull/10690
## 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 #10663.
## 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] Support LogicalPlan::Distinct in unparser [datafusion]
Posted by "gruuya (via GitHub)" <gi...@apache.org>.
gruuya commented on code in PR #10690:
URL: https://github.com/apache/datafusion/pull/10690#discussion_r1617161884
##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -271,8 +273,39 @@ impl Unparser<'_> {
relation,
)
}
- LogicalPlan::Distinct(_distinct) => {
- not_impl_err!("Unsupported operator: {plan:?}")
+ LogicalPlan::Distinct(distinct) => {
+ let (select_distinct, input) = match distinct {
+ Distinct::All(input) => (ast::Distinct::Distinct, input.as_ref()),
+ Distinct::On(on) => {
+ let exprs = on
+ .on_expr
+ .iter()
+ .map(|e| self.expr_to_sql(e))
+ .collect::<Result<Vec<_>>>()?;
+ let items = on
+ .select_expr
+ .iter()
+ .map(|e| self.select_item_to_sql(e))
+ .collect::<Result<Vec<_>>>()?;
+ match &on.sort_expr {
+ Some(sort_expr) => {
+ if let Some(query_ref) = query {
+ query_ref
+ .order_by(self.sort_to_sql(sort_expr.clone())?);
+ } else {
+ return internal_err!(
+ "Sort operator only valid in a statement context."
+ );
Review Comment:
The `order_by`/`sort_by` clause is fetched from the `sqlparser::ast::Query` struct while parsing the `DISTINCT ON` query, so it makes sense that it is added there when de-parsing
https://github.com/apache/datafusion/blob/3dc1773403532752db01da5c545b2bdab415e489/datafusion/sql/src/query.rs#L132-L136
Indeed `sqlparser::ast::Select`s `sort_by` is not being used in DataFusion
https://github.com/apache/datafusion/blob/3dc1773403532752db01da5c545b2bdab415e489/datafusion/sql/src/select.rs#L68-L70
so it also seems that `SelectBuilder::sort_by` is not needed atm.
--
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] Support LogicalPlan::Distinct in unparser [datafusion]
Posted by "yyy1000 (via GitHub)" <gi...@apache.org>.
yyy1000 commented on code in PR #10690:
URL: https://github.com/apache/datafusion/pull/10690#discussion_r1617255797
##########
datafusion/sql/tests/sql_integration.rs:
##########
@@ -4631,6 +4631,8 @@ fn roundtrip_statement() -> Result<()> {
"select (id-1)/2, count(*) / (sum(id/10)-1) as agg_expr from (select (id-1) as id from person) group by id",
"select CAST(id/2 as VARCHAR) NOT LIKE 'foo*' from person where NOT EXISTS (select ta.j1_id, tb.j2_string from j1 ta join j2 tb on (ta.j1_id = tb.j2_id))",
r#"select "First Name" from person_quoted_cols"#,
+ "select DISTINCT id FROM person;",
+ "select DISTINCT on (id) id, first_name from person order by id;",
Review Comment:
Added, thanks for your review. :)
--
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] Support LogicalPlan::Distinct in unparser [datafusion]
Posted by "gruuya (via GitHub)" <gi...@apache.org>.
gruuya commented on code in PR #10690:
URL: https://github.com/apache/datafusion/pull/10690#discussion_r1617278601
##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -271,8 +273,39 @@ impl Unparser<'_> {
relation,
)
}
- LogicalPlan::Distinct(_distinct) => {
- not_impl_err!("Unsupported operator: {plan:?}")
+ LogicalPlan::Distinct(distinct) => {
+ let (select_distinct, input) = match distinct {
+ Distinct::All(input) => (ast::Distinct::Distinct, input.as_ref()),
+ Distinct::On(on) => {
+ let exprs = on
+ .on_expr
+ .iter()
+ .map(|e| self.expr_to_sql(e))
+ .collect::<Result<Vec<_>>>()?;
+ let items = on
+ .select_expr
+ .iter()
+ .map(|e| self.select_item_to_sql(e))
+ .collect::<Result<Vec<_>>>()?;
+ match &on.sort_expr {
+ Some(sort_expr) => {
+ if let Some(query_ref) = query {
+ query_ref
+ .order_by(self.sort_to_sql(sort_expr.clone())?);
+ } else {
+ return internal_err!(
+ "Sort operator only valid in a statement context."
+ );
Review Comment:
> Do you think it's better to remove `SelectBuilder::sort_by` or keep it?
If you're asking me I think it's fine to keep it (though other projects wouldn't be using it as well since `SelectBuilder` is `pub(super)`, so it wouldn't hurt to remove it either).
--
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] Support LogicalPlan::Distinct in unparser [datafusion]
Posted by "yyy1000 (via GitHub)" <gi...@apache.org>.
yyy1000 commented on code in PR #10690:
URL: https://github.com/apache/datafusion/pull/10690#discussion_r1616570923
##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -271,8 +273,39 @@ impl Unparser<'_> {
relation,
)
}
- LogicalPlan::Distinct(_distinct) => {
- not_impl_err!("Unsupported operator: {plan:?}")
+ LogicalPlan::Distinct(distinct) => {
+ let (select_distinct, input) = match distinct {
+ Distinct::All(input) => (ast::Distinct::Distinct, input.as_ref()),
+ Distinct::On(on) => {
+ let exprs = on
+ .on_expr
+ .iter()
+ .map(|e| self.expr_to_sql(e))
+ .collect::<Result<Vec<_>>>()?;
+ let items = on
+ .select_expr
+ .iter()
+ .map(|e| self.select_item_to_sql(e))
+ .collect::<Result<Vec<_>>>()?;
+ match &on.sort_expr {
+ Some(sort_expr) => {
+ if let Some(query_ref) = query {
+ query_ref
+ .order_by(self.sort_to_sql(sort_expr.clone())?);
+ } else {
+ return internal_err!(
+ "Sort operator only valid in a statement context."
+ );
Review Comment:
When implementing order by in a distinct on.
I found the order by should be added to `query` instead of `select`.
So my question is, what's the usage of `sort_by` in `SelectBuilder`? I searched the code but not found an usage. 🤔
--
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] Support LogicalPlan::Distinct in unparser [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #10690:
URL: https://github.com/apache/datafusion/pull/10690#discussion_r1617096612
##########
datafusion/sql/tests/sql_integration.rs:
##########
@@ -4631,6 +4631,8 @@ fn roundtrip_statement() -> Result<()> {
"select (id-1)/2, count(*) / (sum(id/10)-1) as agg_expr from (select (id-1) as id from person) group by id",
"select CAST(id/2 as VARCHAR) NOT LIKE 'foo*' from person where NOT EXISTS (select ta.j1_id, tb.j2_string from j1 ta join j2 tb on (ta.j1_id = tb.j2_id))",
r#"select "First Name" from person_quoted_cols"#,
+ "select DISTINCT id FROM person;",
+ "select DISTINCT on (id) id, first_name from person order by id;",
Review Comment:
Can you also please add a test like this (without an order by)?
```sql
select DISTINCT on (id) id, first_name from person;
```
##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -271,8 +273,39 @@ impl Unparser<'_> {
relation,
)
}
- LogicalPlan::Distinct(_distinct) => {
- not_impl_err!("Unsupported operator: {plan:?}")
+ LogicalPlan::Distinct(distinct) => {
+ let (select_distinct, input) = match distinct {
+ Distinct::All(input) => (ast::Distinct::Distinct, input.as_ref()),
+ Distinct::On(on) => {
+ let exprs = on
+ .on_expr
+ .iter()
+ .map(|e| self.expr_to_sql(e))
+ .collect::<Result<Vec<_>>>()?;
+ let items = on
+ .select_expr
+ .iter()
+ .map(|e| self.select_item_to_sql(e))
+ .collect::<Result<Vec<_>>>()?;
+ match &on.sort_expr {
+ Some(sort_expr) => {
+ if let Some(query_ref) = query {
+ query_ref
+ .order_by(self.sort_to_sql(sort_expr.clone())?);
+ } else {
+ return internal_err!(
+ "Sort operator only valid in a statement context."
+ );
Review Comment:
I think it is to support the postgres style `DISTINCT ON`, which may not have all the columns of the select list
Perhaps @gruuya knows more
--
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] Support LogicalPlan::Distinct in unparser [datafusion]
Posted by "yyy1000 (via GitHub)" <gi...@apache.org>.
yyy1000 commented on code in PR #10690:
URL: https://github.com/apache/datafusion/pull/10690#discussion_r1617253512
##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -271,8 +273,39 @@ impl Unparser<'_> {
relation,
)
}
- LogicalPlan::Distinct(_distinct) => {
- not_impl_err!("Unsupported operator: {plan:?}")
+ LogicalPlan::Distinct(distinct) => {
+ let (select_distinct, input) = match distinct {
+ Distinct::All(input) => (ast::Distinct::Distinct, input.as_ref()),
+ Distinct::On(on) => {
+ let exprs = on
+ .on_expr
+ .iter()
+ .map(|e| self.expr_to_sql(e))
+ .collect::<Result<Vec<_>>>()?;
+ let items = on
+ .select_expr
+ .iter()
+ .map(|e| self.select_item_to_sql(e))
+ .collect::<Result<Vec<_>>>()?;
+ match &on.sort_expr {
+ Some(sort_expr) => {
+ if let Some(query_ref) = query {
+ query_ref
+ .order_by(self.sort_to_sql(sort_expr.clone())?);
+ } else {
+ return internal_err!(
+ "Sort operator only valid in a statement context."
+ );
Review Comment:
Cool. Yeah, by running test I found sort_expr will be added to distinct_on plan, but it will not be added to sort.🤔
Do you think it's better to remove `SelectBuilder::sort_by` or keep it?
--
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] Support LogicalPlan::Distinct in unparser [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #10690:
URL: https://github.com/apache/datafusion/pull/10690
--
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