You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/01/23 14:04:53 UTC

[GitHub] [spark] chasingegg opened a new pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

chasingegg opened a new pull request #35290:
URL: https://github.com/apache/spark/pull/35290


   This is the backport PR replated to #35168.
   
   ### What changes were proposed in this pull request?
   
   When the first child of Union has duplicate columns like select a, a from t1 union select a, b from t2, spark only use the first column to aggregate the results, which would make the results incorrect, and this behavior is inconsistent with other engines like PostgreSQL, MySQL. **We could alias the attribute of the first child of union to resolve this, or you could argue that this is the feature of Spark SQL**.
   
   sample query:
   select
   a,
   a
   from values (1, 1), (1, 2) as t1(a, b)
   UNION ALL
   SELECT
   c,
   d
   from values (2, 3), (2, 3) as t2(c, d)
   
   result is   (1, 1), (1, 1), (3, 3), (3, 3) 
   expected (1, 1), (1, 1), (2, 3), (2, 3)
   
   ---
   
   select
   a,
   a
   from values (1, 1), (1, 2) as t1(a, b)
   UNION
   SELECT
   c,
   d
   from values (2, 3), (2, 3) as t2(c, d)
   
   result is  (1, 1), (2, 2)
   expected (1, 1), (2, 3)
   
   
   ### Why are the changes needed?
   
   It is possibly a bug.
   
   
   ### Does this PR introduce _any_ user-facing change?
    
   Yes. When we union with the first child has duplicate columns, the result would be different.
   
   
   ### How was this patch tested?
   
   Add new UT.
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1019849857


   Is it a breaking change? Seems it will break `val df = ...; df.select("a", "a").select(df("a"));`
   
   I'd like to have a surgical fix for this bug. Ideally, when a plan has conflicting output attr ids, the columns with the same attr id should output the same value, so it doesn't matter which column you bind to at the end. However, `Union` breaks this assumption. We don't want to fix `Union` in old branches as it's a breaking change, and we should fix the attribute binding logic for `Union` to minimize the impact.
   
   My proposal is:
   1. For SQL API, the plan is `Distinct(Union(A, B))`. In the rule `ReplaceDistinctWithAggregate`, we replace `Distinct` with a group-only `Aggregate`, and take all the output attrs of `Union` as the grouping columns. We can add a special metadata property to `AttributeReference`, to indicate the actual column it wants to bind. e.g. `__index_of_attributes_with_same_id=1` means it should bind to the second column that has this attribute id, and we update `BindReference` accordingly to implement this special binding logic.
   2. For dataframe API (`df.distinct`), the plan is `Deduplicate(attrs, Union(A, B))`. We can use the same idea to add special metadata property in `df.distinct`.
   3. For `PushProjectionThroughUnion`, we skip the optimization if Union has conflicting attr ids in its output.
   
    


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] LantaoJin commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
LantaoJin commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1019678393


   cc @cloud-fan @wangyum 


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] LantaoJin commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
LantaoJin commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1019645300


   Jenkins, ok to test


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] chasingegg commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
chasingegg commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1035997195


   > > My point is when you do select a from (select a, a union select a, b), it will not fail but produce unexpected result.
   > 
   > Are you sure this won't fail? I thought the output column names of Union are the same with its first child, so `select a` should fail because the name is ambiguous.
   
   It won't fail. Spark will check if the name could be mapped to multiple attr ids, if not(like our cases: a#1, a#1), it won't fail because Spark thinks that two columns are totally the same, not ambiguous.  It will fail in `a#1, a#2` case, which is what we expect here. Therefore, I will prefer to carefully add alias when two children of union are conflict. I will find some time to implement it and update the PR in master branch.


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1036198728


   > Spark will check if the name could be mapped to multiple attr ids, if not(like our cases: a#1, a#1), it won't fail because Spark thinks that two columns are totally the same, not ambiguous.
   
   I see. But this is hackable as well. We can make it fail if we see 2 attributes with the same id but have the special metadata property I mentioned before.


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1034948641


   `select a from (select a, a union a, b)` should fail. My proposal is to hack attribute binding, not column resolution, so that `select a, a union a, b` can return correct result.


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1035914450


   > My point is when you do select a from (select a, a union select a, b), it will not fail but produce unexpected result.
   
   Are you sure this won't fail? I thought the output column names of Union are the same with its first child, so `select a` should fail because the name is ambiguous.


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] chasingegg commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
chasingegg commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1034961869


   But `select a, a union select a, b` would still produce two same attr ids (like a#1, a#1) with your approach? Then `select a from (select a, a union select a, b)` will not fail. 


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] chasingegg commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
chasingegg commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1035901626


   > > would still produce two same attr ids (like a#1, a#1) with your approach?
   > 
   > It's OK if we can bind it correctly. I can find some time to implement it if you are not familiar with it.
   
   My point is when you do `select a from (select a, a union select a, b)`, it will not fail but produce unexpected result. Of course you could also do some tricks to make `Project` know the two attr ids(like a#1, a#2) are actually different, but I don't think it is a good way to do 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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1035836139


   > would still produce two same attr ids (like a#1, a#1) with your approach? 
   
   It's OK if we can bind it correctly. I can find some time to implement it if you are not familiar with 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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] chasingegg edited a comment on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
chasingegg edited a comment on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1024823661


   > Is it a breaking change? Seems it will break `val df = ...; df.select("a", "a").select(df("a"));`
   > 
   > I'd like to have a surgical fix for this bug. Ideally, when a plan has conflicting output attr ids, the columns with the same attr id should output the same value, so it doesn't matter which column you bind to at the end. However, `Union` breaks this assumption. We don't want to fix `Union` in old branches as it's a breaking change, and we should fix the attribute binding logic for `Union` to minimize the impact.
   > 
   > My proposal is:
   > 
   > 1. For SQL API, the plan is `Distinct(Union(A, B))`. In the rule `ReplaceDistinctWithAggregate`, we replace `Distinct` with a group-only `Aggregate`, and take all the output attrs of `Union` as the grouping columns. We can add a special metadata property to `AttributeReference`, to indicate the actual column it wants to bind. e.g. `__index_of_attributes_with_same_id=1` means it should bind to the second column that has this attribute id, and we update `BindReference` accordingly to implement this special binding logic.
   > 2. For dataframe API (`df.distinct`), the plan is `Deduplicate(attrs, Union(A, B))`. We can use the same idea to add special metadata property in `df.distinct`.
   > 3. For `PushProjectionThroughUnion`, we skip the optimization if Union has conflicting attr ids in its output.
   
   @cloud-fan  Sorry for the late reply. Thanks for your proposal! However I don't think it is a good way to fix it like that. First of all, I think we should clear that what the result of union all/union should be. I still use some examples with duplicate columns of first union's child. There are two cases. 
   We have a `conflicting example`.
   sample query:
   select
   a,
   a
   from values (1, 1), (1, 2) as t1(a, b)
   UNION ALL
   SELECT
   c,
   d
   from values (2, 3), (2, 3) as t2(c, d)
   
   result is (1, 1), (1, 1), (3, 3), (3, 3)
   expected (1, 1), (1, 1), (2, 3), (2, 3)
   
   And we have a `non-conflicting` example like below
   sample query:
   select
   a,
   a
   from values (1, 1), (1, 2) as t1(a, b)
   UNION ALL
   SELECT
   c,
   c
   from values (2, 3), (2, 3) as t2(c, d)
   
   result is (1, 1), (1, 1), (2, 2), (2, 2)
   
   ---
   As you said, Union would break the assumption that the columns with the same attr id should output the same value. So I think it should alias as two different attr ids since their values could be different. Let's take `conflicting example` as the subquery, and we do **select a from subquery**, it is an expected behavior that the query would be broken since there are two columns with the same name but their values are different. 
   But whether the `non-conflicting example` should also be broken, that is the question. If we alias the output of union, it would complain about both two cases. Or we could carefully alias only when it is the `conflicting example`.
   
   P.S. I tested on PostgreSQL, it would complain about both two cases as it is `ambiguous`, so I think it alias the outputs of Union. 
   
   
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1032705677


   OK we have two choices here:
   1. Only fix it in the master branch (Spark 3.3), then your approach will be acceptable.
   2. Use my approach to fix 3.0/3.1/3.2, and fix the Union problem completely in the master branch.
   
   Which one do you prefer? Or do you have a better idea to fix this bug without any BREAKING change?


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] chasingegg commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
chasingegg commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1034924638


   I am still not very clear about your approach, maybe I misunderstand it. So if we have select a, a union a, b. And assume we get `1, 1 union 1, 2`, the correct result is  `(1, 1), (1, 2)`. As your approach goes, if we `select a from (select a, a union a, b)`, it will produce `1, 1`, am I right? But I think it is proper to make such sql broken because it is ambiguous, `1, 1` or `1, 2` are neither correct. If my understanding is right, I would prefer to fix it only in the master with my approach.


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] chasingegg commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
chasingegg commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1024823661


   > Is it a breaking change? Seems it will break `val df = ...; df.select("a", "a").select(df("a"));`
   > 
   > I'd like to have a surgical fix for this bug. Ideally, when a plan has conflicting output attr ids, the columns with the same attr id should output the same value, so it doesn't matter which column you bind to at the end. However, `Union` breaks this assumption. We don't want to fix `Union` in old branches as it's a breaking change, and we should fix the attribute binding logic for `Union` to minimize the impact.
   > 
   > My proposal is:
   > 
   > 1. For SQL API, the plan is `Distinct(Union(A, B))`. In the rule `ReplaceDistinctWithAggregate`, we replace `Distinct` with a group-only `Aggregate`, and take all the output attrs of `Union` as the grouping columns. We can add a special metadata property to `AttributeReference`, to indicate the actual column it wants to bind. e.g. `__index_of_attributes_with_same_id=1` means it should bind to the second column that has this attribute id, and we update `BindReference` accordingly to implement this special binding logic.
   > 2. For dataframe API (`df.distinct`), the plan is `Deduplicate(attrs, Union(A, B))`. We can use the same idea to add special metadata property in `df.distinct`.
   > 3. For `PushProjectionThroughUnion`, we skip the optimization if Union has conflicting attr ids in its output.
   
   @cloud-fan  Sorry for the late reply. Thans for your proposal! However I don't think it is a good way to fix it like that. First of all, I think we should clear that what the result of union all/union should be. I still use some examples with duplicate columns of first union's child. There are two cases. 
   We have a `conflicting example`.
   sample query:
   select
   a,
   a
   from values (1, 1), (1, 2) as t1(a, b)
   UNION ALL
   SELECT
   c,
   d
   from values (2, 3), (2, 3) as t2(c, d)
   
   result is (1, 1), (1, 1), (3, 3), (3, 3)
   expected (1, 1), (1, 1), (2, 3), (2, 3)
   
   And we have a `non-conflicting` example like below
   sample query:
   select
   a,
   a
   from values (1, 1), (1, 2) as t1(a, b)
   UNION ALL
   SELECT
   c,
   c
   from values (2, 3), (2, 3) as t2(c, d)
   
   result is (1, 1), (1, 1), (2, 2), (2, 2)
   
   ---
   As you said, Union would break the assumption that the columns with the same attr id should output the same value. So I think it should alias as two different attr ids since their values could be different. Let's take `conflicting example` as the subquery, and we do **select a from subquery**, it is an expected behavior that the query would be broken since there are two columns with the same name but their values are different. 
   But whether the `non-conflicting example` should also be broken, that is the question. If we alias the output of union, it would complain about both two cases. Or we could carefully alias only when it is the `conflicting example`.
   
   P.S. I tested on PostgreSQL, it would complain about both two cases as it is `ambiguous`, so I think it alias the outputs of Union. 
   
   
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] dongjoon-hyun commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
dongjoon-hyun commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1086798303


   Hi, @chasingegg . Thank you for making a PR. However, Apache Spark 3.0.0 was released on June 18, 2020. According to [Apache Spark Versioning Policy](https://spark.apache.org/versioning-policy.html), `branch-3.0` is no longer considered maintained because it passed 18 months already. No more 3.0.x releases should be expected, even for bug fixes.


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #35290: [SPARK-37865][SQL][3.0]Fix union bug when the first child of union has duplicate columns

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #35290:
URL: https://github.com/apache/spark/pull/35290#issuecomment-1019657166


   Can one of the admins verify this patch?


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org