diff --git a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java index 45610e37ab..2073049d65 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java @@ -37,6 +37,7 @@ import java.io.IOException; import java.io.PrintStream; import java.nio.ByteBuffer; +import java.sql.SQLIntegrityConstraintViolationException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -73,6 +74,7 @@ import org.apache.calcite.rel.core.TableScan; import org.apache.calcite.rex.RexBuilder; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileChecksum; import org.apache.hadoop.fs.FileStatus; @@ -1044,6 +1046,13 @@ public void dropTable(String dbName, String tableName, boolean deleteData, if (!ignoreUnknownTab) { throw new HiveException(e); } + } catch (MetaException e) { + int idx = ExceptionUtils.indexOfType(e, SQLIntegrityConstraintViolationException.class); + if (idx != -1 && ExceptionUtils.getThrowables(e)[idx].getMessage().contains("MV_TABLES_USED")) { + throw new HiveException("Cannot drop table since it is used by at least one materialized view definition. " + + "Please drop any materialized view that uses the table before dropping it", e); + } + throw new HiveException(e); } catch (Exception e) { throw new HiveException(e); } diff --git a/ql/src/test/results/clientnegative/drop_table_used_by_mv.q.out b/ql/src/test/results/clientnegative/drop_table_used_by_mv.q.out index 635f31d0af..88e3b7dcde 100644 --- a/ql/src/test/results/clientnegative/drop_table_used_by_mv.q.out +++ b/ql/src/test/results/clientnegative/drop_table_used_by_mv.q.out @@ -32,4 +32,4 @@ PREHOOK: query: drop table mytable PREHOOK: type: DROPTABLE PREHOOK: Input: default@mytable PREHOOK: Output: default@mytable -FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:Exception thrown flushing changes to datastore) +FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Cannot drop table since it is used by at least one materialized view definition. Please drop any materialized view that uses the table before dropping it