QiangCai commented on a change in pull request #3608: [CARBONDATA-3680][alpha-feature]Support Secondary Index feature on carbon table. URL: https://github.com/apache/carbondata/pull/3608#discussion_r378771821
########## File path: integration/spark2/src/main/scala/org/apache/spark/sql/secondaryindex/command/SICreationCommand.scala ########## @@ -0,0 +1,574 @@ +/* + * 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.secondaryindex.command + +import java.io.IOException +import java.util +import java.util.UUID + +import scala.collection.JavaConverters._ +import scala.language.implicitConversions + +import org.apache.log4j.Logger +import org.apache.spark.sql._ +import org.apache.spark.sql.catalyst.TableIdentifier +import org.apache.spark.sql.catalyst.parser.ParseException +import org.apache.spark.sql.execution.command.DataCommand +import org.apache.spark.sql.hive.{CarbonHiveMetadataUtil, CarbonRelation} +import org.apache.spark.sql.secondaryindex.exception.IndexTableExistException +import org.apache.spark.sql.secondaryindex.load.CarbonInternalLoaderUtil +import org.apache.spark.sql.secondaryindex.util.{CarbonInternalScalaUtil, IndexTableUtil} + +import org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException +import org.apache.carbondata.common.logging.LogServiceFactory +import org.apache.carbondata.core.constants.CarbonCommonConstants +import org.apache.carbondata.core.datastore.impl.FileFactory +import org.apache.carbondata.core.locks.{CarbonLockFactory, CarbonLockUtil, ICarbonLock, LockUsage} +import org.apache.carbondata.core.metadata.AbsoluteTableIdentifier +import org.apache.carbondata.core.metadata.datatype.{DataType, DataTypes} +import org.apache.carbondata.core.metadata.encoder.Encoding +import org.apache.carbondata.core.metadata.schema.{SchemaEvolution, SchemaEvolutionEntry, SchemaReader} +import org.apache.carbondata.core.metadata.schema.indextable.{IndexMetadata, IndexTableInfo} +import org.apache.carbondata.core.metadata.schema.table.{CarbonTable, TableInfo, TableSchema} +import org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema +import org.apache.carbondata.core.service.impl.ColumnUniqueIdGenerator +import org.apache.carbondata.core.util.{CarbonProperties, CarbonUtil} +import org.apache.carbondata.core.util.path.CarbonTablePath +import org.apache.carbondata.events.{CreateTablePostExecutionEvent, CreateTablePreExecutionEvent, OperationContext, OperationListenerBus} + +class ErrorMessage(message: String) extends Exception(message) { +} + + /** + * Command for index table creation + * @param indexModel SecondaryIndex model holding the index infomation + * @param tableProperties SI table properties + * @param isCreateSIndex if false then will not create index table schema in the carbonstore + * and will avoid dataload for SI creation. + */ + private[sql] case class CreateIndexTable(indexModel: SecondaryIndex, + tableProperties: scala.collection.mutable.Map[String, String], + var isCreateSIndex: Boolean = true) + extends DataCommand { + + val LOGGER: Logger = LogServiceFactory.getLogService(this.getClass.getCanonicalName) + + override def processData(sparkSession: SparkSession): Seq[Row] = { + val databaseName = CarbonEnv.getDatabaseName(indexModel.databaseName)(sparkSession) + indexModel.databaseName = Some(databaseName) + val tableName = indexModel.tableName + val storePath = CarbonProperties.getStorePath + val dbLocation = CarbonEnv.getDatabaseLocation(databaseName, sparkSession) + val indexTableName = indexModel.indexTableName + + val tablePath = dbLocation + CarbonCommonConstants.FILE_SEPARATOR + indexTableName + setAuditTable(databaseName, indexTableName) + setAuditInfo(Map("Column names" -> indexModel.columnNames.toString(), + "Parent TableName" -> indexModel.tableName, + "SI Table Properties" -> tableProperties.toString())) + LOGGER.info( + s"Creating Index with Database name [$databaseName] and Index name [$indexTableName]") + val catalog = CarbonEnv.getInstance(sparkSession).carbonMetaStore + val identifier = TableIdentifier(tableName, indexModel.databaseName) + var carbonTable: CarbonTable = null + var locks: List[ICarbonLock] = List() + var oldIndexInfo = "" + + try { + carbonTable = CarbonEnv.getCarbonTable(indexModel.databaseName, tableName)(sparkSession) + if (carbonTable == null) { + throw new ErrorMessage(s"Parent Table $databaseName.$tableName is not found") + } + + if (carbonTable != null && + (carbonTable.isFileLevelFormat || !carbonTable.getTableInfo.isTransactionalTable)) { + throw new MalformedCarbonCommandException( + "Unsupported operation on non transactional table") + } + + if (carbonTable.isStreamingSink) { + throw new ErrorMessage( + s"Parent Table ${ carbonTable.getDatabaseName }." + + s"${ carbonTable.getTableName }" + + s" is Streaming Table and Secondary index on Streaming table is not supported ") + } + + if (carbonTable.isHivePartitionTable) { + throw new ErrorMessage( + s"Parent Table ${ carbonTable.getDatabaseName }." + + s"${ carbonTable.getTableName }" + + s" is Partition Table and Secondary index on Partition table is not supported ") Review comment: is it easy to support it in the future? ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services