private static Expression orNullHashCode(Expression expression) { return new CoalesceExpression(expression, new LongLiteral(String.valueOf(NULL_HASH_CODE))); }
@Override protected Object visitCoalesceExpression(CoalesceExpression node, Object context) { Type type = type(node); List<Object> values = node.getOperands().stream() .map(value -> processWithExceptionHandling(value, context)) .filter(Objects::nonNull) .collect(Collectors.toList()); if ((!values.isEmpty() && !(values.get(0) instanceof Expression)) || values.size() == 1) { return values.get(0); } List<Expression> expressions = values.stream() .map(value -> toExpression(value, type)) .collect(Collectors.toList()); if (expressions.isEmpty()) { return null; } return new CoalesceExpression(expressions); }
public static SelectItem aliasedNullToEmpty(String column, String alias) { return new SingleColumn(new CoalesceExpression(identifier(column), new StringLiteral("")), identifier(alias)); }
public static Optional<Expression> getHashExpression(List<Symbol> symbols) { if (symbols.isEmpty()) { return Optional.empty(); } Expression result = new LongLiteral(String.valueOf(INITIAL_HASH_VALUE)); for (Symbol symbol : symbols) { Expression hashField = new FunctionCall( QualifiedName.of(HASH_CODE), Optional.empty(), false, ImmutableList.of(new SymbolReference(symbol.getName()))); hashField = new CoalesceExpression(hashField, new LongLiteral(String.valueOf(NULL_HASH_CODE))); result = new FunctionCall(QualifiedName.of("combine_hash"), ImmutableList.of(result, hashField)); } return Optional.of(result); }
for (Symbol symbol : outerJoin.getOutputSymbols()) { if (aggregationNode.getAggregations().containsKey(symbol)) { assignmentsBuilder.put(symbol, new CoalesceExpression(symbol.toSymbolReference(), sourceAggregationToOverNullMapping.get(symbol).toSymbolReference()));
@Test(timeOut = 10_000) public void testPossibleExponentialOptimizationTime() { Expression expression = new LongLiteral("1"); ImmutableMap.Builder<NodeRef<Expression>, Type> types = ImmutableMap.builder(); types.put(NodeRef.of(expression), BIGINT); for (int i = 0; i < 100; i++) { expression = new CoalesceExpression(expression, new LongLiteral("2")); types.put(NodeRef.of(expression), BIGINT); } translateAndOptimize(expression, types.build()); }
for (Symbol symbol : aggregationNode.getOutputSymbols()) { if (coalesceSymbols.containsKey(symbol)) { Expression expression = new CoalesceExpression(symbol.toSymbolReference(), new Cast(new LongLiteral("0"), "bigint")); outputSymbols.put(coalesceSymbols.get(symbol), expression);
check(!filter.isPresent(), "FILTER not valid for 'coalesce' function", context); return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
Symbol output = symbolAllocator.newSymbol(column, analysis.getType(column)); outputs.add(output); assignments.put(output, new CoalesceExpression( leftJoinColumns.get(column).toSymbolReference(), rightJoinColumns.get(column).toSymbolReference()));
private Optional<PlanNode> rewriteToNonDefaultAggregation(ApplyNode applyNode, Context context) { checkState(applyNode.getSubquery().getOutputSymbols().isEmpty(), "Expected subquery output symbols to be pruned"); Symbol exists = getOnlyElement(applyNode.getSubqueryAssignments().getSymbols()); Symbol subqueryTrue = context.getSymbolAllocator().newSymbol("subqueryTrue", BOOLEAN); Assignments.Builder assignments = Assignments.builder(); assignments.putIdentities(applyNode.getInput().getOutputSymbols()); assignments.put(exists, new CoalesceExpression(ImmutableList.of(subqueryTrue.toSymbolReference(), BooleanLiteral.FALSE_LITERAL))); PlanNode subquery = new ProjectNode( context.getIdAllocator().getNextId(), new LimitNode( context.getIdAllocator().getNextId(), applyNode.getSubquery(), 1L, false), Assignments.of(subqueryTrue, TRUE_LITERAL)); PlanNodeDecorrelator decorrelator = new PlanNodeDecorrelator(context.getIdAllocator(), context.getLookup()); if (!decorrelator.decorrelateFilters(subquery, applyNode.getCorrelation()).isPresent()) { return Optional.empty(); } return Optional.of(new ProjectNode(context.getIdAllocator().getNextId(), new LateralJoinNode( applyNode.getId(), applyNode.getInput(), subquery, applyNode.getCorrelation(), LEFT, applyNode.getOriginSubquery()), assignments.build())); }
@Test public void testCoalesce() { assertInvalidExpression("coalesce()", "The 'coalesce' function must have at least two arguments"); assertInvalidExpression("coalesce(5)", "The 'coalesce' function must have at least two arguments"); assertInvalidExpression("coalesce(1, 2) filter (where true)", "FILTER not valid for 'coalesce' function"); assertInvalidExpression("coalesce(1, 2) OVER ()", "OVER clause not valid for 'coalesce' function"); assertExpression("coalesce(13, 42)", new CoalesceExpression(new LongLiteral("13"), new LongLiteral("42"))); assertExpression("coalesce(6, 7, 8)", new CoalesceExpression(new LongLiteral("6"), new LongLiteral("7"), new LongLiteral("8"))); assertExpression("coalesce(13, null)", new CoalesceExpression(new LongLiteral("13"), new NullLiteral())); assertExpression("coalesce(null, 13)", new CoalesceExpression(new NullLiteral(), new LongLiteral("13"))); assertExpression("coalesce(null, null)", new CoalesceExpression(new NullLiteral(), new NullLiteral())); }
for (JoinNode.EquiJoinClause equality : node.getCriteria()) { if (equality.getLeft().equals(column) || equality.getRight().equals(column)) { coalesceExpressions.add(new CoalesceExpression(ImmutableList.of(equality.getLeft().toSymbolReference(), equality.getRight().toSymbolReference())));
private static Expression orNullHashCode(Expression expression) { return new CoalesceExpression(expression, new LongLiteral(String.valueOf(TypeUtils.NULL_HASH_CODE))); } }
@Override protected Object visitCoalesceExpression(CoalesceExpression node, Object context) { Type type = type(node); List<Object> values = node.getOperands().stream() .map(value -> processWithExceptionHandling(value, context)) .filter(value -> value != null) .collect(Collectors.toList()); if ((!values.isEmpty() && !(values.get(0) instanceof Expression)) || values.size() == 1) { return values.get(0); } List<Expression> expressions = values.stream() .map(value -> toExpression(value, type)) .collect(Collectors.toList()); if (expressions.isEmpty()) { return null; } return new CoalesceExpression(expressions); }
public static SelectItem aliasedNullToEmpty(String column, String alias) { return new SingleColumn(new CoalesceExpression(nameReference(column), new StringLiteral("")), alias); }
public static SelectItem aliasedNullToEmpty(String column, String alias) { return new SingleColumn(new CoalesceExpression(identifier(column), new StringLiteral("")), identifier(alias)); }
private static Expression oneIfNull(Optional<Symbol> symbol) { if (symbol.isPresent()) { return new CoalesceExpression(new QualifiedNameReference(symbol.get().toQualifiedName()), new LongLiteral("1")); } else { return new LongLiteral("1"); } }
@Test(timeOut = 10_000) public void testPossibleExponentialOptimizationTime() { Expression expression = new LongLiteral("1"); IdentityHashMap<Expression, Type> types = new IdentityHashMap<>(); types.put(expression, BIGINT); for (int i = 0; i < 100; i++) { expression = new CoalesceExpression(expression); types.put(expression, BIGINT); } SqlToRowExpressionTranslator.translate(expression, SCALAR, types, FUNCTION_REGISTRY, TYPE_MANAGER, TEST_SESSION, true); } }
check(!distinct, "DISTINCT not valid for 'coalesce' function", context); return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
@Test public void testCoalesce() { assertInvalidExpression("coalesce()", "The 'coalesce' function must have at least two arguments"); assertInvalidExpression("coalesce(5)", "The 'coalesce' function must have at least two arguments"); assertInvalidExpression("coalesce(1, 2) filter (where true)", "FILTER not valid for 'coalesce' function"); assertInvalidExpression("coalesce(1, 2) OVER ()", "OVER clause not valid for 'coalesce' function"); assertExpression("coalesce(13, 42)", new CoalesceExpression(new LongLiteral("13"), new LongLiteral("42"))); assertExpression("coalesce(6, 7, 8)", new CoalesceExpression(new LongLiteral("6"), new LongLiteral("7"), new LongLiteral("8"))); assertExpression("coalesce(13, null)", new CoalesceExpression(new LongLiteral("13"), new NullLiteral())); assertExpression("coalesce(null, 13)", new CoalesceExpression(new NullLiteral(), new LongLiteral("13"))); assertExpression("coalesce(null, null)", new CoalesceExpression(new NullLiteral(), new NullLiteral())); }