Search in sources :

Example 1 with SingleColumn

use of com.facebook.presto.sql.tree.SingleColumn in project presto by prestodb.

the class TestShadowing method testCreateTableAsSelect.

@Test
public void testCreateTableAsSelect() throws Exception {
    handle.execute("CREATE TABLE \"my_test_table\" (column1 BIGINT, column2 DOUBLE)");
    SqlParser parser = new SqlParser();
    Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "CREATE TABLE my_test_table AS SELECT 1 column1, CAST(2.0 AS DOUBLE) column2 LIMIT 1", ImmutableList.of(), null, null, ImmutableMap.of());
    QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS));
    Query rewrittenQuery = rewriter.shadowQuery(query);
    assertEquals(rewrittenQuery.getPreQueries().size(), 1);
    assertEquals(rewrittenQuery.getPostQueries().size(), 1);
    CreateTableAsSelect createTableAs = (CreateTableAsSelect) parser.createStatement(rewrittenQuery.getPreQueries().get(0));
    assertEquals(createTableAs.getName().getParts().size(), 1);
    assertTrue(createTableAs.getName().getSuffix().startsWith("tmp_"));
    assertFalse(createTableAs.getName().getSuffix().contains("my_test_table"));
    assertEquals(PrestoVerifier.statementToQueryType(parser, rewrittenQuery.getQuery()), READ);
    Table table = new Table(createTableAs.getName());
    SingleColumn column1 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("column1"))));
    SingleColumn column2 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("column2"), new LongLiteral("1"))))));
    Select select = new Select(false, ImmutableList.of(column1, column2));
    QuerySpecification querySpecification = new QuerySpecification(select, Optional.of(table), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    assertEquals(parser.createStatement(rewrittenQuery.getQuery()), new com.facebook.presto.sql.tree.Query(Optional.empty(), querySpecification, Optional.empty(), Optional.empty()));
    assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0)), new DropTable(createTableAs.getName(), true));
}
Also used : Table(com.facebook.presto.sql.tree.Table) DropTable(com.facebook.presto.sql.tree.DropTable) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SqlParser(com.facebook.presto.sql.parser.SqlParser) Duration(io.airlift.units.Duration) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) DropTable(com.facebook.presto.sql.tree.DropTable) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) Identifier(com.facebook.presto.sql.tree.Identifier) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) Select(com.facebook.presto.sql.tree.Select) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 2 with SingleColumn

use of com.facebook.presto.sql.tree.SingleColumn in project presto by prestodb.

the class MaterializedViewQueryOptimizer method visitSingleColumn.

@Override
protected Node visitSingleColumn(SingleColumn node, Void context) {
    // For a single table, without sub-queries, the column prefix is unnecessary. Here It is removed so that it can be mapped to the view column properly.
    // For relations other than single table, it needs to be reserved to differentiate columns from different tables.
    // One way to do so is to process the prefix within `visitDereferenceExpression()` since the prefix information is saved as `base` in `DereferenceExpression` node.
    node = removeSingleColumnPrefix(node, removablePrefix);
    Expression expression = node.getExpression();
    Optional<Set<Expression>> groupByOfMaterializedView = materializedViewInfo.getGroupBy();
    // TODO: Replace this logic with rule-based validation framework.
    if (groupByOfMaterializedView.isPresent() && validateExpressionForGroupBy(groupByOfMaterializedView.get(), expression) && (!expressionsInGroupBy.isPresent() || !expressionsInGroupBy.get().contains(expression))) {
        throw new IllegalStateException("Query a column presents in materialized view group by: " + expression.toString());
    }
    Expression processedColumn = (Expression) process(expression, context);
    Optional<Identifier> alias = node.getAlias();
    // If a column name was rewritten, make sure we re-alias to same name as base query
    if (!alias.isPresent() && processedColumn instanceof Identifier && !processedColumn.equals(node.getExpression())) {
        alias = Optional.of((Identifier) node.getExpression());
    }
    return new SingleColumn(processedColumn, alias);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Identifier(com.facebook.presto.sql.tree.Identifier) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) SingleColumn(com.facebook.presto.sql.tree.SingleColumn)

Example 3 with SingleColumn

