Github user qiuchenjian commented on a diff in the pull request:

    https://github.com/apache/carbondata/pull/2990#discussion_r242010398
  
    --- Diff: 
integration/spark2/src/main/scala/org/apache/spark/sql/execution/command/schema/CarbonAlterTableColRenameDataTypeChangeCommand.scala
 ---
    @@ -0,0 +1,345 @@
    +/*
    + * 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.execution.command.schema
    +
    +import scala.collection.JavaConverters._
    +import scala.collection.mutable
    +import scala.collection.mutable.ArrayBuffer
    +
    +import org.apache.spark.sql.{CarbonEnv, Row, SparkSession}
    +import 
org.apache.spark.sql.execution.command.{AlterTableColRenameAndDataTypeChangeModel,
 DataTypeInfo, MetadataCommand}
    +import org.apache.spark.sql.hive.CarbonSessionCatalog
    +import org.apache.spark.util.AlterTableUtil
    +
    +import 
org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException
    +import org.apache.carbondata.common.logging.LogServiceFactory
    +import org.apache.carbondata.core.features.TableOperation
    +import org.apache.carbondata.core.locks.{ICarbonLock, LockUsage}
    +import 
org.apache.carbondata.core.metadata.converter.ThriftWrapperSchemaConverterImpl
    +import org.apache.carbondata.core.metadata.schema.table.CarbonTable
    +import org.apache.carbondata.core.metadata.schema.table.column.CarbonColumn
    +import 
org.apache.carbondata.events.{AlterTableColRenameAndDataTypeChangePostEvent, 
AlterTableColRenameAndDataTypeChangePreEvent, OperationContext, 
OperationListenerBus}
    +import org.apache.carbondata.format.{ColumnSchema, SchemaEvolutionEntry, 
TableInfo}
    +import org.apache.carbondata.spark.util.DataTypeConverterUtil
    +
    +private[sql] case class CarbonAlterTableColRenameDataTypeChangeCommand(
    +    alterTableColRenameAndDataTypeChangeModel: 
AlterTableColRenameAndDataTypeChangeModel)
    +  extends MetadataCommand {
    +
    +  override def processMetadata(sparkSession: SparkSession): Seq[Row] = {
    +    val LOGGER = 
LogServiceFactory.getLogService(this.getClass.getCanonicalName)
    +    val tableName = alterTableColRenameAndDataTypeChangeModel.tableName
    +    val dbName = alterTableColRenameAndDataTypeChangeModel.databaseName
    +      .getOrElse(sparkSession.catalog.currentDatabase)
    +    var isColumnRenameOnly = false
    +    var isDataTypeChangeOnly = false
    +    var isBothColRenameAndDataTypeChange = false
    +    setAuditTable(dbName, tableName)
    +    setAuditInfo(Map(
    +      "column" -> alterTableColRenameAndDataTypeChangeModel.columnName,
    +      "newColumn" -> 
alterTableColRenameAndDataTypeChangeModel.newColumnName,
    +      "newType" -> 
alterTableColRenameAndDataTypeChangeModel.dataTypeInfo.dataType))
    +    val locksToBeAcquired = List(LockUsage.METADATA_LOCK, 
LockUsage.COMPACTION_LOCK)
    +    var locks = List.empty[ICarbonLock]
    +    // get the latest carbon table and check for column existence
    +    var carbonTable: CarbonTable = null
    +    var timeStamp = 0L
    +    try {
    +      locks = AlterTableUtil
    +        .validateTableAndAcquireLock(dbName, tableName, 
locksToBeAcquired)(sparkSession)
    +      val metastore = CarbonEnv.getInstance(sparkSession).carbonMetastore
    +      carbonTable = CarbonEnv.getCarbonTable(Some(dbName), 
tableName)(sparkSession)
    +      if (!carbonTable.canAllow(carbonTable, 
TableOperation.ALTER_COL_RENAME_AND_CHANGE_DATATYPE,
    +        alterTableColRenameAndDataTypeChangeModel.columnName)) {
    +        throw new MalformedCarbonCommandException(
    +          "alter table change datatype or column rename is not supported 
for index datamap")
    +      }
    +      val operationContext = new OperationContext
    +      val alterTableColRenameAndDataTypeChangePreEvent =
    +        AlterTableColRenameAndDataTypeChangePreEvent(sparkSession, 
carbonTable,
    +          alterTableColRenameAndDataTypeChangeModel)
    +      OperationListenerBus.getInstance()
    +        .fireEvent(alterTableColRenameAndDataTypeChangePreEvent, 
operationContext)
    +      val newColumnName = 
alterTableColRenameAndDataTypeChangeModel.newColumnName
    +      val oldColumnName = 
alterTableColRenameAndDataTypeChangeModel.columnName
    +      val carbonColumns = 
carbonTable.getCreateOrderColumn(tableName).asScala.filter(!_.isInvisible)
    +      if 
(!carbonColumns.exists(_.getColName.equalsIgnoreCase(oldColumnName))) {
    +        throwMetadataException(dbName, tableName, s"Column does not exist: 
$oldColumnName")
    +      }
    +
    +      val carbonColumn = 
carbonColumns.filter(_.getColName.equalsIgnoreCase(oldColumnName))
    +      if (carbonColumn.size != 1) {
    +        throwMetadataException(dbName, tableName, s"Invalid Column: 
$oldColumnName")
    +      }
    +      if (alterTableColRenameAndDataTypeChangeModel.isColumnRename) {
    +        // check whether new column name is already an existing column name
    +        if 
(carbonColumns.exists(_.getColName.equalsIgnoreCase(newColumnName))) {
    +          throw new MalformedCarbonCommandException(s"Column Rename 
Operation failed. New " +
    +                                                    s"column name 
$newColumnName already exists" +
    +                                                    s" in table 
$tableName")
    +        }
    +
    +        // if the datatype is source datatype, then it is just a column 
rename operation, else do
    +        // the datatype validation for not source datatype
    +        if (carbonColumn.head.getDataType.getName
    +          
.equalsIgnoreCase(alterTableColRenameAndDataTypeChangeModel.dataTypeInfo.dataType))
 {
    +          isColumnRenameOnly = true
    +        } else {
    +          isBothColRenameAndDataTypeChange = true
    +        }
    +
    +        // if column rename operation is on partition column, then fail 
the rename operation
    +        if (null != carbonTable.getPartitionInfo) {
    +          val partitionColumns = 
carbonTable.getPartitionInfo.getColumnSchemaList
    +          partitionColumns.asScala.foreach {
    +            col =>
    +              if (col.getColumnName.equalsIgnoreCase(oldColumnName)) {
    +                throw new MalformedCarbonCommandException(
    +                  s"Column Rename Operation failed. Renaming " +
    +                  s"the partition column $newColumnName is not " +
    +                  s"allowed")
    +              }
    +          }
    +        }
    +      } else {
    +        isDataTypeChangeOnly = true
    --- End diff --
    
    Why isDataTypeChangeOnly is true, when it is not partition table


---

Reply via email to