From 86aad43f5131aa37398ae341104a4e5603030a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=89=BF=E7=A5=A5?= Date: Wed, 6 Jul 2022 18:24:56 +0800 Subject: [PATCH 1/4] support copyToTable on call --- .../procedures/CopyToTableProcedure.scala | 128 +++++++ .../command/procedures/HoodieProcedures.scala | 1 + .../procedure/TestCopyToTableProcedure.scala | 321 ++++++++++++++++++ 3 files changed, 450 insertions(+) create mode 100644 hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/CopyToTableProcedure.scala create mode 100644 hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala diff --git a/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/CopyToTableProcedure.scala b/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/CopyToTableProcedure.scala new file mode 100644 index 000000000000..b49875c94c11 --- /dev/null +++ b/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/CopyToTableProcedure.scala @@ -0,0 +1,128 @@ +/* + * 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.command.procedures + +import org.apache.hudi.DataSourceReadOptions +import org.apache.spark.internal.Logging +import org.apache.spark.sql.types.{DataTypes, Metadata, StructField, StructType} +import org.apache.spark.sql.{Row, SaveMode} + +import java.util.function.Supplier + +class CopyToTableProcedure extends BaseProcedure with ProcedureBuilder with Logging { + + + private val PARAMETERS = Array[ProcedureParameter]( + ProcedureParameter.required(0, "table", DataTypes.StringType, None), + ProcedureParameter.optional(1, "query_type", DataTypes.StringType, DataSourceReadOptions.QUERY_TYPE_SNAPSHOT_OPT_VAL), + ProcedureParameter.required(2, "new_table", DataTypes.StringType, None), + ProcedureParameter.optional(3, "begin_instance_time", DataTypes.StringType, ""), + ProcedureParameter.optional(4, "end_instance_time", DataTypes.StringType, ""), + ProcedureParameter.optional(5, "as_of_instant", DataTypes.StringType, ""), + ProcedureParameter.optional(6, "save_mode", DataTypes.StringType, "overwrite") + ) + + private val OUTPUT_TYPE = new StructType(Array[StructField]( + StructField("status", DataTypes.IntegerType, nullable = true, Metadata.empty)) + ) + + def parameters: Array[ProcedureParameter] = PARAMETERS + + def outputType: StructType = OUTPUT_TYPE + + override def call(args: ProcedureArgs): Seq[Row] = { + super.checkArgs(PARAMETERS, args) + + val tableName = getArgValueOrDefault(args, PARAMETERS(0)) + val queryType = getArgValueOrDefault(args, PARAMETERS(1)).get.asInstanceOf[String] + val newTableName = getArgValueOrDefault(args, PARAMETERS(2)).get.asInstanceOf[String] + val beginInstance = getArgValueOrDefault(args, PARAMETERS(3)).get.asInstanceOf[String] + val endInstance = getArgValueOrDefault(args, PARAMETERS(4)).get.asInstanceOf[String] + val asOfInstant = getArgValueOrDefault(args, PARAMETERS(5)).get.asInstanceOf[String] + val saveModeStr = getArgValueOrDefault(args, PARAMETERS(6)).get.asInstanceOf[String] + + assert(saveModeStr.nonEmpty, "save_mode(append,overwrite) can not be null.") + + val saveMode: Any = saveModeStr.toLowerCase match { + case "overwrite" => SaveMode.Overwrite + case "append" => SaveMode.Append + case _ => assert(assertion = false, s"save_mode not support $saveModeStr.") + } + + + val tablePath = getBasePath(tableName) + + val sourceDataFrame = queryType match { + case DataSourceReadOptions.QUERY_TYPE_SNAPSHOT_OPT_VAL => if (asOfInstant.nonEmpty) { + sparkSession.read + .format("org.apache.hudi") + .option(DataSourceReadOptions.QUERY_TYPE.key, DataSourceReadOptions.QUERY_TYPE_SNAPSHOT_OPT_VAL) + .option(DataSourceReadOptions.TIME_TRAVEL_AS_OF_INSTANT.key, asOfInstant) + .load(tablePath) + } else { + sparkSession.read + .format("org.apache.hudi") + .option(DataSourceReadOptions.QUERY_TYPE.key, DataSourceReadOptions.QUERY_TYPE_SNAPSHOT_OPT_VAL) + .load(tablePath) + } + case DataSourceReadOptions.QUERY_TYPE_INCREMENTAL_OPT_VAL => + assert(beginInstance.nonEmpty && endInstance.nonEmpty, "when the query_type is incremental, begin_instance_time and end_instance_time can not be null.") + sparkSession.read + .format("org.apache.hudi") + .option(DataSourceReadOptions.QUERY_TYPE.key, DataSourceReadOptions.QUERY_TYPE_INCREMENTAL_OPT_VAL) + .option(DataSourceReadOptions.BEGIN_INSTANTTIME.key, beginInstance) + .option(DataSourceReadOptions.END_INSTANTTIME.key, endInstance) + .load(tablePath) + case DataSourceReadOptions.QUERY_TYPE_READ_OPTIMIZED_OPT_VAL => + sparkSession.read + .format("org.apache.hudi") + .option(DataSourceReadOptions.QUERY_TYPE.key, DataSourceReadOptions.QUERY_TYPE_READ_OPTIMIZED_OPT_VAL) + .load(tablePath) + } + if (sparkSession.catalog.tableExists(newTableName)) { + val schema = sparkSession.read.table(newTableName).schema + val selectColumns = schema.fields.toStream.map(_.name) + sourceDataFrame.selectExpr(selectColumns: _*) + .write + .mode(saveMode.toString) + .saveAsTable(newTableName) + } else { + sourceDataFrame.write + .mode(saveMode.toString) + .saveAsTable(newTableName) + } + + + Seq(Row(0)) + } + + override def build = new CopyToTableProcedure() +} + +object CopyToTableProcedure { + val NAME = "copy_to_table" + + def builder: Supplier[ProcedureBuilder] = new Supplier[ProcedureBuilder] { + override def get() = new CopyToTableProcedure() + } +} + + + + + diff --git a/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/HoodieProcedures.scala b/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/HoodieProcedures.scala index 0545c140bb3e..5f2728597e8d 100644 --- a/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/HoodieProcedures.scala +++ b/hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/HoodieProcedures.scala @@ -74,6 +74,7 @@ object HoodieProcedures { mapBuilder.put(ShowMetadataStatsProcedure.NAME, ShowMetadataStatsProcedure.builder) mapBuilder.put(ValidateMetadataFilesProcedure.NAME, ValidateMetadataFilesProcedure.builder) mapBuilder.put(ShowFsPathDetailProcedure.NAME, ShowFsPathDetailProcedure.builder) + mapBuilder.put(CopyToTableProcedure.NAME, CopyToTableProcedure.builder) mapBuilder.build } } diff --git a/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala new file mode 100644 index 000000000000..2e65fdd49c3f --- /dev/null +++ b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala @@ -0,0 +1,321 @@ +/* + * 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.procedure + +import org.apache.spark.sql.Row +import org.apache.spark.sql.hudi.HoodieSparkSqlTestBase + +import java.util + +class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { + + test("Test Call copy_to_table Procedure with default params") { + withTempDir { tmp => + val tableName = generateTableName + // create table + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | location '${tmp.getCanonicalPath}/$tableName' + | tblproperties ( + | primaryKey = 'id', + | preCombineField = 'ts' + | ) + """.stripMargin) + + // insert data to table + spark.sql(s"insert into $tableName select 1, 'a1', 10, 1000") + spark.sql(s"insert into $tableName select 2, 'a2', 20, 1500") + spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000") + spark.sql(s"insert into $tableName select 4, 'a4', 40, 2500") + + val copyTableName = "copyTable" + // Check required fields + checkExceptionContain(s"call copy_to_table(table=>'$tableName')")(s"Argument: new_table is required") + + val row = spark.sql(s"""call copy_to_table(table=>'$tableName',new_table=>'$copyTableName')""").collectAsList() + assert(row.size() == 1 && row.get(0).get(0) == 0) + val copyTableCount = spark.sql(s"""select count(1) from $copyTableName""").collectAsList() + assert(copyTableCount.size() == 1 && copyTableCount.get(0).get(0) == 4) + } + } + + test("Test Call copy_to_table Procedure with snapshot") { + withTempDir { tmp => + val tableName = generateTableName + // create table + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | location '${tmp.getCanonicalPath}/$tableName' + | tblproperties ( + | primaryKey = 'id', + | preCombineField = 'ts' + | ) + """.stripMargin) + + // insert 4 rows data to table + spark.sql(s"insert into $tableName select 1, 'a1', 10, 1000") + spark.sql(s"insert into $tableName select 2, 'a2', 20, 1500") + spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000") + spark.sql(s"insert into $tableName select 4, 'a4', 40, 2500") + + val copyTableName = "copyTable" + val row = spark.sql(s"""call copy_to_table(table=>'$tableName',new_table=>'$copyTableName',query_type=>'snapshot')""").collectAsList() + // check exit code + assert(row.size() == 1 && row.get(0).get(0) == 0) + val copyTableCount = spark.sql(s"""select count(1) from $copyTableName""").collectAsList() + assert(copyTableCount.size() == 1 && copyTableCount.get(0).get(0) == 4) + + + // insert 4 rows data to table + spark.sql(s"insert into $tableName select 5, 'a1', 10, 1000") + // mark max instanceTime.total row is 5 + val instanceTime = spark.sql(s"select max(_hoodie_commit_time) from $tableName").collectAsList().get(0).get(0) + spark.sql(s"insert into $tableName select 6, 'a1', 10, 1000") + spark.sql(s"insert into $tableName select 7, 'a1', 10, 1000") + spark.sql(s"insert into $tableName select 8, 'a1', 10, 1000") + + // check row count after twice insert + val finalTableCount = spark.sql(s"select * from $tableName").count() + assert(finalTableCount == 8) + + // check snapshot copy with mark instanceTime + val copyTableName2 = "copyTable2" + val row2 = spark.sql(s"""call copy_to_table(table=>'$tableName',new_table=>'$copyTableName2',query_type=>'snapshot',as_of_instant=>'$instanceTime')""").collectAsList() + // check exit code + assert(row2.size() == 1 && row2.get(0).get(0) == 0) + val df = spark.sql(s"""select * from $copyTableName2""") + assert(df.count() == 5) + + val ids: util.List[Row] = df.selectExpr("id").collectAsList() + assert(ids.containsAll(util.Arrays.asList(Row(1), Row(2), Row(3), Row(4), Row(5)))) + + } + } + + test("Test Call copy_to_table Procedure with incremental") { + withTempDir { tmp => + val tableName = generateTableName + // create table + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | location '${tmp.getCanonicalPath}/$tableName' + | tblproperties ( + | primaryKey = 'id', + | preCombineField = 'ts' + | ) + """.stripMargin) + + // insert 4 rows data to table + spark.sql(s"insert into $tableName select 1, 'a1', 10, 1000") + spark.sql(s"insert into $tableName select 2, 'a2', 20, 1500") + + // mark beginTime + val beginTime = spark.sql(s"select max(_hoodie_commit_time) from $tableName").collectAsList().get(0).get(0) + spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000") + spark.sql(s"insert into $tableName select 4, 'a4', 40, 2500") + val endTime = spark.sql(s"select max(_hoodie_commit_time) from $tableName").collectAsList().get(0).get(0) + + + val copyTableName = "copyTable" + // Check required fields + checkExceptionContain(s"call copy_to_table(table=>'$tableName',new_table=>'$copyTableName',query_type=>'incremental')")("begin_instance_time and end_instance_time can not be null") + + //copy from tableName with begin_instance_time态end_instance_time + val copyCmd = spark.sql(s"call copy_to_table" + s"(table=>'$tableName'" + + s",new_table=>'$copyTableName'" + + s",query_type=>'incremental'" + + s",begin_instance_time=>'$beginTime'" + + s",end_instance_time=>'$endTime')").collectAsList() + assert(copyCmd.size() == 1 && copyCmd.get(0).get(0) == 0) + + val df = spark.sql(s"select * from $copyTableName") + assert(df.count() == 2) + val ids = df.selectExpr("id").collectAsList() + assert(ids.containsAll(util.Arrays.asList(Row(3), Row(4)))) + } + } + + test("Test Call copy_to_table Procedure with read_optimized") { + withTempDir { tmp => + val tableName = generateTableName + // create mor table + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | location '${tmp.getCanonicalPath}/$tableName' + | options ( + | type='mor', + | primaryKey = 'id', + | preCombineField = 'ts' + | ) + """.stripMargin) + + // set hoodie.compact.inline.max.delta.commits=5 + spark.sql("set hoodie.compact.inline.max.delta.commits=5") + spark.sql("set hoodie.compact.inline=true") + //add 5 delta commit + spark.sql(s"insert into $tableName select 1, 'a1', 10, 1000") + spark.sql(s"update $tableName set ts=2000 where id = 1") + spark.sql(s"update $tableName set ts=3000 where id = 1") + spark.sql(s"update $tableName set ts=4000 where id = 1") + + val copyTableName = "copyTable" + + val copyCmd = spark.sql(s"""call copy_to_table(table=>'$tableName',new_table=>'$copyTableName',query_type=>'read_optimized')""").collectAsList() + assert(copyCmd.size() == 1 && copyCmd.get(0).get(0) == 0) + val copyDf = spark.sql(s"select * from $copyTableName") + assert(copyDf.count() == 1) + //check ts + assert(copyDf.selectExpr("ts").collectAsList().contains(Row(1000))) + + // trigger compact (delta_commit==5) + spark.sql(s"update $tableName set ts=5000 where id = 1") + + val copyTableName2 = "copyTable2" + val copyCmd2 = spark.sql(s"""call copy_to_table(table=>'$tableName',new_table=>'$copyTableName2',query_type=>'read_optimized')""").collectAsList() + assert(copyCmd2.size() == 1 && copyCmd2.get(0).get(0) == 0) + val copyDf2 = spark.sql(s"select * from $copyTableName2") + assert(copyDf2.count() == 1) + //check ts + assert(copyDf2.selectExpr("ts").collectAsList().contains(Row(5000))) + } + } + + test("Test Call copy_to_table Procedure with append_mode") { + withTempDir { tmp => + val tableName = generateTableName + // create table + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | location '${tmp.getCanonicalPath}/$tableName' + | tblproperties ( + | primaryKey = 'id', + | preCombineField = 'ts' + | ) + """.stripMargin) + + // insert data to table + spark.sql(s"insert into $tableName select 1, 'a1', 10, 1000") + spark.sql(s"insert into $tableName select 2, 'a2', 20, 1500") + spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000") + spark.sql(s"insert into $tableName select 4, 'a4', 40, 2500") + + val copyTableName = "copyTable" + // Check required fields + checkExceptionContain(s"call copy_to_table(table=>'$tableName')")(s"Argument: new_table is required") + + val copyCmd = spark.sql(s"call copy_to_table(table=>'$tableName',new_table=>'$copyTableName')").collectAsList() + assert(copyCmd.size() == 1 && copyCmd.get(0).get(0) == 0) + val copyTableCount = spark.sql(s"""select count(1) from $copyTableName""").collectAsList() + assert(copyTableCount.size() == 1 && copyTableCount.get(0).get(0) == 4) + + // add 2 rows + spark.sql(s"insert into $tableName select 5, 'a1', 10, 1000") + spark.sql(s"insert into $tableName select 6, 'a2', 20, 1500") + + val copyCmd2 = spark.sql(s"call copy_to_table(table=>'$tableName',new_table=>'$copyTableName',save_mode=>'append')").collectAsList() + assert(copyCmd2.size() == 1 && copyCmd2.get(0).get(0) == 0) + + val df2 = spark.sql(s"""select * from $copyTableName""") + // total insert 4+6=10 rows + assert(df2.count() == 10) + val ids2 = df2.selectExpr("id").collectAsList() + assert(ids2.containsAll(util.Arrays.asList(Row(1), Row(2), Row(3), Row(4), Row(5), Row(6)))) + + } + } + + test("Test Call copy_to_table Procedure with overwrite_mode") { + withTempDir { tmp => + val tableName = generateTableName + // create table + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | location '${tmp.getCanonicalPath}/$tableName' + | tblproperties ( + | primaryKey = 'id', + | preCombineField = 'ts' + | ) + """.stripMargin) + + // insert data to table + spark.sql(s"insert into $tableName select 1, 'a1', 10, 1000") + spark.sql(s"insert into $tableName select 2, 'a2', 20, 1500") + spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000") + spark.sql(s"insert into $tableName select 4, 'a4', 40, 2500") + + val copyTableName = "copyTable" + // Check required fields + checkExceptionContain(s"call copy_to_table(table=>'$tableName')")(s"Argument: new_table is required") + + val copyCmd = spark.sql(s"call copy_to_table(table=>'$tableName',new_table=>'$copyTableName')").collectAsList() + assert(copyCmd.size() == 1 && copyCmd.get(0).get(0) == 0) + val copyTableCount = spark.sql(s"""select count(1) from $copyTableName""").collectAsList() + assert(copyTableCount.size() == 1 && copyTableCount.get(0).get(0) == 4) + + // add 2 rows + spark.sql(s"insert into $tableName select 5, 'a1', 10, 1000") + spark.sql(s"insert into $tableName select 6, 'a2', 20, 1500") + + val copyCmd2 = spark.sql(s"call copy_to_table(table=>'$tableName',new_table=>'$copyTableName',save_mode=>'Overwrite')").collectAsList() + assert(copyCmd2.size() == 1 && copyCmd2.get(0).get(0) == 0) + + val df2 = spark.sql(s"""select * from $copyTableName""") + // total insert 6 rows + assert(df2.count() == 6) + val ids2 = df2.selectExpr("id").collectAsList() + assert(ids2.containsAll(util.Arrays.asList(Row(1), Row(2), Row(3), Row(4), Row(5), Row(6)))) + + } + } +} From 6bd8508685999f838464fdbf9e8bee313aef60f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=89=BF=E7=A5=A5?= Date: Wed, 6 Jul 2022 20:33:45 +0800 Subject: [PATCH 2/4] ut fix --- .../procedure/TestCopyToTableProcedure.scala | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala index 2e65fdd49c3f..d7ece61ae498 100644 --- a/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala +++ b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala @@ -49,7 +49,7 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000") spark.sql(s"insert into $tableName select 4, 'a4', 40, 2500") - val copyTableName = "copyTable" + val copyTableName = generateTableName // Check required fields checkExceptionContain(s"call copy_to_table(table=>'$tableName')")(s"Argument: new_table is required") @@ -85,7 +85,7 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000") spark.sql(s"insert into $tableName select 4, 'a4', 40, 2500") - val copyTableName = "copyTable" + val copyTableName = generateTableName val row = spark.sql(s"""call copy_to_table(table=>'$tableName',new_table=>'$copyTableName',query_type=>'snapshot')""").collectAsList() // check exit code assert(row.size() == 1 && row.get(0).get(0) == 0) @@ -106,7 +106,7 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { assert(finalTableCount == 8) // check snapshot copy with mark instanceTime - val copyTableName2 = "copyTable2" + val copyTableName2 = generateTableName val row2 = spark.sql(s"""call copy_to_table(table=>'$tableName',new_table=>'$copyTableName2',query_type=>'snapshot',as_of_instant=>'$instanceTime')""").collectAsList() // check exit code assert(row2.size() == 1 && row2.get(0).get(0) == 0) @@ -149,7 +149,7 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { val endTime = spark.sql(s"select max(_hoodie_commit_time) from $tableName").collectAsList().get(0).get(0) - val copyTableName = "copyTable" + val copyTableName = generateTableName // Check required fields checkExceptionContain(s"call copy_to_table(table=>'$tableName',new_table=>'$copyTableName',query_type=>'incremental')")("begin_instance_time and end_instance_time can not be null") @@ -171,7 +171,7 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { test("Test Call copy_to_table Procedure with read_optimized") { withTempDir { tmp => val tableName = generateTableName - // create mor table + // create mor table with hoodie.compact.inline.max.delta.commits=5 spark.sql( s""" |create table $tableName ( @@ -184,20 +184,20 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { | options ( | type='mor', | primaryKey = 'id', - | preCombineField = 'ts' + | preCombineField = 'ts', + | hoodie.compact.inline.max.delta.commits='5', + | hoodie.compact.inline='true' + | | ) """.stripMargin) - // set hoodie.compact.inline.max.delta.commits=5 - spark.sql("set hoodie.compact.inline.max.delta.commits=5") - spark.sql("set hoodie.compact.inline=true") - //add 5 delta commit + //add 4 delta commit spark.sql(s"insert into $tableName select 1, 'a1', 10, 1000") spark.sql(s"update $tableName set ts=2000 where id = 1") spark.sql(s"update $tableName set ts=3000 where id = 1") spark.sql(s"update $tableName set ts=4000 where id = 1") - val copyTableName = "copyTable" + val copyTableName = generateTableName val copyCmd = spark.sql(s"""call copy_to_table(table=>'$tableName',new_table=>'$copyTableName',query_type=>'read_optimized')""").collectAsList() assert(copyCmd.size() == 1 && copyCmd.get(0).get(0) == 0) @@ -209,7 +209,7 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { // trigger compact (delta_commit==5) spark.sql(s"update $tableName set ts=5000 where id = 1") - val copyTableName2 = "copyTable2" + val copyTableName2 = generateTableName val copyCmd2 = spark.sql(s"""call copy_to_table(table=>'$tableName',new_table=>'$copyTableName2',query_type=>'read_optimized')""").collectAsList() assert(copyCmd2.size() == 1 && copyCmd2.get(0).get(0) == 0) val copyDf2 = spark.sql(s"select * from $copyTableName2") @@ -244,7 +244,7 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000") spark.sql(s"insert into $tableName select 4, 'a4', 40, 2500") - val copyTableName = "copyTable" + val copyTableName = generateTableName // Check required fields checkExceptionContain(s"call copy_to_table(table=>'$tableName')")(s"Argument: new_table is required") @@ -294,7 +294,7 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000") spark.sql(s"insert into $tableName select 4, 'a4', 40, 2500") - val copyTableName = "copyTable" + val copyTableName = generateTableName // Check required fields checkExceptionContain(s"call copy_to_table(table=>'$tableName')")(s"Argument: new_table is required") From fb543cdfd5c7c3f4f1237047ad3420903e0fc7d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=89=BF=E7=A5=A5?= Date: Thu, 7 Jul 2022 10:16:31 +0800 Subject: [PATCH 3/4] add copy_to_table ut --- .../procedure/TestCopyToTableProcedure.scala | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala index d7ece61ae498..8a5b940bdf25 100644 --- a/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala +++ b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala @@ -318,4 +318,36 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { } } + + test("Test Call copy_to_table Procedure with not support mode") { + withTempDir { tmp => + val tableName = generateTableName + // create table + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | location '${tmp.getCanonicalPath}/$tableName' + | tblproperties ( + | primaryKey = 'id', + | preCombineField = 'ts' + | ) + """.stripMargin) + + // insert data to table + spark.sql(s"insert into $tableName select 1, 'a1', 10, 1000") + spark.sql(s"insert into $tableName select 2, 'a2', 20, 1500") + spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000") + spark.sql(s"insert into $tableName select 4, 'a4', 40, 2500") + + val copyTableName = generateTableName + // Check required fields + checkExceptionContain(s"call copy_to_table(table=>'$tableName',new_table=>'$copyTableName',save_mode=>'append1')")(s"save_mode not support append1") + + } + } } From a2a4e1c830cf895318af85b71565cc12586e33e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=89=BF=E7=A5=A5?= Date: Fri, 8 Jul 2022 10:45:01 +0800 Subject: [PATCH 4/4] fix --- .../spark/sql/hudi/procedure/TestCopyToTableProcedure.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala index 8a5b940bdf25..57025ab0b6bc 100644 --- a/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala +++ b/hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestCopyToTableProcedure.scala @@ -338,7 +338,7 @@ class TestCopyToTableProcedure extends HoodieSparkSqlTestBase { | ) """.stripMargin) - // insert data to table + // insert 4 rows data to table spark.sql(s"insert into $tableName select 1, 'a1', 10, 1000") spark.sql(s"insert into $tableName select 2, 'a2', 20, 1500") spark.sql(s"insert into $tableName select 3, 'a3', 30, 2000")