use of com.facebook.presto.sql.tree.SingleColumn in project presto by prestodb.

the class RowColumnValidator method generateChecksumColumns.

@Override
public List<SingleColumn> generateChecksumColumns(Column column) {
    checkColumnType(column);
    ImmutableList.Builder<SingleColumn> columnsBuilder = ImmutableList.builder();
    List<Field> fields = getFields(column);
    for (int i = 0; i < fields.size(); i++) {
        Field field = fields.get(i);
        Column fieldColumn = getFieldAsColumn(column, field, i);
        List<SingleColumn> fieldColumns = columnValidators.get(fieldColumn.getCategory()).get().generateChecksumColumns(fieldColumn);
        columnsBuilder.addAll(fieldColumns);
    }
    return columnsBuilder.build();
}
Also used : Field(com.facebook.presto.common.type.RowType.Field) Column(com.facebook.presto.verifier.framework.Column) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) ImmutableList(com.google.common.collect.ImmutableList) SingleColumn(com.facebook.presto.sql.tree.SingleColumn)

Example 4 with SingleColumn

use of com.facebook.presto.sql.tree.SingleColumn in project presto by prestodb.

the class ArrayColumnValidator method generateChecksumColumns.

@Override
public List<SingleColumn> generateChecksumColumns(Column column) {
    Expression checksum = generateArrayChecksum(column.getExpression(), column.getType());
    Expression arrayCardinalityChecksum = functionCall("checksum", functionCall("cardinality", column.getExpression()));
    Expression arrayCardinalitySum = new CoalesceExpression(functionCall("sum", functionCall("cardinality", column.getExpression())), new LongLiteral("0"));
    return ImmutableList.of(new SingleColumn(checksum, Optional.of(delimitedIdentifier(getChecksumColumnAlias(column)))), new SingleColumn(arrayCardinalityChecksum, Optional.of(delimitedIdentifier(getCardinalityChecksumColumnAlias(column)))), new SingleColumn(arrayCardinalitySum, Optional.of(delimitedIdentifier(getCardinalitySumColumnAlias(column)))));
}
Also used : Expression(com.facebook.presto.sql.tree.Expression) TryExpression(com.facebook.presto.sql.tree.TryExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression)

Example 5 with SingleColumn

use of com.facebook.presto.sql.tree.SingleColumn in project presto by prestodb.

the class ChecksumValidator method generateChecksumQuery.

public Query generateChecksumQuery(QualifiedName tableName, List<Column> columns) {
    ImmutableList.Builder<SelectItem> selectItems = ImmutableList.builder();
    selectItems.add(new SingleColumn(new FunctionCall(QualifiedName.of("count"), ImmutableList.of())));
    for (Column column : columns) {
        selectItems.addAll(columnValidators.get(column.getCategory()).get().generateChecksumColumns(column));
    }
    return simpleQuery(new Select(false, selectItems.build()), new Table(tableName));
}
Also used : Table(com.facebook.presto.sql.tree.Table) Column(com.facebook.presto.verifier.framework.Column) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) SelectItem(com.facebook.presto.sql.tree.SelectItem) Select(com.facebook.presto.sql.tree.Select) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) FunctionCall(com.facebook.presto.sql.tree.FunctionCall)

Aggregations

SingleColumn (com.facebook.presto.sql.tree.SingleColumn)13 Expression (com.facebook.presto.sql.tree.Expression)8 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)8 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)6 Identifier (com.facebook.presto.sql.tree.Identifier)6 Select (com.facebook.presto.sql.tree.Select)6 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)5 ImmutableList (com.google.common.collect.ImmutableList)5 CreateTableAsSelect (com.facebook.presto.sql.tree.CreateTableAsSelect)4 SelectItem (com.facebook.presto.sql.tree.SelectItem)4 Table (com.facebook.presto.sql.tree.Table)4 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)3 Query (com.facebook.presto.sql.tree.Query)3 ArrayType (com.facebook.presto.common.type.ArrayType)2 MapType (com.facebook.presto.common.type.MapType)2 Type (com.facebook.presto.common.type.Type)2 AllColumns (com.facebook.presto.sql.tree.AllColumns)2 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)2 Cast (com.facebook.presto.sql.tree.Cast)2 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)2