use of io.prestosql.sql.tree.DereferenceExpression in project hetu-core by openlookeng.
the class TestSqlParser method testAggregationWithOrderBy.
@Test
public void testAggregationWithOrderBy() {
assertExpression("array_agg(x ORDER BY x DESC)", new FunctionCall(QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(identifier("x"), DESCENDING, UNDEFINED)))), false, ImmutableList.of(identifier("x"))));
assertStatement("SELECT array_agg(x ORDER BY t.y) FROM t", new Query(Optional.empty(), new QuerySpecification(selectList(new FunctionCall(QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new DereferenceExpression(new Identifier("t"), identifier("y")), ASCENDING, UNDEFINED)))), false, ImmutableList.of(new Identifier("x")))), Optional.of(table(QualifiedName.of("t"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
}
use of io.prestosql.sql.tree.DereferenceExpression in project hetu-core by openlookeng.
the class TestSqlParser method testSelectWithRowType.
@Test
public void testSelectWithRowType() {
assertStatement("SELECT col1.f1, col2, col3.f1.f2.f3 FROM table1", new Query(Optional.empty(), new QuerySpecification(selectList(new DereferenceExpression(new Identifier("col1"), identifier("f1")), new Identifier("col2"), new DereferenceExpression(new DereferenceExpression(new DereferenceExpression(new Identifier("col3"), identifier("f1")), identifier("f2")), identifier("f3"))), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
assertStatement("SELECT col1.f1[0], col2, col3[2].f2.f3, col4[4] FROM table1", new Query(Optional.empty(), new QuerySpecification(selectList(new SubscriptExpression(new DereferenceExpression(new Identifier("col1"), identifier("f1")), new LongLiteral("0")), new Identifier("col2"), new DereferenceExpression(new DereferenceExpression(new SubscriptExpression(new Identifier("col3"), new LongLiteral("2")), identifier("f2")), identifier("f3")), new SubscriptExpression(new Identifier("col4"), new LongLiteral("4"))), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
assertStatement("SELECT CAST(ROW(11, 12) AS ROW(COL0 INTEGER, COL1 INTEGER)).col0", new Query(Optional.empty(), new QuerySpecification(selectList(new DereferenceExpression(new Cast(new Row(Lists.newArrayList(new LongLiteral("11"), new LongLiteral("12"))), "ROW(COL0 INTEGER,COL1 INTEGER)"), identifier("col0"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
}
use of io.prestosql.sql.tree.DereferenceExpression in project hetu-core by openlookeng.
the class TestEqualityInference method testExpressionsThatMayReturnNullOnNonNullInput.
@Test
public void testExpressionsThatMayReturnNullOnNonNullInput() {
List<Expression> candidates = ImmutableList.of(// try_cast
new Cast(nameReference("b"), "BIGINT", true), new FunctionCallBuilder(metadata).setName(QualifiedName.of(TryFunction.NAME)).addArgument(new FunctionType(ImmutableList.of(), VARCHAR), new LambdaExpression(ImmutableList.of(), nameReference("b"))).build(), new NullIfExpression(nameReference("b"), number(1)), new IfExpression(nameReference("b"), number(1), new NullLiteral()), new DereferenceExpression(nameReference("b"), identifier("x")), new InPredicate(nameReference("b"), new InListExpression(ImmutableList.of(new NullLiteral()))), new SearchedCaseExpression(ImmutableList.of(new WhenClause(new IsNotNullPredicate(nameReference("b")), new NullLiteral())), Optional.empty()), new SimpleCaseExpression(nameReference("b"), ImmutableList.of(new WhenClause(number(1), new NullLiteral())), Optional.empty()), new SubscriptExpression(new ArrayConstructor(ImmutableList.of(new NullLiteral())), nameReference("b")));
for (Expression candidate : candidates) {
EqualityInference.Builder builder = new EqualityInference.Builder();
builder.extractInferenceCandidates(equals(nameReference("b"), nameReference("x")));
builder.extractInferenceCandidates(equals(nameReference("a"), candidate));
EqualityInference inference = builder.build();
List<Expression> equalities = inference.generateEqualitiesPartitionedBy(matchesSymbols("b")).getScopeStraddlingEqualities();
assertEquals(equalities.size(), 1);
assertTrue(equalities.get(0).equals(equals(nameReference("x"), nameReference("b"))) || equalities.get(0).equals(equals(nameReference("b"), nameReference("x"))));
}
}
use of io.prestosql.sql.tree.DereferenceExpression in project hetu-core by openlookeng.
the class FunctionAssertions method createExpression.
public static Expression createExpression(Session session, String expression, Metadata metadata, TypeProvider symbolTypes) {
Expression parsedExpression = SQL_PARSER.createExpression(expression, createParsingOptions(session));
parsedExpression = rewriteIdentifiersToSymbolReferences(parsedExpression);
final ExpressionAnalysis analysis = analyzeExpressions(session, metadata, SQL_PARSER, symbolTypes, ImmutableList.of(parsedExpression), ImmutableList.of(), WarningCollector.NOOP, false);
Expression rewrittenExpression = ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>() {
@Override
public Expression rewriteExpression(Expression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
Expression rewrittenExpression = treeRewriter.defaultRewrite(node, context);
// cast expression if coercion is registered
Type coercion = analysis.getCoercion(node);
if (coercion != null) {
rewrittenExpression = new Cast(rewrittenExpression, coercion.getTypeSignature().toString(), false, analysis.isTypeOnlyCoercion(node));
}
return rewrittenExpression;
}
@Override
public Expression rewriteDereferenceExpression(DereferenceExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
if (analysis.isColumnReference(node)) {
return rewriteExpression(node, context, treeRewriter);
}
Expression rewrittenExpression = treeRewriter.defaultRewrite(node, context);
// cast expression if coercion is registered
Type coercion = analysis.getCoercion(node);
if (coercion != null) {
rewrittenExpression = new Cast(rewrittenExpression, coercion.getTypeSignature().toString());
}
return rewrittenExpression;
}
}, parsedExpression);
return CanonicalizeExpressionRewriter.rewrite(rewrittenExpression, session, metadata, new TypeAnalyzer(SQL_PARSER, metadata), symbolTypes);
}
use of io.prestosql.sql.tree.DereferenceExpression in project hetu-core by openlookeng.
the class TreePrinter method print.
public void print(Node root) {
AstVisitor<Void, Integer> printer = new DefaultTraversalVisitor<Void, Integer>() {
@Override
protected Void visitNode(Node node, Integer indentLevel) {
throw new UnsupportedOperationException("not yet implemented: " + node);
}
@Override
protected Void visitQuery(Query node, Integer indentLevel) {
print(indentLevel, "Query ");
Integer tmpIndentLevel = indentLevel;
tmpIndentLevel++;
print(tmpIndentLevel, "QueryBody");
process(node.getQueryBody(), tmpIndentLevel);
if (node.getOrderBy().isPresent()) {
print(tmpIndentLevel, "OrderBy");
process(node.getOrderBy().get(), tmpIndentLevel + 1);
}
if (node.getLimit().isPresent()) {
print(tmpIndentLevel, "Limit: " + node.getLimit().get());
}
return null;
}
@Override
protected Void visitQuerySpecification(QuerySpecification node, Integer indentLevel) {
print(indentLevel, "QuerySpecification ");
Integer tmpIndentLevel = indentLevel;
tmpIndentLevel++;
process(node.getSelect(), tmpIndentLevel);
if (node.getFrom().isPresent()) {
print(tmpIndentLevel, "From");
process(node.getFrom().get(), tmpIndentLevel + 1);
}
if (node.getWhere().isPresent()) {
print(tmpIndentLevel, "Where");
process(node.getWhere().get(), tmpIndentLevel + 1);
}
if (node.getGroupBy().isPresent()) {
String distinct = "";
if (node.getGroupBy().get().isDistinct()) {
distinct = "[DISTINCT]";
}
print(tmpIndentLevel, "GroupBy" + distinct);
for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) {
print(tmpIndentLevel, "SimpleGroupBy");
if (groupingElement instanceof SimpleGroupBy) {
for (Expression column : groupingElement.getExpressions()) {
process(column, tmpIndentLevel + 1);
}
} else if (groupingElement instanceof GroupingSets) {
print(tmpIndentLevel + 1, "GroupingSets");
for (List<Expression> set : ((GroupingSets) groupingElement).getSets()) {
print(tmpIndentLevel + 2, "GroupingSet[");
for (Expression expression : set) {
process(expression, tmpIndentLevel + 3);
}
print(tmpIndentLevel + 2, "]");
}
} else if (groupingElement instanceof Cube) {
print(tmpIndentLevel + 1, "Cube");
for (Expression column : groupingElement.getExpressions()) {
process(column, tmpIndentLevel + 1);
}
} else if (groupingElement instanceof Rollup) {
print(tmpIndentLevel + 1, "Rollup");
for (Expression column : groupingElement.getExpressions()) {
process(column, tmpIndentLevel + 1);
}
}
}
}
if (node.getHaving().isPresent()) {
print(tmpIndentLevel, "Having");
process(node.getHaving().get(), tmpIndentLevel + 1);
}
if (node.getOrderBy().isPresent()) {
print(tmpIndentLevel, "OrderBy");
process(node.getOrderBy().get(), tmpIndentLevel + 1);
}
if (node.getLimit().isPresent()) {
print(tmpIndentLevel, "Limit: " + node.getLimit().get());
}
return null;
}
protected Void visitOrderBy(OrderBy node, Integer indentLevel) {
for (SortItem sortItem : node.getSortItems()) {
process(sortItem, indentLevel);
}
return null;
}
@Override
protected Void visitSelect(Select node, Integer indentLevel) {
String distinct = "";
if (node.isDistinct()) {
distinct = "[DISTINCT]";
}
print(indentLevel, "Select" + distinct);
// visit children
super.visitSelect(node, indentLevel + 1);
return null;
}
@Override
protected Void visitAllColumns(AllColumns node, Integer indent) {
if (node.getPrefix().isPresent()) {
print(indent, node.getPrefix() + ".*");
} else {
print(indent, "*");
}
return null;
}
@Override
protected Void visitSingleColumn(SingleColumn node, Integer indent) {
if (node.getAlias().isPresent()) {
print(indent, "Alias: " + node.getAlias().get());
}
// visit children
super.visitSingleColumn(node, indent + 1);
return null;
}
@Override
protected Void visitComparisonExpression(ComparisonExpression node, Integer indentLevel) {
print(indentLevel, node.getOperator().toString());
super.visitComparisonExpression(node, indentLevel + 1);
return null;
}
@Override
protected Void visitArithmeticBinary(ArithmeticBinaryExpression node, Integer indentLevel) {
print(indentLevel, node.getOperator().toString());
super.visitArithmeticBinary(node, indentLevel + 1);
return null;
}
@Override
protected Void visitLogicalBinaryExpression(LogicalBinaryExpression node, Integer indentLevel) {
print(indentLevel, node.getOperator().toString());
super.visitLogicalBinaryExpression(node, indentLevel + 1);
return null;
}
@Override
protected Void visitStringLiteral(StringLiteral node, Integer indentLevel) {
print(indentLevel, "String[" + node.getValue() + "]");
return null;
}
@Override
protected Void visitBinaryLiteral(BinaryLiteral node, Integer indentLevel) {
print(indentLevel, "Binary[" + node.toHexString() + "]");
return null;
}
@Override
protected Void visitBooleanLiteral(BooleanLiteral node, Integer indentLevel) {
print(indentLevel, "Boolean[" + node.getValue() + "]");
return null;
}
@Override
protected Void visitLongLiteral(LongLiteral node, Integer indentLevel) {
print(indentLevel, "Long[" + node.getValue() + "]");
return null;
}
@Override
protected Void visitLikePredicate(LikePredicate node, Integer indentLevel) {
print(indentLevel, "LIKE");
super.visitLikePredicate(node, indentLevel + 1);
return null;
}
@Override
protected Void visitIdentifier(Identifier node, Integer indentLevel) {
QualifiedName resolved = resolvedNameReferences.get(node);
String resolvedName = "";
if (resolved != null) {
resolvedName = "=>" + resolved.toString();
}
print(indentLevel, "Identifier[" + node.getValue() + resolvedName + "]");
return null;
}
@Override
protected Void visitDereferenceExpression(DereferenceExpression node, Integer indentLevel) {
QualifiedName resolved = resolvedNameReferences.get(node);
String resolvedName = "";
if (resolved != null) {
resolvedName = "=>" + resolved.toString();
}
print(indentLevel, "DereferenceExpression[" + node + resolvedName + "]");
return null;
}
@Override
protected Void visitFunctionCall(FunctionCall node, Integer indentLevel) {
String name = Joiner.on('.').join(node.getName().getParts());
print(indentLevel, "FunctionCall[" + name + "]");
super.visitFunctionCall(node, indentLevel + 1);
return null;
}
@Override
protected Void visitTable(Table node, Integer indentLevel) {
String name = Joiner.on('.').join(node.getName().getParts());
print(indentLevel, "Table[" + name + "]");
return null;
}
@Override
protected Void visitValues(Values node, Integer indentLevel) {
print(indentLevel, "Values");
super.visitValues(node, indentLevel + 1);
return null;
}
@Override
protected Void visitRow(Row node, Integer indentLevel) {
print(indentLevel, "Row");
super.visitRow(node, indentLevel + 1);
return null;
}
@Override
protected Void visitAliasedRelation(AliasedRelation node, Integer indentLevel) {
print(indentLevel, "Alias[" + node.getAlias() + "]");
super.visitAliasedRelation(node, indentLevel + 1);
return null;
}
@Override
protected Void visitSampledRelation(SampledRelation node, Integer indentLevel) {
print(indentLevel, "TABLESAMPLE[" + node.getType() + " (" + node.getSamplePercentage() + ")]");
super.visitSampledRelation(node, indentLevel + 1);
return null;
}
@Override
protected Void visitTableSubquery(TableSubquery node, Integer indentLevel) {
print(indentLevel, "SubQuery");
super.visitTableSubquery(node, indentLevel + 1);
return null;
}
@Override
protected Void visitInPredicate(InPredicate node, Integer indentLevel) {
print(indentLevel, "IN");
super.visitInPredicate(node, indentLevel + 1);
return null;
}
@Override
protected Void visitSubqueryExpression(SubqueryExpression node, Integer indentLevel) {
print(indentLevel, "SubQuery");
super.visitSubqueryExpression(node, indentLevel + 1);
return null;
}
};
printer.process(root, 0);
}
Aggregations