This is an automated email from the ASF dual-hosted git repository. mikeb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/impala.git
commit 663e9ccc9e4b5167d73c5e28ac4becbd0813afca Author: Fredy Wijaya <fwij...@cloudera.com> AuthorDate: Fri Jan 11 08:32:20 2019 -0800 IMPALA-7679: Improve error message when adding NULL column in CTAS, CREATE VIEW and ALTER VIEW This patch improves the error message to be more helpful when inserting a NULL column in CTAS, CREATE VIEW, and ALTER VIEW without an explicit cast due to the inability to infer the column type. Testing: - Added new FE tests - Ran all FE tests Change-Id: Ia2fe1fc96f0650477c0e897c010be3fadabfaf82 Reviewed-on: http://gerrit.cloudera.org:8080/12217 Reviewed-by: Impala Public Jenkins <impala-public-jenk...@cloudera.com> Tested-by: Impala Public Jenkins <impala-public-jenk...@cloudera.com> --- .../impala/analysis/CreateOrAlterViewStmtBase.java | 6 +++++ .../impala/analysis/CreateTableAsSelectStmt.java | 5 ++++ .../org/apache/impala/analysis/AnalyzeDDLTest.java | 27 ++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/fe/src/main/java/org/apache/impala/analysis/CreateOrAlterViewStmtBase.java b/fe/src/main/java/org/apache/impala/analysis/CreateOrAlterViewStmtBase.java index 850fd3b..bef1c22 100644 --- a/fe/src/main/java/org/apache/impala/analysis/CreateOrAlterViewStmtBase.java +++ b/fe/src/main/java/org/apache/impala/analysis/CreateOrAlterViewStmtBase.java @@ -21,6 +21,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.apache.impala.catalog.Type; import org.apache.impala.common.AnalysisException; import org.apache.impala.thrift.TCreateOrAlterViewParams; import org.apache.impala.thrift.TTableName; @@ -133,6 +134,11 @@ public abstract class CreateOrAlterViewStmtBase extends StatementBase { // duplicate column names. Set<String> distinctColNames = new HashSet<>(); for (ColumnDef colDesc: finalColDefs_) { + if (colDesc.getType() == Type.NULL) { + throw new AnalysisException(String.format("Unable to infer the column type " + + "for column '%s'. Use cast() to explicitly specify the column type for " + + "column '%s'.", colDesc.getColName(), colDesc.getColName())); + } colDesc.analyze(null); if (!distinctColNames.add(colDesc.getColName().toLowerCase())) { throw new AnalysisException("Duplicate column name: " + colDesc.getColName()); diff --git a/fe/src/main/java/org/apache/impala/analysis/CreateTableAsSelectStmt.java b/fe/src/main/java/org/apache/impala/analysis/CreateTableAsSelectStmt.java index ca311c6..372cfce 100644 --- a/fe/src/main/java/org/apache/impala/analysis/CreateTableAsSelectStmt.java +++ b/fe/src/main/java/org/apache/impala/analysis/CreateTableAsSelectStmt.java @@ -185,6 +185,11 @@ public class CreateTableAsSelectStmt extends StatementBase { ColumnDef colDef = new ColumnDef(tmpQueryStmt.getColLabels().get(i), null, Collections.<ColumnDef.Option, Object>emptyMap()); colDef.setType(tmpQueryStmt.getBaseTblResultExprs().get(i).getType()); + if (colDef.getType() == Type.NULL) { + throw new AnalysisException(String.format("Unable to infer the column type " + + "for column '%s'. Use cast() to explicitly specify the column type for " + + "column '%s'.", colDef.getColName(), colDef.getColName())); + } createStmt_.getColumnDefs().add(colDef); } createStmt_.analyze(analyzer); diff --git a/fe/src/test/java/org/apache/impala/analysis/AnalyzeDDLTest.java b/fe/src/test/java/org/apache/impala/analysis/AnalyzeDDLTest.java index 2c40545..05bd855 100644 --- a/fe/src/test/java/org/apache/impala/analysis/AnalyzeDDLTest.java +++ b/fe/src/test/java/org/apache/impala/analysis/AnalyzeDDLTest.java @@ -1216,6 +1216,17 @@ public class AnalyzeDDLTest extends FrontendTestBase { "select * from temp_view union distinct " + "select tinyint_col, int_col, bigint_col from functional.alltypes", "Self-reference not allowed on view: functional.alltypes_view"); + + // IMPALA-7679: Inserting a null column type without an explicit type should + // throw an error. + AnalyzesOk("alter view functional.alltypes_view as " + + "select cast(null as int) as new_col"); + AnalyzesOk("alter view functional.alltypes_view as " + + "select cast(null as int) as null_col, 1 as one_col"); + AnalysisError("alter view functional.alltypes_view " + + "as select null as new_col", "Unable to infer the column type for " + + "column 'new_col'. Use cast() to explicitly specify the column type for " + + "column 'new_col'."); } @Test @@ -1930,6 +1941,14 @@ public class AnalyzeDDLTest extends FrontendTestBase { " stored as kudu as SELECT INT_COL, SMALLINT_COL, ID, BIGINT_COL," + " DATE_STRING_COL, STRING_COL, TIMESTAMP_COL, YEAR, MONTH FROM " + " functional.alltypes"); + + // IMPALA-7679: Inserting a null column type without an explicit type should + // throw an error. + AnalyzesOk("create table t as select cast(null as int) as new_col"); + AnalyzesOk("create table t as select cast(null as int) as null_col, 1 as one_col"); + AnalysisError("create table t as select null as new_col", + "Unable to infer the column type for column 'new_col'. Use cast() to " + + "explicitly specify the column type for column 'new_col'."); } @Test @@ -3131,6 +3150,14 @@ public class AnalyzeDDLTest extends FrontendTestBase { "from functional.allcomplextypes", "Expr 'int_array_col' in select list returns a complex type 'ARRAY<INT>'.\n" + "Only scalar types are allowed in the select list."); + + // IMPALA-7679: Inserting a null column type without an explicit type should + // throw an error. + AnalyzesOk("create view v as select cast(null as int) as new_col"); + AnalyzesOk("create view v as select cast(null as int) as null_col, 1 as one_col"); + AnalysisError("create view v as select null as new_col", + "Unable to infer the column type for column 'new_col'. Use cast() to " + + "explicitly specify the column type for column 'new_col'."); } @Test