use of io.prestosql.sql.tree.FunctionCall in project hetu-core by openlookeng.
the class AstBuilder method visitNormalize.
@Override
public Node visitNormalize(SqlBaseParser.NormalizeContext context) {
Expression str = (Expression) visit(context.valueExpression());
String normalForm = Optional.ofNullable(context.normalForm()).map(ParserRuleContext::getText).orElse("NFC");
return new FunctionCall(getLocation(context), QualifiedName.of("normalize"), ImmutableList.of(str, new StringLiteral(getLocation(context), normalForm)));
}
use of io.prestosql.sql.tree.FunctionCall 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.FunctionCall in project hetu-core by openlookeng.
the class TestSetSessionTask method testSetSessionWithParameters.
@Test
public void testSetSessionWithParameters() {
FunctionCall functionCall = new FunctionCallBuilder(metadata).setName(QualifiedName.of("concat")).addArgument(VARCHAR, new StringLiteral("ban")).addArgument(VARCHAR, new Parameter(0)).build();
testSetSessionWithParameters("bar", functionCall, "banana", ImmutableList.of(new StringLiteral("ana")));
}
use of io.prestosql.sql.tree.FunctionCall in project hetu-core by openlookeng.
the class TestEffectivePredicateExtractor method testValues.
@Test
public void testValues() {
TypeProvider types = TypeProvider.copyOf(ImmutableMap.<Symbol, Type>builder().put(A, BIGINT).put(B, BIGINT).build());
// one column
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(bigintLiteralRowExpression(2)))), types, typeAnalyzer), new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))));
// one column with null
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(bigintLiteralRowExpression(2)), ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))))), types, typeAnalyzer), or(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new IsNullPredicate(AE)));
// all nulls
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))))), types, typeAnalyzer), new IsNullPredicate(AE));
// many rows
List<List<RowExpression>> rows = IntStream.range(0, 500).mapToObj(TestEffectivePredicateExtractor::bigintLiteralRowExpression).map(ImmutableList::of).collect(toImmutableList());
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), rows), types, typeAnalyzer), new BetweenPredicate(AE, bigintLiteral(0), bigintLiteral(499)));
// multiple columns
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), bigintLiteralRowExpression(100)), ImmutableList.of(bigintLiteralRowExpression(2), bigintLiteralRowExpression(200)))), types, typeAnalyzer), and(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new InPredicate(BE, new InListExpression(ImmutableList.of(bigintLiteral(100), bigintLiteral(200))))));
// multiple columns with null
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))), ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString())), bigintLiteralRowExpression(200)))), types, typeAnalyzer), and(or(new ComparisonExpression(EQUAL, AE, bigintLiteral(1)), new IsNullPredicate(AE)), or(new ComparisonExpression(EQUAL, BE, bigintLiteral(200)), new IsNullPredicate(BE))));
// non-deterministic
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), castToRowExpression(new FunctionCall(QualifiedName.of("rand"), ImmutableList.of()))))), types, typeAnalyzer), new ComparisonExpression(EQUAL, AE, bigintLiteral(1)));
// non-constant
assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(castToRowExpression(BE)))), types, typeAnalyzer), TRUE_LITERAL);
}
use of io.prestosql.sql.tree.FunctionCall in project hetu-core by openlookeng.
the class TestExpressionDomainTranslator method testExpressionConstantFolding.
@Test
public void testExpressionConstantFolding() {
FunctionCall fromHex = new FunctionCallBuilder(metadata).setName(QualifiedName.of("from_hex")).addArgument(VARCHAR, stringLiteral("123456")).build();
Expression originalExpression = comparison(GREATER_THAN, toSymbolReference(C_VARBINARY), fromHex);
ExtractionResult result = fromPredicate(originalExpression);
assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456"));
assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_VARBINARY, Domain.create(ValueSet.ofRanges(Range.greaterThan(VARBINARY, value)), false))));
Expression expression = toPredicate(result.getTupleDomain());
assertEquals(expression, comparison(GREATER_THAN, toSymbolReference(C_VARBINARY), varbinaryLiteral(value)));
}
Aggregations