Search in sources :

Example 61 with Identifier

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

the class TestSqlParser method testGrant.

@Test
public void testGrant() {
    assertStatement("GRANT INSERT, DELETE ON t TO u", new Grant(Optional.of(ImmutableList.of("INSERT", "DELETE")), false, QualifiedName.of("t"), new PrincipalSpecification(PrincipalSpecification.Type.UNSPECIFIED, new Identifier("u")), false));
    assertStatement("GRANT SELECT ON t TO ROLE PUBLIC WITH GRANT OPTION", new Grant(Optional.of(ImmutableList.of("SELECT")), false, QualifiedName.of("t"), new PrincipalSpecification(PrincipalSpecification.Type.ROLE, new Identifier("PUBLIC")), true));
    assertStatement("GRANT ALL PRIVILEGES ON t TO USER u", new Grant(Optional.empty(), false, QualifiedName.of("t"), new PrincipalSpecification(PrincipalSpecification.Type.USER, new Identifier("u")), false));
    assertStatement("GRANT taco ON \"t\" TO ROLE \"public\" WITH GRANT OPTION", new Grant(Optional.of(ImmutableList.of("taco")), false, QualifiedName.of("t"), new PrincipalSpecification(PrincipalSpecification.Type.ROLE, new Identifier("public")), true));
}
Also used : Grant(io.prestosql.sql.tree.Grant) Identifier(io.prestosql.sql.tree.Identifier) QueryUtil.quotedIdentifier(io.prestosql.sql.QueryUtil.quotedIdentifier) PrincipalSpecification(io.prestosql.sql.tree.PrincipalSpecification) Test(org.testng.annotations.Test)

Example 62 with Identifier

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

the class AstBuilder method visitCreateCube.

@Override
public Node visitCreateCube(SqlBaseParser.CreateCubeContext context) {
    if (context.cubeProperties() == null) {
        throw new IllegalArgumentException("Missing properties: AGGREGATIONS, GROUP");
    }
    QualifiedName cubeName = getQualifiedName(context.cubeName);
    QualifiedName sourceTableName = getQualifiedName(context.tableName);
    List<Identifier> groupingSet = ImmutableList.of();
    List<FunctionCall> aggregations = ImmutableList.of();
    List<Property> properties = new ArrayList<>();
    Optional<Expression> optionalExpression = visitIfPresent(context.expression(), Expression.class);
    boolean cubeGroupProvided = false;
    boolean aggregationsProvided = false;
    boolean sourceFilterProvided = false;
    Optional<Expression> sourceFilterPredicate = Optional.empty();
    for (SqlBaseParser.CubePropertyContext propertyContext : context.cubeProperties().cubeProperty()) {
        if (propertyContext.cubeGroup() != null) {
            if (cubeGroupProvided) {
                throw new IllegalArgumentException("Duplicate property: GROUP");
            }
            groupingSet = visit(propertyContext.cubeGroup().identifier(), Identifier.class);
            cubeGroupProvided = true;
        } else if (propertyContext.aggregations() != null) {
            if (aggregationsProvided) {
                throw new IllegalArgumentException("Duplicate property: AGGREGATIONS");
            }
            aggregations = visit(propertyContext.aggregations().expression(), FunctionCall.class);
            aggregationsProvided = true;
        } else if (propertyContext.sourceFilter() != null) {
            if (sourceFilterProvided) {
                throw new IllegalArgumentException("Duplicate Property: FILTER");
            }
            sourceFilterPredicate = visitIfPresent(propertyContext.sourceFilter().expression(), Expression.class);
            sourceFilterProvided = true;
        } else if (propertyContext.property() != null) {
            properties.add((Property) visitProperty(propertyContext.property()));
        }
    }
    if (!cubeGroupProvided) {
        throw new IllegalArgumentException("Missing property: GROUP");
    }
    if (!aggregationsProvided) {
        throw new IllegalArgumentException("Missing property: AGGREGATIONS");
    }
    List<Identifier> decomposedGroupingSet = new ArrayList<>();
    groupingSet.forEach(groupItem -> {
        decomposedGroupingSet.add(new Identifier(groupItem.getLocation().get(), groupItem.getValue().toLowerCase(Locale.ENGLISH), groupItem.isDelimited()));
    });
    Set<FunctionCall> decomposedAggregations = new LinkedHashSet<>();
    aggregations.forEach(aggItem -> {
        List<Expression> listArguments = aggItem.getArguments();
        List<Expression> newArguments = new ArrayList<>();
        for (Expression argument : listArguments) {
            if (argument instanceof Identifier) {
                newArguments.add(new Identifier(argument.getLocation().get(), ((Identifier) argument).getValue().toLowerCase(Locale.ENGLISH), ((Identifier) argument).isDelimited()));
            } else {
                newArguments.add(argument);
            }
        }
        if (!"avg".equals(aggItem.getName().toString())) {
            decomposedAggregations.add(new FunctionCall(aggItem.getLocation(), aggItem.getName(), aggItem.getWindow(), aggItem.getFilter(), aggItem.getOrderBy(), aggItem.isDistinct(), newArguments));
        } else {
            decomposedAggregations.add(aggItem);
            decomposedAggregations.add(new FunctionCall(aggItem.getLocation(), QualifiedName.of("sum"), aggItem.getWindow(), aggItem.getFilter(), aggItem.getOrderBy(), aggItem.isDistinct(), newArguments));
            decomposedAggregations.add(new FunctionCall(aggItem.getLocation(), QualifiedName.of("count"), aggItem.getWindow(), aggItem.getFilter(), aggItem.getOrderBy(), aggItem.isDistinct(), newArguments));
        }
    });
    return new CreateCube(getLocation(context), cubeName, sourceTableName, decomposedGroupingSet, decomposedAggregations, context.EXISTS() != null, properties, optionalExpression, sourceFilterPredicate.orElse(null));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) QualifiedName(io.prestosql.sql.tree.QualifiedName) ArrayList(java.util.ArrayList) Identifier(io.prestosql.sql.tree.Identifier) 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) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) TryExpression(io.prestosql.sql.tree.TryExpression) CreateCube(io.prestosql.sql.tree.CreateCube) FunctionCall(io.prestosql.sql.tree.FunctionCall) Property(io.prestosql.sql.tree.Property) FunctionProperty(io.prestosql.sql.tree.FunctionProperty)

