Search in sources :

Example 6 with CreateTableAsSelect

use of io.prestosql.sql.tree.CreateTableAsSelect in project hetu-core by openlookeng.

the class LogicalPlanner method planStatement.

public PlanNode planStatement(Analysis analysis, Statement statement) {
    if ((statement instanceof CreateTableAsSelect) && analysis.isCreateTableAsSelectNoOp()) {
        checkState(analysis.getCreateTableDestination().isPresent(), "Table destination is missing");
        Symbol symbol = planSymbolAllocator.newSymbol("rows", BIGINT);
        PlanNode source = new ValuesNode(idAllocator.getNextId(), ImmutableList.of(symbol), ImmutableList.of(ImmutableList.of(new ConstantExpression(0L, BIGINT))));
        return new OutputNode(idAllocator.getNextId(), source, ImmutableList.of("rows"), ImmutableList.of(symbol));
    }
    return createOutputPlan(planStatementWithoutOutput(analysis, statement), analysis);
}
Also used : ValuesNode(io.prestosql.spi.plan.ValuesNode) OutputNode(io.prestosql.sql.planner.plan.OutputNode) PlanNode(io.prestosql.spi.plan.PlanNode) Symbol(io.prestosql.spi.plan.Symbol) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) ConstantExpression(io.prestosql.spi.relation.ConstantExpression)

Example 7 with CreateTableAsSelect

use of io.prestosql.sql.tree.CreateTableAsSelect in project hetu-core by openlookeng.

the class HiveAstBuilder method visitCreateTableAsSelect.

@Override
public Node visitCreateTableAsSelect(HiveSqlParser.CreateTableAsSelectContext context) {
    if (context.TEMPORARY() != null) {
        addDiff(DiffType.UNSUPPORTED, context.TEMPORARY().getText(), "[TEMPORARY] is not supported");
        throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported statement: CREATE TEMPORARY TABLE", context);
    }
    Optional<String> comment = Optional.empty();
    if (context.COMMENT() != null) {
        comment = Optional.of(((StringLiteral) visit(context.string(0))).getValue());
    }
    Optional<List<Identifier>> columnAliases = Optional.empty();
    if (context.columnAliases() != null) {
        columnAliases = Optional.of(visit(context.columnAliases().identifier(), Identifier.class));
    }
    List<Property> properties = new ArrayList<>();
    if (context.TRANSACTIONAL() != null) {
        Identifier name = new Identifier("transactional");
        Expression value = new Identifier("true");
        properties.add(new Property(name, value));
        addDiff(DiffType.MODIFIED, context.TRANSACTIONAL().getText(), "transactional = true", "[TRANSACTIONAL] is formatted");
    }
    if (context.STORED_AS() != null) {
        Identifier name = new Identifier("format");
        String storedAsString = ((Identifier) visit(context.stored_as)).getValue();
        Expression value = new StringLiteral(getFileFormat(storedAsString));
        properties.add(new Property(name, value));
        addDiff(DiffType.MODIFIED, context.STORED_AS().getText(), FORMAT, "[STORED AS] is formatted");
    }
    if (context.LOCATION() != null) {
        Identifier name = new Identifier("location");
        Expression value = (StringLiteral) visit(context.location);
        properties.add(new Property(name, value));
    }
    if (context.TBLPROPERTIES() != null) {
        List<Property> tableProperties = visit(context.properties().property(), Property.class);
        for (int i = 0; i < tableProperties.size(); i++) {
            Property property = tableProperties.get(i);
            if (property.getName().getValue().equalsIgnoreCase("transactional")) {
                Identifier name = new Identifier("transactional");
                Expression value = new Identifier(unquote(property.getValue().toString()));
                properties.add(new Property(name, value));
                addDiff(DiffType.MODIFIED, property.getName().getValue(), "transactional = ", "[TRANSACTIONAL] is formatted");
            } else {
                addDiff(DiffType.UNSUPPORTED, property.getName().getValue(), "[TBLPROPERTIES] has unsupported properties");
                throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, format("Unsupported attribute: %s", property.getName().getValue()), context.properties());
            }
        }
    }
    return new CreateTableAsSelect(getLocation(context), getQualifiedName(context.qualifiedName()), (Query) visit(context.query()), context.EXISTS() != null, properties, true, columnAliases, comment);
}
Also used : Identifier(io.prestosql.sql.tree.Identifier) StringLiteral(io.prestosql.sql.tree.StringLiteral) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) Property(io.prestosql.sql.tree.Property)

