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));
}
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));
}
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();
}
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));
}
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());
}
Aggregations