@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)); }
private static ArithmeticBinaryExpression.Type getArithmeticBinaryOperator(Token operator) { switch (operator.getType()) { case SqlBaseLexer.PLUS: return ArithmeticBinaryExpression.Type.ADD; case SqlBaseLexer.MINUS: return ArithmeticBinaryExpression.Type.SUBTRACT; case SqlBaseLexer.ASTERISK: return ArithmeticBinaryExpression.Type.MULTIPLY; case SqlBaseLexer.SLASH: return ArithmeticBinaryExpression.Type.DIVIDE; case SqlBaseLexer.PERCENT: return ArithmeticBinaryExpression.Type.MODULUS; } throw new UnsupportedOperationException("Unsupported operator: " + operator.getText()); }
@Override protected RowExpression visitArithmeticBinary(ArithmeticBinaryExpression node, Void context) { RowExpression left = process(node.getLeft(), context); RowExpression right = process(node.getRight(), context); return call( arithmeticExpressionSignature(node.getType(), types.get(node), left.getType(), right.getType()), types.get(node), left, right); }
@Test public void testArithmeticExpressionWithNulls() { for (ArithmeticBinaryExpression.Type type : ArithmeticBinaryExpression.Type.values()) { assertProjection("NULL " + type.getValue() + " NULL", null); assertProjection("42 " + type.getValue() + " NULL", null); assertProjection("NULL " + type.getValue() + " 42", null); assertProjection("11.1 " + type.getValue() + " NULL", null); assertProjection("NULL " + type.getValue() + " 11.1", null); } }
@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 String visitArithmeticBinary(ArithmeticBinaryExpression node, StackableAstVisitorContext<Integer> indent) { return formatBinaryExpression(node.getType().getValue(), node.getLeft(), node.getRight(), indent); }
public ArithmeticBinaryExpression.Type getType(){ return type; }
@Override protected ICalculation visitArithmeticBinary(ArithmeticBinaryExpression node, QueryState state){ ICalculation left = visitExpression(node.getLeft(), state); ICalculation right = visitExpression(node.getRight(), state); return new SimpleCalculation(left, right, node.getType()); }
public static Signature arithmeticExpressionSignature(ArithmeticBinaryExpression.Type expressionType, Type returnType, Type leftType, Type rightType) { return internalOperator(expressionType.name(), returnType.getTypeSignature(), leftType.getTypeSignature(), rightType.getTypeSignature()); }
@Override protected Boolean visitArithmeticBinary(ArithmeticBinaryExpression node, Void context) { return process(node.getLeft(), context) && process(node.getRight(), context); }
@Override protected Type visitArithmeticBinary(ArithmeticBinaryExpression node, StackableAstVisitorContext<AnalysisContext> context) { return getOperator(context, node, OperatorType.valueOf(node.getType().name()), node.getLeft(), node.getRight()); }
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 protected String visitArithmeticBinary(ArithmeticBinaryExpression node, Boolean unmangleNames) { return formatBinaryExpression(node.getType().getValue(), node.getLeft(), node.getRight(), unmangleNames); }
@Test public void testPrecedenceAndAssociativity() throws Exception { assertExpression("1 AND 2 OR 3", new LogicalBinaryExpression(LogicalBinaryExpression.Type.OR, new LogicalBinaryExpression(LogicalBinaryExpression.Type.AND, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3"))); assertExpression("1 OR 2 AND 3", new LogicalBinaryExpression(LogicalBinaryExpression.Type.OR, new LongLiteral("1"), new LogicalBinaryExpression(LogicalBinaryExpression.Type.AND, new LongLiteral("2"), new LongLiteral("3")))); assertExpression("NOT 1 AND 2", new LogicalBinaryExpression(LogicalBinaryExpression.Type.AND, new NotExpression(new LongLiteral("1")), new LongLiteral("2"))); assertExpression("NOT 1 OR 2", new LogicalBinaryExpression(LogicalBinaryExpression.Type.OR, new NotExpression(new LongLiteral("1")), 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")))); }