This is an automated email from the ASF dual-hosted git repository.
prashantwason pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hudi.git
The following commit(s) were added to refs/heads/master by this push:
new 60f77e9cc9c4 fix(spark): rewrite time travel inside CTE definitions
(#20414)
60f77e9cc9c4 is described below
commit 60f77e9cc9c4f8644bb2659b5620eef7533e37f7
Author: Prashant Wason <[email protected]>
AuthorDate: Wed Nov 4 10:34:21 2026 -0800
fix(spark): rewrite time travel inside CTE definitions (#20414)
* fix(spark): rewrite time travel inside CTE definitions
`TIMESTAMP AS OF '<instant>'` with a Hudi instant (yyyyMMddHHmmssSSS)
fails inside a WITH clause with INVALID_TIME_TRAVEL_TIMESTAMP_EXPR,
while the same query works at top level and in a derived table.
HoodieCommonSqlParser.convertNativeTimeTravel rewrites Spark's
RelationTimeTravel into TimeTravelRelation with
transformDownWithSubqueries. CTE definitions are held in
UnresolvedWith.cteRelations, which are not children, so the transform
never reaches them and Spark's TimeTravelSpec rejects the instant.
Match UnresolvedWith and rewrite its CTE definitions recursively.
* fix(spark): handle the Spark 4.1+ UnresolvedWith CTE tuple shape
Spark 4.1 added the max recursion level to UnresolvedWith.cteRelations,
changing it from Seq[(String, SubqueryAlias)] to
Seq[(String, SubqueryAlias, Option[Int])], so the 2-tuple pattern in
HoodieCommonSqlParser did not compile for Spark 4.1/4.2. Move the CTE
rewrite into HoodieCatalystPlansUtils.transformCteDefinitions with an
implementation per tuple shape.
---
.../spark/sql/HoodieCatalystPlansUtils.scala | 14 +++-
.../spark/sql/parser/HoodieCommonSqlParser.scala | 6 +-
.../sql/hudi/dml/others/TestTimeTravelInCte.scala | 87 ++++++++++++++++++++++
.../spark/sql/HoodieSpark3CatalystPlanUtils.scala | 8 +-
.../spark/sql/HoodieSpark40CatalystPlanUtils.scala | 8 +-
.../spark/sql/HoodieSpark41CatalystPlanUtils.scala | 8 +-
.../spark/sql/HoodieSpark42CatalystPlanUtils.scala | 8 +-
7 files changed, 133 insertions(+), 6 deletions(-)
diff --git
a/hudi-client/hudi-spark-client/src/main/scala/org/apache/spark/sql/HoodieCatalystPlansUtils.scala
b/hudi-client/hudi-spark-client/src/main/scala/org/apache/spark/sql/HoodieCatalystPlansUtils.scala
index 32fe134f9b46..86c01be31e92 100644
---
a/hudi-client/hudi-spark-client/src/main/scala/org/apache/spark/sql/HoodieCatalystPlansUtils.scala
+++
b/hudi-client/hudi-spark-client/src/main/scala/org/apache/spark/sql/HoodieCatalystPlansUtils.scala
@@ -21,7 +21,7 @@ import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.catalog.CatalogStorageFormat
import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression}
import org.apache.spark.sql.catalyst.plans.JoinType
-import org.apache.spark.sql.catalyst.plans.logical.{Assignment, Join,
LogicalPlan}
+import org.apache.spark.sql.catalyst.plans.logical.{Assignment, Join,
LogicalPlan, UnresolvedWith}
import org.apache.spark.sql.execution.datasources.LogicalRelation
import org.apache.spark.sql.internal.SQLConf
@@ -179,4 +179,16 @@ trait HoodieCatalystPlansUtils {
* [[org.apache.spark.sql.execution.streaming.runtime]] package
*/
def extractJsonFromSerializedOffset(offset: Any): Option[String]
+
+ /**
+ * Applies `f` to the plan of each CTE definition of an [[UnresolvedWith]].
The CTE definitions are
+ * not children of [[UnresolvedWith]], so plan transforms do not reach them.
+ *
+ * Before Spark 4.1:
+ * cteRelations: Seq[(String, SubqueryAlias)]
+ *
+ * Since Spark 4.1:
+ * cteRelations: Seq[(String, SubqueryAlias, Option[Int])]
+ */
+ def transformCteDefinitions(withClause: UnresolvedWith, f: LogicalPlan =>
LogicalPlan): LogicalPlan
}
diff --git
a/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/parser/HoodieCommonSqlParser.scala
b/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/parser/HoodieCommonSqlParser.scala
index 99c3b60193e7..e46df6c76642 100644
---
a/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/parser/HoodieCommonSqlParser.scala
+++
b/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/parser/HoodieCommonSqlParser.scala
@@ -29,7 +29,7 @@ import org.apache.spark.sql.catalyst.{FunctionIdentifier,
TableIdentifier}
import org.apache.spark.sql.catalyst.analysis.{RelationTimeTravel,
UnresolvedRelation}
import org.apache.spark.sql.catalyst.expressions.Expression
import org.apache.spark.sql.catalyst.parser.{ParseErrorListener,
ParseException, ParserInterface}
-import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan,
TimeTravelRelation}
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan,
TimeTravelRelation, UnresolvedWith}
import org.apache.spark.sql.catalyst.trees.Origin
import org.apache.spark.sql.types.{DataType, StructType}
@@ -56,6 +56,10 @@ class HoodieCommonSqlParser(session: SparkSession, delegate:
ParserInterface)
plan.transformDownWithSubqueries {
case RelationTimeTravel(relation: UnresolvedRelation, timestamp,
version) =>
TimeTravelRelation(relation, timestamp, version)
+ // The CTE definitions of a WITH clause are not children of
UnresolvedWith, so the transform does
+ // not reach them; rewrite them separately.
+ case withClause: UnresolvedWith =>
+ sparkAdapter.getCatalystPlanUtils.transformCteDefinitions(withClause,
convertNativeTimeTravel)
}
}
diff --git
a/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/dml/others/TestTimeTravelInCte.scala
b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/dml/others/TestTimeTravelInCte.scala
new file mode 100644
index 000000000000..81953fc3ecb2
--- /dev/null
+++
b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/dml/others/TestTimeTravelInCte.scala
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+package org.apache.spark.sql.hudi.dml.others
+
+import org.apache.hudi.testutils.HoodieClientTestUtils.createMetaClient
+
+import org.apache.spark.sql.hudi.common.HoodieSparkSqlTestBase
+
+/**
+ * TIMESTAMP AS OF with a Hudi-format instant (yyyyMMddHHmmssSSS) in the
positions a production query puts it:
+ * top level, inside a CTE, inside two CTEs joined, and inside a derived table.
+ */
+class TestTimeTravelInCte extends HoodieSparkSqlTestBase {
+
+ private def withTwoCommitTable(f: (String, String, String) => Unit): Unit = {
+ withTempDir { tmp =>
+ val tableName = generateTableName
+ spark.sql(
+ s"""
+ |create table $tableName (
+ | id int,
+ | name string,
+ | ts long,
+ | datestr string
+ |) using hudi
+ | tblproperties (type = 'cow', primaryKey = 'id', preCombineField =
'ts')
+ | partitioned by (datestr)
+ | location '${tmp.getCanonicalPath}/$tableName'
+ """.stripMargin)
+ spark.sql(s"insert into $tableName values(1, 'a1', 1000, '2026-10-19')")
+ val metaClient = createMetaClient(spark,
s"${tmp.getCanonicalPath}/$tableName")
+ val instant1 =
metaClient.getActiveTimeline.getAllCommitsTimeline.lastInstant().get().requestedTime
+ spark.sql(s"insert into $tableName values(2, 'a2', 2000, '2026-10-19')")
+ metaClient.reloadActiveTimeline()
+ val instant2 =
metaClient.getActiveTimeline.getAllCommitsTimeline.lastInstant().get().requestedTime
+ f(tableName, instant1, instant2)
+ }
+ }
+
+ test("Time travel with a Hudi instant at top level") {
+ withTwoCommitTable { (t, i1, _) =>
+ checkAnswer(s"select count(*) from $t TIMESTAMP AS OF '$i1' where
datestr = '2026-10-19'")(Seq(1))
+ }
+ }
+
+ test("Time travel with a Hudi instant inside a CTE") {
+ withTwoCommitTable { (t, i1, _) =>
+ checkAnswer(
+ s"""with a as (select * from $t TIMESTAMP AS OF '$i1' where datestr =
'2026-10-19')
+ |select count(*) from a""".stripMargin)(Seq(1))
+ }
+ }
+
+ test("Time travel with Hudi instants inside two joined CTEs") {
+ withTwoCommitTable { (t, i1, i2) =>
+ checkAnswer(
+ s"""with old as (select id, name from $t TIMESTAMP AS OF '$i1' where
datestr = '2026-10-19'),
+ |new as (select id, name from $t TIMESTAMP AS OF '$i2' where
datestr = '2026-10-19')
+ |select new.id from new left join old on new.id = old.id where
old.id is null""".stripMargin)(Seq(2))
+ }
+ }
+
+ test("Time travel with a Hudi instant inside a derived table") {
+ withTwoCommitTable { (t, i1, _) =>
+ checkAnswer(
+ s"select count(*) from (select * from $t TIMESTAMP AS OF '$i1' where
datestr = '2026-10-19') x")(Seq(1))
+ }
+ }
+}
diff --git
a/hudi-spark-datasource/hudi-spark3-common/src/main/scala/org/apache/spark/sql/HoodieSpark3CatalystPlanUtils.scala
b/hudi-spark-datasource/hudi-spark3-common/src/main/scala/org/apache/spark/sql/HoodieSpark3CatalystPlanUtils.scala
index eb22674d9de7..1ced2b527690 100644
---
a/hudi-spark-datasource/hudi-spark3-common/src/main/scala/org/apache/spark/sql/HoodieSpark3CatalystPlanUtils.scala
+++
b/hudi-spark-datasource/hudi-spark3-common/src/main/scala/org/apache/spark/sql/HoodieSpark3CatalystPlanUtils.scala
@@ -19,7 +19,7 @@
package org.apache.spark.sql
import org.apache.spark.sql.catalyst.expressions.Expression
-import org.apache.spark.sql.catalyst.plans.logical.{Assignment, UpdateAction}
+import org.apache.spark.sql.catalyst.plans.logical.{Assignment, LogicalPlan,
UnresolvedWith, UpdateAction}
import org.apache.spark.sql.execution.streaming.SerializedOffset
/**
@@ -41,4 +41,10 @@ abstract class HoodieSpark3CatalystPlanUtils extends
BaseHoodieCatalystPlanUtils
case _ => None
}
}
+
+ override def transformCteDefinitions(withClause: UnresolvedWith, f:
LogicalPlan => LogicalPlan): LogicalPlan = {
+ withClause.copy(cteRelations = withClause.cteRelations.map { case (name,
cte) =>
+ (name, cte.copy(child = f(cte.child)))
+ })
+ }
}
diff --git
a/hudi-spark-datasource/hudi-spark4.0.x/src/main/scala/org/apache/spark/sql/HoodieSpark40CatalystPlanUtils.scala
b/hudi-spark-datasource/hudi-spark4.0.x/src/main/scala/org/apache/spark/sql/HoodieSpark40CatalystPlanUtils.scala
index f4838b9bc8af..c921ab17b760 100644
---
a/hudi-spark-datasource/hudi-spark4.0.x/src/main/scala/org/apache/spark/sql/HoodieSpark40CatalystPlanUtils.scala
+++
b/hudi-spark-datasource/hudi-spark4.0.x/src/main/scala/org/apache/spark/sql/HoodieSpark40CatalystPlanUtils.scala
@@ -19,7 +19,7 @@
package org.apache.spark.sql
import org.apache.spark.sql.catalyst.expressions.Expression
-import org.apache.spark.sql.catalyst.plans.logical.{Assignment, UpdateAction}
+import org.apache.spark.sql.catalyst.plans.logical.{Assignment, LogicalPlan,
UnresolvedWith, UpdateAction}
import org.apache.spark.sql.execution.streaming.SerializedOffset
object HoodieSpark40CatalystPlanUtils extends HoodieSpark4CatalystPlanUtils {
@@ -37,4 +37,10 @@ object HoodieSpark40CatalystPlanUtils extends
HoodieSpark4CatalystPlanUtils {
case _ => None
}
}
+
+ override def transformCteDefinitions(withClause: UnresolvedWith, f:
LogicalPlan => LogicalPlan): LogicalPlan = {
+ withClause.copy(cteRelations = withClause.cteRelations.map { case (name,
cte) =>
+ (name, cte.copy(child = f(cte.child)))
+ })
+ }
}
diff --git
a/hudi-spark-datasource/hudi-spark4.1.x/src/main/scala/org/apache/spark/sql/HoodieSpark41CatalystPlanUtils.scala
b/hudi-spark-datasource/hudi-spark4.1.x/src/main/scala/org/apache/spark/sql/HoodieSpark41CatalystPlanUtils.scala
index 24489bb4f16a..540149add30f 100644
---
a/hudi-spark-datasource/hudi-spark4.1.x/src/main/scala/org/apache/spark/sql/HoodieSpark41CatalystPlanUtils.scala
+++
b/hudi-spark-datasource/hudi-spark4.1.x/src/main/scala/org/apache/spark/sql/HoodieSpark41CatalystPlanUtils.scala
@@ -19,7 +19,7 @@
package org.apache.spark.sql
import org.apache.spark.sql.catalyst.expressions.Expression
-import org.apache.spark.sql.catalyst.plans.logical.{Assignment, UpdateAction}
+import org.apache.spark.sql.catalyst.plans.logical.{Assignment, LogicalPlan,
UnresolvedWith, UpdateAction}
import org.apache.spark.sql.execution.streaming.runtime.SerializedOffset
object HoodieSpark41CatalystPlanUtils extends HoodieSpark4CatalystPlanUtils {
@@ -37,4 +37,10 @@ object HoodieSpark41CatalystPlanUtils extends
HoodieSpark4CatalystPlanUtils {
case _ => None
}
}
+
+ override def transformCteDefinitions(withClause: UnresolvedWith, f:
LogicalPlan => LogicalPlan): LogicalPlan = {
+ withClause.copy(cteRelations = withClause.cteRelations.map { case (name,
cte, maxRecursionLevel) =>
+ (name, cte.copy(child = f(cte.child)), maxRecursionLevel)
+ })
+ }
}
diff --git
a/hudi-spark-datasource/hudi-spark4.2.x/src/main/scala/org/apache/spark/sql/HoodieSpark42CatalystPlanUtils.scala
b/hudi-spark-datasource/hudi-spark4.2.x/src/main/scala/org/apache/spark/sql/HoodieSpark42CatalystPlanUtils.scala
index f99a0ba7708a..427943ad4ba8 100644
---
a/hudi-spark-datasource/hudi-spark4.2.x/src/main/scala/org/apache/spark/sql/HoodieSpark42CatalystPlanUtils.scala
+++
b/hudi-spark-datasource/hudi-spark4.2.x/src/main/scala/org/apache/spark/sql/HoodieSpark42CatalystPlanUtils.scala
@@ -19,7 +19,7 @@
package org.apache.spark.sql
import org.apache.spark.sql.catalyst.expressions.Expression
-import org.apache.spark.sql.catalyst.plans.logical.{Assignment, UpdateAction}
+import org.apache.spark.sql.catalyst.plans.logical.{Assignment, LogicalPlan,
UnresolvedWith, UpdateAction}
import org.apache.spark.sql.execution.streaming.runtime.SerializedOffset
object HoodieSpark42CatalystPlanUtils extends HoodieSpark4CatalystPlanUtils {
@@ -37,4 +37,10 @@ object HoodieSpark42CatalystPlanUtils extends
HoodieSpark4CatalystPlanUtils {
case _ => None
}
}
+
+ override def transformCteDefinitions(withClause: UnresolvedWith, f:
LogicalPlan => LogicalPlan): LogicalPlan = {
+ withClause.copy(cteRelations = withClause.cteRelations.map { case (name,
cte, maxRecursionLevel) =>
+ (name, cte.copy(child = f(cte.child)), maxRecursionLevel)
+ })
+ }
}