use of io.prestosql.sql.tree.QualifiedName in project hetu-core by openlookeng.
the class TestSqlParser method testInsertInto.
@Test
public void testInsertInto() {
QualifiedName table = QualifiedName.of("a");
Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
assertStatement("INSERT INTO a SELECT * FROM t", new Insert(table, Optional.empty(), query));
assertStatement("INSERT INTO a (c1, c2) SELECT * FROM t", new Insert(table, Optional.of(ImmutableList.of(identifier("c1"), identifier("c2"))), query));
}
use of io.prestosql.sql.tree.QualifiedName in project hetu-core by openlookeng.
the class AstBuilder method visitFunctionCall.
@Override
public Node visitFunctionCall(SqlBaseParser.FunctionCallContext context) {
Optional<Expression> filter = visitIfPresent(context.filter(), Expression.class);
Optional<Window> window = visitIfPresent(context.over(), Window.class);
Optional<OrderBy> orderBy = Optional.empty();
if (context.ORDER() != null) {
orderBy = Optional.of(new OrderBy(visit(context.sortItem(), SortItem.class)));
}
QualifiedName name = getQualifiedName(context.qualifiedName());
boolean distinct = isDistinct(context.setQuantifier());
boolean ignoreNulls = context.nullTreatment() != null && context.nullTreatment().IGNORE() != null;
if (name.toString().equalsIgnoreCase("if")) {
check(context.expression().size() == 2 || context.expression().size() == 3, "Invalid number of arguments for 'if' function", context);
check(!window.isPresent(), "OVER clause not valid for 'if' function", context);
check(!distinct, "DISTINCT not valid for 'if' function", context);
check(!filter.isPresent(), "FILTER not valid for 'if' function", context);
Expression elseExpression = null;
if (context.expression().size() == 3) {
elseExpression = (Expression) visit(context.expression(2));
}
return new IfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)), elseExpression);
}
if (name.toString().equalsIgnoreCase("nullif")) {
check(context.expression().size() == 2, "Invalid number of arguments for 'nullif' function", context);
check(!window.isPresent(), "OVER clause not valid for 'nullif' function", context);
check(!distinct, "DISTINCT not valid for 'nullif' function", context);
check(!filter.isPresent(), "FILTER not valid for 'nullif' function", context);
return new NullIfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)));
}
if (name.toString().equalsIgnoreCase("coalesce")) {
check(context.expression().size() >= 2, "The 'coalesce' function must have at least two arguments", context);
check(!window.isPresent(), "OVER clause not valid for 'coalesce' function", context);
check(!distinct, "DISTINCT not valid for 'coalesce' function", context);
check(!filter.isPresent(), "FILTER not valid for 'coalesce' function", context);
return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
}
if (name.toString().equalsIgnoreCase("try")) {
check(context.expression().size() == 1, "The 'try' function must have exactly one argument", context);
check(!window.isPresent(), "OVER clause not valid for 'try' function", context);
check(!distinct, "DISTINCT not valid for 'try' function", context);
check(!filter.isPresent(), "FILTER not valid for 'try' function", context);
return new TryExpression(getLocation(context), (Expression) visit(getOnlyElement(context.expression())));
}
if (name.toString().equalsIgnoreCase("format")) {
check(context.expression().size() >= 2, "The 'format' function must have at least two arguments", context);
check(!window.isPresent(), "OVER clause not valid for 'format' function", context);
check(!distinct, "DISTINCT not valid for 'format' function", context);
check(!filter.isPresent(), "FILTER not valid for 'format' function", context);
return new Format(getLocation(context), visit(context.expression(), Expression.class));
}
if (name.toString().equalsIgnoreCase("$internal$bind")) {
check(context.expression().size() >= 1, "The '$internal$bind' function must have at least one arguments", context);
check(!window.isPresent(), "OVER clause not valid for '$internal$bind' function", context);
check(!distinct, "DISTINCT not valid for '$internal$bind' function", context);
check(!filter.isPresent(), "FILTER not valid for '$internal$bind' function", context);
int numValues = context.expression().size() - 1;
List<Expression> arguments = context.expression().stream().map(this::visit).map(Expression.class::cast).collect(toImmutableList());
return new BindExpression(getLocation(context), arguments.subList(0, numValues), arguments.get(numValues));
}
return new FunctionCall(Optional.of(getLocation(context)), getQualifiedName(context.qualifiedName()), window, filter, orderBy, distinct, ignoreNulls, visit(context.expression(), Expression.class));
}
use of io.prestosql.sql.tree.QualifiedName 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.QualifiedName in project hetu-core by openlookeng.
the class TestDropCacheTask method testDropCacheNotExistsTrue.
@Test
public void testDropCacheNotExistsTrue() {
QualifiedName tableName = QualifiedName.of(CATALOG_NAME, schema, table);
DropCache statement = new DropCache(tableName, true);
getFutureValue(new DropCacheTask().execute(statement, createTestTransactionManager(), metadata, new AllowAllAccessControl(), stateMachine, Collections.emptyList(), new HeuristicIndexerManager(new FileSystemClientManager(), new HetuMetaStoreManager())));
assertFalse(SplitCacheMap.getInstance().cacheExists(tableName));
QualifiedName table2Name = QualifiedName.of(CATALOG_NAME, schema, table2);
DropCache statement2 = new DropCache(table2Name, true);
assertTrue(SplitCacheMap.getInstance().cacheExists(table2Name));
getFutureValue(new DropCacheTask().execute(statement2, createTestTransactionManager(), metadata, new AllowAllAccessControl(), stateMachine, Collections.emptyList(), new HeuristicIndexerManager(new FileSystemClientManager(), new HetuMetaStoreManager())));
assertFalse(SplitCacheMap.getInstance().cacheExists(table2Name));
}
use of io.prestosql.sql.tree.QualifiedName in project hetu-core by openlookeng.
the class TestSetSessionTask method testSetSessionWithParameters.
private void testSetSessionWithParameters(String property, Expression expression, String expectedValue, List<Expression> parameters) {
QualifiedName qualifiedPropName = QualifiedName.of(CATALOG_NAME, property);
QueryStateMachine stateMachine = QueryStateMachine.begin(format("set %s = 'old_value'", qualifiedPropName), Optional.empty(), TEST_SESSION, URI.create("fake://uri"), new ResourceGroupId("test"), new NoOpResourceGroupManager(), false, transactionManager, accessControl, executor, metadata, WarningCollector.NOOP);
getFutureValue(new SetSessionTask().execute(new SetSession(qualifiedPropName, expression), transactionManager, metadata, accessControl, stateMachine, parameters, new HeuristicIndexerManager(new FileSystemClientManager(), new HetuMetaStoreManager())));
Map<String, String> sessionProperties = stateMachine.getSetSessionProperties();
assertEquals(sessionProperties, ImmutableMap.of(qualifiedPropName.toString(), expectedValue));
}
Aggregations