Peter Toth created SPARK-62391:
----------------------------------
Summary: InlineCTE is not idempotent when it inlines a CTE whose
body holds a materialized CTE
Key: SPARK-62391
URL: https://issues.apache.org/jira/browse/SPARK-62391
Project: Spark
Issue Type: Bug
Components: SQL
Affects Versions: 4.4.0
Reporter: Peter Toth
When {{InlineCTE}} inlines a CTE at more than one reference, and that CTE's
body holds a {{MATERIALIZED}} CTE that is kept, the copies of the kept CTE keep
its CTE ID but get new attributes. A second application of {{InlineCTE}} then
rebuilds every copy from the definition first registered for that ID. So the
second copy gets the first copy's attributes, while its reference keeps its own.
To reproduce, in a {{SharedSparkSession}} test with
{{spark.range(20).createOrReplaceTempView("small")}}:
{code:sql}
with a as (select id, (with n as materialized (select id, id * 2 v, id * 3 w
from small)
select max(v) from n where id > 1) m from small)
select * from a a1 join a a2 on a1.id = a2.id
{code}
It fails with "Once strategy's idempotence is broken for batch Inline CTE".
Applying {{InlineCTE()}} twice to the analyzed plan shows the change:
{noformat}
once: WithCTE [def 0, out 39, 40, 41] WithCTE [def 0, out 48, 49, 50]
twice: WithCTE [def 0, out 39, 40, 41] WithCTE [def 0, out 39, 40, 41]
{noformat}
The references keep 42, 43, 44 and 51, 52, 53 after both.
>From reading {{InlineCTE}}:
* {{inlineReference}} copies the CTE body with
{{DeduplicateRelations.deduplicateRight}}. This renews the attributes, but not
the IDs of the CTE definitions inside the body.
* {{buildCTEMap}} skips a {{WithCTE}} whose definitions are all registered
already, as a duplicate that must be self-contained (SPARK-50722).
* {{inlineCTE}} rebuilds each {{WithCTE}} from {{refInfo.cteDef}}, the
definition first registered for the ID.
Skipping a duplicate is right for the {{WithCTE}} copies of a view or a
DataFrame used twice, since those are identical. It isn't right for the copies
{{InlineCTE}} makes itself, since their attributes differ.
Outside the test harness {{InlineCTE}} runs once. With
{{PushdownPredicatesAndPruneColumnsForCTEDef}} excluded, the query above then
returns the right rows. With it, master fails at execution, which SPARK-61957
fixes. So I found no wrong result from {{InlineCTE}} itself. But such shapes
cannot be SQL tests, and SPARK-61957 needed them.
The repro needs {{MATERIALIZED}} (SPARK-59372, 4.4.0). Without it, an inner CTE
is kept only when it is non-deterministic, which makes the outer CTE
non-deterministic too, so the outer one is not inlined.
A possible fix: give the CTE definitions inside a copied body new IDs, and
remap the references to them inside the copy. The copies are then separate
CTEs, for {{InlineCTE}} and for later rules such as
{{PushdownPredicatesAndPruneColumnsForCTEDef}}.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]