Skip to content

Commit

Permalink
Disallow view column type to be unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
harunurhan authored and electrum committed Feb 11, 2016
1 parent 918ecbe commit 5ca2069
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions presto-docs/src/main/sphinx/release/release-0.137.rst
Expand Up @@ -12,6 +12,7 @@ General Changes
* Improve expression optimizer to remove some redundant operations. * Improve expression optimizer to remove some redundant operations.
* Allow using double input for :func:`approx_percentile` with an array of * Allow using double input for :func:`approx_percentile` with an array of
percentiles. percentiles.
* Do not allow creating views with a column type of ``UNKNOWN``.


Hive Changes Hive Changes
------------ ------------
Expand Down
Expand Up @@ -60,6 +60,8 @@ public enum SemanticErrorCode
DUPLICATE_COLUMN_NAME, DUPLICATE_COLUMN_NAME,
COLUMN_NAME_NOT_SPECIFIED, COLUMN_NAME_NOT_SPECIFIED,


COLUMN_TYPE_UNKNOWN,

EXPRESSION_NOT_CONSTANT, EXPRESSION_NOT_CONSTANT,


VIEW_PARSE_ERROR, VIEW_PARSE_ERROR,
Expand Down
Expand Up @@ -158,6 +158,7 @@
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.AMBIGUOUS_ATTRIBUTE; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.AMBIGUOUS_ATTRIBUTE;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.CATALOG_NOT_SPECIFIED; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.CATALOG_NOT_SPECIFIED;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.COLUMN_NAME_NOT_SPECIFIED; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.COLUMN_NAME_NOT_SPECIFIED;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.COLUMN_TYPE_UNKNOWN;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.DUPLICATE_COLUMN_NAME; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.DUPLICATE_COLUMN_NAME;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.DUPLICATE_RELATION; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.DUPLICATE_RELATION;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.INVALID_ORDINAL; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.INVALID_ORDINAL;
Expand Down Expand Up @@ -660,7 +661,7 @@ protected RelationType visitCreateTableAsSelect(CreateTableAsSelect node, Analys
// analyze the query that creates the table // analyze the query that creates the table
RelationType descriptor = process(node.getQuery(), context); RelationType descriptor = process(node.getQuery(), context);


validateColumnNames(node, descriptor); validateColumns(node, descriptor);


return new RelationType(Field.newUnqualified("rows", BIGINT)); return new RelationType(Field.newUnqualified("rows", BIGINT));
} }
Expand All @@ -684,12 +685,12 @@ protected RelationType visitCreateView(CreateView node, AnalysisContext context)
QualifiedObjectName viewName = createQualifiedObjectName(session, node, node.getName()); QualifiedObjectName viewName = createQualifiedObjectName(session, node, node.getName());
accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), viewName); accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), viewName);


validateColumnNames(node, descriptor); validateColumns(node, descriptor);


return descriptor; return descriptor;
} }


private static void validateColumnNames(Statement node, RelationType descriptor) private static void validateColumns(Statement node, RelationType descriptor)
{ {
// verify that all column names are specified and unique // verify that all column names are specified and unique
// TODO: collect errors and return them all at once // TODO: collect errors and return them all at once
Expand All @@ -702,6 +703,9 @@ private static void validateColumnNames(Statement node, RelationType descriptor)
if (!names.add(fieldName.get())) { if (!names.add(fieldName.get())) {
throw new SemanticException(DUPLICATE_COLUMN_NAME, node, "Column name '%s' specified more than once", fieldName.get()); throw new SemanticException(DUPLICATE_COLUMN_NAME, node, "Column name '%s' specified more than once", fieldName.get());
} }
if (field.getType().equals(UNKNOWN)) {
throw new SemanticException(COLUMN_TYPE_UNKNOWN, node, "Column type is unknown: %s", fieldName.get());
}
} }
} }


Expand Down
Expand Up @@ -52,6 +52,7 @@
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.CANNOT_HAVE_AGGREGATIONS_OR_WINDOWS; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.CANNOT_HAVE_AGGREGATIONS_OR_WINDOWS;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.CATALOG_NOT_SPECIFIED; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.CATALOG_NOT_SPECIFIED;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.COLUMN_NAME_NOT_SPECIFIED; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.COLUMN_NAME_NOT_SPECIFIED;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.COLUMN_TYPE_UNKNOWN;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.DUPLICATE_COLUMN_NAME; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.DUPLICATE_COLUMN_NAME;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.DUPLICATE_RELATION; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.DUPLICATE_RELATION;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.INVALID_LITERAL; import static com.facebook.presto.sql.analyzer.SemanticErrorCode.INVALID_LITERAL;
Expand Down Expand Up @@ -801,12 +802,22 @@ public void testTableSampleOutOfRange()
assertFails(SAMPLE_PERCENTAGE_OUT_OF_RANGE, "SELECT * FROM t1 TABLESAMPLE BERNOULLI (-101)"); assertFails(SAMPLE_PERCENTAGE_OUT_OF_RANGE, "SELECT * FROM t1 TABLESAMPLE BERNOULLI (-101)");
} }


@Test
public void testCreateTableAsColumns()
throws Exception
{
assertFails(COLUMN_NAME_NOT_SPECIFIED, "CREATE TABLE test AS SELECT 123");
assertFails(DUPLICATE_COLUMN_NAME, "CREATE TABLE test AS SELECT 1 a, 2 a");
assertFails(COLUMN_TYPE_UNKNOWN, "CREATE TABLE test AS SELECT null a");
}

@Test @Test
public void testCreateViewColumns() public void testCreateViewColumns()
throws Exception throws Exception
{ {
assertFails(COLUMN_NAME_NOT_SPECIFIED, "CREATE VIEW test AS SELECT 123"); assertFails(COLUMN_NAME_NOT_SPECIFIED, "CREATE VIEW test AS SELECT 123");
assertFails(DUPLICATE_COLUMN_NAME, "CREATE VIEW test AS SELECT 1 a, 2 a"); assertFails(DUPLICATE_COLUMN_NAME, "CREATE VIEW test AS SELECT 1 a, 2 a");
assertFails(COLUMN_TYPE_UNKNOWN, "CREATE VIEW test AS SELECT null a");
} }


@Test @Test
Expand Down

0 comments on commit 5ca2069

Please sign in to comment.