private static Expression multiply(Expression expression1, Expression expression2) { return new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, expression1, expression2); }
private static Expression add(Expression expression1, Expression expression2) { return new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, expression1, expression2); }
public static Expression rewriteGroupingOperation(GroupingOperation expression, List<Set<Integer>> groupingSets, Map<NodeRef<Expression>, FieldId> columnReferenceFields, Optional<Symbol> groupIdSymbol) { requireNonNull(groupIdSymbol, "groupIdSymbol is null"); // No GroupIdNode and a GROUPING() operation imply a single grouping, which // means that any columns specified as arguments to GROUPING() will be included // in the group and none of them will be aggregated over. Hence, re-write the // GroupingOperation to a constant literal of 0. // See SQL:2011:4.16.2 and SQL:2011:6.9.10. if (groupingSets.size() == 1) { return new LongLiteral("0"); } else { checkState(groupIdSymbol.isPresent(), "groupId symbol is missing"); RelationId relationId = columnReferenceFields.get(NodeRef.of(expression.getGroupingColumns().get(0))).getRelationId(); List<Integer> columns = expression.getGroupingColumns().stream() .map(NodeRef::of) .peek(groupingColumn -> checkState(columnReferenceFields.containsKey(groupingColumn), "the grouping column is not in the columnReferencesField map")) .map(columnReferenceFields::get) .map(fieldId -> translateFieldToInteger(fieldId, relationId)) .collect(toImmutableList()); List<Expression> groupingResults = groupingSets.stream() .map(groupingSet -> String.valueOf(calculateGrouping(groupingSet, columns))) .map(LongLiteral::new) .collect(toImmutableList()); // It is necessary to add a 1 to the groupId because the underlying array is indexed starting at 1 return new SubscriptExpression( new ArrayConstructor(groupingResults), new ArithmeticBinaryExpression(ADD, groupIdSymbol.get().toSymbolReference(), new GenericLiteral("BIGINT", "1"))); } }
@Override public Node visitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext context) { return new ArithmeticBinaryExpression( getLocation(context.operator), getArithmeticBinaryOperator(context.operator), (Expression) visit(context.left), (Expression) visit(context.right)); }
@Test public void testTryExpressionDesugaringRewriter() { // 1 + try(2) Expression before = new ArithmeticBinaryExpression( ADD, new DecimalLiteral("1"), new TryExpression(new DecimalLiteral("2"))); // 1 + try_function(() -> 2) Expression after = new ArithmeticBinaryExpression( ADD, new DecimalLiteral("1"), new FunctionCall( QualifiedName.of("$internal$try"), ImmutableList.of(new LambdaExpression(ImmutableList.of(), new DecimalLiteral("2"))))); assertEquals(DesugarTryExpressionRewriter.rewrite(before), after); } }
@Override protected Object visitArithmeticBinary(ArithmeticBinaryExpression node, Object context) { Object left = process(node.getLeft(), context); if (left == null) { return null; } Object right = process(node.getRight(), context); if (right == null) { return null; } if (hasUnresolvedValue(left, right)) { return new ArithmeticBinaryExpression(node.getOperator(), toExpression(left, type(node.getLeft())), toExpression(right, type(node.getRight()))); } return invokeOperator(OperatorType.valueOf(node.getOperator().name()), types(node.getLeft(), node.getRight()), ImmutableList.of(left, right)); }
ComparisonExpression abcFilter = new ComparisonExpression( LESS_THAN, new ArithmeticBinaryExpression(ADD, a1.toSymbolReference(), c1.toSymbolReference()), b1.toSymbolReference()); JoinNode joinNode = p.join(
new LongLiteral("2"))); assertExpression("-1 + 2", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, negative(new LongLiteral("1")), new LongLiteral("2"))); assertExpression("1 - 2 - 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3"))); assertExpression("1 / 2 / 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.DIVIDE, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.DIVIDE, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3"))); assertExpression("1 + 2 * 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, new LongLiteral("1"), new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new LongLiteral("2"), new LongLiteral("3"))));
Symbol cTimes3 = p.symbol("c_times_3"); return p.project( Assignments.of(cTimes3, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, c.toSymbolReference(), new LongLiteral("3"))), p.union( ImmutableListMultimap.<Symbol, Symbol>builder()
return p.project( Assignments.builder() .put(aTimes5, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new SymbolReference("a"), new LongLiteral("5"))) .put(bTimes5, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new SymbolReference("b"), new LongLiteral("5"))) .put(hTimes5, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new SymbolReference("h"), new LongLiteral("5"))) .build(), p.exchange(e -> e
return p.project( Assignments.builder() .put(aTimes5, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new SymbolReference("a"), new LongLiteral("5"))) .put(bTimes5, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new SymbolReference("b"), new LongLiteral("5"))) .put(hTimes5, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new SymbolReference("h"), new LongLiteral("5"))) .build(), p.exchange(e -> e
private static Expression add(Expression expression1, Expression expression2) { return new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Type.ADD, expression1, expression2); }
private static Expression multiply(Expression expression1, Expression expression2) { return new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Type.MULTIPLY, expression1, expression2); }
@Override public Node visitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext context) { return new ArithmeticBinaryExpression( getLocation(context.operator), getArithmeticBinaryOperator(context.operator), (Expression) visit(context.left), (Expression) visit(context.right)); }
@Override public Node visitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext context) { return new ArithmeticBinaryExpression( getLocation(context.operator), getArithmeticBinaryOperator(context.operator), (Expression) visit(context.left), (Expression) visit(context.right)); }
@Override protected Object visitArithmeticBinary(ArithmeticBinaryExpression node, Object context) { Object left = process(node.getLeft(), context); if (left == null) { return null; } Object right = process(node.getRight(), context); if (right == null) { return null; } if (hasUnresolvedValue(left, right)) { return new ArithmeticBinaryExpression(node.getType(), toExpression(left, expressionTypes.get(node.getLeft())), toExpression(right, expressionTypes.get(node.getRight()))); } return invokeOperator(OperatorType.valueOf(node.getType().name()), types(node.getLeft(), node.getRight()), ImmutableList.of(left, right)); }
Expression expression = new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Type.MULTIPLY, oneIfNull(leftPlanBuilder.getSampleWeight()), oneIfNull(rightPlanBuilder.getSampleWeight()));
new LongLiteral("2"))); assertExpression("-1 + 2", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, negative(new LongLiteral("1")), new LongLiteral("2"))); assertExpression("1 - 2 - 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3"))); assertExpression("1 / 2 / 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.DIVIDE, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.DIVIDE, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3"))); assertExpression("1 + 2 * 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, new LongLiteral("1"), new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new LongLiteral("2"), new LongLiteral("3"))));
new LongLiteral("2"))); assertExpression("-1 + 2", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Type.ADD, negative(new LongLiteral("1")), new LongLiteral("2"))); assertExpression("1 - 2 - 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Type.SUBTRACT, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Type.SUBTRACT, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3"))); assertExpression("1 / 2 / 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Type.DIVIDE, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Type.DIVIDE, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3"))); assertExpression("1 + 2 * 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Type.ADD, new LongLiteral("1"), new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Type.MULTIPLY, new LongLiteral("2"), new LongLiteral("3"))));