jonvex commented on code in PR #7230: URL: https://github.com/apache/hudi/pull/7230#discussion_r1029507420
########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/functional/TestSparkSql.scala: ########## @@ -0,0 +1,518 @@ +/* + * 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.hudi.functional + +import org.apache.hudi.common.config.HoodieMetadataConfig +import org.apache.hudi.{DataSourceReadOptions, HoodieDataSourceHelpers, HoodieSparkUtils} +import org.apache.hudi.common.fs.FSUtils +import org.apache.hudi.common.model.HoodieRecord +import org.apache.hudi.common.testutils.HoodieTestDataGenerator +import org.apache.spark.sql +import org.apache.spark.sql.{Dataset, Row} +import org.apache.spark.sql.hudi.HoodieSparkSqlTestBase +import org.junit.jupiter.api.Assertions.{assertEquals, assertTrue} +import org.apache.hudi.common.testutils.RawTripTestPayload.recordsToStrings +import org.scalatest.Inspectors.forAll + +import java.io.File +import scala.collection.JavaConversions._ + + +class TestSparkSql extends HoodieSparkSqlTestBase { + //params for core flow tests + val params: List[String] = List( + "COPY_ON_WRITE|false|false|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|BLOOM", + "COPY_ON_WRITE|true|false|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|BLOOM", + "COPY_ON_WRITE|true|true|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|BLOOM", + "COPY_ON_WRITE|false|false|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|SIMPLE", + "COPY_ON_WRITE|true|false|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|SIMPLE", + "COPY_ON_WRITE|true|true|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|SIMPLE", + "COPY_ON_WRITE|false|false|org.apache.hudi.keygen.NonpartitionedKeyGenerator|_row_key|GLOBAL_BLOOM", + "COPY_ON_WRITE|true|false|org.apache.hudi.keygen.NonpartitionedKeyGenerator|_row_key|GLOBAL_BLOOM", + "COPY_ON_WRITE|true|true|org.apache.hudi.keygen.NonpartitionedKeyGenerator|_row_key|GLOBAL_BLOOM", + "MERGE_ON_READ|false|false|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|BLOOM", + "MERGE_ON_READ|true|false|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|BLOOM", + "MERGE_ON_READ|true|true|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|BLOOM", + "MERGE_ON_READ|false|false|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|SIMPLE", + "MERGE_ON_READ|true|false|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|SIMPLE", + "MERGE_ON_READ|true|true|org.apache.hudi.keygen.SimpleKeyGenerator|_row_key|SIMPLE", + "MERGE_ON_READ|false|false|org.apache.hudi.keygen.NonpartitionedKeyGenerator|_row_key|GLOBAL_BLOOM", + "MERGE_ON_READ|true|false|org.apache.hudi.keygen.NonpartitionedKeyGenerator|_row_key|GLOBAL_BLOOM", + "MERGE_ON_READ|true|true|org.apache.hudi.keygen.NonpartitionedKeyGenerator|_row_key|GLOBAL_BLOOM" + ) + + //extracts the params and runs each core flow test + forAll (params) { (paramStr: String) => + test(s"Core flow with params: $paramStr") { + val splits = paramStr.split('|') + withTempDir { tmp => + testCoreFlows(tmp, + tableType = splits(0), + isMetadataEnabledOnWrite = splits(1).toBoolean, + isMetadataEnabledOnRead = splits(2).toBoolean, + keyGenClass = splits(3), + recordKeys = splits(4), + indexType = splits(5)) + } + } + } + + def testCoreFlows(tmp: File, tableType: String, isMetadataEnabledOnWrite: Boolean, isMetadataEnabledOnRead: Boolean, keyGenClass: String, recordKeys: String, indexType: String): Unit = { + val tableName = generateTableName + val basePath = tmp.getCanonicalPath + "/" + tableName + val writeOptions = getWriteOptions(tableName, tableType, isMetadataEnabledOnWrite, keyGenClass, + recordKeys, indexType) + val partitionedBy = if (!keyGenClass.equals("org.apache.hudi.keygen.NonpartitionedKeyGenerator")) { + "partitioned by (partition_path)" + } else { + "" + } + spark.sql( + s""" + | create table $tableName ( + | timestamp long, + | _row_key string, + | rider string, + | driver string, + | begin_lat double, + | begin_lon double, + | end_lat double, + | end_lon double, + | fare STRUCT< + | amount: double, + | currency: string >, + | _hoodie_is_deleted boolean, + | partition_path string + |) using hudi + | $partitionedBy + | $writeOptions + | location '$basePath' + | + """.stripMargin) + val cols = Seq("timestamp", "_row_key", "partition_path", "rider", "driver", "begin_lat", "begin_lon", "end_lat", "end_lon", "fare.amount", "fare.currency", "_hoodie_is_deleted") + val colsToSelect = getColsToSelect(cols) + val fs = FSUtils.getFs(basePath, spark.sparkContext.hadoopConfiguration) + + //Insert Operation + val dataGen = new HoodieTestDataGenerator(HoodieTestDataGenerator.TRIP_NESTED_EXAMPLE_SCHEMA, 0xDEED) + val (inputDf0, _, values0) = generateInserts(dataGen, "000", 100) + insertInto(tableName, values0, "bulk_insert", isMetadataEnabledOnWrite, keyGenClass) + assertTrue(HoodieDataSourceHelpers.hasNewCommits(fs, basePath, "000")) + + //Snapshot query + val snapshotDf1 = doRead(tableName, isMetadataEnabledOnRead) + snapshotDf1.cache() + assertEquals(100, snapshotDf1.count()) + + val (updateDf, _, values1) = generateUniqueUpdates(dataGen, "001", 50) + insertInto(tableName, values1, "upsert", isMetadataEnabledOnWrite, keyGenClass) + + val commitInstantTime2 = HoodieDataSourceHelpers.latestCommit(fs, basePath) + + val snapshotDf2 = doRead(tableName, isMetadataEnabledOnRead) + snapshotDf2.cache() + assertEquals(100, snapshotDf2.count()) + + compareUpdateDfWithHudiDf(updateDf, snapshotDf2, snapshotDf1, colsToSelect) + + val (inputDf2, _, values2) = generateUniqueUpdates(dataGen, "002", 60) + val uniqueKeyCnt2 = inputDf2.select("_row_key").distinct().count() + insertInto(tableName, values2, "upsert", isMetadataEnabledOnWrite, keyGenClass) + + val commitInstantTime3 = HoodieDataSourceHelpers.latestCommit(fs, basePath) + assertEquals(3, HoodieDataSourceHelpers.listCommitsSince(fs, basePath, "000").size()) + + // Snapshot Query + val snapshotDf3 = doRead(tableName, isMetadataEnabledOnRead) + snapshotDf3.cache() + assertEquals(100, snapshotDf3.count()) + + compareUpdateDfWithHudiDf(inputDf2, snapshotDf3, snapshotDf3, colsToSelect) + + // Read Incremental Query, need to use spark-ds because functionality does not exist for spark sql + // we have 2 commits, try pulling the first commit (which is not the latest) + val firstCommit = HoodieDataSourceHelpers.listCommitsSince(fs, basePath, "000").get(0) + val hoodieIncViewDf1 = spark.read.format("org.apache.hudi") + .option(DataSourceReadOptions.QUERY_TYPE.key, DataSourceReadOptions.QUERY_TYPE_INCREMENTAL_OPT_VAL) + .option(DataSourceReadOptions.BEGIN_INSTANTTIME.key, "000") + .option(DataSourceReadOptions.END_INSTANTTIME.key, firstCommit) + .load(basePath) + //val hoodieIncViewDf1 = doIncRead(tableName, isMetadataEnabledOnRead, "000", firstCommit) + assertEquals(100, hoodieIncViewDf1.count()) // 100 initial inserts must be pulled + var countsPerCommit = hoodieIncViewDf1.groupBy("_hoodie_commit_time").count().collect() + assertEquals(1, countsPerCommit.length) + assertEquals(firstCommit, countsPerCommit(0).get(0).toString) + + val (inputDf3, _, values3) = generateUniqueUpdates(dataGen, "003", 80) + val uniqueKeyCnt3 = inputDf3.select("_row_key").distinct().count() + insertInto(tableName, values3, "upsert", isMetadataEnabledOnWrite, keyGenClass) + + //another incremental query with commit2 and commit3 + val hoodieIncViewDf2 = spark.read.format("org.apache.hudi") Review Comment: I added jira number but I think having the spark ds query is still good for validation -- 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: commits-unsubscr...@hudi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org