use of io.prestosql.sql.tree.NullIfExpression in project hetu-core by openlookeng.
the class TestSqlParser method testNullIf.
@Test
public void testNullIf() {
assertExpression("nullif(42, 87)", new NullIfExpression(new LongLiteral("42"), new LongLiteral("87")));
assertExpression("nullif(42, null)", new NullIfExpression(new LongLiteral("42"), new NullLiteral()));
assertExpression("nullif(null, null)", new NullIfExpression(new NullLiteral(), new NullLiteral()));
assertInvalidExpression("nullif(1)", "Invalid number of arguments for 'nullif' function");
assertInvalidExpression("nullif(1, 2, 3)", "Invalid number of arguments for 'nullif' function");
assertInvalidExpression("nullif(42, 87) filter (where true)", "FILTER not valid for 'nullif' function");
assertInvalidExpression("nullif(42, 87) OVER ()", "OVER clause not valid for 'nullif' function");
}
use of io.prestosql.sql.tree.NullIfExpression 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.NullIfExpression in project hetu-core by openlookeng.
the class ImpalaAstBuilder method visitFunctionCall.
@Override
public Node visitFunctionCall(ImpalaSqlParser.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());
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));
}
return new FunctionCall(Optional.of(getLocation(context)), getQualifiedName(context.qualifiedName()), window, filter, orderBy, distinct, false, visit(context.expression(), Expression.class));
}
use of io.prestosql.sql.tree.NullIfExpression in project hetu-core by openlookeng.
the class HiveAstBuilder method visitFunctionCall.
@Override
public Node visitFunctionCall(HiveSqlParser.FunctionCallContext context) {
Optional<Expression> filter = Optional.empty();
Optional<OrderBy> orderBy = Optional.empty();
Optional<Window> window = visitIfPresent(context.over(), Window.class);
boolean distinct = isDistinct(context.setQuantifier());
QualifiedName name = getQualifiedName(context.qualifiedName());
if (name.toString().equalsIgnoreCase("if")) {
check(context.expression().size() == 3, "Illegal arguments for 'if' function", context);
check(!window.isPresent(), "OVER not valid for 'if' function", context);
check(!distinct, "DISTINCT not valid for 'if' function", context);
Expression 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, "Illegal arguments for 'nullif' function", context);
check(!window.isPresent(), "OVER not valid for 'nullif' function", context);
check(!distinct, "DISTINCT 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() > 0, "The 'coalesce' function must have at least one argument", context);
check(!window.isPresent(), "OVER not valid for 'coalesce' function", context);
check(!distinct, "DISTINCT not valid for 'coalesce' function", context);
return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
}
return new FunctionCall(Optional.of(getLocation(context)), getQualifiedName(context.qualifiedName()), window, filter, orderBy, distinct, false, visit(context.expression(), Expression.class));
}
use of io.prestosql.sql.tree.NullIfExpression 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));
}
Aggregations