You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by mg...@apache.org on 2020/03/06 06:24:21 UTC
[hive] branch master updated: HIVE-22907 Break up
DDLSemanticAnalyzer - extract the rest of the Alter Table analyzers (Miklos
Gergely, reviewed by Jesus Camacho Rodriguez)
This is an automated email from the ASF dual-hosted git repository.
mgergely pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git
The following commit(s) were added to refs/heads/master by this push:
new 3bed626 HIVE-22907 Break up DDLSemanticAnalyzer - extract the rest of the Alter Table analyzers (Miklos Gergely, reviewed by Jesus Camacho Rodriguez)
3bed626 is described below
commit 3bed626d5b6a7bab3659bb0422c67b4168935ee6
Author: miklosgergely <mg...@cloudera.com>
AuthorDate: Tue Feb 18 19:43:47 2020 +0100
HIVE-22907 Break up DDLSemanticAnalyzer - extract the rest of the Alter Table analyzers (Miklos Gergely, reviewed by Jesus Camacho Rodriguez)
---
.../ql/ddl/table/AbstractAlterTableAnalyzer.java | 136 +---
.../ql/ddl/table/AbstractAlterTableOperation.java | 4 +-
...er.java => AbstractBaseAlterTableAnalyzer.java} | 35 +-
.../drop/AlterTableDropConstraintOperation.java | 4 +-
.../hive/ql/ddl/table/drop/DropTableOperation.java | 2 +-
.../AlterTableUpdateColumnStatistictAnalyzer.java | 92 +++
.../misc/owner/AlterTableSetOwnerAnalyzer.java | 60 ++
.../misc/{ => owner}/AlterTableSetOwnerDesc.java | 2 +-
.../{ => owner}/AlterTableSetOwnerOperation.java | 2 +-
.../misc/{ => preinsert}/PreInsertTableDesc.java | 2 +-
.../{ => preinsert}/PreInsertTableOperation.java | 2 +-
.../AbstractAlterTablePropertiesAnalyzer.java | 146 ++++
.../AbstractAlterTableSetPropertiesAnalyzer.java | 50 ++
.../AbstractAlterTableUnsetPropertiesAnalyzer.java | 61 ++
.../AlterTableSetPropertiesAnalyzer.java} | 33 +-
.../AlterTableSetPropertiesDesc.java | 2 +-
.../AlterTableSetPropertiesOperation.java | 2 +-
.../AlterTableUnsetPropertiesAnalyzer.java} | 32 +-
.../AlterTableUnsetPropertiesDesc.java | 2 +-
.../AlterTableUnsetPropertiesOperation.java | 2 +-
.../rename/AbstractAlterTableRenameAnalyzer.java | 56 ++
.../AlterTableRenameAnalyzer.java} | 32 +-
.../misc/{ => rename}/AlterTableRenameDesc.java | 2 +-
.../{ => rename}/AlterTableRenameOperation.java | 8 +-
.../table/misc/touch/AlterTableTouchAnalyzer.java | 72 ++
.../misc/{ => touch}/AlterTableTouchDesc.java | 2 +-
.../misc/{ => touch}/AlterTableTouchOperation.java | 2 +-
.../table/misc/truncate/TruncateTableAnalyzer.java | 326 ++++++++
.../misc/{ => truncate}/TruncateTableDesc.java | 2 +-
.../{ => truncate}/TruncateTableOperation.java | 2 +-
.../drop/AlterTableDropPartitionOperation.java | 2 +-
.../archive/AlterTableArchiveOperation.java | 6 +-
.../archive/AlterTableUnarchiveOperation.java | 6 +-
.../compact/AlterTableCompactOperation.java | 1 -
.../hive/ql/ddl/view/drop/DropViewOperation.java | 2 +-
.../AlterMaterializedViewRewriteAnalyzer.java | 4 +-
.../drop/DropMaterializedViewOperation.java | 2 +-
.../AlterViewSetPropertiesAnalyzer.java} | 33 +-
.../AlterViewUnsetPropertiesAnalyzer.java} | 33 +-
.../rename/AlterViewRenameAnalyzer.java} | 33 +-
.../incremental/IncrementalLoadTasksBuilder.java | 2 +-
.../hadoop/hive/ql/exec/repl/util/ReplUtils.java | 2 +-
.../hive/ql/parse/AcidExportSemanticAnalyzer.java | 4 +-
.../hadoop/hive/ql/parse/BaseSemanticAnalyzer.java | 63 +-
.../hadoop/hive/ql/parse/DDLSemanticAnalyzer.java | 818 ---------------------
.../hadoop/hive/ql/parse/SemanticAnalyzer.java | 6 +-
.../hive/ql/parse/SemanticAnalyzerFactory.java | 19 -
.../HiveAuthorizationTaskFactoryImpl.java | 3 +-
.../repl/load/message/RenameTableHandler.java | 2 +-
.../load/message/TruncatePartitionHandler.java | 2 +-
.../repl/load/message/TruncateTableHandler.java | 2 +-
.../parse/authorization/AuthorizationTestUtil.java | 4 -
...bleprops_external_with_default_constraint.q.out | 2 +-
...bleprops_external_with_notnull_constraint.q.out | 2 +-
54 files changed, 1056 insertions(+), 1172 deletions(-)
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableAnalyzer.java
index 0acd501..6a28ef0 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableAnalyzer.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableAnalyzer.java
@@ -21,31 +21,17 @@ package org.apache.hadoop.hive.ql.ddl.table;
import java.util.Map;
import org.apache.hadoop.hive.common.TableName;
-import org.apache.hadoop.hive.conf.HiveConf;
-import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants;
import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
-import org.apache.hadoop.hive.ql.ErrorMsg;
import org.apache.hadoop.hive.ql.QueryState;
-import org.apache.hadoop.hive.ql.ddl.DDLDesc.DDLDescWithWriteId;
-import org.apache.hadoop.hive.ql.ddl.table.partition.PartitionUtils;
-import org.apache.hadoop.hive.ql.hooks.ReadEntity;
-import org.apache.hadoop.hive.ql.hooks.WriteEntity;
-import org.apache.hadoop.hive.ql.hooks.WriteEntity.WriteType;
-import org.apache.hadoop.hive.ql.io.AcidUtils;
-import org.apache.hadoop.hive.ql.metadata.Partition;
-import org.apache.hadoop.hive.ql.metadata.Table;
import org.apache.hadoop.hive.ql.parse.ASTNode;
-import org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer;
import org.apache.hadoop.hive.ql.parse.HiveParser;
import org.apache.hadoop.hive.ql.parse.SemanticException;
/**
- * Abstract ancestor of all Alter Table analyzer. The alter table commands have this structure:
+ * Abstract ancestor of all Alter Table analyzer, that have this structure:
* tableName command partitionSpec?
*/
-public abstract class AbstractAlterTableAnalyzer extends BaseSemanticAnalyzer {
- // Equivalent to acidSinks, but for DDL operations that change data.
- private DDLDescWithWriteId ddlDescWithWriteId;
+public abstract class AbstractAlterTableAnalyzer extends AbstractBaseAlterTableAnalyzer {
public AbstractAlterTableAnalyzer(QueryState queryState) throws SemanticException {
super(queryState);
@@ -75,122 +61,4 @@ public abstract class AbstractAlterTableAnalyzer extends BaseSemanticAnalyzer {
protected abstract void analyzeCommand(TableName tableName, Map<String, String> partitionSpec, ASTNode command)
throws SemanticException;
-
- protected void setAcidDdlDesc(DDLDescWithWriteId descWithWriteId) {
- if(this.ddlDescWithWriteId != null) {
- throw new IllegalStateException("ddlDescWithWriteId is already set: " + this.ddlDescWithWriteId);
- }
- this.ddlDescWithWriteId = descWithWriteId;
- }
-
- @Override
- public DDLDescWithWriteId getAcidDdlDesc() {
- return ddlDescWithWriteId;
- }
-
- protected void addInputsOutputsAlterTable(TableName tableName, Map<String, String> partitionSpec,
- AbstractAlterTableDesc desc, AlterTableType op, boolean doForceExclusive) throws SemanticException {
- boolean isCascade = desc != null && desc.isCascade();
- boolean alterPartitions = partitionSpec != null && !partitionSpec.isEmpty();
- //cascade only occurs at table level then cascade to partition level
- if (isCascade && alterPartitions) {
- throw new SemanticException(ErrorMsg.ALTER_TABLE_PARTITION_CASCADE_NOT_SUPPORTED, op.getName());
- }
-
- Table table = getTable(tableName, true);
- // cascade only occurs with partitioned table
- if (isCascade && !table.isPartitioned()) {
- throw new SemanticException(ErrorMsg.ALTER_TABLE_NON_PARTITIONED_TABLE_CASCADE_NOT_SUPPORTED);
- }
-
- // Determine the lock type to acquire
- WriteEntity.WriteType writeType = doForceExclusive ?
- WriteType.DDL_EXCLUSIVE : determineAlterTableWriteType(table, desc, op);
-
- if (!alterPartitions) {
- inputs.add(new ReadEntity(table));
- WriteEntity alterTableOutput = new WriteEntity(table, writeType);
- outputs.add(alterTableOutput);
- //do not need the lock for partitions since they are covered by the table lock
- if (isCascade) {
- for (Partition part : PartitionUtils.getPartitions(db, table, partitionSpec, false)) {
- outputs.add(new WriteEntity(part, WriteEntity.WriteType.DDL_NO_LOCK));
- }
- }
- } else {
- ReadEntity re = new ReadEntity(table);
- // In the case of altering a table for its partitions we don't need to lock the table
- // itself, just the partitions. But the table will have a ReadEntity. So mark that
- // ReadEntity as no lock.
- re.noLockNeeded();
- inputs.add(re);
-
- if (AlterTableUtils.isFullPartitionSpec(table, partitionSpec)) {
- // Fully specified partition spec
- Partition part = PartitionUtils.getPartition(db, table, partitionSpec, true);
- outputs.add(new WriteEntity(part, writeType));
- } else {
- // Partial partition spec supplied. Make sure this is allowed.
- if (!AlterTableType.SUPPORT_PARTIAL_PARTITION_SPEC.contains(op)) {
- throw new SemanticException(
- ErrorMsg.ALTER_TABLE_TYPE_PARTIAL_PARTITION_SPEC_NO_SUPPORTED, op.getName());
- } else if (!conf.getBoolVar(HiveConf.ConfVars.DYNAMICPARTITIONING)) {
- throw new SemanticException(ErrorMsg.DYNAMIC_PARTITION_DISABLED);
- }
-
- for (Partition part : PartitionUtils.getPartitions(db, table, partitionSpec, true)) {
- outputs.add(new WriteEntity(part, writeType));
- }
- }
- }
-
- if (desc != null) {
- validateAlterTableType(table, op, desc.expectView());
- }
- }
-
- // For the time while all the alter table operations are getting migrated there is a duplication of this method here
- private WriteType determineAlterTableWriteType(Table table, AbstractAlterTableDesc desc, AlterTableType op) {
- boolean convertingToAcid = false;
- if (desc != null && desc.getProps() != null &&
- Boolean.parseBoolean(desc.getProps().get(hive_metastoreConstants.TABLE_IS_TRANSACTIONAL))) {
- convertingToAcid = true;
- }
- if (!AcidUtils.isTransactionalTable(table) && convertingToAcid) {
- // non-acid to transactional conversion (property itself) must be mutexed to prevent concurrent writes.
- // See HIVE-16688 for use cases.
- return WriteType.DDL_EXCLUSIVE;
- }
- return WriteEntity.determineAlterTableWriteType(op);
- }
-
- protected void validateAlterTableType(Table table, AlterTableType op, boolean expectView)
- throws SemanticException {
- if (table.isView()) {
- if (!expectView) {
- throw new SemanticException(ErrorMsg.ALTER_COMMAND_FOR_VIEWS.getMsg());
- }
-
- switch (op) {
- case ADDPARTITION:
- case DROPPARTITION:
- case RENAMEPARTITION:
- case ADDPROPS:
- case DROPPROPS:
- case RENAME:
- // allow this form
- break;
- default:
- throw new SemanticException(ErrorMsg.ALTER_VIEW_DISALLOWED_OP.getMsg(op.toString()));
- }
- } else {
- if (expectView) {
- throw new SemanticException(ErrorMsg.ALTER_COMMAND_FOR_TABLES.getMsg());
- }
- }
- if (table.isNonNative() && !AlterTableType.NON_NATIVE_TABLE_ALLOWED.contains(op)) {
- throw new SemanticException(ErrorMsg.ALTER_TABLE_NON_NATIVE.format(
- AlterTableType.NON_NATIVE_TABLE_ALLOWED.toString(), table.getTableName()));
- }
- }
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableOperation.java
index 323cdc4..2ee66e5 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableOperation.java
@@ -98,7 +98,7 @@ public abstract class AbstractAlterTableOperation<T extends AbstractAlterTableDe
}
partitions.add(part);
} else {
- // DDLSemanticAnalyzer has already checked if partial partition specs are allowed,
+ // AbstractBaseAlterTableAnalyzer has already checked if partial partition specs are allowed,
// thus we should not need to check it here.
partitions = context.getDb().getPartitions(tbl, partSpec);
}
@@ -178,7 +178,7 @@ public abstract class AbstractAlterTableOperation<T extends AbstractAlterTableDe
// This is kind of hacky - the read entity contains the old table, whereas the write entity contains the new
// table. This is needed for rename - both the old and the new table names are passed
- // Don't acquire locks for any of these, we have already asked for them in DDLSemanticAnalyzer.
+ // Don't acquire locks for any of these, we have already asked for them in AbstractBaseAlterTableAnalyzer.
if (partitions != null) {
for (Partition partition : partitions) {
context.getWork().getInputs().add(new ReadEntity(partition));
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractBaseAlterTableAnalyzer.java
similarity index 81%
copy from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableAnalyzer.java
copy to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractBaseAlterTableAnalyzer.java
index 0acd501..dcf4842 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractAlterTableAnalyzer.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/AbstractBaseAlterTableAnalyzer.java
@@ -23,7 +23,6 @@ import java.util.Map;
import org.apache.hadoop.hive.common.TableName;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants;
-import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
import org.apache.hadoop.hive.ql.ErrorMsg;
import org.apache.hadoop.hive.ql.QueryState;
import org.apache.hadoop.hive.ql.ddl.DDLDesc.DDLDescWithWriteId;
@@ -34,48 +33,20 @@ import org.apache.hadoop.hive.ql.hooks.WriteEntity.WriteType;
import org.apache.hadoop.hive.ql.io.AcidUtils;
import org.apache.hadoop.hive.ql.metadata.Partition;
import org.apache.hadoop.hive.ql.metadata.Table;
-import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer;
-import org.apache.hadoop.hive.ql.parse.HiveParser;
import org.apache.hadoop.hive.ql.parse.SemanticException;
/**
- * Abstract ancestor of all Alter Table analyzer. The alter table commands have this structure:
- * tableName command partitionSpec?
+ * Abstract ancestor of all Alter Table analyzer.
*/
-public abstract class AbstractAlterTableAnalyzer extends BaseSemanticAnalyzer {
+public abstract class AbstractBaseAlterTableAnalyzer extends BaseSemanticAnalyzer {
// Equivalent to acidSinks, but for DDL operations that change data.
private DDLDescWithWriteId ddlDescWithWriteId;
- public AbstractAlterTableAnalyzer(QueryState queryState) throws SemanticException {
+ public AbstractBaseAlterTableAnalyzer(QueryState queryState) throws SemanticException {
super(queryState);
}
- @Override
- public void analyzeInternal(ASTNode root) throws SemanticException {
- TableName tableName = getQualifiedTableName((ASTNode) root.getChild(0), MetaStoreUtils.getDefaultCatalog(conf));
-
- ASTNode command = (ASTNode)root.getChild(1);
-
- Map<String, String> partitionSpec = null;
- ASTNode partitionSpecNode = (ASTNode)root.getChild(2);
- if (partitionSpecNode != null) {
- // We can use alter table partition rename to convert/normalize the legacy partition
- // column values. In so, we should not enable the validation to the old partition spec
- // passed in this command.
- if (command.getType() == HiveParser.TOK_ALTERTABLE_RENAMEPART) {
- partitionSpec = getPartSpec(partitionSpecNode);
- } else {
- partitionSpec = getValidatedPartSpec(getTable(tableName), partitionSpecNode, conf, false);
- }
- }
-
- analyzeCommand(tableName, partitionSpec, command);
- }
-
- protected abstract void analyzeCommand(TableName tableName, Map<String, String> partitionSpec, ASTNode command)
- throws SemanticException;
-
protected void setAcidDdlDesc(DDLDescWithWriteId descWithWriteId) {
if(this.ddlDescWithWriteId != null) {
throw new IllegalStateException("ddlDescWithWriteId is already set: " + this.ddlDescWithWriteId);
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/constraint/drop/AlterTableDropConstraintOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/constraint/drop/AlterTableDropConstraintOperation.java
index 49d58d3..32e3334 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/constraint/drop/AlterTableDropConstraintOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/constraint/drop/AlterTableDropConstraintOperation.java
@@ -43,8 +43,8 @@ public class AlterTableDropConstraintOperation extends DDLOperation<AlterTableDr
}
try {
- context.getDb().dropConstraint(desc.getTableName().getDb(),
- desc.getTableName().getTable(), desc.getConstraintName());
+ context.getDb().dropConstraint(desc.getTableName().getDb(), desc.getTableName().getTable(),
+ desc.getConstraintName());
} catch (NoSuchObjectException e) {
throw new HiveException(e);
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/drop/DropTableOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/drop/DropTableOperation.java
index f4d1a35..17458cf 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/drop/DropTableOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/drop/DropTableOperation.java
@@ -44,7 +44,7 @@ public class DropTableOperation extends DDLOperation<DropTableDesc> {
public int execute() throws HiveException {
Table table = getTable();
if (table == null) {
- return 0; // dropping not existing table is handled by DDLSemanticAnalyzer
+ return 0; // dropping not existing table is handled by DropTableAnalyzer
}
if (desc.getValidationRequired()) {
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/columnstats/AlterTableUpdateColumnStatistictAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/columnstats/AlterTableUpdateColumnStatistictAnalyzer.java
new file mode 100644
index 0000000..0ae0a1a
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/columnstats/AlterTableUpdateColumnStatistictAnalyzer.java
@@ -0,0 +1,92 @@
+/*
+ * 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.hadoop.hive.ql.ddl.table.misc.columnstats;
+
+import java.util.Map;
+
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.metastore.Warehouse;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableAnalyzer;
+import org.apache.hadoop.hive.ql.ddl.table.AlterTableType;
+import org.apache.hadoop.hive.ql.exec.ColumnStatsUpdateTask;
+import org.apache.hadoop.hive.ql.exec.TaskFactory;
+import org.apache.hadoop.hive.ql.io.AcidUtils;
+import org.apache.hadoop.hive.ql.metadata.Table;
+import org.apache.hadoop.hive.ql.parse.ASTNode;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+import org.apache.hadoop.hive.ql.plan.ColumnStatsUpdateWork;
+
+/**
+ * Analyzer for update column statistics commands.
+ */
+@DDLType(types = {HiveParser.TOK_ALTERTABLE_UPDATECOLSTATS, HiveParser.TOK_ALTERPARTITION_UPDATECOLSTATS})
+public class AlterTableUpdateColumnStatistictAnalyzer extends AbstractAlterTableAnalyzer {
+ public AlterTableUpdateColumnStatistictAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
+ }
+
+ @Override
+ protected void analyzeCommand(TableName tableName, Map<String, String> partitionSpec, ASTNode command)
+ throws SemanticException {
+ Table table = getTable(tableName);
+ String columnName = getUnescapedName((ASTNode) command.getChild(0));
+ Map<String, String> properties = getProps((ASTNode) (command.getChild(1)).getChild(0));
+
+ String partitionName = getPartitionName(partitionSpec);
+ String columnType = getColumnType(table, columnName);
+
+ ColumnStatsUpdateWork work = new ColumnStatsUpdateWork(partitionName, properties, table.getDbName(),
+ table.getTableName(), columnName, columnType);
+ ColumnStatsUpdateTask task = (ColumnStatsUpdateTask) TaskFactory.get(work);
+ // TODO: doesn't look like this path is actually ever exercised. Maybe this needs to be removed.
+ addInputsOutputsAlterTable(tableName, partitionSpec, null, AlterTableType.UPDATESTATS, false);
+ if (AcidUtils.isTransactionalTable(table)) {
+ setAcidDdlDesc(work);
+ }
+
+ rootTasks.add(task);
+ }
+
+ private String getPartitionName(Map<String, String> partitionSpec) throws SemanticException {
+ String partitionName = null;
+ if (partitionSpec != null) {
+ try {
+ partitionName = Warehouse.makePartName(partitionSpec, false);
+ } catch (MetaException e) {
+ throw new SemanticException("partition " + partitionSpec.toString() + " not found");
+ }
+ }
+ return partitionName;
+ }
+
+ private String getColumnType(Table table, String columnName) throws SemanticException {
+ for (FieldSchema column : table.getCols()) {
+ if (columnName.equalsIgnoreCase(column.getName())) {
+ return column.getType();
+ }
+ }
+
+ throw new SemanticException("column type not found");
+ }
+}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/owner/AlterTableSetOwnerAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/owner/AlterTableSetOwnerAnalyzer.java
new file mode 100644
index 0000000..2da0ee1
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/owner/AlterTableSetOwnerAnalyzer.java
@@ -0,0 +1,60 @@
+/*
+ * 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.hadoop.hive.ql.ddl.table.misc.owner;
+
+import java.util.Map;
+
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLWork;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.ddl.privilege.PrincipalDesc;
+import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableAnalyzer;
+import org.apache.hadoop.hive.ql.exec.TaskFactory;
+import org.apache.hadoop.hive.ql.parse.ASTNode;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+import org.apache.hadoop.hive.ql.parse.authorization.AuthorizationParseUtils;
+
+/**
+ * Analyzer for set owner commands.
+ */
+@DDLType(types = HiveParser.TOK_ALTERTABLE_OWNER)
+public class AlterTableSetOwnerAnalyzer extends AbstractAlterTableAnalyzer {
+ public AlterTableSetOwnerAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
+ }
+
+ @Override
+ protected void analyzeCommand(TableName tableName, Map<String, String> partitionSpecFromFramework, ASTNode command)
+ throws SemanticException {
+ PrincipalDesc ownerPrincipal = AuthorizationParseUtils.getPrincipalDesc((ASTNode) command.getChild(0));
+
+ if (ownerPrincipal.getType() == null) {
+ throw new SemanticException("Owner type can't be null in alter table set owner command");
+ }
+
+ if (ownerPrincipal.getName() == null) {
+ throw new SemanticException("Owner name can't be null in alter table set owner command");
+ }
+
+ AlterTableSetOwnerDesc desc = new AlterTableSetOwnerDesc(tableName, ownerPrincipal);
+ rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(), desc), conf));
+ }
+}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetOwnerDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/owner/AlterTableSetOwnerDesc.java
similarity index 97%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetOwnerDesc.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/owner/AlterTableSetOwnerDesc.java
index 1fb11ce..2eb8f99 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetOwnerDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/owner/AlterTableSetOwnerDesc.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.owner;
import org.apache.hadoop.hive.common.TableName;
import org.apache.hadoop.hive.ql.ddl.privilege.PrincipalDesc;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetOwnerOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/owner/AlterTableSetOwnerOperation.java
similarity index 96%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetOwnerOperation.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/owner/AlterTableSetOwnerOperation.java
index 02b9b91..3f165c6 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetOwnerOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/owner/AlterTableSetOwnerOperation.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.owner;
import org.apache.hadoop.hive.ql.ddl.DDLOperationContext;
import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableOperation;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/preinsert/PreInsertTableDesc.java
similarity index 96%
copy from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
copy to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/preinsert/PreInsertTableDesc.java
index bc37880..1cb6cc6 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/preinsert/PreInsertTableDesc.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.preinsert;
import org.apache.hadoop.hive.ql.ddl.DDLDesc;
import org.apache.hadoop.hive.ql.metadata.Table;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/preinsert/PreInsertTableOperation.java
similarity index 96%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableOperation.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/preinsert/PreInsertTableOperation.java
index 928ace9..45e1dd9 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/preinsert/PreInsertTableOperation.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.preinsert;
import org.apache.hadoop.hive.ql.ddl.DDLOperationContext;
import org.apache.hadoop.hive.metastore.DefaultHiveMetaHook;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AbstractAlterTablePropertiesAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AbstractAlterTablePropertiesAnalyzer.java
new file mode 100644
index 0000000..3df6367
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AbstractAlterTablePropertiesAnalyzer.java
@@ -0,0 +1,146 @@
+/*
+ * 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.hadoop.hive.ql.ddl.table.misc.properties;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.hadoop.hive.common.StatsSetupConst;
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
+import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants;
+import org.apache.hadoop.hive.ql.ErrorMsg;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLWork;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableAnalyzer;
+import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableDesc;
+import org.apache.hadoop.hive.ql.exec.TaskFactory;
+import org.apache.hadoop.hive.ql.io.AcidUtils;
+import org.apache.hadoop.hive.ql.metadata.DefaultConstraint;
+import org.apache.hadoop.hive.ql.metadata.Hive;
+import org.apache.hadoop.hive.ql.metadata.NotNullConstraint;
+import org.apache.hadoop.hive.ql.parse.ASTNode;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+import org.apache.hadoop.hive.ql.plan.HiveOperation;
+
+/**
+ * Analyzer for setting/unsetting the properties of table like entities commands.
+ */
+@DDLType(types = HiveParser.TOK_ALTERTABLE_PROPERTIES)
+public abstract class AbstractAlterTablePropertiesAnalyzer extends AbstractAlterTableAnalyzer {
+ public AbstractAlterTablePropertiesAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
+ }
+
+ @Override
+ protected void analyzeCommand(TableName tableName, Map<String, String> partitionSpec, ASTNode command)
+ throws SemanticException {
+ Map<String, String> properties = getProps((ASTNode) (command.getChild(0)).getChild(0));
+
+ boolean updateStats = validate(tableName, properties);
+
+ EnvironmentContext environmentContext = null;
+ if (updateStats) {
+ environmentContext = new EnvironmentContext();
+ environmentContext.putToProperties(StatsSetupConst.STATS_GENERATED, StatsSetupConst.USER);
+ }
+
+ boolean isToTxn = AcidUtils.isTablePropertyTransactional(properties) ||
+ properties.containsKey(hive_metastoreConstants.TABLE_TRANSACTIONAL_PROPERTIES);
+ boolean isExplicitStatsUpdate = updateStats && AcidUtils.isTransactionalTable(getTable(tableName, true));
+
+ AbstractAlterTableDesc desc = createDesc(command, tableName, partitionSpec, properties, isToTxn,
+ isExplicitStatsUpdate, environmentContext);
+
+ addInputsOutputsAlterTable(tableName, partitionSpec, desc, desc.getType(), isToTxn);
+ DDLWork ddlWork = new DDLWork(getInputs(), getOutputs(), desc);
+ if (isToTxn) {
+ ddlWork.setNeedLock(true); // Hmm... why don't many other operations here need locks?
+ }
+ if (isToTxn || isExplicitStatsUpdate) {
+ setAcidDdlDesc(desc);
+ }
+
+ rootTasks.add(TaskFactory.get(ddlWork));
+ }
+
+ /**
+ * @return If it is executed after an update statistics command.
+ */
+ private boolean validate(TableName tableName, Map<String, String> properties) throws SemanticException {
+ // We need to check if the properties are valid, especially for stats.
+ // They might be changed via alter table .. update statistics or alter table .. set tblproperties.
+ // If the property is not row_count or raw_data_size, it could not be changed through update statistics.
+ boolean changeStats = false;
+ for (Entry<String, String> entry : properties.entrySet()) {
+ // we make sure that we do not change anything if there is anything wrong.
+ if (entry.getKey().equals(StatsSetupConst.ROW_COUNT) || entry.getKey().equals(StatsSetupConst.RAW_DATA_SIZE)) {
+ try {
+ Long.parseLong(entry.getValue());
+ changeStats = true;
+ } catch (Exception e) {
+ throw new SemanticException("AlterTable " + entry.getKey() + " failed with value " + entry.getValue());
+ }
+ } else if (entry.getKey().equals("external") && entry.getValue().equals("true")) {
+ // if table is being modified to be external we need to make sure existing table
+ // doesn't have enabled constraint since constraints are disallowed with such tables
+ if (hasConstraintsEnabled(tableName.getTable())) {
+ throw new SemanticException(ErrorMsg.INVALID_CSTR_SYNTAX.getMsg(String.format(
+ "Table: %s has constraints enabled. Please remove those constraints to change this property.",
+ tableName.getNotEmptyDbTable())));
+ }
+ } else {
+ if (queryState.getCommandType().equals(HiveOperation.ALTERTABLE_UPDATETABLESTATS.getOperationName()) ||
+ queryState.getCommandType().equals(HiveOperation.ALTERTABLE_UPDATEPARTSTATS.getOperationName())) {
+ throw new SemanticException(String.format(
+ "AlterTable UpdateStats %s failed because the only valid keys are %s and %s",
+ entry.getKey(), StatsSetupConst.ROW_COUNT, StatsSetupConst.RAW_DATA_SIZE));
+ }
+ }
+ }
+ return changeStats;
+ }
+
+ private boolean hasConstraintsEnabled(String tableName) throws SemanticException{
+ NotNullConstraint notNullConstriant = null;
+ DefaultConstraint defaultConstraint = null;
+ try {
+ // retrieve enabled NOT NULL constraint from metastore
+ notNullConstriant = Hive.get().getEnabledNotNullConstraints(db.getDatabaseCurrent().getName(), tableName);
+ defaultConstraint = Hive.get().getEnabledDefaultConstraints(db.getDatabaseCurrent().getName(), tableName);
+ } catch (Exception e) {
+ if (e instanceof SemanticException) {
+ throw (SemanticException) e;
+ } else {
+ throw (new RuntimeException(e));
+ }
+ }
+ return
+ (notNullConstriant != null && !notNullConstriant.getNotNullConstraints().isEmpty()) ||
+ (defaultConstraint != null && !defaultConstraint.getDefaultConstraints().isEmpty());
+ }
+
+ protected abstract AbstractAlterTableDesc createDesc(ASTNode command, TableName tableName,
+ Map<String, String> partitionSpec, Map<String, String> properties, boolean isToTxn, boolean isExplicitStatsUpdate,
+ EnvironmentContext environmentContext) throws SemanticException;
+
+ protected abstract boolean isView();
+}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AbstractAlterTableSetPropertiesAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AbstractAlterTableSetPropertiesAnalyzer.java
new file mode 100644
index 0000000..c36b486
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AbstractAlterTableSetPropertiesAnalyzer.java
@@ -0,0 +1,50 @@
+/*
+ * 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.hadoop.hive.ql.ddl.table.misc.properties;
+
+import java.util.Map;
+
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableDesc;
+import org.apache.hadoop.hive.ql.io.AcidUtils;
+import org.apache.hadoop.hive.ql.parse.ASTNode;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+
+/**
+ * Analyzer for setting the properties of table like entities commands.
+ */
+public abstract class AbstractAlterTableSetPropertiesAnalyzer extends AbstractAlterTablePropertiesAnalyzer {
+ public AbstractAlterTableSetPropertiesAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
+ }
+
+ protected AbstractAlterTableDesc createDesc(ASTNode command, TableName tableName, Map<String, String> partitionSpec,
+ Map<String, String> properties, boolean isToTxn, boolean isExplicitStatsUpdate,
+ EnvironmentContext environmentContext) throws SemanticException {
+ addPropertyReadEntry(properties, inputs);
+ boolean isAcidConversion = isToTxn && AcidUtils.isFullAcidTable(properties)
+ && !AcidUtils.isFullAcidTable(getTable(tableName, true));
+ return new AlterTableSetPropertiesDesc(tableName, partitionSpec, null, isView(), properties,
+ isExplicitStatsUpdate, isAcidConversion, environmentContext);
+ }
+
+ protected abstract boolean isView();
+}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AbstractAlterTableUnsetPropertiesAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AbstractAlterTableUnsetPropertiesAnalyzer.java
new file mode 100644
index 0000000..96fdf0c
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AbstractAlterTableUnsetPropertiesAnalyzer.java
@@ -0,0 +1,61 @@
+/*
+ * 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.hadoop.hive.ql.ddl.table.misc.properties;
+
+import java.util.Map;
+
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
+import org.apache.hadoop.hive.ql.ErrorMsg;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableDesc;
+import org.apache.hadoop.hive.ql.metadata.Table;
+import org.apache.hadoop.hive.ql.parse.ASTNode;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+
+/**
+ * Analyzer for unsetting the properties of table like entities commands.
+ */
+public abstract class AbstractAlterTableUnsetPropertiesAnalyzer extends AbstractAlterTablePropertiesAnalyzer {
+ public AbstractAlterTableUnsetPropertiesAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
+ }
+
+ protected AbstractAlterTableDesc createDesc(ASTNode command, TableName tableName, Map<String, String> partitionSpec,
+ Map<String, String> properties, boolean isToTxn, boolean isExplicitStatsUpdate,
+ EnvironmentContext environmentContext) throws SemanticException {
+ boolean dropIfExists = command.getChild(1) != null;
+ // validate Unset Non Existed Table Properties
+ if (!dropIfExists) {
+ Table table = getTable(tableName, true);
+ Map<String, String> tableParams = table.getTTable().getParameters();
+ for (String key : properties.keySet()) {
+ if (!tableParams.containsKey(key)) {
+ String errorMsg = "The following property " + key + " does not exist in " + table.getTableName();
+ throw new SemanticException(ErrorMsg.ALTER_TBL_UNSET_NON_EXIST_PROPERTY.getMsg(errorMsg));
+ }
+ }
+ }
+
+ return new AlterTableUnsetPropertiesDesc(tableName, partitionSpec, null, isView(), properties,
+ isExplicitStatsUpdate, environmentContext);
+ }
+
+ protected abstract boolean isView();
+}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableSetPropertiesAnalyzer.java
similarity index 52%
copy from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
copy to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableSetPropertiesAnalyzer.java
index bc37880..15b8bfc 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableSetPropertiesAnalyzer.java
@@ -16,31 +16,26 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.properties;
-import org.apache.hadoop.hive.ql.ddl.DDLDesc;
-import org.apache.hadoop.hive.ql.metadata.Table;
-import org.apache.hadoop.hive.ql.plan.Explain;
-import org.apache.hadoop.hive.ql.plan.Explain.Level;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
/**
- * DDL task description for PRE INSERT commands.
+ * Analyzer for alter table set properties commands.
*/
-@Explain(displayName = "Pre-Insert task", explainLevels = { Level.USER, Level.DEFAULT, Level.EXTENDED })
-public class PreInsertTableDesc implements DDLDesc {
- private final Table table;
- private final boolean isOverwrite;
+@DDLType(types = {HiveParser.TOK_ALTERTABLE_PROPERTIES, HiveParser.TOK_ALTERTABLE_UPDATESTATS,
+ HiveParser.TOK_ALTERPARTITION_UPDATESTATS})
+public class AlterTableSetPropertiesAnalyzer extends AbstractAlterTableSetPropertiesAnalyzer {
- public PreInsertTableDesc(Table table, boolean overwrite) {
- this.table = table;
- this.isOverwrite = overwrite;
+ public AlterTableSetPropertiesAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
}
- public Table getTable() {
- return table;
- }
-
- public boolean isOverwrite() {
- return isOverwrite;
+ @Override
+ protected boolean isView() {
+ return false;
}
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetPropertiesDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableSetPropertiesDesc.java
similarity index 97%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetPropertiesDesc.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableSetPropertiesDesc.java
index 2d615a6..0c2099d 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetPropertiesDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableSetPropertiesDesc.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.properties;
import java.util.Map;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetPropertiesOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableSetPropertiesOperation.java
similarity index 99%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetPropertiesOperation.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableSetPropertiesOperation.java
index ff6b08b..fb29d07 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableSetPropertiesOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableSetPropertiesOperation.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.properties;
import java.util.ArrayList;
import java.util.List;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableUnsetPropertiesAnalyzer.java
similarity index 52%
copy from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
copy to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableUnsetPropertiesAnalyzer.java
index bc37880..b7f587a 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableUnsetPropertiesAnalyzer.java
@@ -16,31 +16,25 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.properties;
-import org.apache.hadoop.hive.ql.ddl.DDLDesc;
-import org.apache.hadoop.hive.ql.metadata.Table;
-import org.apache.hadoop.hive.ql.plan.Explain;
-import org.apache.hadoop.hive.ql.plan.Explain.Level;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
/**
- * DDL task description for PRE INSERT commands.
+ * Analyzer for alter table unset properties commands.
*/
-@Explain(displayName = "Pre-Insert task", explainLevels = { Level.USER, Level.DEFAULT, Level.EXTENDED })
-public class PreInsertTableDesc implements DDLDesc {
- private final Table table;
- private final boolean isOverwrite;
+@DDLType(types = HiveParser.TOK_ALTERTABLE_DROPPROPERTIES)
+public class AlterTableUnsetPropertiesAnalyzer extends AbstractAlterTableUnsetPropertiesAnalyzer {
- public PreInsertTableDesc(Table table, boolean overwrite) {
- this.table = table;
- this.isOverwrite = overwrite;
+ public AlterTableUnsetPropertiesAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
}
- public Table getTable() {
- return table;
- }
-
- public boolean isOverwrite() {
- return isOverwrite;
+ @Override
+ protected boolean isView() {
+ return false;
}
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableUnsetPropertiesDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableUnsetPropertiesDesc.java
similarity index 97%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableUnsetPropertiesDesc.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableUnsetPropertiesDesc.java
index bea9a36..a23e1a6 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableUnsetPropertiesDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableUnsetPropertiesDesc.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.properties;
import java.util.Map;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableUnsetPropertiesOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableUnsetPropertiesOperation.java
similarity index 97%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableUnsetPropertiesOperation.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableUnsetPropertiesOperation.java
index 31a27fc..e70b451 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableUnsetPropertiesOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/properties/AlterTableUnsetPropertiesOperation.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.properties;
import java.util.Set;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AbstractAlterTableRenameAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AbstractAlterTableRenameAnalyzer.java
new file mode 100644
index 0000000..eb2280f
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AbstractAlterTableRenameAnalyzer.java
@@ -0,0 +1,56 @@
+/*
+ * 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.hadoop.hive.ql.ddl.table.misc.rename;
+
+import java.util.Map;
+
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLWork;
+import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableAnalyzer;
+import org.apache.hadoop.hive.ql.exec.TaskFactory;
+import org.apache.hadoop.hive.ql.io.AcidUtils;
+import org.apache.hadoop.hive.ql.metadata.Table;
+import org.apache.hadoop.hive.ql.parse.ASTNode;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+
+/**
+ * Analyzer for rename table like entities commands.
+ */
+public abstract class AbstractAlterTableRenameAnalyzer extends AbstractAlterTableAnalyzer {
+ public AbstractAlterTableRenameAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
+ }
+
+ @Override
+ protected void analyzeCommand(TableName tableName, Map<String, String> partitionSpec, ASTNode command)
+ throws SemanticException {
+ TableName target = getQualifiedTableName((ASTNode) command.getChild(0));
+
+ AlterTableRenameDesc desc = new AlterTableRenameDesc(tableName, null, isView(), target.getNotEmptyDbTable());
+ Table table = getTable(tableName.getNotEmptyDbTable(), true);
+ if (AcidUtils.isTransactionalTable(table)) {
+ setAcidDdlDesc(desc);
+ }
+ addInputsOutputsAlterTable(tableName, null, desc, desc.getType(), false);
+ rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(), desc)));
+ }
+
+ protected abstract boolean isView();
+}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AlterTableRenameAnalyzer.java
similarity index 52%
copy from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
copy to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AlterTableRenameAnalyzer.java
index bc37880..cfba48b 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AlterTableRenameAnalyzer.java
@@ -16,31 +16,23 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.rename;
-import org.apache.hadoop.hive.ql.ddl.DDLDesc;
-import org.apache.hadoop.hive.ql.metadata.Table;
-import org.apache.hadoop.hive.ql.plan.Explain;
-import org.apache.hadoop.hive.ql.plan.Explain.Level;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
/**
- * DDL task description for PRE INSERT commands.
+ * Analyzer for rename table commands.
*/
-@Explain(displayName = "Pre-Insert task", explainLevels = { Level.USER, Level.DEFAULT, Level.EXTENDED })
-public class PreInsertTableDesc implements DDLDesc {
- private final Table table;
- private final boolean isOverwrite;
-
- public PreInsertTableDesc(Table table, boolean overwrite) {
- this.table = table;
- this.isOverwrite = overwrite;
- }
-
- public Table getTable() {
- return table;
+@DDLType(types = HiveParser.TOK_ALTERTABLE_RENAME)
+public class AlterTableRenameAnalyzer extends AbstractAlterTableRenameAnalyzer {
+ public AlterTableRenameAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
}
- public boolean isOverwrite() {
- return isOverwrite;
+ protected boolean isView() {
+ return false;
}
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableRenameDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AlterTableRenameDesc.java
similarity index 97%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableRenameDesc.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AlterTableRenameDesc.java
index 091c146..5c1ce89 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableRenameDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AlterTableRenameDesc.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.rename;
import org.apache.hadoop.hive.common.TableName;
import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableDesc;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableRenameOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AlterTableRenameOperation.java
similarity index 88%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableRenameOperation.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AlterTableRenameOperation.java
index 73ea400..f06776c 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableRenameOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/rename/AlterTableRenameOperation.java
@@ -16,11 +16,11 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.rename;
+import org.apache.hadoop.hive.common.TableName;
import org.apache.hadoop.hive.ql.ddl.DDLOperationContext;
import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableOperation;
-import org.apache.hadoop.hive.ql.exec.Utilities;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.metadata.Partition;
import org.apache.hadoop.hive.ql.metadata.Table;
@@ -37,8 +37,8 @@ public class AlterTableRenameOperation extends AbstractAlterTableOperation<Alter
@Override
public int execute() throws HiveException {
- String[] names = Utilities.getDbTableName(desc.getDbTableName());
- if (Utils.isBootstrapDumpInProgress(context.getDb(), names[0])) {
+ TableName tableName = HiveTableName.of(desc.getDbTableName());
+ if (Utils.isBootstrapDumpInProgress(context.getDb(), tableName.getDb())) {
LOG.error("DDLTask: Rename Table not allowed as bootstrap dump in progress");
throw new HiveException("Rename Table: Not allowed as bootstrap dump in progress");
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/touch/AlterTableTouchAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/touch/AlterTableTouchAnalyzer.java
new file mode 100644
index 0000000..61f8f1d
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/touch/AlterTableTouchAnalyzer.java
@@ -0,0 +1,72 @@
+/*
+ * 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.hadoop.hive.ql.ddl.table.misc.touch;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLWork;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableAnalyzer;
+import org.apache.hadoop.hive.ql.ddl.table.AlterTableType;
+import org.apache.hadoop.hive.ql.ddl.table.partition.PartitionUtils;
+import org.apache.hadoop.hive.ql.exec.TaskFactory;
+import org.apache.hadoop.hive.ql.hooks.ReadEntity;
+import org.apache.hadoop.hive.ql.hooks.WriteEntity;
+import org.apache.hadoop.hive.ql.metadata.Table;
+import org.apache.hadoop.hive.ql.parse.ASTNode;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+
+/**
+ * Analyzer for touch commands.
+ *
+ * Rewrite the metadata for one or more partitions in a table. Useful when an external process modifies files on HDFS
+ * and you want the pre/post hooks to be fired for the specified partition.
+ */
+@DDLType(types = HiveParser.TOK_ALTERTABLE_TOUCH)
+public class AlterTableTouchAnalyzer extends AbstractAlterTableAnalyzer {
+ public AlterTableTouchAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
+ }
+
+ @Override
+ protected void analyzeCommand(TableName tableName, Map<String, String> partitionSpecFromFramework, ASTNode command)
+ throws SemanticException {
+ Table table = getTable(tableName);
+ validateAlterTableType(table, AlterTableType.TOUCH, false);
+ inputs.add(new ReadEntity(table));
+
+ List<Map<String, String>> partitionSpecs = getPartitionSpecs(table, command);
+
+ if (partitionSpecs.isEmpty()) {
+ AlterTableTouchDesc desc = new AlterTableTouchDesc(tableName.getNotEmptyDbTable(), null);
+ outputs.add(new WriteEntity(table, WriteEntity.WriteType.DDL_NO_LOCK));
+ rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(), desc)));
+ } else {
+ PartitionUtils.addTablePartsOutputs(db, outputs, table, partitionSpecs, false, WriteEntity.WriteType.DDL_NO_LOCK);
+ for (Map<String, String> partitionSpec : partitionSpecs) {
+ AlterTableTouchDesc desc = new AlterTableTouchDesc(tableName.getNotEmptyDbTable(), partitionSpec);
+ rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(), desc)));
+ }
+ }
+ }
+}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableTouchDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/touch/AlterTableTouchDesc.java
similarity index 96%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableTouchDesc.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/touch/AlterTableTouchDesc.java
index 207f5b4..243656a 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableTouchDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/touch/AlterTableTouchDesc.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.touch;
import java.util.Map;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableTouchOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/touch/AlterTableTouchOperation.java
similarity index 98%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableTouchOperation.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/touch/AlterTableTouchOperation.java
index 8d8ac20..a58bc5a 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/AlterTableTouchOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/touch/AlterTableTouchOperation.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.touch;
import org.apache.hadoop.hive.common.StatsSetupConst;
import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/truncate/TruncateTableAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/truncate/TruncateTableAnalyzer.java
new file mode 100644
index 0000000..1655f3f
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/truncate/TruncateTableAnalyzer.java
@@ -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.hadoop.hive.ql.ddl.table.misc.truncate;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.TableName;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.TableType;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
+import org.apache.hadoop.hive.ql.ErrorMsg;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.ddl.table.AbstractBaseAlterTableAnalyzer;
+import org.apache.hadoop.hive.ql.ddl.table.AlterTableType;
+import org.apache.hadoop.hive.ql.ddl.table.AlterTableUtils;
+import org.apache.hadoop.hive.ql.ddl.table.partition.PartitionUtils;
+import org.apache.hadoop.hive.ql.ddl.DDLWork;
+import org.apache.hadoop.hive.ql.exec.ArchiveUtils;
+import org.apache.hadoop.hive.ql.exec.Task;
+import org.apache.hadoop.hive.ql.exec.TaskFactory;
+import org.apache.hadoop.hive.ql.exec.Utilities;
+import org.apache.hadoop.hive.ql.hooks.WriteEntity;
+import org.apache.hadoop.hive.ql.io.AcidUtils;
+import org.apache.hadoop.hive.ql.io.RCFileInputFormat;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.metadata.Partition;
+import org.apache.hadoop.hive.ql.metadata.Table;
+import org.apache.hadoop.hive.ql.parse.ASTNode;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.HiveTableName;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
+import org.apache.hadoop.hive.ql.plan.BasicStatsWork;
+import org.apache.hadoop.hive.ql.plan.ListBucketingCtx;
+import org.apache.hadoop.hive.ql.plan.LoadTableDesc;
+import org.apache.hadoop.hive.ql.plan.MoveWork;
+import org.apache.hadoop.hive.ql.plan.StatsWork;
+import org.apache.hadoop.hive.ql.plan.TableDesc;
+import org.apache.hadoop.mapred.InputFormat;
+
+/**
+ * Analyzer for truncate table commands.
+ */
+@DDLType(types = HiveParser.TOK_TRUNCATETABLE)
+public class TruncateTableAnalyzer extends AbstractBaseAlterTableAnalyzer {
+ public TruncateTableAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
+ }
+
+ @Override
+ public void analyzeInternal(ASTNode root) throws SemanticException {
+ ASTNode tableNode = (ASTNode) root.getChild(0); // TOK_TABLE_PARTITION
+ String tableNameString = getUnescapedName((ASTNode) tableNode.getChild(0));
+ Table table = getTable(tableNameString, true);
+ TableName tableName = HiveTableName.of(table);
+ checkTruncateEligibility(root, tableNode, tableNameString, table);
+
+ Map<String, String> partitionSpec = getPartSpec((ASTNode) tableNode.getChild(1));
+ addTruncateTableOutputs(tableNode, table, partitionSpec);
+
+ Task<?> truncateTask = null;
+ ASTNode colNamesNode = (ASTNode) root.getFirstChildWithType(HiveParser.TOK_TABCOLNAME);
+ if (colNamesNode == null) {
+ truncateTask = getTruncateTaskWithoutColumnNames(tableName, partitionSpec, table);
+ } else {
+ truncateTask = getTruncateTaskWithColumnNames(tableNode, tableName, table, partitionSpec, colNamesNode);
+ }
+
+ rootTasks.add(truncateTask);
+ }
+
+ private void checkTruncateEligibility(ASTNode ast, ASTNode root, String tableName, Table table)
+ throws SemanticException {
+ boolean isForce = ast.getFirstChildWithType(HiveParser.TOK_FORCE) != null;
+ if (!isForce &&
+ table.getTableType() != TableType.MANAGED_TABLE &&
+ table.getParameters().getOrDefault(MetaStoreUtils.EXTERNAL_TABLE_PURGE, "FALSE").equalsIgnoreCase("FALSE")) {
+ throw new SemanticException(ErrorMsg.TRUNCATE_FOR_NON_MANAGED_TABLE.format(tableName));
+ }
+
+ if (table.isNonNative()) {
+ throw new SemanticException(ErrorMsg.TRUNCATE_FOR_NON_NATIVE_TABLE.format(tableName)); //TODO
+ }
+
+ if (!table.isPartitioned() && root.getChildCount() > 1) {
+ throw new SemanticException(ErrorMsg.PARTSPEC_FOR_NON_PARTITIONED_TABLE.format(tableName));
+ }
+ }
+
+ private void addTruncateTableOutputs(ASTNode root, Table table, Map<String, String> partitionSpec)
+ throws SemanticException {
+ if (partitionSpec == null) {
+ if (!table.isPartitioned()) {
+ outputs.add(new WriteEntity(table, WriteEntity.WriteType.DDL_EXCLUSIVE));
+ } else {
+ for (Partition partition : PartitionUtils.getPartitions(db, table, null, false)) {
+ outputs.add(new WriteEntity(partition, WriteEntity.WriteType.DDL_EXCLUSIVE));
+ }
+ }
+ } else {
+ if (AlterTableUtils.isFullPartitionSpec(table, partitionSpec)) {
+ validatePartSpec(table, partitionSpec, (ASTNode) root.getChild(1), conf, true);
+ Partition partition = PartitionUtils.getPartition(db, table, partitionSpec, true);
+ outputs.add(new WriteEntity(partition, WriteEntity.WriteType.DDL_EXCLUSIVE));
+ } else {
+ validatePartSpec(table, partitionSpec, (ASTNode) root.getChild(1), conf, false);
+ for (Partition partition : PartitionUtils.getPartitions(db, table, partitionSpec, false)) {
+ outputs.add(new WriteEntity(partition, WriteEntity.WriteType.DDL_EXCLUSIVE));
+ }
+ }
+ }
+ }
+
+ private Task<?> getTruncateTaskWithoutColumnNames(TableName tableName, Map<String, String> partitionSpec,
+ Table table) {
+ TruncateTableDesc desc = new TruncateTableDesc(tableName, partitionSpec, null, table);
+ if (desc.mayNeedWriteId()) {
+ setAcidDdlDesc(desc);
+ }
+
+ DDLWork ddlWork = new DDLWork(getInputs(), getOutputs(), desc);
+ return TaskFactory.get(ddlWork);
+ }
+
+ private Task<?> getTruncateTaskWithColumnNames(ASTNode root, TableName tableName, Table table,
+ Map<String, String> partitionSpec, ASTNode columnNamesNode) throws SemanticException {
+ try {
+ // It would be possible to support this, but this is such a pointless command.
+ if (AcidUtils.isInsertOnlyTable(table.getParameters())) {
+ throw new SemanticException("Truncating MM table columns not presently supported");
+ }
+
+ List<String> columnNames = getColumnNames(columnNamesNode);
+
+ if (table.isPartitioned()) {
+ return truncatePartitionedTableWithColumnNames(root, tableName, table, partitionSpec, columnNames);
+ } else {
+ return truncateUnpartitionedTableWithColumnNames(root, tableName, table, partitionSpec, columnNames);
+ }
+ } catch (HiveException e) {
+ throw new SemanticException(e);
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ private Task<?> truncatePartitionedTableWithColumnNames(ASTNode root, TableName tableName, Table table,
+ Map<String, String> partitionSpec, List<String> columnNames) throws HiveException, SemanticException {
+ Partition partition = db.getPartition(table, partitionSpec, false);
+
+ Path tablePath = table.getPath();
+ Path partitionPath = partition.getDataLocation();
+
+ // if the table is in a different dfs than the partition, replace the partition's dfs with the table's dfs.
+ Path oldPartitionLocation = partitionPath;
+ Path newPartitionLocation = new Path(tablePath.toUri().getScheme(), tablePath.toUri().getAuthority(),
+ partitionPath.toUri().getPath());
+
+ List<FieldSchema> columns = partition.getCols();
+ List<String> bucketColumns = partition.getBucketCols();
+ List<String> listBucketColumns = partition.getSkewedColNames();
+
+ Class<? extends InputFormat> inputFormatClass = partition.getInputFormatClass();
+ boolean isArchived = ArchiveUtils.isArchived(partition);
+ ListBucketingCtx lbCtx = constructListBucketingCtx(partition.getSkewedColNames(), partition.getSkewedColValues(),
+ partition.getSkewedColValueLocationMaps(), partition.isStoredAsSubDirectories());
+ boolean isListBucketed = partition.isStoredAsSubDirectories();
+
+ return createTasks(root, tableName, table, partitionSpec, columnNames, bucketColumns, inputFormatClass,
+ isArchived, newPartitionLocation, oldPartitionLocation, columns, lbCtx, isListBucketed, listBucketColumns);
+ }
+
+ @SuppressWarnings("rawtypes")
+ private Task<?> truncateUnpartitionedTableWithColumnNames(ASTNode root, TableName tableName, Table table,
+ Map<String, String> partitionSpec, List<String> columnNames) throws SemanticException {
+ // input and output are the same
+ Path oldPartitionLocation = table.getPath();
+ Path newPartitionLocation = table.getPath();
+
+ List<FieldSchema> columns = table.getCols();
+ List<String> bucketColumns = table.getBucketCols();
+ List<String> listBucketColumns = table.getSkewedColNames();
+
+ Class<? extends InputFormat> inputFormatClass = table.getInputFormatClass();
+ ListBucketingCtx lbCtx = constructListBucketingCtx(table.getSkewedColNames(), table.getSkewedColValues(),
+ table.getSkewedColValueLocationMaps(), table.isStoredAsSubDirectories());
+ boolean isListBucketed = table.isStoredAsSubDirectories();
+
+ return createTasks(root, tableName, table, partitionSpec, columnNames, bucketColumns, inputFormatClass,
+ false, newPartitionLocation, oldPartitionLocation, columns, lbCtx, isListBucketed, listBucketColumns);
+ }
+
+ @SuppressWarnings("rawtypes")
+ private Task<?> createTasks(ASTNode root, TableName tableName, Table table, Map<String, String> partitionSpec,
+ List<String> columnNames, List<String> bucketColumns, Class<? extends InputFormat> inputFormatClass,
+ boolean isArchived, Path newPartitionLocation, Path oldPartitionLocation, List<FieldSchema> columns,
+ ListBucketingCtx lbCtx, boolean isListBucketed, List<String> listBucketColumns) throws SemanticException {
+ if (!inputFormatClass.equals(RCFileInputFormat.class)) {
+ throw new SemanticException(ErrorMsg.TRUNCATE_COLUMN_NOT_RC.getMsg());
+ }
+
+ if (isArchived) {
+ throw new SemanticException(ErrorMsg.TRUNCATE_COLUMN_ARCHIVED.getMsg());
+ }
+
+ Set<Integer> columnIndexes =
+ getColumnIndexes(columnNames, bucketColumns, columns, isListBucketed, listBucketColumns);
+
+ addInputsOutputsAlterTable(tableName, partitionSpec, null, AlterTableType.TRUNCATE, false);
+
+ TableDesc tableDesc = Utilities.getTableDesc(table);
+ Path queryTmpdir = ctx.getExternalTmpPath(newPartitionLocation);
+
+ Task<?> truncateTask =
+ createTruncateTask(tableName, table, partitionSpec, oldPartitionLocation, lbCtx, columnIndexes, queryTmpdir);
+
+ addMoveTask(root, table, partitionSpec, oldPartitionLocation, newPartitionLocation, lbCtx, queryTmpdir,
+ truncateTask, tableDesc);
+
+ return truncateTask;
+ }
+
+ private Set<Integer> getColumnIndexes(List<String> columnNames, List<String> bucketColumns, List<FieldSchema> columns,
+ boolean isListBucketed, List<String> listBucketColumns) throws SemanticException {
+ Set<Integer> columnIndexes = new HashSet<Integer>();
+ for (String columnName : columnNames) {
+ boolean found = false;
+ for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
+ if (columnName.equalsIgnoreCase(columns.get(columnIndex).getName())) {
+ columnIndexes.add(columnIndex);
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ throw new SemanticException(ErrorMsg.INVALID_COLUMN.getMsg(columnName));
+ }
+
+ for (String bucketColumn : bucketColumns) {
+ if (bucketColumn.equalsIgnoreCase(columnName)) {
+ throw new SemanticException(ErrorMsg.TRUNCATE_BUCKETED_COLUMN.getMsg(columnName));
+ }
+ }
+
+ if (isListBucketed) {
+ for (String listBucketColumn : listBucketColumns) {
+ if (listBucketColumn.equalsIgnoreCase(columnName)) {
+ throw new SemanticException(ErrorMsg.TRUNCATE_LIST_BUCKETED_COLUMN.getMsg(columnName));
+ }
+ }
+ }
+ }
+ return columnIndexes;
+ }
+
+ private Task<?> createTruncateTask(TableName tableName, Table table, Map<String, String> partitionSpec,
+ Path oldPartitionLocation, ListBucketingCtx lbCtx, Set<Integer> columnIndexes, Path queryTmpdir) {
+ TruncateTableDesc desc = new TruncateTableDesc(tableName, partitionSpec, null, table,
+ new ArrayList<Integer>(columnIndexes), oldPartitionLocation, queryTmpdir, lbCtx);
+ if (desc.mayNeedWriteId()) {
+ setAcidDdlDesc(desc);
+ }
+
+ DDLWork ddlWork = new DDLWork(getInputs(), getOutputs(), desc);
+ ddlWork.setNeedLock(true);
+ return TaskFactory.get(ddlWork);
+ }
+
+ private void addMoveTask(ASTNode root, Table table, Map<String, String> partitionSpec, Path oldPartitionLocation,
+ Path newPartitionLocation, ListBucketingCtx lbCtx, Path queryTmpdir, Task<?> truncateTask, TableDesc tableDesc)
+ throws SemanticException {
+ // Write the output to temporary directory and move it to the final location at the end
+ // so the operation is atomic.
+ LoadTableDesc loadTableDesc =
+ new LoadTableDesc(queryTmpdir, tableDesc, partitionSpec == null ? new HashMap<>() : partitionSpec);
+ loadTableDesc.setLbCtx(lbCtx);
+ Task<MoveWork> moveTask = TaskFactory.get(new MoveWork(null, null, loadTableDesc, null, false));
+ truncateTask.addDependentTask(moveTask);
+
+ addStatTask(root, table, oldPartitionLocation, newPartitionLocation, loadTableDesc, moveTask);
+ }
+
+ private void addStatTask(ASTNode root, Table table, Path oldPartitionLocation, Path newPartitionLocation,
+ LoadTableDesc loadTableDesc, Task<MoveWork> moveTask) throws SemanticException {
+ // Recalculate the HDFS stats if auto gather stats is set
+ if (conf.getBoolVar(HiveConf.ConfVars.HIVESTATSAUTOGATHER)) {
+ BasicStatsWork basicStatsWork;
+ if (oldPartitionLocation.equals(newPartitionLocation)) {
+ // If we're merging to the same location, we can avoid some metastore calls
+ TableSpec partitionSpec = new TableSpec(db, conf, root);
+ basicStatsWork = new BasicStatsWork(partitionSpec);
+ } else {
+ basicStatsWork = new BasicStatsWork(loadTableDesc);
+ }
+ basicStatsWork.setNoStatsAggregator(true);
+ basicStatsWork.setClearAggregatorStats(true);
+ StatsWork columnStatsWork = new StatsWork(table, basicStatsWork, conf);
+
+ Task<?> statTask = TaskFactory.get(columnStatsWork);
+ moveTask.addDependentTask(statTask);
+ }
+ }
+}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/TruncateTableDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/truncate/TruncateTableDesc.java
similarity index 98%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/TruncateTableDesc.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/truncate/TruncateTableDesc.java
index 04fa1b9..6bac319 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/TruncateTableDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/truncate/TruncateTableDesc.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.truncate;
import java.io.Serializable;
import java.util.List;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/TruncateTableOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/truncate/TruncateTableOperation.java
similarity index 98%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/TruncateTableOperation.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/truncate/TruncateTableOperation.java
index 3c62b0a..e738d4f 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/TruncateTableOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/truncate/TruncateTableOperation.java
@@ -16,7 +16,7 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.table.misc.truncate;
import java.util.ArrayList;
import java.util.Map;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/partition/drop/AlterTableDropPartitionOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/partition/drop/AlterTableDropPartitionOperation.java
index 9d186db..5caa31c 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/partition/drop/AlterTableDropPartitionOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/partition/drop/AlterTableDropPartitionOperation.java
@@ -104,7 +104,7 @@ public class AlterTableDropPartitionOperation extends DDLOperation<AlterTableDro
}
private void dropPartitions() throws HiveException {
- // ifExists is currently verified in DDLSemanticAnalyzer
+ // ifExists is currently verified in AlterTableDropPartitionAnalyzer
TableName tablenName = HiveTableName.of(desc.getTableName());
List<Pair<Integer, byte[]>> partitionExpressions = new ArrayList<>(desc.getPartSpecs().size());
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/archive/AlterTableArchiveOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/archive/AlterTableArchiveOperation.java
index ffa7a67..f54ea6a 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/archive/AlterTableArchiveOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/archive/AlterTableArchiveOperation.java
@@ -309,9 +309,9 @@ public class AlterTableArchiveOperation extends DDLOperation<AlterTableArchiveDe
private void deleteIntermediateOriginalDir(Table table, Path intermediateOriginalDir) throws HiveException {
if (HdfsUtils.pathExists(intermediateOriginalDir, context.getConf())) {
- AlterTableArchiveUtils.deleteDir(intermediateOriginalDir,
- ReplChangeManager.shouldEnableCm(context.getDb().getDatabase(table.getDbName()), table.getTTable()),
- context.getConf());
+ boolean shouldEnableCm = ReplChangeManager.shouldEnableCm(context.getDb().getDatabase(table.getDbName()),
+ table.getTTable());
+ AlterTableArchiveUtils.deleteDir(intermediateOriginalDir, shouldEnableCm, context.getConf());
}
}
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/archive/AlterTableUnarchiveOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/archive/AlterTableUnarchiveOperation.java
index 8f0a477..70b7dca 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/archive/AlterTableUnarchiveOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/archive/AlterTableUnarchiveOperation.java
@@ -283,9 +283,9 @@ public class AlterTableUnarchiveOperation extends DDLOperation<AlterTableUnarchi
private void deleteIntermediateArchivedDir(Table table, Path intermediateArchivedDir) throws HiveException {
if (HdfsUtils.pathExists(intermediateArchivedDir, context.getConf())) {
- AlterTableArchiveUtils.deleteDir(intermediateArchivedDir,
- ReplChangeManager.shouldEnableCm(context.getDb().getDatabase(table.getDbName()), table.getTTable()),
- context.getConf());
+ boolean shouldEnableCm = ReplChangeManager.shouldEnableCm(context.getDb().getDatabase(table.getDbName()),
+ table.getTTable());
+ AlterTableArchiveUtils.deleteDir(intermediateArchivedDir, shouldEnableCm, context.getConf());
}
}
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/compact/AlterTableCompactOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/compact/AlterTableCompactOperation.java
index 5684df0..ee7af1d 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/compact/AlterTableCompactOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/compact/AlterTableCompactOperation.java
@@ -39,7 +39,6 @@ import org.apache.hadoop.hive.ql.metadata.Table;
* Operation process of compacting a table.
*/
public class AlterTableCompactOperation extends DDLOperation<AlterTableCompactDesc> {
-
public AlterTableCompactOperation(DDLOperationContext context, AlterTableCompactDesc desc) {
super(context, desc);
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/drop/DropViewOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/drop/DropViewOperation.java
index a029b6c..ad626e8 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/drop/DropViewOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/drop/DropViewOperation.java
@@ -38,7 +38,7 @@ public class DropViewOperation extends DDLOperation<DropViewDesc> {
public int execute() throws HiveException {
Table table = getTable();
if (table == null) {
- return 0; // dropping not existing view is handled by DDLSemanticAnalyzer
+ return 0; // dropping not existing view is handled by DropViewAnalyzer
}
if (!table.isView()) {
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/materialized/alter/rewrite/AlterMaterializedViewRewriteAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/materialized/alter/rewrite/AlterMaterializedViewRewriteAnalyzer.java
index 2cc32a0..5c57340 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/materialized/alter/rewrite/AlterMaterializedViewRewriteAnalyzer.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/materialized/alter/rewrite/AlterMaterializedViewRewriteAnalyzer.java
@@ -69,8 +69,8 @@ public class AlterMaterializedViewRewriteAnalyzer extends BaseSemanticAnalyzer {
// One last test: if we are enabling the rewrite, we need to check that query
// only uses transactional (MM and ACID) tables
if (rewriteEnable) {
- for (String tName : materializedViewTable.getCreationMetadata().getTablesUsed()) {
- Table table = getTable(tName, true);
+ for (String sourceTableName : materializedViewTable.getCreationMetadata().getTablesUsed()) {
+ Table table = getTable(sourceTableName, true);
if (!AcidUtils.isTransactionalTable(table)) {
throw new SemanticException("Automatic rewriting for materialized view cannot be enabled if the " +
"materialized view uses non-transactional tables");
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/materialized/drop/DropMaterializedViewOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/materialized/drop/DropMaterializedViewOperation.java
index 93b25d4..cda84eb 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/materialized/drop/DropMaterializedViewOperation.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/materialized/drop/DropMaterializedViewOperation.java
@@ -39,7 +39,7 @@ public class DropMaterializedViewOperation extends DDLOperation<DropMaterialized
public int execute() throws HiveException {
Table table = getTable();
if (table == null) {
- return 0; // dropping not existing materialized view is handled by DDLSemanticAnalyzer
+ return 0; // dropping not existing materialized view is handled by DropMaterializedViewAnalyzer
}
if (!table.isMaterializedView()) {
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/properties/AlterViewSetPropertiesAnalyzer.java
similarity index 52%
copy from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
copy to ql/src/java/org/apache/hadoop/hive/ql/ddl/view/properties/AlterViewSetPropertiesAnalyzer.java
index bc37880..b3646f7 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/properties/AlterViewSetPropertiesAnalyzer.java
@@ -16,31 +16,26 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.view.properties;
-import org.apache.hadoop.hive.ql.ddl.DDLDesc;
-import org.apache.hadoop.hive.ql.metadata.Table;
-import org.apache.hadoop.hive.ql.plan.Explain;
-import org.apache.hadoop.hive.ql.plan.Explain.Level;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.ddl.table.misc.properties.AbstractAlterTableSetPropertiesAnalyzer;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
/**
- * DDL task description for PRE INSERT commands.
+ * Analyzer for alter view set properties commands.
*/
-@Explain(displayName = "Pre-Insert task", explainLevels = { Level.USER, Level.DEFAULT, Level.EXTENDED })
-public class PreInsertTableDesc implements DDLDesc {
- private final Table table;
- private final boolean isOverwrite;
+@DDLType(types = HiveParser.TOK_ALTERVIEW_PROPERTIES)
+public class AlterViewSetPropertiesAnalyzer extends AbstractAlterTableSetPropertiesAnalyzer {
- public PreInsertTableDesc(Table table, boolean overwrite) {
- this.table = table;
- this.isOverwrite = overwrite;
+ public AlterViewSetPropertiesAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
}
- public Table getTable() {
- return table;
- }
-
- public boolean isOverwrite() {
- return isOverwrite;
+ @Override
+ protected boolean isView() {
+ return true;
}
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/properties/AlterViewUnsetPropertiesAnalyzer.java
similarity index 51%
copy from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
copy to ql/src/java/org/apache/hadoop/hive/ql/ddl/view/properties/AlterViewUnsetPropertiesAnalyzer.java
index bc37880..dd4dcc3 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/properties/AlterViewUnsetPropertiesAnalyzer.java
@@ -16,31 +16,26 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.view.properties;
-import org.apache.hadoop.hive.ql.ddl.DDLDesc;
-import org.apache.hadoop.hive.ql.metadata.Table;
-import org.apache.hadoop.hive.ql.plan.Explain;
-import org.apache.hadoop.hive.ql.plan.Explain.Level;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.ddl.table.misc.properties.AbstractAlterTableUnsetPropertiesAnalyzer;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
/**
- * DDL task description for PRE INSERT commands.
+ * Analyzer for alter view unset properties commands.
*/
-@Explain(displayName = "Pre-Insert task", explainLevels = { Level.USER, Level.DEFAULT, Level.EXTENDED })
-public class PreInsertTableDesc implements DDLDesc {
- private final Table table;
- private final boolean isOverwrite;
+@DDLType(types = HiveParser.TOK_ALTERVIEW_DROPPROPERTIES)
+public class AlterViewUnsetPropertiesAnalyzer extends AbstractAlterTableUnsetPropertiesAnalyzer {
- public PreInsertTableDesc(Table table, boolean overwrite) {
- this.table = table;
- this.isOverwrite = overwrite;
+ public AlterViewUnsetPropertiesAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
}
- public Table getTable() {
- return table;
- }
-
- public boolean isOverwrite() {
- return isOverwrite;
+ @Override
+ protected boolean isView() {
+ return true;
}
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/rename/AlterViewRenameAnalyzer.java
similarity index 52%
rename from ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
rename to ql/src/java/org/apache/hadoop/hive/ql/ddl/view/rename/AlterViewRenameAnalyzer.java
index bc37880..53da7c2 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/PreInsertTableDesc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/view/rename/AlterViewRenameAnalyzer.java
@@ -16,31 +16,24 @@
* limitations under the License.
*/
-package org.apache.hadoop.hive.ql.ddl.table.misc;
+package org.apache.hadoop.hive.ql.ddl.view.rename;
-import org.apache.hadoop.hive.ql.ddl.DDLDesc;
-import org.apache.hadoop.hive.ql.metadata.Table;
-import org.apache.hadoop.hive.ql.plan.Explain;
-import org.apache.hadoop.hive.ql.plan.Explain.Level;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType;
+import org.apache.hadoop.hive.ql.ddl.table.misc.rename.AbstractAlterTableRenameAnalyzer;
+import org.apache.hadoop.hive.ql.parse.HiveParser;
+import org.apache.hadoop.hive.ql.parse.SemanticException;
/**
- * DDL task description for PRE INSERT commands.
+ * Analyzer for rename view commands.
*/
-@Explain(displayName = "Pre-Insert task", explainLevels = { Level.USER, Level.DEFAULT, Level.EXTENDED })
-public class PreInsertTableDesc implements DDLDesc {
- private final Table table;
- private final boolean isOverwrite;
-
- public PreInsertTableDesc(Table table, boolean overwrite) {
- this.table = table;
- this.isOverwrite = overwrite;
- }
-
- public Table getTable() {
- return table;
+@DDLType(types = HiveParser.TOK_ALTERVIEW_RENAME)
+public class AlterViewRenameAnalyzer extends AbstractAlterTableRenameAnalyzer {
+ public AlterViewRenameAnalyzer(QueryState queryState) throws SemanticException {
+ super(queryState);
}
- public boolean isOverwrite() {
- return isOverwrite;
+ protected boolean isView() {
+ return true;
}
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/incremental/IncrementalLoadTasksBuilder.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/incremental/IncrementalLoadTasksBuilder.java
index d7b6eea..7e844d3 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/incremental/IncrementalLoadTasksBuilder.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/incremental/IncrementalLoadTasksBuilder.java
@@ -30,7 +30,7 @@ import org.apache.hadoop.hive.ql.Context;
import org.apache.hadoop.hive.ql.ddl.DDLWork;
import org.apache.hadoop.hive.ql.ddl.database.alter.poperties.AlterDatabaseSetPropertiesDesc;
import org.apache.hadoop.hive.ql.ddl.misc.flags.ReplRemoveFirstIncLoadPendFlagDesc;
-import org.apache.hadoop.hive.ql.ddl.table.misc.AlterTableSetPropertiesDesc;
+import org.apache.hadoop.hive.ql.ddl.table.misc.properties.AlterTableSetPropertiesDesc;
import org.apache.hadoop.hive.ql.exec.Task;
import org.apache.hadoop.hive.ql.exec.TaskFactory;
import org.apache.hadoop.hive.ql.exec.repl.ReplStateLogWork;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/util/ReplUtils.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/util/ReplUtils.java
index fc7f226..504c9d4 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/util/ReplUtils.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/util/ReplUtils.java
@@ -29,7 +29,7 @@ import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
import org.apache.hadoop.hive.metastore.api.InvalidOperationException;
import org.apache.hadoop.hive.ql.ErrorMsg;
import org.apache.hadoop.hive.ql.ddl.DDLWork;
-import org.apache.hadoop.hive.ql.ddl.table.misc.AlterTableSetPropertiesDesc;
+import org.apache.hadoop.hive.ql.ddl.table.misc.properties.AlterTableSetPropertiesDesc;
import org.apache.hadoop.hive.ql.ddl.table.partition.PartitionUtils;
import org.apache.hadoop.hive.ql.exec.Task;
import org.apache.hadoop.hive.ql.exec.TaskFactory;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/AcidExportSemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/AcidExportSemanticAnalyzer.java
index cc3c5da..8ab3943 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/AcidExportSemanticAnalyzer.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/AcidExportSemanticAnalyzer.java
@@ -40,7 +40,7 @@ import org.apache.hadoop.hive.ql.ddl.DDLTask;
import org.apache.hadoop.hive.ql.ddl.DDLWork;
import org.apache.hadoop.hive.ql.ddl.table.create.like.CreateTableLikeDesc;
import org.apache.hadoop.hive.ql.ddl.table.drop.DropTableDesc;
-import org.apache.hadoop.hive.ql.ddl.table.misc.AlterTableSetPropertiesDesc;
+import org.apache.hadoop.hive.ql.ddl.table.misc.properties.AlterTableSetPropertiesDesc;
import org.apache.hadoop.hive.ql.exec.StatsTask;
import org.apache.hadoop.hive.ql.exec.Task;
import org.apache.hadoop.hive.ql.exec.TaskFactory;
@@ -199,7 +199,7 @@ public class AcidExportSemanticAnalyzer extends RewriteSemanticAnalyzer {
addExportTask(rootTasks, exportTask, TaskFactory.get(new DDLWork(getInputs(), getOutputs(), alterTblDesc)));
// Now make a task to drop temp table
- // {@link DDLSemanticAnalyzer#analyzeDropTable(ASTNode ast, TableType expectedType)
+ // {@link DropTableAnalyzer#analyzeInternal(ASTNode ast)
ReplicationSpec replicationSpec = new ReplicationSpec();
DropTableDesc dropTblDesc = new DropTableDesc(newTableName, false, true, replicationSpec);
Task<DDLWork> dropTask = TaskFactory.get(new DDLWork(new HashSet<>(), new HashSet<>(), dropTblDesc), conf);
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/BaseSemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/BaseSemanticAnalyzer.java
index 46bb37a..4f1e23d 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/BaseSemanticAnalyzer.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/BaseSemanticAnalyzer.java
@@ -97,8 +97,13 @@ import org.apache.hadoop.hive.serde2.io.DateWritableV2;
import org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
+import org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TimestampLocalTZTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
+import org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.security.alias.AbstractJavaKeyStoreProvider;
import org.apache.hadoop.security.alias.CredentialProvider;
@@ -876,8 +881,64 @@ public abstract class BaseSemanticAnalyzer {
case HiveParser.TOK_UNIONTYPE:
return getUnionTypeStringFromAST(typeNode);
default:
- return DDLSemanticAnalyzer.getTypeName(typeNode);
+ return getTypeName(typeNode);
+ }
+ }
+
+ private static final Map<Integer, String> TOKEN_TO_TYPE = new HashMap<Integer, String>();
+
+ static {
+ TOKEN_TO_TYPE.put(HiveParser.TOK_BOOLEAN, serdeConstants.BOOLEAN_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_TINYINT, serdeConstants.TINYINT_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_SMALLINT, serdeConstants.SMALLINT_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_INT, serdeConstants.INT_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_BIGINT, serdeConstants.BIGINT_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_FLOAT, serdeConstants.FLOAT_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_DOUBLE, serdeConstants.DOUBLE_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_STRING, serdeConstants.STRING_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_CHAR, serdeConstants.CHAR_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_VARCHAR, serdeConstants.VARCHAR_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_BINARY, serdeConstants.BINARY_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_DATE, serdeConstants.DATE_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_DATETIME, serdeConstants.DATETIME_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_TIMESTAMP, serdeConstants.TIMESTAMP_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_TIMESTAMPLOCALTZ, serdeConstants.TIMESTAMPLOCALTZ_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_INTERVAL_YEAR_MONTH, serdeConstants.INTERVAL_YEAR_MONTH_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_INTERVAL_DAY_TIME, serdeConstants.INTERVAL_DAY_TIME_TYPE_NAME);
+ TOKEN_TO_TYPE.put(HiveParser.TOK_DECIMAL, serdeConstants.DECIMAL_TYPE_NAME);
+ }
+
+ private static String getTypeName(ASTNode node) throws SemanticException {
+ int token = node.getType();
+ String typeName;
+
+ // datetime type isn't currently supported
+ if (token == HiveParser.TOK_DATETIME) {
+ throw new SemanticException(ErrorMsg.UNSUPPORTED_TYPE.getMsg());
+ }
+
+ switch (token) {
+ case HiveParser.TOK_CHAR:
+ CharTypeInfo charTypeInfo = ParseUtils.getCharTypeInfo(node);
+ typeName = charTypeInfo.getQualifiedName();
+ break;
+ case HiveParser.TOK_VARCHAR:
+ VarcharTypeInfo varcharTypeInfo = ParseUtils.getVarcharTypeInfo(node);
+ typeName = varcharTypeInfo.getQualifiedName();
+ break;
+ case HiveParser.TOK_TIMESTAMPLOCALTZ:
+ TimestampLocalTZTypeInfo timestampLocalTZTypeInfo =
+ TypeInfoFactory.getTimestampTZTypeInfo(null);
+ typeName = timestampLocalTZTypeInfo.getQualifiedName();
+ break;
+ case HiveParser.TOK_DECIMAL:
+ DecimalTypeInfo decTypeInfo = ParseUtils.getDecimalTypeTypeInfo(node);
+ typeName = decTypeInfo.getQualifiedName();
+ break;
+ default:
+ typeName = TOKEN_TO_TYPE.get(token);
}
+ return typeName;
}
private static String getStructTypeStringFromAST(ASTNode typeNode)
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/DDLSemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/DDLSemanticAnalyzer.java
deleted file mode 100644
index ba019c7..0000000
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/DDLSemanticAnalyzer.java
+++ /dev/null
@@ -1,818 +0,0 @@
-/*
- * 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.hadoop.hive.ql.parse;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-
-import org.antlr.runtime.tree.CommonTree;
-import org.apache.hadoop.fs.Path;
-import org.apache.hadoop.hive.common.StatsSetupConst;
-import org.apache.hadoop.hive.common.TableName;
-import org.apache.hadoop.hive.conf.HiveConf;
-import org.apache.hadoop.hive.metastore.TableType;
-import org.apache.hadoop.hive.metastore.Warehouse;
-import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
-import org.apache.hadoop.hive.metastore.api.FieldSchema;
-import org.apache.hadoop.hive.metastore.api.MetaException;
-import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants;
-import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
-import org.apache.hadoop.hive.ql.ErrorMsg;
-import org.apache.hadoop.hive.ql.QueryState;
-import org.apache.hadoop.hive.ql.ddl.DDLDesc.DDLDescWithWriteId;
-import org.apache.hadoop.hive.ql.ddl.DDLWork;
-import org.apache.hadoop.hive.ql.ddl.privilege.PrincipalDesc;
-import org.apache.hadoop.hive.ql.ddl.table.AbstractAlterTableDesc;
-import org.apache.hadoop.hive.ql.ddl.table.AlterTableType;
-import org.apache.hadoop.hive.ql.ddl.table.misc.AlterTableRenameDesc;
-import org.apache.hadoop.hive.ql.ddl.table.misc.AlterTableSetOwnerDesc;
-import org.apache.hadoop.hive.ql.ddl.table.misc.AlterTableSetPropertiesDesc;
-import org.apache.hadoop.hive.ql.ddl.table.misc.AlterTableTouchDesc;
-import org.apache.hadoop.hive.ql.ddl.table.misc.AlterTableUnsetPropertiesDesc;
-import org.apache.hadoop.hive.ql.ddl.table.misc.TruncateTableDesc;
-import org.apache.hadoop.hive.ql.ddl.table.partition.PartitionUtils;
-import org.apache.hadoop.hive.ql.exec.ArchiveUtils;
-import org.apache.hadoop.hive.ql.exec.ColumnStatsUpdateTask;
-import org.apache.hadoop.hive.ql.exec.Task;
-import org.apache.hadoop.hive.ql.exec.TaskFactory;
-import org.apache.hadoop.hive.ql.exec.Utilities;
-import org.apache.hadoop.hive.ql.hooks.ReadEntity;
-import org.apache.hadoop.hive.ql.hooks.WriteEntity;
-import org.apache.hadoop.hive.ql.hooks.WriteEntity.WriteType;
-import org.apache.hadoop.hive.ql.io.AcidUtils;
-import org.apache.hadoop.hive.ql.io.RCFileInputFormat;
-import org.apache.hadoop.hive.ql.metadata.DefaultConstraint;
-import org.apache.hadoop.hive.ql.metadata.Hive;
-import org.apache.hadoop.hive.ql.metadata.HiveException;
-import org.apache.hadoop.hive.ql.metadata.NotNullConstraint;
-import org.apache.hadoop.hive.ql.metadata.Partition;
-import org.apache.hadoop.hive.ql.metadata.Table;
-import org.apache.hadoop.hive.ql.parse.authorization.AuthorizationParseUtils;
-import org.apache.hadoop.hive.ql.plan.BasicStatsWork;
-import org.apache.hadoop.hive.ql.plan.ColumnStatsUpdateWork;
-import org.apache.hadoop.hive.ql.plan.HiveOperation;
-import org.apache.hadoop.hive.ql.plan.ListBucketingCtx;
-import org.apache.hadoop.hive.ql.plan.LoadTableDesc;
-import org.apache.hadoop.hive.ql.plan.MoveWork;
-import org.apache.hadoop.hive.ql.plan.StatsWork;
-import org.apache.hadoop.hive.ql.plan.TableDesc;
-import org.apache.hadoop.hive.serde.serdeConstants;
-import org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo;
-import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
-import org.apache.hadoop.hive.serde2.typeinfo.TimestampLocalTZTypeInfo;
-import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
-import org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo;
-import org.apache.hadoop.mapred.InputFormat;
-
-/**
- * DDLSemanticAnalyzer.
- *
- */
-public class DDLSemanticAnalyzer extends BaseSemanticAnalyzer {
- private static final Map<Integer, String> TokenToTypeName = new HashMap<Integer, String>();
-
- // Equivalent to acidSinks, but for DDL operations that change data.
- private DDLDescWithWriteId ddlDescWithWriteId;
-
- static {
- TokenToTypeName.put(HiveParser.TOK_BOOLEAN, serdeConstants.BOOLEAN_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_TINYINT, serdeConstants.TINYINT_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_SMALLINT, serdeConstants.SMALLINT_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_INT, serdeConstants.INT_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_BIGINT, serdeConstants.BIGINT_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_FLOAT, serdeConstants.FLOAT_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_DOUBLE, serdeConstants.DOUBLE_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_STRING, serdeConstants.STRING_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_CHAR, serdeConstants.CHAR_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_VARCHAR, serdeConstants.VARCHAR_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_BINARY, serdeConstants.BINARY_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_DATE, serdeConstants.DATE_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_DATETIME, serdeConstants.DATETIME_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_TIMESTAMP, serdeConstants.TIMESTAMP_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_TIMESTAMPLOCALTZ, serdeConstants.TIMESTAMPLOCALTZ_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_INTERVAL_YEAR_MONTH, serdeConstants.INTERVAL_YEAR_MONTH_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_INTERVAL_DAY_TIME, serdeConstants.INTERVAL_DAY_TIME_TYPE_NAME);
- TokenToTypeName.put(HiveParser.TOK_DECIMAL, serdeConstants.DECIMAL_TYPE_NAME);
- }
-
- public static String getTypeName(ASTNode node) throws SemanticException {
- int token = node.getType();
- String typeName;
-
- // datetime type isn't currently supported
- if (token == HiveParser.TOK_DATETIME) {
- throw new SemanticException(ErrorMsg.UNSUPPORTED_TYPE.getMsg());
- }
-
- switch (token) {
- case HiveParser.TOK_CHAR:
- CharTypeInfo charTypeInfo = ParseUtils.getCharTypeInfo(node);
- typeName = charTypeInfo.getQualifiedName();
- break;
- case HiveParser.TOK_VARCHAR:
- VarcharTypeInfo varcharTypeInfo = ParseUtils.getVarcharTypeInfo(node);
- typeName = varcharTypeInfo.getQualifiedName();
- break;
- case HiveParser.TOK_TIMESTAMPLOCALTZ:
- TimestampLocalTZTypeInfo timestampLocalTZTypeInfo =
- TypeInfoFactory.getTimestampTZTypeInfo(null);
- typeName = timestampLocalTZTypeInfo.getQualifiedName();
- break;
- case HiveParser.TOK_DECIMAL:
- DecimalTypeInfo decTypeInfo = ParseUtils.getDecimalTypeTypeInfo(node);
- typeName = decTypeInfo.getQualifiedName();
- break;
- default:
- typeName = TokenToTypeName.get(token);
- }
- return typeName;
- }
-
- public DDLSemanticAnalyzer(QueryState queryState) throws SemanticException {
- this(queryState, createHiveDB(queryState.getConf()));
- }
-
- public DDLSemanticAnalyzer(QueryState queryState, Hive db) throws SemanticException {
- super(queryState, db);
- }
-
- @Override
- public void analyzeInternal(ASTNode input) throws SemanticException {
-
- ASTNode ast = input;
- switch (ast.getType()) {
- case HiveParser.TOK_ALTERTABLE: {
- ast = (ASTNode) input.getChild(1);
- final TableName tName =
- getQualifiedTableName((ASTNode) input.getChild(0), MetaStoreUtils.getDefaultCatalog(conf));
- // TODO CAT - for now always use the default catalog. Eventually will want to see if
- // the user specified a catalog
- Map<String, String> partSpec = null;
- ASTNode partSpecNode = (ASTNode)input.getChild(2);
- if (partSpecNode != null) {
- // We can use alter table partition rename to convert/normalize the legacy partition
- // column values. In so, we should not enable the validation to the old partition spec
- // passed in this command.
- if (ast.getType() == HiveParser.TOK_ALTERTABLE_RENAMEPART) {
- partSpec = getPartSpec(partSpecNode);
- } else {
- partSpec = getValidatedPartSpec(getTable(tName), partSpecNode, conf, false);
- }
- }
-
- if (ast.getType() == HiveParser.TOK_ALTERTABLE_RENAME) {
- analyzeAlterTableRename(tName, ast, false);
- } else if (ast.getType() == HiveParser.TOK_ALTERTABLE_TOUCH) {
- analyzeAlterTableTouch(tName, ast);
- } else if (ast.getType() == HiveParser.TOK_ALTERTABLE_PROPERTIES) {
- analyzeAlterTableProps(tName, null, ast, false, false);
- } else if (ast.getType() == HiveParser.TOK_ALTERTABLE_DROPPROPERTIES) {
- analyzeAlterTableProps(tName, null, ast, false, true);
- } else if (ast.getType() == HiveParser.TOK_ALTERTABLE_UPDATESTATS ||
- ast.getType() == HiveParser.TOK_ALTERPARTITION_UPDATESTATS) {
- analyzeAlterTableProps(tName, partSpec, ast, false, false);
- } else if(ast.getToken().getType() == HiveParser.TOK_ALTERTABLE_UPDATECOLSTATS ||
- ast.getToken().getType() == HiveParser.TOK_ALTERPARTITION_UPDATECOLSTATS){
- analyzeAlterTableUpdateStats(ast, tName, partSpec);
- } else if (ast.getToken().getType() == HiveParser.TOK_ALTERTABLE_OWNER) {
- analyzeAlterTableOwner(ast, tName);
- }
- break;
- }
- case HiveParser.TOK_TRUNCATETABLE:
- analyzeTruncateTable(ast);
- break;
- case HiveParser.TOK_ALTERVIEW: {
- final TableName tName = getQualifiedTableName((ASTNode) ast.getChild(0));
- ast = (ASTNode) ast.getChild(1);
- if (ast.getType() == HiveParser.TOK_ALTERVIEW_PROPERTIES) {
- analyzeAlterTableProps(tName, null, ast, true, false);
- } else if (ast.getType() == HiveParser.TOK_ALTERVIEW_DROPPROPERTIES) {
- analyzeAlterTableProps(tName, null, ast, true, true);
- } else if (ast.getType() == HiveParser.TOK_ALTERVIEW_RENAME) {
- analyzeAlterTableRename(tName, ast, true);
- }
- break;
- }
- default:
- throw new SemanticException("Unsupported command: " + ast);
- }
- if (fetchTask != null && !rootTasks.isEmpty()) {
- rootTasks.get(rootTasks.size() - 1).setFetchSource(true);
- }
- }
-
- private void analyzeAlterTableUpdateStats(ASTNode ast, TableName tblName, Map<String, String> partSpec)
- throws SemanticException {
- String colName = getUnescapedName((ASTNode) ast.getChild(0));
- Map<String, String> mapProp = getProps((ASTNode) (ast.getChild(1)).getChild(0));
-
- Table tbl = getTable(tblName);
- String partName = null;
- if (partSpec != null) {
- try {
- partName = Warehouse.makePartName(partSpec, false);
- } catch (MetaException e) {
- throw new SemanticException("partition " + partSpec.toString()
- + " not found");
- }
- }
-
- String colType = null;
- List<FieldSchema> cols = tbl.getCols();
- for (FieldSchema col : cols) {
- if (colName.equalsIgnoreCase(col.getName())) {
- colType = col.getType();
- break;
- }
- }
-
- if (colType == null) {
- throw new SemanticException("column type not found");
- }
-
- ColumnStatsUpdateWork columnStatsUpdateWork =
- new ColumnStatsUpdateWork(partName, mapProp, tbl.getDbName(), tbl.getTableName(), colName, colType);
- ColumnStatsUpdateTask cStatsUpdateTask = (ColumnStatsUpdateTask) TaskFactory
- .get(columnStatsUpdateWork);
- // TODO: doesn't look like this path is actually ever exercised. Maybe this needs to be removed.
- addInputsOutputsAlterTable(tblName, partSpec, null, AlterTableType.UPDATESTATS, false);
- if (AcidUtils.isTransactionalTable(tbl)) {
- setAcidDdlDesc(columnStatsUpdateWork);
- }
- rootTasks.add(cStatsUpdateTask);
- }
-
- private void analyzeTruncateTable(ASTNode ast) throws SemanticException {
- ASTNode root = (ASTNode) ast.getChild(0); // TOK_TABLE_PARTITION
- final String tableName = getUnescapedName((ASTNode) root.getChild(0));
-
- Table table = getTable(tableName, true);
- final TableName tName = HiveTableName.of(table);
- checkTruncateEligibility(ast, root, tableName, table);
-
- Map<String, String> partSpec = getPartSpec((ASTNode) root.getChild(1));
- addTruncateTableOutputs(root, table, partSpec);
-
- Task<?> truncateTask = null;
-
- // Is this a truncate column command
- ASTNode colNamesNode = (ASTNode) ast.getFirstChildWithType(HiveParser.TOK_TABCOLNAME);
- if (colNamesNode == null) {
- truncateTask = getTruncateTaskWithoutColumnNames(tName, partSpec, table);
- } else {
- truncateTask = getTruncateTaskWithColumnNames(root, tName, table, partSpec, colNamesNode);
- }
-
- rootTasks.add(truncateTask);
- }
-
- private void checkTruncateEligibility(ASTNode ast, ASTNode root, String tableName, Table table)
- throws SemanticException {
- boolean isForce = ast.getFirstChildWithType(HiveParser.TOK_FORCE) != null;
- if (!isForce) {
- if (table.getTableType() != TableType.MANAGED_TABLE &&
- (table.getParameters().getOrDefault(MetaStoreUtils.EXTERNAL_TABLE_PURGE, "FALSE"))
- .equalsIgnoreCase("FALSE")) {
- throw new SemanticException(ErrorMsg.TRUNCATE_FOR_NON_MANAGED_TABLE.format(tableName));
- }
- }
- if (table.isNonNative()) {
- throw new SemanticException(ErrorMsg.TRUNCATE_FOR_NON_NATIVE_TABLE.format(tableName)); //TODO
- }
- if (!table.isPartitioned() && root.getChildCount() > 1) {
- throw new SemanticException(ErrorMsg.PARTSPEC_FOR_NON_PARTITIONED_TABLE.format(tableName));
- }
- }
-
- private void addTruncateTableOutputs(ASTNode root, Table table, Map<String, String> partSpec)
- throws SemanticException {
- if (partSpec == null) {
- if (!table.isPartitioned()) {
- outputs.add(new WriteEntity(table, WriteEntity.WriteType.DDL_EXCLUSIVE));
- } else {
- for (Partition partition : PartitionUtils.getPartitions(db, table, null, false)) {
- outputs.add(new WriteEntity(partition, WriteEntity.WriteType.DDL_EXCLUSIVE));
- }
- }
- } else {
- if (isFullSpec(table, partSpec)) {
- validatePartSpec(table, partSpec, (ASTNode) root.getChild(1), conf, true);
- Partition partition = PartitionUtils.getPartition(db, table, partSpec, true);
- outputs.add(new WriteEntity(partition, WriteEntity.WriteType.DDL_EXCLUSIVE));
- } else {
- validatePartSpec(table, partSpec, (ASTNode) root.getChild(1), conf, false);
- for (Partition partition : PartitionUtils.getPartitions(db, table, partSpec, false)) {
- outputs.add(new WriteEntity(partition, WriteEntity.WriteType.DDL_EXCLUSIVE));
- }
- }
- }
- }
-
- private Task<?> getTruncateTaskWithoutColumnNames(TableName tableName, Map<String, String> partSpec, Table table) {
- TruncateTableDesc truncateTblDesc = new TruncateTableDesc(tableName, partSpec, null, table);
- if (truncateTblDesc.mayNeedWriteId()) {
- setAcidDdlDesc(truncateTblDesc);
- }
-
- DDLWork ddlWork = new DDLWork(getInputs(), getOutputs(), truncateTblDesc);
- return TaskFactory.get(ddlWork);
- }
-
- private Task<?> getTruncateTaskWithColumnNames(ASTNode root, TableName tName, Table table,
- Map<String, String> partSpec, ASTNode colNamesNode) throws SemanticException {
- try {
- List<String> columnNames = getColumnNames(colNamesNode);
-
- // It would be possible to support this, but this is such a pointless command.
- if (AcidUtils.isInsertOnlyTable(table.getParameters())) {
- throw new SemanticException("Truncating MM table columns not presently supported");
- }
-
- List<String> bucketCols = null;
- Class<? extends InputFormat> inputFormatClass = null;
- boolean isArchived = false;
- Path newTblPartLoc = null;
- Path oldTblPartLoc = null;
- List<FieldSchema> cols = null;
- ListBucketingCtx lbCtx = null;
- boolean isListBucketed = false;
- List<String> listBucketColNames = null;
-
- if (table.isPartitioned()) {
- Partition part = db.getPartition(table, partSpec, false);
-
- Path tabPath = table.getPath();
- Path partPath = part.getDataLocation();
-
- // if the table is in a different dfs than the partition,
- // replace the partition's dfs with the table's dfs.
- newTblPartLoc = new Path(tabPath.toUri().getScheme(), tabPath.toUri()
- .getAuthority(), partPath.toUri().getPath());
-
- oldTblPartLoc = partPath;
-
- cols = part.getCols();
- bucketCols = part.getBucketCols();
- inputFormatClass = part.getInputFormatClass();
- isArchived = ArchiveUtils.isArchived(part);
- lbCtx = constructListBucketingCtx(part.getSkewedColNames(), part.getSkewedColValues(),
- part.getSkewedColValueLocationMaps(), part.isStoredAsSubDirectories());
- isListBucketed = part.isStoredAsSubDirectories();
- listBucketColNames = part.getSkewedColNames();
- } else {
- // input and output are the same
- oldTblPartLoc = table.getPath();
- newTblPartLoc = table.getPath();
- cols = table.getCols();
- bucketCols = table.getBucketCols();
- inputFormatClass = table.getInputFormatClass();
- lbCtx = constructListBucketingCtx(table.getSkewedColNames(), table.getSkewedColValues(),
- table.getSkewedColValueLocationMaps(), table.isStoredAsSubDirectories());
- isListBucketed = table.isStoredAsSubDirectories();
- listBucketColNames = table.getSkewedColNames();
- }
-
- // throw a HiveException for non-rcfile.
- if (!inputFormatClass.equals(RCFileInputFormat.class)) {
- throw new SemanticException(ErrorMsg.TRUNCATE_COLUMN_NOT_RC.getMsg());
- }
-
- // throw a HiveException if the table/partition is archived
- if (isArchived) {
- throw new SemanticException(ErrorMsg.TRUNCATE_COLUMN_ARCHIVED.getMsg());
- }
-
- Set<Integer> columnIndexes = new HashSet<Integer>();
- for (String columnName : columnNames) {
- boolean found = false;
- for (int columnIndex = 0; columnIndex < cols.size(); columnIndex++) {
- if (columnName.equalsIgnoreCase(cols.get(columnIndex).getName())) {
- columnIndexes.add(columnIndex);
- found = true;
- break;
- }
- }
- // Throw an exception if the user is trying to truncate a column which doesn't exist
- if (!found) {
- throw new SemanticException(ErrorMsg.INVALID_COLUMN.getMsg(columnName));
- }
- // Throw an exception if the table/partition is bucketed on one of the columns
- for (String bucketCol : bucketCols) {
- if (bucketCol.equalsIgnoreCase(columnName)) {
- throw new SemanticException(ErrorMsg.TRUNCATE_BUCKETED_COLUMN.getMsg(columnName));
- }
- }
- if (isListBucketed) {
- for (String listBucketCol : listBucketColNames) {
- if (listBucketCol.equalsIgnoreCase(columnName)) {
- throw new SemanticException(
- ErrorMsg.TRUNCATE_LIST_BUCKETED_COLUMN.getMsg(columnName));
- }
- }
- }
- }
-
- Path queryTmpdir = ctx.getExternalTmpPath(newTblPartLoc);
- TruncateTableDesc truncateTblDesc = new TruncateTableDesc(tName, partSpec, null, table,
- new ArrayList<Integer>(columnIndexes), oldTblPartLoc, queryTmpdir, lbCtx);
- if (truncateTblDesc.mayNeedWriteId()) {
- setAcidDdlDesc(truncateTblDesc);
- }
-
- DDLWork ddlWork = new DDLWork(getInputs(), getOutputs(), truncateTblDesc);
- Task<?> truncateTask = TaskFactory.get(ddlWork);
-
- addInputsOutputsAlterTable(tName, partSpec, null, AlterTableType.TRUNCATE, false);
- ddlWork.setNeedLock(true);
- TableDesc tblDesc = Utilities.getTableDesc(table);
- // Write the output to temporary directory and move it to the final location at the end
- // so the operation is atomic.
- LoadTableDesc ltd = new LoadTableDesc(queryTmpdir, tblDesc, partSpec == null ? new HashMap<>() : partSpec);
- ltd.setLbCtx(lbCtx);
- Task<MoveWork> moveTsk = TaskFactory.get(new MoveWork(null, null, ltd, null, false));
- truncateTask.addDependentTask(moveTsk);
-
- // Recalculate the HDFS stats if auto gather stats is set
- if (conf.getBoolVar(HiveConf.ConfVars.HIVESTATSAUTOGATHER)) {
- BasicStatsWork basicStatsWork;
- if (oldTblPartLoc.equals(newTblPartLoc)) {
- // If we're merging to the same location, we can avoid some metastore calls
- TableSpec tablepart = new TableSpec(this.db, conf, root);
- basicStatsWork = new BasicStatsWork(tablepart);
- } else {
- basicStatsWork = new BasicStatsWork(ltd);
- }
- basicStatsWork.setNoStatsAggregator(true);
- basicStatsWork.setClearAggregatorStats(true);
- StatsWork columnStatsWork = new StatsWork(table, basicStatsWork, conf);
-
- Task<?> statTask = TaskFactory.get(columnStatsWork);
- moveTsk.addDependentTask(statTask);
- }
-
- return truncateTask;
- } catch (HiveException e) {
- throw new SemanticException(e);
- }
- }
-
- public static boolean isFullSpec(Table table, Map<String, String> partSpec) {
- for (FieldSchema partCol : table.getPartCols()) {
- if (partSpec.get(partCol.getName()) == null) {
- return false;
- }
- }
- return true;
- }
-
- private void validateAlterTableType(Table tbl, AlterTableType op) throws SemanticException {
- validateAlterTableType(tbl, op, false);
- }
-
- private void validateAlterTableType(Table tbl, AlterTableType op, boolean expectView)
- throws SemanticException {
- if (tbl.isView()) {
- if (!expectView) {
- throw new SemanticException(ErrorMsg.ALTER_COMMAND_FOR_VIEWS.getMsg());
- }
-
- switch (op) {
- case ADDPARTITION:
- case DROPPARTITION:
- case RENAMEPARTITION:
- case ADDPROPS:
- case DROPPROPS:
- case RENAME:
- // allow this form
- break;
- default:
- throw new SemanticException(ErrorMsg.ALTER_VIEW_DISALLOWED_OP.getMsg(op.toString()));
- }
- } else {
- if (expectView) {
- throw new SemanticException(ErrorMsg.ALTER_COMMAND_FOR_TABLES.getMsg());
- }
- }
- if (tbl.isNonNative() && !AlterTableType.NON_NATIVE_TABLE_ALLOWED.contains(op)) {
- throw new SemanticException(ErrorMsg.ALTER_TABLE_NON_NATIVE.format(
- AlterTableType.NON_NATIVE_TABLE_ALLOWED.toString(), tbl.getTableName()));
- }
- }
-
- private boolean hasConstraintsEnabled(final String tblName) throws SemanticException{
-
- NotNullConstraint nnc = null;
- DefaultConstraint dc = null;
- try {
- // retrieve enabled NOT NULL constraint from metastore
- nnc = Hive.get().getEnabledNotNullConstraints(
- db.getDatabaseCurrent().getName(), tblName);
- dc = Hive.get().getEnabledDefaultConstraints(
- db.getDatabaseCurrent().getName(), tblName);
- } catch (Exception e) {
- if (e instanceof SemanticException) {
- throw (SemanticException) e;
- } else {
- throw (new RuntimeException(e));
- }
- }
- if((nnc != null && !nnc.getNotNullConstraints().isEmpty())
- || (dc != null && !dc.getDefaultConstraints().isEmpty())) {
- return true;
- }
- return false;
- }
-
- private void analyzeAlterTableProps(TableName tableName, Map<String, String> partSpec, ASTNode ast,
- boolean expectView, boolean isUnset) throws SemanticException {
-
- Map<String, String> mapProp = getProps((ASTNode) (ast.getChild(0)).getChild(0));
- EnvironmentContext environmentContext = null;
- // we need to check if the properties are valid, especially for stats.
- // they might be changed via alter table .. update statistics or
- // alter table .. set tblproperties. If the property is not row_count
- // or raw_data_size, it could not be changed through update statistics
- boolean changeStatsSucceeded = false;
- for (Entry<String, String> entry : mapProp.entrySet()) {
- // we make sure that we do not change anything if there is anything
- // wrong.
- if (entry.getKey().equals(StatsSetupConst.ROW_COUNT)
- || entry.getKey().equals(StatsSetupConst.RAW_DATA_SIZE)) {
- try {
- Long.parseLong(entry.getValue());
- changeStatsSucceeded = true;
- } catch (Exception e) {
- throw new SemanticException("AlterTable " + entry.getKey() + " failed with value "
- + entry.getValue());
- }
- }
- // if table is being modified to be external we need to make sure existing table
- // doesn't have enabled constraint since constraints are disallowed with such tables
- else if (entry.getKey().equals("external") && entry.getValue().equals("true")) {
- if (hasConstraintsEnabled(tableName.getTable())) {
- throw new SemanticException(
- ErrorMsg.INVALID_CSTR_SYNTAX.getMsg("Table: " + tableName.getDbTable() + " has constraints enabled."
- + "Please remove those constraints to change this property."));
- }
- }
- else {
- if (queryState.getCommandType()
- .equals(HiveOperation.ALTERTABLE_UPDATETABLESTATS.getOperationName())
- || queryState.getCommandType()
- .equals(HiveOperation.ALTERTABLE_UPDATEPARTSTATS.getOperationName())) {
- throw new SemanticException("AlterTable UpdateStats " + entry.getKey()
- + " failed because the only valid keys are " + StatsSetupConst.ROW_COUNT + " and "
- + StatsSetupConst.RAW_DATA_SIZE);
- }
- }
-
- if (changeStatsSucceeded) {
- environmentContext = new EnvironmentContext();
- environmentContext.putToProperties(StatsSetupConst.STATS_GENERATED, StatsSetupConst.USER);
- }
- }
- boolean isToTxn = AcidUtils.isTablePropertyTransactional(mapProp)
- || mapProp.containsKey(hive_metastoreConstants.TABLE_TRANSACTIONAL_PROPERTIES);
- boolean isExplicitStatsUpdate = changeStatsSucceeded && AcidUtils.isTransactionalTable(getTable(tableName, true));
- AbstractAlterTableDesc alterTblDesc = null;
- DDLWork ddlWork = null;
-
- if (isUnset) {
- boolean dropIfExists = ast.getChild(1) != null;
- // validate Unset Non Existed Table Properties
- if (!dropIfExists) {
- Table tab = getTable(tableName, true);
- Map<String, String> tableParams = tab.getTTable().getParameters();
- for (String currKey : mapProp.keySet()) {
- if (!tableParams.containsKey(currKey)) {
- String errorMsg = "The following property " + currKey + " does not exist in " + tab.getTableName();
- throw new SemanticException(
- ErrorMsg.ALTER_TBL_UNSET_NON_EXIST_PROPERTY.getMsg(errorMsg));
- }
- }
- }
-
- alterTblDesc = new AlterTableUnsetPropertiesDesc(tableName, partSpec, null, expectView, mapProp,
- isExplicitStatsUpdate, environmentContext);
- addInputsOutputsAlterTable(tableName, partSpec, alterTblDesc, alterTblDesc.getType(), isToTxn);
- ddlWork = new DDLWork(getInputs(), getOutputs(), alterTblDesc);
- } else {
- addPropertyReadEntry(mapProp, inputs);
- boolean isAcidConversion = isToTxn && AcidUtils.isFullAcidTable(mapProp)
- && !AcidUtils.isFullAcidTable(getTable(tableName, true));
- alterTblDesc = new AlterTableSetPropertiesDesc(tableName, partSpec, null, expectView, mapProp,
- isExplicitStatsUpdate, isAcidConversion, environmentContext);
- addInputsOutputsAlterTable(tableName, partSpec, alterTblDesc, alterTblDesc.getType(), isToTxn);
- ddlWork = new DDLWork(getInputs(), getOutputs(), alterTblDesc);
- }
- if (isToTxn) {
- ddlWork.setNeedLock(true); // Hmm... why don't many other operations here need locks?
- }
- if (isToTxn || isExplicitStatsUpdate) {
- setAcidDdlDesc(alterTblDesc);
- }
-
- rootTasks.add(TaskFactory.get(ddlWork));
- }
-
- private void setAcidDdlDesc(DDLDescWithWriteId descWithWriteId) {
- if(this.ddlDescWithWriteId != null) {
- throw new IllegalStateException("ddlDescWithWriteId is already set: " + this.ddlDescWithWriteId);
- }
- this.ddlDescWithWriteId = descWithWriteId;
- }
-
- @Override
- public DDLDescWithWriteId getAcidDdlDesc() {
- return ddlDescWithWriteId;
- }
-
- // For the time while all the alter table operations are getting migrated there is a duplication of this method here
- private WriteType determineAlterTableWriteType(Table tab, AbstractAlterTableDesc desc, AlterTableType op) {
- boolean convertingToAcid = false;
- if (desc != null && desc.getProps() != null &&
- Boolean.parseBoolean(desc.getProps().get(hive_metastoreConstants.TABLE_IS_TRANSACTIONAL))) {
- convertingToAcid = true;
- }
- if(!AcidUtils.isTransactionalTable(tab) && convertingToAcid) {
- //non-acid to transactional conversion (property itself) must be mutexed to prevent concurrent writes.
- // See HIVE-16688 for use cases.
- return WriteType.DDL_EXCLUSIVE;
- }
- return WriteEntity.determineAlterTableWriteType(op);
- }
-
- private void addInputsOutputsAlterTable(TableName tableName, Map<String, String> partSpec,
- AbstractAlterTableDesc desc, AlterTableType op, boolean doForceExclusive) throws SemanticException {
- boolean isCascade = desc != null && desc.isCascade();
- boolean alterPartitions = partSpec != null && !partSpec.isEmpty();
- //cascade only occurs at table level then cascade to partition level
- if (isCascade && alterPartitions) {
- throw new SemanticException(
- ErrorMsg.ALTER_TABLE_PARTITION_CASCADE_NOT_SUPPORTED, op.getName());
- }
-
- Table tab = getTable(tableName, true);
- // cascade only occurs with partitioned table
- if (isCascade && !tab.isPartitioned()) {
- throw new SemanticException(
- ErrorMsg.ALTER_TABLE_NON_PARTITIONED_TABLE_CASCADE_NOT_SUPPORTED);
- }
-
- // Determine the lock type to acquire
- WriteEntity.WriteType writeType = doForceExclusive
- ? WriteType.DDL_EXCLUSIVE : determineAlterTableWriteType(tab, desc, op);
-
- if (!alterPartitions) {
- inputs.add(new ReadEntity(tab));
- WriteEntity alterTableOutput = new WriteEntity(tab, writeType);
- outputs.add(alterTableOutput);
- //do not need the lock for partitions since they are covered by the table lock
- if (isCascade) {
- for (Partition part : PartitionUtils.getPartitions(db, tab, partSpec, false)) {
- outputs.add(new WriteEntity(part, WriteEntity.WriteType.DDL_NO_LOCK));
- }
- }
- } else {
- ReadEntity re = new ReadEntity(tab);
- // In the case of altering a table for its partitions we don't need to lock the table
- // itself, just the partitions. But the table will have a ReadEntity. So mark that
- // ReadEntity as no lock.
- re.noLockNeeded();
- inputs.add(re);
-
- if (isFullSpec(tab, partSpec)) {
- // Fully specified partition spec
- Partition part = PartitionUtils.getPartition(db, tab, partSpec, true);
- outputs.add(new WriteEntity(part, writeType));
- } else {
- // Partial partition spec supplied. Make sure this is allowed.
- if (!AlterTableType.SUPPORT_PARTIAL_PARTITION_SPEC.contains(op)) {
- throw new SemanticException(
- ErrorMsg.ALTER_TABLE_TYPE_PARTIAL_PARTITION_SPEC_NO_SUPPORTED, op.getName());
- } else if (!conf.getBoolVar(HiveConf.ConfVars.DYNAMICPARTITIONING)) {
- throw new SemanticException(ErrorMsg.DYNAMIC_PARTITION_DISABLED);
- }
-
- for (Partition part : PartitionUtils.getPartitions(db, tab, partSpec, true)) {
- outputs.add(new WriteEntity(part, writeType));
- }
- }
- }
-
- if (desc != null) {
- validateAlterTableType(tab, op, desc.expectView());
- }
- }
-
- private void analyzeAlterTableOwner(ASTNode ast, TableName tableName) throws SemanticException {
- PrincipalDesc ownerPrincipal = AuthorizationParseUtils.getPrincipalDesc((ASTNode) ast.getChild(0));
-
- if (ownerPrincipal.getType() == null) {
- throw new SemanticException("Owner type can't be null in alter table set owner command");
- }
-
- if (ownerPrincipal.getName() == null) {
- throw new SemanticException("Owner name can't be null in alter table set owner command");
- }
-
- AlterTableSetOwnerDesc alterTblDesc = new AlterTableSetOwnerDesc(tableName, ownerPrincipal);
- rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(), alterTblDesc), conf));
- }
-
- /**
- * Utility class to resolve QualifiedName
- */
- static class QualifiedNameUtil {
-
- /**
- * Get the fully qualified name in the ast. e.g. the ast of the form ^(DOT
- * ^(DOT a b) c) will generate a name of the form a.b.c
- *
- * @param ast
- * The AST from which the qualified name has to be extracted
- * @return String
- */
- static public String getFullyQualifiedName(ASTNode ast) {
- if (ast.getChildCount() == 0) {
- return ast.getText();
- } else if (ast.getChildCount() == 2) {
- return getFullyQualifiedName((ASTNode) ast.getChild(0)) + "."
- + getFullyQualifiedName((ASTNode) ast.getChild(1));
- } else if (ast.getChildCount() == 3) {
- return getFullyQualifiedName((ASTNode) ast.getChild(0)) + "."
- + getFullyQualifiedName((ASTNode) ast.getChild(1)) + "."
- + getFullyQualifiedName((ASTNode) ast.getChild(2));
- } else {
- return null;
- }
- }
- }
-
- private void analyzeAlterTableRename(TableName source, ASTNode ast, boolean expectView)
- throws SemanticException {
- final TableName target = getQualifiedTableName((ASTNode) ast.getChild(0));
-
- AlterTableRenameDesc alterTblDesc = new AlterTableRenameDesc(source, null, expectView, target.getDbTable());
- Table table = getTable(source.getDbTable(), true);
- if (AcidUtils.isTransactionalTable(table)) {
- setAcidDdlDesc(alterTblDesc);
- }
- addInputsOutputsAlterTable(source, null, alterTblDesc, alterTblDesc.getType(), false);
- rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(), alterTblDesc)));
- }
-
- /**
- * Rewrite the metadata for one or more partitions in a table. Useful when
- * an external process modifies files on HDFS and you want the pre/post
- * hooks to be fired for the specified partition.
- *
- * @param ast
- * The parsed command tree.
- * @throws SemanticException
- * Parsing failed
- */
- private void analyzeAlterTableTouch(TableName tName, CommonTree ast) throws SemanticException {
-
- Table tab = getTable(tName);
- validateAlterTableType(tab, AlterTableType.TOUCH);
- inputs.add(new ReadEntity(tab));
-
- // partition name to value
- List<Map<String, String>> partSpecs = getPartitionSpecs(tab, ast);
-
- if (partSpecs.isEmpty()) {
- AlterTableTouchDesc touchDesc = new AlterTableTouchDesc(tName.getDbTable(), null);
- outputs.add(new WriteEntity(tab, WriteEntity.WriteType.DDL_NO_LOCK));
- rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(), touchDesc)));
- } else {
- PartitionUtils.addTablePartsOutputs(db, outputs, tab, partSpecs, false, WriteEntity.WriteType.DDL_NO_LOCK);
- for (Map<String, String> partSpec : partSpecs) {
- AlterTableTouchDesc touchDesc = new AlterTableTouchDesc(tName.getDbTable(), partSpec);
- rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(), touchDesc)));
- }
- }
- }
-}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
index 1af47db..841f92b 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
@@ -108,8 +108,8 @@ import org.apache.hadoop.hive.ql.ddl.misc.hooks.InsertCommitHookDesc;
import org.apache.hadoop.hive.ql.ddl.table.constraint.ConstraintsUtils;
import org.apache.hadoop.hive.ql.ddl.table.create.CreateTableDesc;
import org.apache.hadoop.hive.ql.ddl.table.create.like.CreateTableLikeDesc;
-import org.apache.hadoop.hive.ql.ddl.table.misc.AlterTableUnsetPropertiesDesc;
-import org.apache.hadoop.hive.ql.ddl.table.misc.PreInsertTableDesc;
+import org.apache.hadoop.hive.ql.ddl.table.misc.preinsert.PreInsertTableDesc;
+import org.apache.hadoop.hive.ql.ddl.table.misc.properties.AlterTableUnsetPropertiesDesc;
import org.apache.hadoop.hive.ql.ddl.table.storage.skewed.SkewedTableUtils;
import org.apache.hadoop.hive.ql.ddl.view.create.CreateViewDesc;
import org.apache.hadoop.hive.ql.ddl.view.materialized.update.MaterializedViewUpdateDesc;
@@ -304,7 +304,7 @@ import com.google.common.math.LongMath;
/**
* Implementation of the semantic analyzer. It generates the query plan.
* There are other specific semantic analyzers for some hive operations such as
- * DDLSemanticAnalyzer for ddl operations.
+ * various analyzers for DDL commands.
*/
public class SemanticAnalyzer extends BaseSemanticAnalyzer {
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzerFactory.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzerFactory.java
index 9748fbd..9cb3e70 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzerFactory.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzerFactory.java
@@ -83,32 +83,13 @@ public final class SemanticAnalyzerFactory {
return new ReplicationSemanticAnalyzer(queryState);
case HiveParser.TOK_REPL_STATUS:
return new ReplicationSemanticAnalyzer(queryState);
- case HiveParser.TOK_ALTERTABLE: {
- Tree child = tree.getChild(1);
- queryState.setCommandType(HiveOperation.operationForToken(child.getType()));
- return new DDLSemanticAnalyzer(queryState);
- }
case HiveParser.TOK_ALTERVIEW: {
Tree child = tree.getChild(1);
- switch (child.getType()) {
- case HiveParser.TOK_ALTERVIEW_PROPERTIES:
- case HiveParser.TOK_ALTERVIEW_DROPPROPERTIES:
- case HiveParser.TOK_ALTERVIEW_RENAME:
- opType = HiveOperation.operationForToken(child.getType());
- queryState.setCommandType(opType);
- return new DDLSemanticAnalyzer(queryState);
- }
// TOK_ALTERVIEW_AS
assert child.getType() == HiveParser.TOK_QUERY;
queryState.setCommandType(HiveOperation.ALTERVIEW_AS);
return new SemanticAnalyzer(queryState);
}
- case HiveParser.TOK_MSCK:
- case HiveParser.TOK_SHOWDBLOCKS:
- case HiveParser.TOK_SHOWCONF:
- case HiveParser.TOK_TRUNCATETABLE:
- return new DDLSemanticAnalyzer(queryState);
-
case HiveParser.TOK_ANALYZE:
return new ColumnStatsSemanticAnalyzer(queryState);
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/authorization/HiveAuthorizationTaskFactoryImpl.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/authorization/HiveAuthorizationTaskFactoryImpl.java
index d54325d..c4f6e3b 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/authorization/HiveAuthorizationTaskFactoryImpl.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/authorization/HiveAuthorizationTaskFactoryImpl.java
@@ -53,7 +53,6 @@ import org.apache.hadoop.hive.ql.metadata.Partition;
import org.apache.hadoop.hive.ql.metadata.Table;
import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer;
-import org.apache.hadoop.hive.ql.parse.DDLSemanticAnalyzer;
import org.apache.hadoop.hive.ql.parse.HiveParser;
import org.apache.hadoop.hive.ql.parse.SemanticException;
import org.apache.hadoop.hive.ql.security.authorization.Privilege;
@@ -262,7 +261,7 @@ public class HiveAuthorizationTaskFactoryImpl implements HiveAuthorizationTaskFa
for (int i = 1; i < child.getChildCount(); i++) {
gchild = (ASTNode) child.getChild(i);
if (gchild.getType() == HiveParser.TOK_PARTSPEC) {
- partSpec = DDLSemanticAnalyzer.getPartSpec(gchild);
+ partSpec = BaseSemanticAnalyzer.getPartSpec(gchild);
} else if (gchild.getType() == HiveParser.TOK_TABCOLNAME) {
columns = BaseSemanticAnalyzer.getColumnNames(gchild);
}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/RenameTableHandler.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/RenameTableHandler.java
index 82e50ff..7a4cb93 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/RenameTableHandler.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/RenameTableHandler.java
@@ -21,7 +21,7 @@ import org.apache.hadoop.hive.common.TableName;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.messaging.AlterTableMessage;
import org.apache.hadoop.hive.ql.ddl.DDLWork;
-import org.apache.hadoop.hive.ql.ddl.table.misc.AlterTableRenameDesc;
+import org.apache.hadoop.hive.ql.ddl.table.misc.rename.AlterTableRenameDesc;
import org.apache.hadoop.hive.ql.exec.Task;
import org.apache.hadoop.hive.ql.exec.TaskFactory;
import org.apache.hadoop.hive.ql.exec.repl.util.ReplUtils;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/TruncatePartitionHandler.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/TruncatePartitionHandler.java
index 25e524a..6c3a7eb 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/TruncatePartitionHandler.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/TruncatePartitionHandler.java
@@ -21,7 +21,7 @@ import org.apache.hadoop.hive.common.TableName;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.messaging.AlterPartitionMessage;
import org.apache.hadoop.hive.ql.ddl.DDLWork;
-import org.apache.hadoop.hive.ql.ddl.table.misc.TruncateTableDesc;
+import org.apache.hadoop.hive.ql.ddl.table.misc.truncate.TruncateTableDesc;
import org.apache.hadoop.hive.ql.exec.Task;
import org.apache.hadoop.hive.ql.exec.TaskFactory;
import org.apache.hadoop.hive.ql.exec.repl.util.ReplUtils;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/TruncateTableHandler.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/TruncateTableHandler.java
index 35b8e0e..2b12be4 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/TruncateTableHandler.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/load/message/TruncateTableHandler.java
@@ -20,7 +20,7 @@ package org.apache.hadoop.hive.ql.parse.repl.load.message;
import org.apache.hadoop.hive.common.TableName;
import org.apache.hadoop.hive.metastore.messaging.AlterTableMessage;
import org.apache.hadoop.hive.ql.ddl.DDLWork;
-import org.apache.hadoop.hive.ql.ddl.table.misc.TruncateTableDesc;
+import org.apache.hadoop.hive.ql.ddl.table.misc.truncate.TruncateTableDesc;
import org.apache.hadoop.hive.ql.exec.Task;
import org.apache.hadoop.hive.ql.exec.TaskFactory;
import org.apache.hadoop.hive.ql.exec.repl.util.ReplUtils;
diff --git a/ql/src/test/org/apache/hadoop/hive/ql/parse/authorization/AuthorizationTestUtil.java b/ql/src/test/org/apache/hadoop/hive/ql/parse/authorization/AuthorizationTestUtil.java
index 72a6c66..99eead3 100644
--- a/ql/src/test/org/apache/hadoop/hive/ql/parse/authorization/AuthorizationTestUtil.java
+++ b/ql/src/test/org/apache/hadoop/hive/ql/parse/authorization/AuthorizationTestUtil.java
@@ -27,7 +27,6 @@ import org.apache.hadoop.hive.ql.exec.Task;
import org.apache.hadoop.hive.ql.metadata.Hive;
import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer;
-import org.apache.hadoop.hive.ql.parse.DDLSemanticAnalyzer;
import org.apache.hadoop.hive.ql.parse.ParseUtils;
import org.apache.hadoop.hive.ql.session.SessionState;
import org.junit.Assert;
@@ -39,9 +38,6 @@ public class AuthorizationTestUtil {
public static DDLWork analyze(ASTNode ast, QueryState queryState, Hive db) throws Exception {
BaseSemanticAnalyzer analyzer = DDLSemanticAnalyzerFactory.getAnalyzer(ast, queryState, db);
- if (analyzer == null) {
- analyzer = new DDLSemanticAnalyzer(queryState, db);
- }
SessionState.start(queryState.getConf());
analyzer.analyze(ast, new Context(queryState.getConf()));
List<Task<?>> rootTasks = analyzer.getRootTasks();
diff --git a/ql/src/test/results/clientnegative/alter_tableprops_external_with_default_constraint.q.out b/ql/src/test/results/clientnegative/alter_tableprops_external_with_default_constraint.q.out
index de5e276..47cd924 100644
--- a/ql/src/test/results/clientnegative/alter_tableprops_external_with_default_constraint.q.out
+++ b/ql/src/test/results/clientnegative/alter_tableprops_external_with_default_constraint.q.out
@@ -6,4 +6,4 @@ POSTHOOK: query: CREATE TABLE table1 (a STRING DEFAULT 'hive', b STRING)
POSTHOOK: type: CREATETABLE
POSTHOOK: Output: database:default
POSTHOOK: Output: default@table1
-FAILED: SemanticException [Error 10326]: Invalid Constraint syntax Table: default.table1 has constraints enabled.Please remove those constraints to change this property.
+FAILED: SemanticException [Error 10326]: Invalid Constraint syntax Table: default.table1 has constraints enabled. Please remove those constraints to change this property.
diff --git a/ql/src/test/results/clientnegative/alter_tableprops_external_with_notnull_constraint.q.out b/ql/src/test/results/clientnegative/alter_tableprops_external_with_notnull_constraint.q.out
index 6cfc932..be87d2b 100644
--- a/ql/src/test/results/clientnegative/alter_tableprops_external_with_notnull_constraint.q.out
+++ b/ql/src/test/results/clientnegative/alter_tableprops_external_with_notnull_constraint.q.out
@@ -6,4 +6,4 @@ POSTHOOK: query: CREATE TABLE table1 (a STRING NOT NULL enforced, b STRING)
POSTHOOK: type: CREATETABLE
POSTHOOK: Output: database:default
POSTHOOK: Output: default@table1
-FAILED: SemanticException [Error 10326]: Invalid Constraint syntax Table: default.table1 has constraints enabled.Please remove those constraints to change this property.
+FAILED: SemanticException [Error 10326]: Invalid Constraint syntax Table: default.table1 has constraints enabled. Please remove those constraints to change this property.