Example 8 with CreateTableAsSelect

use of io.prestosql.sql.tree.CreateTableAsSelect in project hetu-core by openlookeng.

the class ImpalaAstBuilder method visitCreateTable.

@Override
public Node visitCreateTable(ImpalaSqlParser.CreateTableContext context) {
    Optional<String> comment = Optional.empty();
    if (context.COMMENT() != null) {
        comment = Optional.of(((StringLiteral) visit(context.comment)).getValue());
    }
    List<Property> properties = new ArrayList<>();
    // external table
    if (context.EXTERNAL() != null) {
        if (context.LOCATION() == null) {
            throw unsupportedError(ErrorType.SYNTAX_ERROR, "External attribute should be used with location", context);
        }
        Identifier name = new Identifier("external");
        Expression value = new Identifier("true");
        properties.add(new Property(name, value));
        addDiff(DiffType.MODIFIED, context.EXTERNAL().getText(), "external = true", "[external] is formatted");
    }
    // handle partitioned by
    List<TableElement> elements = getTableElements(context.tableElement());
    if (context.AS() == null && elements.size() == 0) {
        throw unsupportedError(ErrorType.SYNTAX_ERROR, "Create table should specify at least one column.", context);
    }
    if (context.PARTITIONED() != null) {
        List<ColumnDefinition> columnDefinitions = getColumnDefinitions(context.partitionedBy().columnDefinition());
        List<Expression> expressions = new ArrayList<>();
        Iterator<ColumnDefinition> iterator = columnDefinitions.iterator();
        while (iterator.hasNext()) {
            ColumnDefinition iter = iterator.next();
            expressions.add(new StringLiteral(iter.getName().getValue()));
            // add the partitioned_by column to table columns
            elements.add(new ColumnDefinition(iter.getName(), iter.getType(), true, emptyList(), Optional.empty()));
        }
        Expression value = new ArrayConstructor(expressions);
        properties.add(new Property(new Identifier(PARTITIONED_BY), value));
        addDiff(DiffType.MODIFIED, context.PARTITIONED().getText(), PARTITIONED_BY, "[partitioned by] is formatted");
    }
    // handle sort by
    if (context.SORT() != null) {
        List<Expression> quotedExpressions = new ArrayList<>();
        List<Expression> expressions = getExpressions(context.sortedBy().expression());
        for (int i = 0; i < expressions.size(); i++) {
            quotedExpressions.add(new StringLiteral(expressions.get(i).toString()));
        }
        Expression value = new ArrayConstructor(quotedExpressions);
        properties.add(new Property(new Identifier(SORTED_BY), value));
        addDiff(DiffType.MODIFIED, context.SORT().getText(), SORTED_BY, "[sorted by] is formatted");
    }
    // row format
    if (context.ROW() != null && context.FORMAT() != null) {
        addDiff(DiffType.UNSUPPORTED, context.ROW().getText(), "[ROW FORMAT] is not supported");
        addDiff(DiffType.UNSUPPORTED, context.FORMAT().getText(), null);
        throw unsupportedError(ErrorType.UNSUPPORTED_KEYWORDS, "ROW FORMAT", context);
    }
    // serde properties
    if (context.SERDEPROPERTIES() != null) {
        addDiff(DiffType.UNSUPPORTED, context.SERDEPROPERTIES().getText(), "[WITH SERDEPROPERTIES] is not supported");
        throw unsupportedError(ErrorType.UNSUPPORTED_KEYWORDS, "WITH SERDEPROPERTIES", context);
    }
    // stored as
    if (context.STORED_AS() != null) {
        String storedAsString = ((Identifier) visit(context.stored_as)).getValue();
        Expression value = new StringLiteral(getFileFormat(storedAsString));
        properties.add(new Property(new Identifier(FORMAT), value));
        addDiff(DiffType.MODIFIED, context.STORED_AS().getText(), FORMAT, "[stored as] is formatted");
    }
    // location
    if (context.LOCATION() != null) {
        Expression value = (StringLiteral) visit(context.location);
        properties.add(new Property(new Identifier(LOCATION), value));
        addDiff(DiffType.MODIFIED, context.LOCATION().getText(), LOCATION + " = " + value, "[location] is formatted");
    }
    // cached in
    if (context.CACHED() != null) {
        addDiff(DiffType.UNSUPPORTED, context.CACHED().getText(), "[CACHED IN] is not supported");
        throw unsupportedError(ErrorType.UNSUPPORTED_KEYWORDS, "CACHED IN", context);
    }
    // table properties
    if (context.TBLPROPERTIES() != null) {
        List<Property> tblProperties = visit(context.tblProp.property(), Property.class);
        for (int i = 0; i < tblProperties.size(); i++) {
            Property property = tblProperties.get(i);
            if (property.getName().getValue().equalsIgnoreCase(TRANSACTIONAL)) {
                Identifier name = new Identifier(TRANSACTIONAL);
                Expression value = new Identifier(unquote(property.getValue().toString()));
                properties.add(new Property(name, value));
                addDiff(DiffType.MODIFIED, property.getName().getValue(), "transactional = ", "[transactional] is formatted");
            } else {
                addDiff(DiffType.UNSUPPORTED, property.getName().getValue(), "[TBLPROPERTIES] has unsupported properties");
                throw unsupportedError(ErrorType.UNSUPPORTED_ATTRIBUTE, property.getName().getValue(), context.tblProp);
            }
        }
    }
    // create table as
    if (context.AS() != null) {
        return new CreateTableAsSelect(getLocation(context), getQualifiedName(context.tblName), (Query) visit(context.query()), context.EXISTS() != null, properties, true, Optional.empty(), comment);
    }
    return new CreateTable(getLocation(context), getQualifiedName(context.tblName), elements, context.EXISTS() != null, properties, comment);
}
Also used : ArrayList(java.util.ArrayList) CreateTable(io.prestosql.sql.tree.CreateTable) TableElement(io.prestosql.sql.tree.TableElement) ColumnDefinition(io.prestosql.sql.tree.ColumnDefinition) Identifier(io.prestosql.sql.tree.Identifier) StringLiteral(io.prestosql.sql.tree.StringLiteral) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) ArrayConstructor(io.prestosql.sql.tree.ArrayConstructor) Property(io.prestosql.sql.tree.Property)

