leesf commented on a change in pull request #2761: URL: https://github.com/apache/hudi/pull/2761#discussion_r614779619
########## File path: hudi-spark-datasource/hudi-spark3-extensions_2.12/src/main/scala/org/apache/spark/sql/hudi/execution/CreateHudiTableCommand.scala ########## @@ -0,0 +1,326 @@ +/* + * 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.execution + +import java.util.Properties + +import org.apache.hadoop.fs.{FSDataInputStream, FSDataOutputStream, FileSystem, Path} +import org.apache.hudi.DataSourceWriteOptions +import org.apache.hudi.DataSourceWriteOptions._ +import org.apache.hudi.common.model.{HoodieFileFormat, HoodieRecord} +import org.apache.hudi.common.table.{HoodieTableConfig, HoodieTableMetaClient, TableSchemaResolver} +import org.apache.hudi.config.HoodieWriteConfig.KEYGENERATOR_CLASS_PROP +import org.apache.hudi.execution.HudiSQLUtils +import org.apache.hudi.hadoop.HoodieParquetInputFormat +import org.apache.hudi.hadoop.realtime.HoodieParquetRealtimeInputFormat +import org.apache.hudi.hadoop.utils.HoodieInputFormatUtils +import org.apache.spark.internal.Logging +import org.apache.spark.sql.avro.SchemaConverters +import org.apache.spark.sql.catalyst.analysis.{NoSuchDatabaseException, TableAlreadyExistsException} +import org.apache.spark.sql.{Row, SparkSession} +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTable, CatalogTableType} +import org.apache.spark.sql.execution.command.RunnableCommand +import org.apache.spark.sql.hive.SparkfunctionWrapper +import org.apache.spark.sql.internal.StaticSQLConf +import org.apache.spark.sql.types.{StringType, StructField, StructType} + +import scala.collection.JavaConverters._ + +/** + * Command for create hoodie table + */ +case class CreateHudiTableCommand(table: CatalogTable, ignoreIfExists: Boolean) extends RunnableCommand { + + override def run(sparkSession: SparkSession): Seq[Row] = { + assert(table.tableType != CatalogTableType.VIEW) + assert(table.provider.isDefined) + + val tableName = table.identifier.unquotedString + val sessionState = sparkSession.sessionState + val tableIsExists = sessionState.catalog.tableExists(table.identifier) + if (tableIsExists) { + if (ignoreIfExists) { + // scalastyle:off + return Seq.empty[Row] + // scalastyle:on + } else { + throw new IllegalArgumentException(s"Table ${table.identifier.quotedString} already exists") + } + } + + val enableHive = "hive" == sparkSession.sessionState.conf.getConf(StaticSQLConf.CATALOG_IMPLEMENTATION) + val path = HudiSQLUtils.getTablePath(sparkSession, table) + val conf = sparkSession.sessionState.newHadoopConf() + val isTableExists = HudiSQLUtils.tableExists(path, conf) + val (newSchema, tableOptions) = if (table.tableType == CatalogTableType.EXTERNAL && isTableExists) { + // if this is an external table & the table has already exists in the location, + // infer schema from the table meta + assert(table.schema.isEmpty, s"Should not specified table schema " + + s"for an exists hoodie table: ${table.identifier.unquotedString}") + // get Schema from the external table + val metaClient = HoodieTableMetaClient.builder().setBasePath(path).setConf(conf).build() + val schemaResolver = new TableSchemaResolver(metaClient) + val avroSchema = schemaResolver.getTableAvroSchema(true) + val tableSchema = SchemaConverters.toSqlType(avroSchema).dataType.asInstanceOf[StructType] + (tableSchema, table.storage.properties ++ metaClient.getTableConfig.getProps.asScala) + } else { + // Add the meta fields to the scheme if this is a managed table or an empty external table + val fullSchema: StructType = { + val metaFields = HoodieRecord.HOODIE_META_COLUMNS.asScala + val dataFields = table.schema.fields.filterNot(f => metaFields.contains(f.name)) + val fields = metaFields.map(StructField(_, StringType)) ++ dataFields + StructType(fields) + } + (fullSchema, table.storage.properties) + } + + // Append * to tablePath if create dataSourceV1 hudi table + val newPath = if (!enableHive) { + if (table.partitionColumnNames.nonEmpty) { + var tempPath: String = path + if (path.endsWith("/")) { + tempPath = path.substring(0, path.length - 1) + } + for (_ <- 0 until table.partitionColumnNames.size + 1) { + tempPath = s"$tempPath/*" + } + tempPath + } else { + path + } + } else { + path + } + + val tableType = table.storage.properties.getOrElse(DataSourceWriteOptions.TABLE_TYPE_OPT_KEY, "COPY_ON_WRITE") + tableType match { + case DataSourceWriteOptions.COW_TABLE_TYPE_OPT_VAL => + val newTable = buildNewCatalogTable(table.copy(schema = newSchema), newPath, tableOptions, enableHive) + if (enableHive) { + createHiveDataSourceTable(newTable, sparkSession) + } else { + sessionState.catalog.createTable(newTable, ignoreIfExists = false) + } + case DataSourceWriteOptions.MOR_TABLE_TYPE_OPT_VAL => + val newTable_rt = buildNewCatalogTable(table.copy(schema = newSchema), newPath, tableOptions, enableHive) + val newTable_ro = buildNewCatalogTable(table.copy(schema = newSchema), newPath, tableOptions, enableHive, true) + + // create ro and rt table + if (enableHive) { + createHiveDataSourceTable(newTable_ro, sparkSession) + createHiveDataSourceTable(newTable_rt, sparkSession) + } else { + val newTable = buildNewCatalogTable(table.copy(schema = newSchema), newPath, tableOptions, enableHive) + sessionState.catalog.createTable(newTable, ignoreIfExists = false) + } + case _ => throw new IllegalArgumentException(s"Unknow table Type: $tableType") + } + + CreateHudiTableCommand.initTableIfNeed(sparkSession, table) + // init first commit + HudiSQLUtils.initFirstCommit(sparkSession, newSchema, + HudiSQLUtils.buildDefaultParameter(tableOptions, enableHive), conf, path, table.identifier.table) + + Seq.empty[Row] + } + + /** + + build new CatalogTable, base on tableType Review comment: pls fix the docs. -- 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. For queries about this service, please contact Infrastructure at: [email protected]
