Search in sources :

Example 31 with FunctionCall

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

the class AggregationFunctionMatcher method getAssignedSymbol.

@Override
public Optional<Symbol> getAssignedSymbol(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    Optional<Symbol> result = Optional.empty();
    if (!(node instanceof AggregationNode)) {
        return result;
    }
    AggregationNode aggregationNode = (AggregationNode) node;
    FunctionCall expectedCall = callMaker.getExpectedValue(symbolAliases);
    for (Map.Entry<Symbol, Aggregation> assignment : aggregationNode.getAggregations().entrySet()) {
        Aggregation aggregation = assignment.getValue();
        if (aggregationMatches(aggregation, expectedCall, metadata)) {
            checkState(!result.isPresent(), "Ambiguous function calls in %s", aggregationNode);
            result = Optional.of(assignment.getKey());
        }
    }
    return result;
}
Also used : Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) Symbol(io.prestosql.spi.plan.Symbol) AggregationNode(io.prestosql.spi.plan.AggregationNode) FunctionCall(io.prestosql.sql.tree.FunctionCall) Map(java.util.Map)

Example 32 with FunctionCall

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

the class TestShadowing method testInsert.

@Test
public void testInsert() throws Exception {
    handle.execute("CREATE TABLE \"test_insert_table\" (a BIGINT, b DOUBLE, c VARCHAR)");
    SqlParser parser = new SqlParser();
    Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "INSERT INTO test_insert_table (b, a, c) values (1.1, 1, 'a'), (2.0, 2, 'b'), (3.1, 3, 'c')", ImmutableList.of(), null, null, ImmutableMap.of());
    QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("other_catalog", "other_schema", "tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS));
    Query rewrittenQuery = rewriter.shadowQuery(query);
    assertEquals(rewrittenQuery.getPreQueries().size(), 2);
    CreateTable createTable = (CreateTable) parser.createStatement(rewrittenQuery.getPreQueries().get(0));
    assertEquals(createTable.getName().getParts().size(), 3);
    assertEquals(createTable.getName().getPrefix().get(), QualifiedName.of("other_catalog", "other_schema"));
    assertTrue(createTable.getName().getSuffix().startsWith("tmp_"));
    assertFalse(createTable.getName().getSuffix().contains("test_insert_table"));
    Insert insert = (Insert) parser.createStatement(rewrittenQuery.getPreQueries().get(1));
    assertEquals(insert.getTarget(), createTable.getName());
    assertEquals(insert.getColumns(), Optional.of(ImmutableList.of(identifier("b"), identifier("a"), identifier("c"))));
    Table table = new Table(createTable.getName());
    SingleColumn columnA = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("A"))));
    SingleColumn columnB = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("B"), new LongLiteral("1"))))));
    SingleColumn columnC = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("C"))));
    Select select = new Select(false, ImmutableList.of(columnA, columnB, columnC));
    QuerySpecification querySpecification = new QuerySpecification(select, Optional.of(table), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    assertEquals(parser.createStatement(rewrittenQuery.getQuery()), new io.prestosql.sql.tree.Query(Optional.empty(), querySpecification, Optional.empty(), Optional.empty(), Optional.empty()));
    assertEquals(rewrittenQuery.getPostQueries().size(), 1);
    assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0)), new DropTable(createTable.getName(), true));
}
Also used : Table(io.prestosql.sql.tree.Table) DropTable(io.prestosql.sql.tree.DropTable) CreateTable(io.prestosql.sql.tree.CreateTable) LongLiteral(io.prestosql.sql.tree.LongLiteral) SqlParser(io.prestosql.sql.parser.SqlParser) CreateTable(io.prestosql.sql.tree.CreateTable) Duration(io.airlift.units.Duration) SingleColumn(io.prestosql.sql.tree.SingleColumn) Insert(io.prestosql.sql.tree.Insert) DropTable(io.prestosql.sql.tree.DropTable) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) Identifier(io.prestosql.sql.tree.Identifier) Select(io.prestosql.sql.tree.Select) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) FunctionCall(io.prestosql.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 33 with FunctionCall

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

the class QueryRewriter method checksumSql.

private String checksumSql(List<Column> columns, QualifiedName table) throws QueryRewriteException {
    if (columns.isEmpty()) {
        throw new QueryRewriteException("Table " + table + " has no columns");
    }
    ImmutableList.Builder<SelectItem> selectItems = ImmutableList.builder();
    for (Column column : columns) {
        Expression expression = new Identifier(column.getName());
        if (column.isApproximateType()) {
            expression = new FunctionCall(QualifiedName.of("round"), ImmutableList.of(expression, new LongLiteral(Integer.toString(doublePrecision))));
        }
        selectItems.add(new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(expression))));
    }
    Select select = new Select(false, selectItems.build());
    return formatSql(new QuerySpecification(select, Optional.of(new Table(table)), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty());
}
Also used : Table(io.prestosql.sql.tree.Table) DropTable(io.prestosql.sql.tree.DropTable) CreateTable(io.prestosql.sql.tree.CreateTable) LongLiteral(io.prestosql.sql.tree.LongLiteral) ImmutableList(com.google.common.collect.ImmutableList) SingleColumn(io.prestosql.sql.tree.SingleColumn) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) Identifier(io.prestosql.sql.tree.Identifier) SingleColumn(io.prestosql.sql.tree.SingleColumn) Expression(io.prestosql.sql.tree.Expression) SelectItem(io.prestosql.sql.tree.SelectItem) Select(io.prestosql.sql.tree.Select) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) FunctionCall(io.prestosql.sql.tree.FunctionCall)

Aggregations

FunctionCall (io.prestosql.sql.tree.FunctionCall)33 StringLiteral (io.prestosql.sql.tree.StringLiteral)15 Test (org.testng.annotations.Test)15 Expression (io.prestosql.sql.tree.Expression)14 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)13 Identifier (io.prestosql.sql.tree.Identifier)13 LongLiteral (io.prestosql.sql.tree.LongLiteral)11 QualifiedName (io.prestosql.sql.tree.QualifiedName)10 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)9 IfExpression (io.prestosql.sql.tree.IfExpression)8 InListExpression (io.prestosql.sql.tree.InListExpression)8 LambdaExpression (io.prestosql.sql.tree.LambdaExpression)8 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)8 QuerySpecification (io.prestosql.sql.tree.QuerySpecification)8 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)7 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)7 NotExpression (io.prestosql.sql.tree.NotExpression)7 OrderBy (io.prestosql.sql.tree.OrderBy)7 SortItem (io.prestosql.sql.tree.SortItem)7 SubqueryExpression (io.prestosql.sql.tree.SubqueryExpression)7