Search in sources :

Example 1 with Identifier

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

the class TestCountConstantOptimizer method testCountConstantOptimizer.

@Test
public void testCountConstantOptimizer() throws Exception {
    CountConstantOptimizer optimizer = new CountConstantOptimizer();
    PlanNodeIdAllocator planNodeIdAllocator = new PlanNodeIdAllocator();
    Symbol countAggregationSymbol = new Symbol("count");
    Signature countAggregationSignature = new Signature("count", FunctionKind.AGGREGATE, parseTypeSignature(StandardTypes.BIGINT), parseTypeSignature(StandardTypes.BIGINT));
    ImmutableMap<Symbol, FunctionCall> aggregations = ImmutableMap.of(countAggregationSymbol, new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new SymbolReference("expr"))));
    ImmutableMap<Symbol, Signature> functions = ImmutableMap.of(countAggregationSymbol, countAggregationSignature);
    ValuesNode valuesNode = new ValuesNode(planNodeIdAllocator.getNextId(), ImmutableList.of(new Symbol("col")), ImmutableList.of(ImmutableList.of()));
    AggregationNode eligiblePlan = new AggregationNode(planNodeIdAllocator.getNextId(), new ProjectNode(planNodeIdAllocator.getNextId(), valuesNode, Assignments.of(new Symbol("expr"), new LongLiteral("42"))), aggregations, functions, ImmutableMap.of(), ImmutableList.of(ImmutableList.of()), AggregationNode.Step.INTERMEDIATE, Optional.empty(), Optional.empty());
    assertTrue(((AggregationNode) optimizer.optimize(eligiblePlan, TEST_SESSION, ImmutableMap.of(), new SymbolAllocator(), new PlanNodeIdAllocator())).getAggregations().get(countAggregationSymbol).getArguments().isEmpty());
    AggregationNode ineligiblePlan = new AggregationNode(planNodeIdAllocator.getNextId(), new ProjectNode(planNodeIdAllocator.getNextId(), valuesNode, Assignments.of(new Symbol("expr"), new FunctionCall(QualifiedName.of("function"), ImmutableList.of(new Identifier("x"))))), aggregations, functions, ImmutableMap.of(), ImmutableList.of(ImmutableList.of()), AggregationNode.Step.INTERMEDIATE, Optional.empty(), Optional.empty());
    assertFalse(((AggregationNode) optimizer.optimize(ineligiblePlan, TEST_SESSION, ImmutableMap.of(), new SymbolAllocator(), new PlanNodeIdAllocator())).getAggregations().get(countAggregationSymbol).getArguments().isEmpty());
}
Also used : SymbolAllocator(com.facebook.presto.sql.planner.SymbolAllocator) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Symbol(com.facebook.presto.sql.planner.Symbol) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Identifier(com.facebook.presto.sql.tree.Identifier) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 2 with Identifier

use of com.facebook.presto.sql.tree.Identifier 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 3 with Identifier

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

the class AstBuilder method visitAliasedRelation.

@Override
public Node visitAliasedRelation(SqlBaseParser.AliasedRelationContext context) {
    Relation child = (Relation) visit(context.relationPrimary());
    if (context.identifier() == null) {
        return child;
    }
    List<Identifier> aliases = null;
    if (context.columnAliases() != null) {
        aliases = visit(context.columnAliases().identifier(), Identifier.class);
    }
    return new AliasedRelation(getLocation(context), child, (Identifier) visit(context.identifier()), aliases);
}
Also used : AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) Relation(com.facebook.presto.sql.tree.Relation) Identifier(com.facebook.presto.sql.tree.Identifier) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation)

Example 4 with Identifier

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

the class TestSqlParser method testShowRoleGrants.

@Test
public void testShowRoleGrants() {
    assertStatement("SHOW ROLE GRANTS", new ShowRoleGrants(Optional.empty(), Optional.empty()));
    assertStatement("SHOW ROLE GRANTS FROM catalog", new ShowRoleGrants(Optional.of(new Identifier("catalog"))));
}
Also used : ShowRoleGrants(com.facebook.presto.sql.tree.ShowRoleGrants) Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) Test(org.testng.annotations.Test)

Example 5 with Identifier

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

the class TestSqlParser method testSelectWithOffset.

@Test
public void testSelectWithOffset() {
    assertStatement("SELECT * FROM table1 OFFSET 2 ROWS", simpleQuery(selectList(new AllColumns()), new Table(QualifiedName.of("table1")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset("2")), Optional.empty()));
    assertStatement("SELECT * FROM table1 OFFSET 2", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset("2")), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement("SELECT * FROM table1 order by x OFFSET 2 limit 10", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new Identifier("x"), ASCENDING, UNDEFINED)))), Optional.of(new Offset("2")), Optional.of("10")), Optional.empty(), Optional.empty(), Optional.empty()));
    Query valuesQuery = query(values(row(new LongLiteral("1"), new StringLiteral("1")), row(new LongLiteral("2"), new StringLiteral("2"))));
    assertStatement("SELECT * FROM (VALUES (1, '1'), (2, '2')) OFFSET 2 ROWS", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset("2")), Optional.empty()));
    assertStatement("SELECT * FROM (VALUES (1, '1'), (2, '2')) OFFSET 2", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset("2")), Optional.empty()));
}
Also used : OrderBy(com.facebook.presto.sql.tree.OrderBy) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SortItem(com.facebook.presto.sql.tree.SortItem) CreateTable(com.facebook.presto.sql.tree.CreateTable) DropTable(com.facebook.presto.sql.tree.DropTable) Table(com.facebook.presto.sql.tree.Table) RenameTable(com.facebook.presto.sql.tree.RenameTable) Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) AllColumns(com.facebook.presto.sql.tree.AllColumns) Offset(com.facebook.presto.sql.tree.Offset) Test(org.testng.annotations.Test)

Aggregations

Identifier (com.facebook.presto.sql.tree.Identifier)46 Test (org.testng.annotations.Test)31 QueryUtil.quotedIdentifier (com.facebook.presto.sql.QueryUtil.quotedIdentifier)27 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)15 Query (com.facebook.presto.sql.tree.Query)11 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)10 AllColumns (com.facebook.presto.sql.tree.AllColumns)9 DropTable (com.facebook.presto.sql.tree.DropTable)9 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)9 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)9 Table (com.facebook.presto.sql.tree.Table)9 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)8 CreateTable (com.facebook.presto.sql.tree.CreateTable)8 Expression (com.facebook.presto.sql.tree.Expression)8 QueryUtil.simpleQuery (com.facebook.presto.sql.QueryUtil.simpleQuery)7 SingleColumn (com.facebook.presto.sql.tree.SingleColumn)7 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)7 WithQuery (com.facebook.presto.sql.tree.WithQuery)7 CreateTableAsSelect (com.facebook.presto.sql.tree.CreateTableAsSelect)6 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)6