xushiyan commented on a change in pull request #4720:
URL: https://github.com/apache/hudi/pull/4720#discussion_r817782917



##########
File path: 
hudi-client/hudi-spark-client/src/main/scala/org/apache/hudi/SparkAdapterSupport.scala
##########
@@ -27,7 +27,9 @@ import org.apache.spark.sql.hudi.SparkAdapter
 trait SparkAdapterSupport {
 
   lazy val sparkAdapter: SparkAdapter = {
-    val adapterClass = if (HoodieSparkUtils.isSpark3) {
+    val adapterClass = if (HoodieSparkUtils.isSpark3_2) {

Review comment:
       use `gteqSpark3_2` here to be future-proof ?

##########
File path: 
hudi-client/hudi-spark-client/src/main/scala/org/apache/hudi/SparkAdapterSupport.scala
##########
@@ -27,7 +27,9 @@ import org.apache.spark.sql.hudi.SparkAdapter
 trait SparkAdapterSupport {
 
   lazy val sparkAdapter: SparkAdapter = {
-    val adapterClass = if (HoodieSparkUtils.isSpark3) {
+    val adapterClass = if (HoodieSparkUtils.isSpark3_2) {
+      "org.apache.spark.sql.adapter.Spark3_2Adapter"
+    } else if (HoodieSparkUtils.isSpark3 || HoodieSparkUtils.isSpark3_1) {

Review comment:
       we should deprecate `isSpark3` since it's ambiguous. 

##########
File path: 
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/TestTimeTravelParser.scala
##########
@@ -0,0 +1,80 @@
+/*
+ * 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
+
+import org.apache.hudi.HoodieSparkUtils
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.catalyst.analysis.{UnresolvedRelation, 
UnresolvedStar}
+import org.apache.spark.sql.catalyst.expressions.Literal
+import org.apache.spark.sql.catalyst.plans.logical.{Project, 
TimeTravelRelation}
+
+class TestTimeTravelParser extends TestHoodieSqlBase {
+  private val parser = spark.sessionState.sqlParser
+
+  test("time travel of timestamp") {
+    if (HoodieSparkUtils.isSpark3_2) {
+      val timeTravelPlan1 = parser.parsePlan("SELECT * FROM A.B " +
+        "TIMESTAMP AS OF '2019-01-29 00:37:58'")
+
+      assertResult(Project(Seq(UnresolvedStar(None)),
+        TimeTravelRelation(
+          UnresolvedRelation(new TableIdentifier("B", Option.apply("A"))),
+          Some(Literal("2019-01-29 00:37:58")),
+          None))) {
+        timeTravelPlan1
+      }
+
+      val timeTravelPlan2 = parser.parsePlan("SELECT * FROM A.B " +
+        "TIMESTAMP AS OF 1643119574")
+
+      assertResult(Project(Seq(UnresolvedStar(None)),
+        TimeTravelRelation(
+          UnresolvedRelation(new TableIdentifier("B", Option.apply("A"))),
+          Some(Literal(1643119574)),
+          None))) {
+        timeTravelPlan2
+      }
+    }
+  }
+
+  test("time travel of version") {
+    if (HoodieSparkUtils.isSpark3_2) {

Review comment:
       ditto

##########
File path: 
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/TestTimeTravelParser.scala
##########
@@ -0,0 +1,80 @@
+/*
+ * 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
+
+import org.apache.hudi.HoodieSparkUtils
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.catalyst.analysis.{UnresolvedRelation, 
UnresolvedStar}
+import org.apache.spark.sql.catalyst.expressions.Literal
+import org.apache.spark.sql.catalyst.plans.logical.{Project, 
TimeTravelRelation}
+
+class TestTimeTravelParser extends TestHoodieSqlBase {
+  private val parser = spark.sessionState.sqlParser
+
+  test("time travel of timestamp") {
+    if (HoodieSparkUtils.isSpark3_2) {

Review comment:
       better negate the checking and early return 

##########
File path: 
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/analysis/HoodieAnalysis.scala
##########
@@ -350,6 +353,35 @@ case class HoodieResolveReferences(sparkSession: 
SparkSession) extends Rule[Logi
         l
       }
 
+    case TimeTravelRelation(plan: UnresolvedRelation, timestamp, version) =>
+      // TODO: How to use version to perform time travel?
+      if (timestamp.isEmpty && version.nonEmpty) {
+        throw new AnalysisException(
+          "version expression is not support for time travel")

Review comment:
       ```suggestion
             "version expression is not supported for time travel")
   ```

##########
File path: 
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/analysis/HoodieAnalysis.scala
##########
@@ -350,6 +353,35 @@ case class HoodieResolveReferences(sparkSession: 
SparkSession) extends Rule[Logi
         l
       }
 
+    case TimeTravelRelation(plan: UnresolvedRelation, timestamp, version) =>
+      // TODO: How to use version to perform time travel?

Review comment:
       2nd thoughts: we should keep the usage similar to what has been done 
here https://hudi.apache.org/docs/quick-start-guide/#time-travel-query
   
   so just support `INSTANT AS OF ` and parse those 3 time formats?

##########
File path: 
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/TestTimeTravelTable.scala
##########
@@ -0,0 +1,200 @@
+/*
+ * 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
+
+import org.apache.hudi.HoodieSparkUtils
+import org.apache.hudi.common.table.HoodieTableMetaClient
+
+class TestTimeTravelTable extends TestHoodieSqlBase {
+
+  test("Test Two Table's Union Join with time travel") {
+    if (HoodieSparkUtils.isSpark3_2) {
+      withTempDir { tmp =>
+        Seq("cow", "mor").foreach { tableType =>
+          val tableName = generateTableName
+
+          val basePath = tmp.getCanonicalPath
+          val tableName1 = tableName + "_1"
+          val tableName2 = tableName + "_2"
+          val path1 = s"$basePath/$tableName1"
+          val path2 = s"$basePath/$tableName2"
+
+          spark.sql(
+            s"""
+               |create table $tableName1 (
+               |  id int,
+               |  name string,
+               |  price double,
+               |  ts long
+               |) using hudi
+               | tblproperties (
+               |  type = '$tableType',
+               |  primaryKey = 'id',
+               |  preCombineField = 'ts'
+               | )
+               | location '$path1'
+       """.stripMargin)
+
+          spark.sql(
+            s"""
+               |create table $tableName2 (
+               |  id int,
+               |  name string,
+               |  price double,
+               |  ts long
+               |) using hudi
+               | tblproperties (
+               |  type = '$tableType',
+               |  primaryKey = 'id',
+               |  preCombineField = 'ts'
+               | )
+               | location '$path2'
+       """.stripMargin)
+
+          spark.sql(s"insert into $tableName1 values(1, 'a1', 10, 1000)")
+          spark.sql(s"insert into $tableName1 values(2, 'a2', 20, 1000)")
+
+          checkAnswer(s"select id, name, price, ts from $tableName1")(
+            Seq(1, "a1", 10.0, 1000),
+            Seq(2, "a2", 20.0, 1000)
+          )
+
+          checkAnswer(s"select id, name, price, ts from $tableName1")(
+            Seq(1, "a1", 10.0, 1000),
+            Seq(2, "a2", 20.0, 1000)
+          )
+
+          spark.sql(s"insert into $tableName2 values(3, 'a3', 10, 1000)")
+          spark.sql(s"insert into $tableName2 values(4, 'a4', 20, 1000)")
+
+          checkAnswer(s"select id, name, price, ts from $tableName2")(
+            Seq(3, "a3", 10.0, 1000),
+            Seq(4, "a4", 20.0, 1000)
+          )
+
+          val metaClient1 = HoodieTableMetaClient.builder()
+            .setBasePath(path1)
+            .setConf(spark.sessionState.newHadoopConf())
+            .build()
+
+          val metaClient2 = HoodieTableMetaClient.builder()
+            .setBasePath(path2)
+            .setConf(spark.sessionState.newHadoopConf())
+            .build()
+
+          val instant1 = metaClient1.getActiveTimeline.getAllCommitsTimeline
+            .lastInstant().get().getTimestamp
+
+          val instant2 = metaClient2.getActiveTimeline.getAllCommitsTimeline
+            .lastInstant().get().getTimestamp
+
+          val sql =
+            s"""
+               |select id, name, price, ts from $tableName1 TIMESTAMP AS OF 
'$instant1' where id=1
+               |union
+               |select id, name, price, ts from $tableName2 TIMESTAMP AS OF 
'$instant2' where id>1
+               |""".stripMargin

Review comment:
       not sure how exactly this union query can test time travel functionality 
effectively. if nothing particular about union here, then we should rewrite the 
test for time travel specifically. I think we need to insert at commit1 and 
update the same record at commit2. Then use as of to query and get the value at 
commit1's time.

##########
File path: 
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/analysis/HoodieAnalysis.scala
##########
@@ -350,6 +353,35 @@ case class HoodieResolveReferences(sparkSession: 
SparkSession) extends Rule[Logi
         l
       }
 
+    case TimeTravelRelation(plan: UnresolvedRelation, timestamp, version) =>
+      // TODO: How to use version to perform time travel?

Review comment:
       do you have a follow-up ticket for supporting version-only time-travel?

##########
File path: 
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/TestTimeTravelParser.scala
##########
@@ -0,0 +1,80 @@
+/*
+ * 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
+
+import org.apache.hudi.HoodieSparkUtils
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.catalyst.analysis.{UnresolvedRelation, 
UnresolvedStar}
+import org.apache.spark.sql.catalyst.expressions.Literal
+import org.apache.spark.sql.catalyst.plans.logical.{Project, 
TimeTravelRelation}
+
+class TestTimeTravelParser extends TestHoodieSqlBase {
+  private val parser = spark.sessionState.sqlParser
+
+  test("time travel of timestamp") {
+    if (HoodieSparkUtils.isSpark3_2) {
+      val timeTravelPlan1 = parser.parsePlan("SELECT * FROM A.B " +
+        "TIMESTAMP AS OF '2019-01-29 00:37:58'")
+
+      assertResult(Project(Seq(UnresolvedStar(None)),
+        TimeTravelRelation(
+          UnresolvedRelation(new TableIdentifier("B", Option.apply("A"))),
+          Some(Literal("2019-01-29 00:37:58")),
+          None))) {
+        timeTravelPlan1
+      }
+
+      val timeTravelPlan2 = parser.parsePlan("SELECT * FROM A.B " +
+        "TIMESTAMP AS OF 1643119574")
+
+      assertResult(Project(Seq(UnresolvedStar(None)),
+        TimeTravelRelation(
+          UnresolvedRelation(new TableIdentifier("B", Option.apply("A"))),
+          Some(Literal(1643119574)),
+          None))) {
+        timeTravelPlan2
+      }
+    }
+  }
+
+  test("time travel of version") {
+    if (HoodieSparkUtils.isSpark3_2) {
+      val timeTravelPlan1 = parser.parsePlan("SELECT * FROM A.B " +
+        "VERSION AS OF 'Snapshot123456789'")

Review comment:
       shouldn't this throw exception as not supported?

##########
File path: 
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/TestTimeTravelTable.scala
##########
@@ -0,0 +1,200 @@
+/*
+ * 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
+
+import org.apache.hudi.HoodieSparkUtils
+import org.apache.hudi.common.table.HoodieTableMetaClient
+
+class TestTimeTravelTable extends TestHoodieSqlBase {
+
+  test("Test Two Table's Union Join with time travel") {
+    if (HoodieSparkUtils.isSpark3_2) {

Review comment:
       same here. gteqSpark3_2 should work better. also better early return




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to