public static ArithmeticUnaryExpression positive(NodeLocation location, Expression value) { return new ArithmeticUnaryExpression(Optional.of(location), Sign.PLUS, value); }
@Override protected String visitArithmeticUnary(ArithmeticUnaryExpression node, Void context) { String value = process(node.getValue(), context); switch (node.getSign()) { case MINUS: // this is to avoid turning a sequence of "-" into a comment (i.e., "-- comment") String separator = value.startsWith("-") ? " " : ""; return "-" + separator + value; case PLUS: return "+" + value; default: throw new UnsupportedOperationException("Unsupported sign: " + node.getSign()); } }
@Override protected Object visitArithmeticUnary(ArithmeticUnaryExpression node, Object context) { Object value = process(node.getValue(), context); if (value == null) { return null; } if (value instanceof Expression) { return new ArithmeticUnaryExpression(node.getSign(), toExpression(value, type(node.getValue()))); } switch (node.getSign()) { case PLUS: return value; case MINUS: Signature operatorSignature = metadata.getFunctionRegistry().resolveOperator(OperatorType.NEGATION, types(node.getValue())); MethodHandle handle = metadata.getFunctionRegistry().getScalarFunctionImplementation(operatorSignature).getMethodHandle(); if (handle.type().parameterCount() > 0 && handle.type().parameterType(0) == ConnectorSession.class) { handle = handle.bindTo(session); } try { return handle.invokeWithArguments(value); } catch (Throwable throwable) { throwIfInstanceOf(throwable, RuntimeException.class); throwIfInstanceOf(throwable, Error.class); throw new RuntimeException(throwable.getMessage(), throwable); } } throw new UnsupportedOperationException("Unsupported unary operator: " + node.getSign()); }
@Override public Node visitArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext context) { Expression child = (Expression) visit(context.valueExpression()); switch (context.operator.getType()) { case SqlBaseLexer.MINUS: return ArithmeticUnaryExpression.negative(getLocation(context), child); case SqlBaseLexer.PLUS: return ArithmeticUnaryExpression.positive(getLocation(context), child); default: throw new UnsupportedOperationException("Unsupported sign: " + context.operator.getText()); } }
@Override protected R visitArithmeticUnary(ArithmeticUnaryExpression node, C context) { return process(node.getValue(), context); }
@Override protected Object visitArithmeticUnary(ArithmeticUnaryExpression node, Object context) { Object value = process(node.getValue(), context); if (value == null) { return null; } if (value instanceof Expression) { return new ArithmeticUnaryExpression(node.getSign(), toExpression(value, type(node.getValue()))); } switch (node.getSign()) { case PLUS: return value; case MINUS: Signature operatorSignature = metadata.getFunctionRegistry().resolveOperator(OperatorType.NEGATION, types(node.getValue())); MethodHandle handle = metadata.getFunctionRegistry().getScalarFunctionImplementation(operatorSignature).getMethodHandle(); if (handle.type().parameterCount() > 0 && handle.type().parameterType(0) == ConnectorSession.class) { handle = handle.bindTo(session); } try { return handle.invokeWithArguments(value); } catch (Throwable throwable) { throwIfInstanceOf(throwable, RuntimeException.class); throwIfInstanceOf(throwable, Error.class); throw new RuntimeException(throwable.getMessage(), throwable); } } throw new UnsupportedOperationException("Unsupported unary operator: " + node.getSign()); }
@Override public Node visitArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext context) { Expression child = (Expression) visit(context.valueExpression()); switch (context.operator.getType()) { case SqlBaseLexer.MINUS: return ArithmeticUnaryExpression.negative(getLocation(context), child); case SqlBaseLexer.PLUS: return ArithmeticUnaryExpression.positive(getLocation(context), child); default: throw new UnsupportedOperationException("Unsupported sign: " + context.operator.getText()); } }
@Override protected Boolean visitArithmeticUnary(ArithmeticUnaryExpression node, Void context) { return process(node.getValue(), context); }
@Override protected String visitArithmeticUnary(ArithmeticUnaryExpression node, Void context) { String value = process(node.getValue(), context); switch (node.getSign()) { case MINUS: // this is to avoid turning a sequence of "-" into a comment (i.e., "-- comment") String separator = value.startsWith("-") ? " " : ""; return "-" + separator + value; case PLUS: return "+" + value; default: throw new UnsupportedOperationException("Unsupported sign: " + node.getSign()); } }
public static ArithmeticUnaryExpression positive(NodeLocation location, Expression value) { return new ArithmeticUnaryExpression(Optional.of(location), Sign.PLUS, value); }
@Test public void testArithmeticUnary() { assertExpression("9", new LongLiteral("9")); assertExpression("+9", positive(new LongLiteral("9"))); assertExpression("+ 9", positive(new LongLiteral("9"))); assertExpression("++9", positive(positive(new LongLiteral("9")))); assertExpression("+ +9", positive(positive(new LongLiteral("9")))); assertExpression("+ + 9", positive(positive(new LongLiteral("9")))); assertExpression("+++9", positive(positive(positive(new LongLiteral("9"))))); assertExpression("+ + +9", positive(positive(positive(new LongLiteral("9"))))); assertExpression("+ + + 9", positive(positive(positive(new LongLiteral("9"))))); assertExpression("-9", negative(new LongLiteral("9"))); assertExpression("- 9", negative(new LongLiteral("9"))); assertExpression("- + 9", negative(positive(new LongLiteral("9")))); assertExpression("-+9", negative(positive(new LongLiteral("9")))); assertExpression("+ - + 9", positive(negative(positive(new LongLiteral("9"))))); assertExpression("+-+9", positive(negative(positive(new LongLiteral("9"))))); assertExpression("- -9", negative(negative(new LongLiteral("9")))); assertExpression("- - 9", negative(negative(new LongLiteral("9")))); assertExpression("- + - + 9", negative(positive(negative(positive(new LongLiteral("9")))))); assertExpression("-+-+9", negative(positive(negative(positive(new LongLiteral("9")))))); assertExpression("+ - + - + 9", positive(negative(positive(negative(positive(new LongLiteral("9"))))))); assertExpression("+-+-+9", positive(negative(positive(negative(positive(new LongLiteral("9"))))))); assertExpression("- - -9", negative(negative(negative(new LongLiteral("9"))))); assertExpression("- - - 9", negative(negative(negative(new LongLiteral("9"))))); }
negative(new LongLiteral("1")), new LongLiteral("2")));
@Override protected Boolean visitArithmeticUnary(ArithmeticUnaryExpression node, Void context) { return process(node.getValue(), context); }
@Override protected Type visitArithmeticUnary(ArithmeticUnaryExpression node, StackableAstVisitorContext<Context> context) { switch (node.getSign()) { case PLUS: Type type = process(node.getValue(), context); if (!type.equals(DOUBLE) && !type.equals(REAL) && !type.equals(BIGINT) && !type.equals(INTEGER) && !type.equals(SMALLINT) && !type.equals(TINYINT)) { // TODO: figure out a type-agnostic way of dealing with this. Maybe add a special unary operator // that types can chose to implement, or piggyback on the existence of the negation operator throw new SemanticException(TYPE_MISMATCH, node, "Unary '+' operator cannot by applied to %s type", type); } return setExpressionType(node, type); case MINUS: return getOperator(context, node, OperatorType.NEGATION, node.getValue()); } throw new UnsupportedOperationException("Unsupported unary operator: " + node.getSign()); }
public static ArithmeticUnaryExpression positive(Expression value) { return new ArithmeticUnaryExpression(Optional.empty(), Sign.PLUS, value); }
@Override protected R visitArithmeticUnary(ArithmeticUnaryExpression node, C context) { return process(node.getValue(), context); }
@Override protected Type visitArithmeticUnary(ArithmeticUnaryExpression node, StackableAstVisitorContext<Context> context) { switch (node.getSign()) { case PLUS: Type type = process(node.getValue(), context); if (!type.equals(DOUBLE) && !type.equals(REAL) && !type.equals(BIGINT) && !type.equals(INTEGER) && !type.equals(SMALLINT) && !type.equals(TINYINT)) { // TODO: figure out a type-agnostic way of dealing with this. Maybe add a special unary operator // that types can chose to implement, or piggyback on the existence of the negation operator throw new SemanticException(TYPE_MISMATCH, node, "Unary '+' operator cannot by applied to %s type", type); } return setExpressionType(node, type); case MINUS: return getOperator(context, node, OperatorType.NEGATION, node.getValue()); } throw new UnsupportedOperationException("Unsupported unary operator: " + node.getSign()); }
public static ArithmeticUnaryExpression positive(Expression value) { return new ArithmeticUnaryExpression(Optional.empty(), Sign.PLUS, value); }