Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.facebook.presto.spi.PrestoWarning;
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.StandardErrorCode;
import com.facebook.presto.spi.StandardWarningCode;
import com.facebook.presto.spi.TableHandle;
import com.facebook.presto.spi.WarningCollector;
import com.facebook.presto.spi.analyzer.AccessControlInfoForTable;
Expand Down Expand Up @@ -763,6 +764,9 @@ protected Scope visitCreateTableAsSelect(CreateTableAsSelect node, Optional<Scop
if (metadataResolver.tableExists(targetTable)) {
if (node.isNotExists()) {
analysis.setCreateTableAsSelectNoOp(true);
warningCollector.add(new PrestoWarning(
StandardWarningCode.SEMANTIC_WARNING,
format("Table '%s' already exists, skipping table creation", targetTable)));
return createAndAssignScope(node, scope, Field.newUnqualified(node.getLocation(), "rows", BIGINT));
}
throw new SemanticException(TABLE_ALREADY_EXISTS, node, "Destination table '%s' already exists", targetTable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,19 @@ protected Void visitQuerySpecification(QuerySpecification querySpec, Context con

// Wildcards are unresolved in the QuerySpecification's list of SelectItem, so we use output expressions from analysis instead
List<Expression> selectItems = analysis.getOutputExpressions(querySpec);
if (!context.prunable) {
// Examine all the output expressions
for (Expression expression : selectItems) {
process(expression, context);
// selectItems can be null for statements like CREATE TABLE IF NOT EXISTS when the table already exists
if (selectItems != null) {
if (!context.prunable) {
// Examine all the output expressions
for (Expression expression : selectItems) {
process(expression, context);
}
}
}
else {
// Prune (Only examine output expressions that have been referenced)
for (FieldId fieldId : context.getFieldIdsToExploreInRelation(querySpec)) {
process(selectItems.get(fieldId.getFieldIndex()), context);
else {
// Prune (Only examine output expressions that have been referenced)
for (FieldId fieldId : context.getFieldIdsToExploreInRelation(querySpec)) {
process(selectItems.get(fieldId.getFieldIndex()), context);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ private static void assertNoWarning(WarningCollector warningCollector)
assertTrue(warnings.isEmpty());
}

@Test
public void testCTASIfNotExistsWhenExists()
{
assertHasWarning(analyzeWithWarnings("CREATE TABLE IF NOT EXISTS t1 AS SELECT a, b FROM t1"),
SEMANTIC_WARNING, "Table 'tpch.s1.t1' already exists, skipping table creation");
}

@Test
public void testNonComparableGroupBy()
{
Expand Down
Loading