use of io.trino.sql.tree.BooleanLiteral in project trino by trinodb.
the class TestMergeProjectWithValues method testMergeProjectWithValues.
@Test
public void testMergeProjectWithValues() {
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> {
Symbol a = p.symbol("a");
Symbol b = p.symbol("b");
Symbol c = p.symbol("c");
Symbol d = p.symbol("d");
Symbol e = p.symbol("e");
Symbol f = p.symbol("f");
Assignments.Builder assignments = Assignments.builder();
// identity assignment
assignments.putIdentity(a);
// renaming assignment
assignments.put(d, b.toSymbolReference());
// expression involving input symbol
assignments.put(e, new IsNullPredicate(a.toSymbolReference()));
// constant expression
assignments.put(f, new LongLiteral("1"));
return p.project(assignments.build(), p.valuesOfExpressions(ImmutableList.of(a, b, c), ImmutableList.of(new Row(ImmutableList.of(new CharLiteral("x"), new BooleanLiteral("true"), new LongLiteral("1"))), new Row(ImmutableList.of(new CharLiteral("y"), new BooleanLiteral("false"), new LongLiteral("2"))), new Row(ImmutableList.of(new CharLiteral("z"), new BooleanLiteral("true"), new LongLiteral("3"))))));
}).matches(values(ImmutableList.of("a", "d", "e", "f"), ImmutableList.of(ImmutableList.of(new CharLiteral("x"), new BooleanLiteral("true"), new IsNullPredicate(new CharLiteral("x")), new LongLiteral("1")), ImmutableList.of(new CharLiteral("y"), new BooleanLiteral("false"), new IsNullPredicate(new CharLiteral("y")), new LongLiteral("1")), ImmutableList.of(new CharLiteral("z"), new BooleanLiteral("true"), new IsNullPredicate(new CharLiteral("z")), new LongLiteral("1")))));
// ValuesNode has no rows
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> {
Symbol a = p.symbol("a");
Symbol b = p.symbol("b");
Symbol c = p.symbol("c");
Symbol d = p.symbol("d");
Symbol e = p.symbol("e");
Symbol f = p.symbol("f");
Assignments.Builder assignments = Assignments.builder();
// identity assignment
assignments.putIdentity(a);
// renaming assignment
assignments.put(d, b.toSymbolReference());
// expression involving input symbol
assignments.put(e, new IsNullPredicate(a.toSymbolReference()));
// constant expression
assignments.put(f, new LongLiteral("1"));
return p.project(assignments.build(), p.values(ImmutableList.of(a, b, c), ImmutableList.of()));
}).matches(values(ImmutableList.of("a", "d", "e", "f"), ImmutableList.of()));
}
use of io.trino.sql.tree.BooleanLiteral in project trino by trinodb.
the class TestMergeProjectWithValues method testProjectWithoutOutputSymbols.
@Test
public void testProjectWithoutOutputSymbols() {
// ValuesNode has two output symbols and two rows
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(), p.valuesOfExpressions(ImmutableList.of(p.symbol("a"), p.symbol("b")), ImmutableList.of(new Row(ImmutableList.of(new CharLiteral("x"), new BooleanLiteral("true"))), new Row(ImmutableList.of(new CharLiteral("y"), new BooleanLiteral("false"))))))).matches(values(2));
// ValuesNode has no output symbols and two rows
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(), p.values(ImmutableList.of(), ImmutableList.of(ImmutableList.of(), ImmutableList.of())))).matches(values(2));
// ValuesNode has two output symbols and no rows
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(), p.values(ImmutableList.of(p.symbol("a"), p.symbol("b")), ImmutableList.of()))).matches(values());
// ValuesNode has no output symbols and no rows
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(), p.values(ImmutableList.of(), ImmutableList.of()))).matches(values());
}
use of io.trino.sql.tree.BooleanLiteral in project trino by trinodb.
the class AstBuilder method visitListagg.
/**
* Returns the corresponding {@link FunctionCall} for the `LISTAGG` primary expression.
* <p>
* Although the syntax tree should represent the structure of the original parsed query
* as closely as possible and any semantic interpretation should be part of the
* analysis/planning phase, in case of `LISTAGG` aggregation function it is more pragmatic
* now to create a synthetic {@link FunctionCall} expression during the parsing of the syntax tree.
*
* @param context `LISTAGG` expression context
*/
@Override
public Node visitListagg(SqlBaseParser.ListaggContext context) {
Optional<Window> window = Optional.empty();
OrderBy orderBy = new OrderBy(visit(context.sortItem(), SortItem.class));
boolean distinct = isDistinct(context.setQuantifier());
Expression expression = (Expression) visit(context.expression());
StringLiteral separator = context.string() == null ? new StringLiteral(getLocation(context), "") : (StringLiteral) (visit(context.string()));
BooleanLiteral overflowError = new BooleanLiteral(getLocation(context), "true");
StringLiteral overflowFiller = new StringLiteral(getLocation(context), "...");
BooleanLiteral showOverflowEntryCount = new BooleanLiteral(getLocation(context), "false");
SqlBaseParser.ListAggOverflowBehaviorContext overflowBehavior = context.listAggOverflowBehavior();
if (overflowBehavior != null) {
if (overflowBehavior.ERROR() != null) {
overflowError = new BooleanLiteral(getLocation(context), "true");
} else if (overflowBehavior.TRUNCATE() != null) {
overflowError = new BooleanLiteral(getLocation(context), "false");
if (overflowBehavior.string() != null) {
overflowFiller = (StringLiteral) (visit(overflowBehavior.string()));
}
SqlBaseParser.ListaggCountIndicationContext listaggCountIndicationContext = overflowBehavior.listaggCountIndication();
if (listaggCountIndicationContext.WITH() != null) {
showOverflowEntryCount = new BooleanLiteral(getLocation(context), "true");
} else if (listaggCountIndicationContext.WITHOUT() != null) {
showOverflowEntryCount = new BooleanLiteral(getLocation(context), "false");
}
}
}
List<Expression> arguments = ImmutableList.of(expression, separator, overflowError, overflowFiller, showOverflowEntryCount);
// TODO model this as a ListAgg node in the AST
return new FunctionCall(Optional.of(getLocation(context)), QualifiedName.of("LISTAGG"), window, Optional.empty(), Optional.of(orderBy), distinct, Optional.empty(), Optional.empty(), arguments);
}
use of io.trino.sql.tree.BooleanLiteral in project trino by trinodb.
the class TreePrinter method print.
public void print(Node root) {
AstVisitor<Void, Integer> printer = new DefaultTraversalVisitor<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 ");
indentLevel++;
print(indentLevel, "QueryBody");
process(node.getQueryBody(), indentLevel);
if (node.getOrderBy().isPresent()) {
print(indentLevel, "OrderBy");
process(node.getOrderBy().get(), indentLevel + 1);
}
if (node.getLimit().isPresent()) {
print(indentLevel, "Limit: " + node.getLimit().get());
}
return null;
}
@Override
protected Void visitQuerySpecification(QuerySpecification node, Integer indentLevel) {
print(indentLevel, "QuerySpecification ");
indentLevel++;
process(node.getSelect(), indentLevel);
if (node.getFrom().isPresent()) {
print(indentLevel, "From");
process(node.getFrom().get(), indentLevel + 1);
}
if (node.getWhere().isPresent()) {
print(indentLevel, "Where");
process(node.getWhere().get(), indentLevel + 1);
}
if (node.getGroupBy().isPresent()) {
String distinct = "";
if (node.getGroupBy().get().isDistinct()) {
distinct = "[DISTINCT]";
}
print(indentLevel, "GroupBy" + distinct);
for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) {
print(indentLevel, "SimpleGroupBy");
if (groupingElement instanceof SimpleGroupBy) {
for (Expression column : groupingElement.getExpressions()) {
process(column, indentLevel + 1);
}
} else if (groupingElement instanceof GroupingSets) {
print(indentLevel + 1, "GroupingSets");
for (List<Expression> set : ((GroupingSets) groupingElement).getSets()) {
print(indentLevel + 2, "GroupingSet[");
for (Expression expression : set) {
process(expression, indentLevel + 3);
}
print(indentLevel + 2, "]");
}
} else if (groupingElement instanceof Cube) {
print(indentLevel + 1, "Cube");
for (Expression column : groupingElement.getExpressions()) {
process(column, indentLevel + 1);
}
} else if (groupingElement instanceof Rollup) {
print(indentLevel + 1, "Rollup");
for (Expression column : groupingElement.getExpressions()) {
process(column, indentLevel + 1);
}
}
}
}
if (node.getHaving().isPresent()) {
print(indentLevel, "Having");
process(node.getHaving().get(), indentLevel + 1);
}
if (!node.getWindows().isEmpty()) {
print(indentLevel, "Window");
for (WindowDefinition windowDefinition : node.getWindows()) {
process(windowDefinition, indentLevel + 1);
}
}
if (node.getOrderBy().isPresent()) {
print(indentLevel, "OrderBy");
process(node.getOrderBy().get(), indentLevel + 1);
}
if (node.getLimit().isPresent()) {
print(indentLevel, "Limit: " + node.getLimit().get());
}
return null;
}
@Override
protected Void visitOrderBy(OrderBy node, Integer indentLevel) {
for (SortItem sortItem : node.getSortItems()) {
process(sortItem, indentLevel);
}
return null;
}
@Override
protected Void visitWindowDefinition(WindowDefinition node, Integer indentLevel) {
print(indentLevel, "WindowDefinition[" + node.getName() + "]");
process(node.getWindow(), indentLevel + 1);
return null;
}
@Override
protected Void visitWindowReference(WindowReference node, Integer indentLevel) {
print(indentLevel, "WindowReference[" + node.getName() + "]");
return null;
}
@Override
public Void visitWindowSpecification(WindowSpecification node, Integer indentLevel) {
if (node.getExistingWindowName().isPresent()) {
print(indentLevel, "ExistingWindowName " + node.getExistingWindowName().get());
}
if (!node.getPartitionBy().isEmpty()) {
print(indentLevel, "PartitionBy");
for (Expression expression : node.getPartitionBy()) {
process(expression, indentLevel + 1);
}
}
if (node.getOrderBy().isPresent()) {
print(indentLevel, "OrderBy");
process(node.getOrderBy().get(), indentLevel + 1);
}
if (node.getFrame().isPresent()) {
print(indentLevel, "Frame");
process(node.getFrame().get(), indentLevel + 1);
}
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) {
StringBuilder aliases = new StringBuilder();
if (!node.getAliases().isEmpty()) {
aliases.append(" [Aliases: ");
Joiner.on(", ").appendTo(aliases, node.getAliases());
aliases.append("]");
}
print(indent, "All columns" + aliases.toString());
if (node.getTarget().isPresent()) {
// visit child
super.visitAllColumns(node, indent + 1);
}
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 visitLogicalExpression(LogicalExpression node, Integer indentLevel) {
print(indentLevel, node.getOperator().toString());
super.visitLogicalExpression(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);
}
use of io.trino.sql.tree.BooleanLiteral in project trino by trinodb.
the class TestSqlParser method testSetMaterializedViewProperties.
@Test
public void testSetMaterializedViewProperties() {
assertStatement("ALTER MATERIALIZED VIEW a SET PROPERTIES foo='bar'", new SetProperties(MATERIALIZED_VIEW, QualifiedName.of("a"), ImmutableList.of(new Property(new Identifier("foo"), new StringLiteral("bar")))));
assertStatement("ALTER MATERIALIZED VIEW a SET PROPERTIES foo=true", new SetProperties(MATERIALIZED_VIEW, QualifiedName.of("a"), ImmutableList.of(new Property(new Identifier("foo"), new BooleanLiteral("true")))));
assertStatement("ALTER MATERIALIZED VIEW a SET PROPERTIES foo=123", new SetProperties(MATERIALIZED_VIEW, QualifiedName.of("a"), ImmutableList.of(new Property(new Identifier("foo"), new LongLiteral("123")))));
assertStatement("ALTER MATERIALIZED VIEW a SET PROPERTIES foo=123, bar=456", new SetProperties(MATERIALIZED_VIEW, QualifiedName.of("a"), ImmutableList.of(new Property(new Identifier("foo"), new LongLiteral("123")), new Property(new Identifier("bar"), new LongLiteral("456")))));
assertStatement("ALTER MATERIALIZED VIEW a SET PROPERTIES \" s p a c e \"='bar'", new SetProperties(MATERIALIZED_VIEW, QualifiedName.of("a"), ImmutableList.of(new Property(new Identifier(" s p a c e "), new StringLiteral("bar")))));
assertStatement("ALTER MATERIALIZED VIEW a SET PROPERTIES foo=123, bar=DEFAULT", new SetProperties(MATERIALIZED_VIEW, QualifiedName.of("a"), ImmutableList.of(new Property(new Identifier("foo"), new LongLiteral("123")), new Property(new Identifier("bar")))));
assertStatementIsInvalid("ALTER MATERIALIZED VIEW a SET PROPERTIES").withMessage("line 1:41: mismatched input '<EOF>'. Expecting: <identifier>");
assertStatementIsInvalid("ALTER MATERIALIZED VIEW a SET PROPERTIES ()").withMessage("line 1:42: mismatched input '('. Expecting: <identifier>");
assertStatementIsInvalid("ALTER MATERIALIZED VIEW a SET PROPERTIES (foo='bar')").withMessage("line 1:42: mismatched input '('. Expecting: <identifier>");
}
Aggregations