Example 63 with Identifier

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

the class TestDeallocateTask method executeDeallocate.

private Set<String> executeDeallocate(String statementName, String sqlString, Session session) {
    TransactionManager transactionManager = createTestTransactionManager();
    AccessControl accessControl = new AccessControlManager(transactionManager);
    QueryStateMachine stateMachine = QueryStateMachine.begin(sqlString, Optional.empty(), session, URI.create("fake://uri"), new ResourceGroupId("test"), new NoOpResourceGroupManager(), false, transactionManager, accessControl, executor, metadata, WarningCollector.NOOP);
    Deallocate deallocate = new Deallocate(new Identifier(statementName));
    new DeallocateTask().execute(deallocate, transactionManager, metadata, new AllowAllAccessControl(), stateMachine, emptyList(), new HeuristicIndexerManager(new FileSystemClientManager(), new HetuMetaStoreManager()));
    return stateMachine.getDeallocatedPreparedStatements();
}
Also used : AccessControlManager(io.prestosql.security.AccessControlManager) ResourceGroupId(io.prestosql.spi.resourcegroups.ResourceGroupId) HeuristicIndexerManager(io.prestosql.heuristicindex.HeuristicIndexerManager) AllowAllAccessControl(io.prestosql.security.AllowAllAccessControl) AccessControl(io.prestosql.security.AccessControl) FileSystemClientManager(io.prestosql.filesystem.FileSystemClientManager) Identifier(io.prestosql.sql.tree.Identifier) Deallocate(io.prestosql.sql.tree.Deallocate) TransactionManager(io.prestosql.transaction.TransactionManager) InMemoryTransactionManager.createTestTransactionManager(io.prestosql.transaction.InMemoryTransactionManager.createTestTransactionManager) AllowAllAccessControl(io.prestosql.security.AllowAllAccessControl) HetuMetaStoreManager(io.prestosql.metastore.HetuMetaStoreManager) NoOpResourceGroupManager(io.prestosql.execution.resourcegroups.NoOpResourceGroupManager)

Example 64 with Identifier

use of io.prestosql.sql.tree.Identifier 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 65 with Identifier

use of io.prestosql.sql.tree.Identifier 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

Identifier (io.prestosql.sql.tree.Identifier)65 Test (org.testng.annotations.Test)34 QueryUtil.quotedIdentifier (io.prestosql.sql.QueryUtil.quotedIdentifier)29 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)20 Expression (io.prestosql.sql.tree.Expression)19 StringLiteral (io.prestosql.sql.tree.StringLiteral)17 LongLiteral (io.prestosql.sql.tree.LongLiteral)16 Property (io.prestosql.sql.tree.Property)16 ArrayList (java.util.ArrayList)16 FunctionCall (io.prestosql.sql.tree.FunctionCall)15 CreateTable (io.prestosql.sql.tree.CreateTable)14 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)13 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)12 QualifiedName (io.prestosql.sql.tree.QualifiedName)12 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)11 Table (io.prestosql.sql.tree.Table)11 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)10 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)9 DropTable (io.prestosql.sql.tree.DropTable)9 IfExpression (io.prestosql.sql.tree.IfExpression)9