[
https://issues.apache.org/jira/browse/SPARK-27784?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16844404#comment-16844404
]
Ryan Blue commented on SPARK-27784:
-----------------------------------
[~cloud_fan], I don't see this happening in master because an additional
Project is added somewhere. Any idea what adds it?
{code:java}
== Parsed Logical Plan ==
Union
:- Project [id#43, data#42]
: +- Join Inner, (id#43 = id#40)
: :- Project [id#43, coalesce(data#44, _) AS data#42]
: : +- SubqueryAlias `default`.`t1`
: : +- Relation[id#43,data#44] parquet
: +- Project [value#37 AS id#40]
: +- LocalRelation [value#37]
+- Project [id#49, coalesce(cast(data#51 as string), _) AS data#42]
+- Project [id#49, null AS data#51]
+- SubqueryAlias `default`.`t2`
+- Relation[id#49] parquet
== Analyzed Logical Plan ==
id: int, data: string
Union
:- Project [id#43, data#42]
<----------------------------------------------------------- same ID
: +- Join Inner, (id#43 = id#40)
: :- Project [id#43, coalesce(data#44, _) AS data#42]
: : +- SubqueryAlias `default`.`t1`
: : +- Relation[id#43,data#44] parquet
: +- Project [value#37 AS id#40]
: +- LocalRelation [value#37]
+- Project [id#49 AS id#81, data#42 AS data#82]
+- Project [id#49, coalesce(cast(data#51 as string), _) AS data#42]
<------------------- same ID
+- Project [id#49, null AS data#51]
+- SubqueryAlias `default`.`t2`
+- Relation[id#49] parquet
== Optimized Logical Plan ==
Union
:- Project [id#43, data#42]
: +- Join Inner, (id#43 = id#40)
: :- Project [id#43, coalesce(data#44, _) AS data#42]
: : +- Filter isnotnull(id#43)
: : +- Relation[id#43,data#44] parquet
: +- Project [value#37 AS id#40]
: +- LocalRelation [value#37]
+- Project [id#49, _ AS data#82]
+- Relation[id#49] parquet
== Physical Plan ==
Union
:- *(2) Project [id#43, data#42]
: +- *(2) BroadcastHashJoin [id#43], [id#40], Inner, BuildRight
: :- *(2) Project [id#43, coalesce(data#44, _) AS data#42]
: : +- *(2) Filter isnotnull(id#43)
: : +- *(2) FileScan parquet default.t1[id#43,data#44] Batched: true,
DataFilters: [isnotnull(id#43)], Format: Parquet, Location:
InMemoryFileIndex[file:/home/blue/workspace/spark/common/kvstore/spark-warehouse/org.apache.spark...,
PartitionFilters: [], PushedFilters: [IsNotNull(id)], ReadSchema:
struct<id:int,data:string>
: +- BroadcastExchange HashedRelationBroadcastMode(List(cast(input[0, int,
false] as bigint)))
: +- *(1) Project [value#37 AS id#40]
: +- LocalTableScan [value#37]
+- *(3) Project [id#49, _ AS data#82] <----------------- reused ID eliminated
by collapsing projections
+- *(3) FileScan parquet default.t2[id#49] Batched: true, DataFilters: [],
Format: Parquet, Location:
InMemoryFileIndex[file:/home/blue/workspace/spark/common/kvstore/spark-warehouse/org.apache.spark...,
PartitionFilters: [], PushedFilters: [], ReadSchema: struct<id:int>
{code}
> Alias ID reuse can break correctness when substituting foldable expressions
> ---------------------------------------------------------------------------
>
> Key: SPARK-27784
> URL: https://issues.apache.org/jira/browse/SPARK-27784
> Project: Spark
> Issue Type: Bug
> Components: SQL
> Affects Versions: 2.1.1, 2.3.2
> Reporter: Ryan Blue
> Priority: Major
> Labels: correctness
>
> This is a correctness bug when reusing a set of project expressions in the
> DataFrame API.
> Use case: a user was migrating a table to a new version with an additional
> column ("data" in the repro case). To migrate the user unions the old table
> ("t2") with the new table ("t1"), and applies a common set of projections to
> ensure the union doesn't hit an issue with ordering (SPARK-22335). In some
> cases, this produces an incorrect query plan:
> {code:java}
> Seq((4, "a"), (5, "b"), (6, "c")).toDF("id", "data").write.saveAsTable("t1")
> Seq(1, 2, 3).toDF("id").write.saveAsTable("t2")
> val dim = Seq(2, 3, 4).toDF("id")
> val outputCols = Seq($"id", coalesce($"data", lit("_")).as("data"))
> val t1 = spark.table("t1").select(outputCols:_*)
> val t2 = spark.table("t2").withColumn("data", lit(null)).select(outputCols:_*)
> t1.join(dim, t1("id") === dim("id")).select(t1("id"),
> t1("data")).union(t2).explain(true){code}
> {code:java}
> == Physical Plan ==
> Union
> :- *Project [id#330, _ AS data#237] <------------------------ THE CONSTANT IS
> FROM THE OTHER SIDE OF THE UNION
> : +- *BroadcastHashJoin [id#330], [id#234], Inner, BuildRight
> : :- *Project [id#330]
> : : +- *Filter isnotnull(id#330)
> : : +- *FileScan parquet t1[id#330] Batched: true, Format: Parquet,
> Location: CatalogFileIndex[s3://.../t1], PartitionFilters: [], PushedFilters:
> [IsNotNull(id)], ReadSchema: struct<id:int>
> : +- BroadcastExchange HashedRelationBroadcastMode(List(cast(input[0, int,
> false] as bigint)))
> : +- LocalTableScan [id#234]
> +- *Project [id#340, _ AS data#237]
> +- *FileScan parquet t2[id#340] Batched: true, Format: Parquet, Location:
> CatalogFileIndex[s3://.../t2], PartitionFilters: [], PushedFilters: [],
> ReadSchema: struct<id:int>{code}
> The problem happens because "outputCols" has an alias. The ID for that alias
> is created when the projection Seq is created, so it is reused in both sides
> of the union.
> When FoldablePropagation runs, it identifies that "data" in the t2 side of
> the union is a foldable expression and replaces all references to it,
> including the references in the t1 side of the union.
> The join to a dimension table is necessary to reproduce the problem because
> it requires a Projection on top of the join that uses an AttributeReference
> for data#237. Otherwise, the projections are collapsed and the projection
> includes an Alias that does not get rewritten by FoldablePropagation.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]