Example 9 with CreateTableAsSelect

use of io.prestosql.sql.tree.CreateTableAsSelect in project hetu-core by openlookeng.

the class SqlQueryExecution method checkSnapshotSupport.

// Check if snapshot feature conflict with other aspects of the query.
// If any requirement is not met, then proceed as if snapshot was not enabled,
// i.e. session.isSnapshotEnabled() and SystemSessionProperties.isSnapshotEnabled(session) return false
private void checkSnapshotSupport(Session session) {
    List<String> reasons = new ArrayList<>();
    // Only support create-table-as-select and insert statements
    Statement statement = analysis.getStatement();
    if (statement instanceof CreateTableAsSelect) {
        if (analysis.isCreateTableAsSelectNoOp()) {
            // Table already exists. Ask catalog if target table supports snapshot
            if (!metadata.isSnapshotSupportedAsOutput(session, analysis.getCreateTableAsSelectNoOpTarget())) {
                reasons.add("Only support inserting into tables in Hive with ORC format");
            }
        } else {
            // Ask catalog if new table supports snapshot
            Map<String, Object> tableProperties = analysis.getCreateTableMetadata().getProperties();
            if (!metadata.isSnapshotSupportedAsNewTable(session, analysis.getTarget().get().getCatalogName(), tableProperties)) {
                reasons.add("Only support creating tables in Hive with ORC format");
            }
        }
    } else if (statement instanceof Insert) {
        // Ask catalog if target table supports snapshot
        if (!metadata.isSnapshotSupportedAsOutput(session, analysis.getInsert().get().getTarget())) {
            reasons.add("Only support inserting into tables in Hive with ORC format");
        }
    } else if (statement instanceof InsertCube) {
        reasons.add("INSERT INTO CUBE is not supported, only support CTAS (create table as select) and INSERT INTO (tables) statements");
    } else {
        reasons.add("Only support CTAS (create table as select) and INSERT INTO (tables) statements");
    }
    // Doesn't work with the following features
    if (SystemSessionProperties.isReuseTableScanEnabled(session) || SystemSessionProperties.isCTEReuseEnabled(session)) {
        reasons.add("No support along with reuse_table_scan or cte_reuse_enabled features");
    }
    // All input tables must support snapshotting
    for (TableHandle tableHandle : analysis.getTables()) {
        if (!metadata.isSnapshotSupportedAsInput(session, tableHandle)) {
            reasons.add("Only support reading from Hive, TPCDS, and TPCH source tables");
            break;
        }
    }
    // Must have more than 1 worker
    if (nodeScheduler.createNodeSelector(null, false, null).selectableNodeCount() == 1) {
        reasons.add("Requires more than 1 worker nodes");
    }
    if (!snapshotManager.getSnapshotUtils().hasStoreClient()) {
        String snapshotProfile = snapshotManager.getSnapshotUtils().getSnapshotProfile();
        if (snapshotProfile == null) {
            reasons.add("Property hetu.experimental.snapshot.profile is not specified");
        } else {
            reasons.add("Specified value '" + snapshotProfile + "' for property hetu.experimental.snapshot.profile is not valid");
        }
    }
    if (!reasons.isEmpty()) {
        // Disable snapshot support in the session. If this value has been used before this point,
        // then we may need to remedy those places to disable snapshot as well. Fortunately,
        // most accesses occur before this point, except for classes like ExecutingStatementResource,
        // where the "snapshot enabled" info is retrieved and set in ExchangeClient. This is harmless.
        // The ExchangeClient may still have snapshotEnabled=true while it's disabled in the session.
        // This does not alter ExchangeClient's behavior, because this instance (in coordinator)
        // will never receive any marker.
        session.disableSnapshot();
        String reasonsMessage = "Snapshot feature is disabled: \n" + String.join(". \n", reasons);
        warningCollector.add(new PrestoWarning(StandardWarningCode.SNAPSHOT_NOT_SUPPORTED, reasonsMessage));
    }
}
Also used : Statement(io.prestosql.sql.tree.Statement) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) ArrayList(java.util.ArrayList) PrestoWarning(io.prestosql.spi.PrestoWarning) TableHandle(io.prestosql.spi.metadata.TableHandle) Insert(io.prestosql.sql.tree.Insert) InsertCube(io.prestosql.sql.tree.InsertCube)

Aggregations

CreateTableAsSelect (io.prestosql.sql.tree.CreateTableAsSelect)9 Identifier (io.prestosql.sql.tree.Identifier)5 Test (org.testng.annotations.Test)4 CreateTable (io.prestosql.sql.tree.CreateTable)3 LongLiteral (io.prestosql.sql.tree.LongLiteral)3 Property (io.prestosql.sql.tree.Property)3 QualifiedName (io.prestosql.sql.tree.QualifiedName)3 StringLiteral (io.prestosql.sql.tree.StringLiteral)3 ArrayList (java.util.ArrayList)3 Duration (io.airlift.units.Duration)2 QueryUtil.quotedIdentifier (io.prestosql.sql.QueryUtil.quotedIdentifier)2 QueryUtil.simpleQuery (io.prestosql.sql.QueryUtil.simpleQuery)2 SqlParser (io.prestosql.sql.parser.SqlParser)2 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)2 ArithmeticUnaryExpression (io.prestosql.sql.tree.ArithmeticUnaryExpression)2 ArrayConstructor (io.prestosql.sql.tree.ArrayConstructor)2 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)2 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)2 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)2 DropTable (io.prestosql.sql.tree.DropTable)2