Search in sources :

Example 1 with Format

use of io.prestosql.sql.tree.Format in project hetu-core by openlookeng.

the class TestSqlParser method testFormat.

@Test
public void testFormat() {
    assertExpression("format('%s', 'abc')", new Format(ImmutableList.of(new StringLiteral("%s"), new StringLiteral("abc"))));
    assertExpression("format('%d %s', 123, 'x')", new Format(ImmutableList.of(new StringLiteral("%d %s"), new LongLiteral("123"), new StringLiteral("x"))));
    assertInvalidExpression("format()", "The 'format' function must have at least two arguments");
    assertInvalidExpression("format('%s')", "The 'format' function must have at least two arguments");
}
Also used : ExplainFormat(io.prestosql.sql.tree.ExplainFormat) Format(io.prestosql.sql.tree.Format) StringLiteral(io.prestosql.sql.tree.StringLiteral) LongLiteral(io.prestosql.sql.tree.LongLiteral) Test(org.testng.annotations.Test)

Example 2 with Format

use of io.prestosql.sql.tree.Format in project hetu-core by openlookeng.

the class ExpressionFormatter method formatGroupBy.

static String formatGroupBy(List<GroupingElement> groupingElements, Optional<List<Expression>> parameters) {
    ImmutableList.Builder<String> resultStrings = ImmutableList.builder();
    for (GroupingElement groupingElement : groupingElements) {
        String result = "";
        if (groupingElement instanceof SimpleGroupBy) {
            List<Expression> columns = groupingElement.getExpressions();
            if (columns.size() == 1) {
                result = formatExpression(getOnlyElement(columns), parameters);
            } else {
                result = formatGroupingSet(columns, parameters);
            }
        } else if (groupingElement instanceof GroupingSets) {
            result = format("GROUPING SETS (%s)", Joiner.on(", ").join(((GroupingSets) groupingElement).getSets().stream().map(e -> formatGroupingSet(e, parameters)).iterator()));
        } else if (groupingElement instanceof Cube) {
            result = format("CUBE %s", formatGroupingSet(groupingElement.getExpressions(), parameters));
        } else if (groupingElement instanceof Rollup) {
            result = format("ROLLUP %s", formatGroupingSet(groupingElement.getExpressions(), parameters));
        }
        resultStrings.add(result);
    }
    return Joiner.on(", ").join(resultStrings.build());
}
Also used : GroupingSets(io.prestosql.sql.tree.GroupingSets) Parameter(io.prestosql.sql.tree.Parameter) DecimalFormatSymbols(java.text.DecimalFormatSymbols) QualifiedName(io.prestosql.sql.tree.QualifiedName) FieldReference(io.prestosql.sql.tree.FieldReference) Cast(io.prestosql.sql.tree.Cast) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Rollup(io.prestosql.sql.tree.Rollup) WindowFrame(io.prestosql.sql.tree.WindowFrame) Locale(java.util.Locale) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) ArrayConstructor(io.prestosql.sql.tree.ArrayConstructor) Window(io.prestosql.sql.tree.Window) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) Identifier(io.prestosql.sql.tree.Identifier) Cube(io.prestosql.sql.tree.Cube) WhenClause(io.prestosql.sql.tree.WhenClause) BindExpression(io.prestosql.sql.tree.BindExpression) Row(io.prestosql.sql.tree.Row) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) GroupingOperation(io.prestosql.sql.tree.GroupingOperation) IsNotNullPredicate(io.prestosql.sql.tree.IsNotNullPredicate) GroupingElement(io.prestosql.sql.tree.GroupingElement) NullLiteral(io.prestosql.sql.tree.NullLiteral) TimeLiteral(io.prestosql.sql.tree.TimeLiteral) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) List(java.util.List) DoubleLiteral(io.prestosql.sql.tree.DoubleLiteral) GenericLiteral(io.prestosql.sql.tree.GenericLiteral) LongLiteral(io.prestosql.sql.tree.LongLiteral) CharLiteral(io.prestosql.sql.tree.CharLiteral) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) SymbolReference(io.prestosql.sql.tree.SymbolReference) SimpleGroupBy(io.prestosql.sql.tree.SimpleGroupBy) StringLiteral(io.prestosql.sql.tree.StringLiteral) AtTimeZone(io.prestosql.sql.tree.AtTimeZone) Optional(java.util.Optional) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) FrameBound(io.prestosql.sql.tree.FrameBound) IfExpression(io.prestosql.sql.tree.IfExpression) GroupingSets(io.prestosql.sql.tree.GroupingSets) Joiner(com.google.common.base.Joiner) InPredicate(io.prestosql.sql.tree.InPredicate) Extract(io.prestosql.sql.tree.Extract) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) CurrentPath(io.prestosql.sql.tree.CurrentPath) AllColumns(io.prestosql.sql.tree.AllColumns) DecimalLiteral(io.prestosql.sql.tree.DecimalLiteral) Node(io.prestosql.sql.tree.Node) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) BooleanLiteral(io.prestosql.sql.tree.BooleanLiteral) IntervalLiteral(io.prestosql.sql.tree.IntervalLiteral) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) LikePredicate(io.prestosql.sql.tree.LikePredicate) AstVisitor(io.prestosql.sql.tree.AstVisitor) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) SortItem(io.prestosql.sql.tree.SortItem) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) TimestampLiteral(io.prestosql.sql.tree.TimestampLiteral) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) CurrentTime(io.prestosql.sql.tree.CurrentTime) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) CharMatcher(com.google.common.base.CharMatcher) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) OrderBy(io.prestosql.sql.tree.OrderBy) DecimalFormat(java.text.DecimalFormat) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) CurrentUser(io.prestosql.sql.tree.CurrentUser) LambdaArgumentDeclaration(io.prestosql.sql.tree.LambdaArgumentDeclaration) PrimitiveIterator(java.util.PrimitiveIterator) Collectors.toList(java.util.stream.Collectors.toList) ExistsPredicate(io.prestosql.sql.tree.ExistsPredicate) TryExpression(io.prestosql.sql.tree.TryExpression) BinaryLiteral(io.prestosql.sql.tree.BinaryLiteral) Format(io.prestosql.sql.tree.Format) Expression(io.prestosql.sql.tree.Expression) GroupingElement(io.prestosql.sql.tree.GroupingElement) SimpleGroupBy(io.prestosql.sql.tree.SimpleGroupBy) Rollup(io.prestosql.sql.tree.Rollup) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) TryExpression(io.prestosql.sql.tree.TryExpression) Expression(io.prestosql.sql.tree.Expression) Cube(io.prestosql.sql.tree.Cube) ImmutableList(com.google.common.collect.ImmutableList)

Example 3 with Format

use of io.prestosql.sql.tree.Format 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));
}
Also used : Window(io.prestosql.sql.tree.Window) OrderBy(io.prestosql.sql.tree.OrderBy) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) IfExpression(io.prestosql.sql.tree.IfExpression) QualifiedName(io.prestosql.sql.tree.QualifiedName) TryExpression(io.prestosql.sql.tree.TryExpression) SortItem(io.prestosql.sql.tree.SortItem) ExplainFormat(io.prestosql.sql.tree.ExplainFormat) Format(io.prestosql.sql.tree.Format) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) TryExpression(io.prestosql.sql.tree.TryExpression) BindExpression(io.prestosql.sql.tree.BindExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) FunctionCall(io.prestosql.sql.tree.FunctionCall) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression)

Aggregations

Format (io.prestosql.sql.tree.Format)3 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)2 ArithmeticUnaryExpression (io.prestosql.sql.tree.ArithmeticUnaryExpression)2 BindExpression (io.prestosql.sql.tree.BindExpression)2 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)2 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)2 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)2 Expression (io.prestosql.sql.tree.Expression)2 FunctionCall (io.prestosql.sql.tree.FunctionCall)2 IfExpression (io.prestosql.sql.tree.IfExpression)2 InListExpression (io.prestosql.sql.tree.InListExpression)2 LambdaExpression (io.prestosql.sql.tree.LambdaExpression)2 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)2 NotExpression (io.prestosql.sql.tree.NotExpression)2 NullIfExpression (io.prestosql.sql.tree.NullIfExpression)2 OrderBy (io.prestosql.sql.tree.OrderBy)2 QualifiedName (io.prestosql.sql.tree.QualifiedName)2 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)2 SearchedCaseExpression (io.prestosql.sql.tree.SearchedCaseExpression)2 SimpleCaseExpression (io.prestosql.sql.tree.SimpleCaseExpression)2