Search in sources :

Example 1 with Parenthesis

use of net.sf.jsqlparser.expression.Parenthesis in project herddb by diennea.

the class SQLExpressionCompiler method compileExpression.

// this method never returns NULL
public static CompiledSQLExpression compileExpression(String validatedTableAlias, Expression exp) {
    if (exp instanceof BinaryExpression) {
        CompiledSQLExpression compiled = compileSpecialBinaryExpression(validatedTableAlias, exp);
        if (compiled != null) {
            return compiled;
        }
    }
    if (exp instanceof net.sf.jsqlparser.schema.Column) {
        return compileColumnExpression(validatedTableAlias, exp);
    } else if (exp instanceof StringValue) {
        return new ConstantExpression(RawString.of(((StringValue) exp).getValue()));
    } else if (exp instanceof LongValue) {
        try {
            return new ConstantExpression(((LongValue) exp).getValue());
        } catch (NumberFormatException largeNumber) {
            return new ConstantExpression(Double.valueOf(((LongValue) exp).getStringValue()));
        }
    } else if (exp instanceof DoubleValue) {
        return new ConstantExpression(((DoubleValue) exp).getValue());
    } else if (exp instanceof TimestampValue) {
        return new ConstantExpression(((TimestampValue) exp).getValue());
    } else if (exp instanceof NullValue) {
        return new ConstantExpression(null);
    } else if (exp instanceof TimeKeyExpression) {
        TimeKeyExpression ext = (TimeKeyExpression) exp;
        if (CURRENT_TIMESTAMP.equalsIgnoreCase(ext.getStringValue())) {
            return new ConstantExpression(new java.sql.Timestamp(System.currentTimeMillis()));
        } else {
            throw new StatementExecutionException("unhandled expression " + exp);
        }
    } else if (exp instanceof JdbcParameter) {
        int index = ((JdbcParameter) exp).getIndex() - 1;
        return new JdbcParameterExpression(index);
    } else if (exp instanceof AndExpression) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAndExpression(a, b, c));
    } else if (exp instanceof OrExpression) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledOrExpression(a, b, c));
    } else if (exp instanceof Function) {
        return CompiledFunction.create((Function) exp, validatedTableAlias);
    } else if (exp instanceof Addition) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAddExpression(a, b, c));
    } else if (exp instanceof Subtraction) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledSubtractExpression(a, b, c));
    } else if (exp instanceof Multiplication) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMultiplyExpression(a, b, c));
    } else if (exp instanceof Division) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledDivideExpression(a, b, c));
    } else if (exp instanceof Parenthesis) {
        Parenthesis p = (Parenthesis) exp;
        CompiledSQLExpression inner = compileExpression(validatedTableAlias, p.getExpression());
        return new CompiledParenthesisExpression(p.isNot(), inner);
    } else if (exp instanceof EqualsTo) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledEqualsExpression(a, b, c));
    } else if (exp instanceof NotEqualsTo) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledNotEqualsExpression(a, b, c));
    } else if (exp instanceof MinorThan) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenExpression(a, b, c));
    } else if (exp instanceof MinorThanEquals) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenEqualsExpression(a, b, c));
    } else if (exp instanceof GreaterThan) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenExpression(a, b, c));
    } else if (exp instanceof GreaterThanEquals) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenEqualsExpression(a, b, c));
    } else if (exp instanceof LikeExpression) {
        return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledLikeExpression(a, b, c));
    } else if (exp instanceof Between) {
        return CompiledBetweenExpression.create(validatedTableAlias, (Between) exp);
    } else if (exp instanceof SignedExpression) {
        SignedExpression s = (SignedExpression) exp;
        CompiledSQLExpression inner = compileExpression(validatedTableAlias, s.getExpression());
        return new CompiledSignedExpression(s.getSign(), inner);
    } else if (exp instanceof InExpression) {
        InExpression in = (InExpression) exp;
        return CompiledInExpression.create(in, validatedTableAlias);
    } else if (exp instanceof IsNullExpression) {
        IsNullExpression i = (IsNullExpression) exp;
        CompiledSQLExpression left = compileExpression(validatedTableAlias, i.getLeftExpression());
        return new CompiledIsNullExpression(i.isNot(), left);
    } else if (exp instanceof CaseExpression) {
        return CompiledCaseExpression.create(validatedTableAlias, (CaseExpression) exp);
    }
    throw new StatementExecutionException("unsupported operand " + exp.getClass() + ", expression is " + exp);
}
Also used : Arrays(java.util.Arrays) Multiplication(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication) BigDecimal(java.math.BigDecimal) Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) RawString(herddb.utils.RawString) StatementExecutionException(herddb.model.StatementExecutionException) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) RexLiteral(org.apache.calcite.rex.RexLiteral) NullValue(net.sf.jsqlparser.expression.NullValue) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) Expression(net.sf.jsqlparser.expression.Expression) MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) SqlCastFunction(org.apache.calcite.sql.fun.SqlCastFunction) RexInputRef(org.apache.calcite.rex.RexInputRef) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) Subtraction(net.sf.jsqlparser.expression.operators.arithmetic.Subtraction) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) List(java.util.List) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) BinaryExpressionBuilder(herddb.sql.expressions.CompiledSQLExpression.BinaryExpressionBuilder) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) CalcitePlanner(herddb.sql.CalcitePlanner) RexCall(org.apache.calcite.rex.RexCall) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) BuiltinFunctions(herddb.sql.functions.BuiltinFunctions) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) Division(net.sf.jsqlparser.expression.operators.arithmetic.Division) CURRENT_TIMESTAMP(herddb.sql.functions.BuiltinFunctions.CURRENT_TIMESTAMP) MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) ArrayList(java.util.ArrayList) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Function(net.sf.jsqlparser.expression.Function) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) LongValue(net.sf.jsqlparser.expression.LongValue) AbstractMap(java.util.AbstractMap) TimestampValue(net.sf.jsqlparser.expression.TimestampValue) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) Between(net.sf.jsqlparser.expression.operators.relational.Between) Collections(java.util.Collections) StringValue(net.sf.jsqlparser.expression.StringValue) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) Multiplication(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) StatementExecutionException(herddb.model.StatementExecutionException) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) NullValue(net.sf.jsqlparser.expression.NullValue) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) StringValue(net.sf.jsqlparser.expression.StringValue) Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) Between(net.sf.jsqlparser.expression.operators.relational.Between) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) LongValue(net.sf.jsqlparser.expression.LongValue) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) SqlCastFunction(org.apache.calcite.sql.fun.SqlCastFunction) Function(net.sf.jsqlparser.expression.Function) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) TimestampValue(net.sf.jsqlparser.expression.TimestampValue) Division(net.sf.jsqlparser.expression.operators.arithmetic.Division) MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) Subtraction(net.sf.jsqlparser.expression.operators.arithmetic.Subtraction) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) SignedExpression(net.sf.jsqlparser.expression.SignedExpression)

Example 2 with Parenthesis

use of net.sf.jsqlparser.expression.Parenthesis in project JSqlParser by JSQLParser.

the class CCJSqlParserUtilTest method testParseExpression2.

@Test
public void testParseExpression2() throws Exception {
    Expression result = CCJSqlParserUtil.parseExpression("2*(a+6.0)");
    assertEquals("2 * (a + 6.0)", result.toString());
    assertTrue(result instanceof Multiplication);
    Multiplication mult = (Multiplication) result;
    assertTrue(mult.getLeftExpression() instanceof LongValue);
    assertTrue(mult.getRightExpression() instanceof Parenthesis);
}
Also used : Multiplication(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) Expression(net.sf.jsqlparser.expression.Expression) LongValue(net.sf.jsqlparser.expression.LongValue) Test(org.junit.Test)

Example 3 with Parenthesis

use of net.sf.jsqlparser.expression.Parenthesis in project JSqlParser by JSQLParser.

the class CloneHelper method changeBack.

/**
 * This helper method is used to change the multiple expression into
 * the binary form, respectively and return the root of the expression tree.
 * @param isMultiOr variable tells whether the expression is or.
 * @param exp the expression that needs to be converted.
 * @return the root of the expression tree.
 */
public Expression changeBack(Boolean isMultiOr, Expression exp) {
    if (!(exp instanceof MultipleExpression)) {
        return exp;
    }
    MultipleExpression changed = (MultipleExpression) exp;
    Expression result = changed.getChild(0);
    for (int i = 1; i < changed.size(); i++) {
        Expression left = result;
        Expression right = changed.getChild(i);
        if (isMultiOr) {
            result = new OrExpression(left, right);
        } else {
            result = new AndExpression(left, right);
        }
    }
    if (isMultiOr) {
        return new Parenthesis(result);
    }
    return result;
}
Also used : Parenthesis(net.sf.jsqlparser.expression.Parenthesis) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) Expression(net.sf.jsqlparser.expression.Expression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression)

Example 4 with Parenthesis

use of net.sf.jsqlparser.expression.Parenthesis in project JSqlParser by JSQLParser.

the class CloneHelper method modify.

/**
 * This method is used for changing the logical structure of the tree.
 * The main method is to convert and operator and or operator to let
 * them have multiple children (reflected in MultipleExpression.java
 * from the conditional package). Note if the value from the conditional
 * operator has a not attached to it we need to append an not operator
 * ahead of it since the not operator needed to be pushed down during
 * the second step. Also, I will leave out all the parenthesis expression
 * which is connected to the conditional operator.
 * @param express the expression that will be modified
 * @return the modified expression.
 */
public Expression modify(Expression express) {
    if (express instanceof NotExpression) {
        return new NotExpression(modify(((NotExpression) express).getExpression()));
    }
    if (express instanceof Parenthesis) {
        Parenthesis parenthesis = (Parenthesis) express;
        Expression result = modify(parenthesis.getExpression());
        if (parenthesis.isNot()) {
            return new NotExpression(result);
        }
        return result;
    }
    if (express instanceof AndExpression) {
        AndExpression and = (AndExpression) express;
        List<Expression> list = new ArrayList<Expression>();
        list.add(modify(and.getLeftExpression()));
        list.add(modify(and.getRightExpression()));
        MultiAndExpression result = new MultiAndExpression(list);
        if (and.isNot()) {
            return new NotExpression(result);
        }
        return result;
    }
    if (express instanceof OrExpression) {
        OrExpression or = (OrExpression) express;
        List<Expression> list = new ArrayList<Expression>();
        list.add(modify(or.getLeftExpression()));
        list.add(modify(or.getRightExpression()));
        MultiOrExpression result = new MultiOrExpression(list);
        if (or.isNot()) {
            return new NotExpression(result);
        }
        return result;
    }
    if (express instanceof BinaryExpression) {
        BinaryExpression binary = (BinaryExpression) express;
        if (binary.isNot()) {
            binary.removeNot();
            return new NotExpression(modify(binary));
        }
    }
    /* at this stage, there is no need to modify, just simply return. */
    return express;
}
Also used : Parenthesis(net.sf.jsqlparser.expression.Parenthesis) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) NotExpression(net.sf.jsqlparser.expression.NotExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression)

Aggregations

Expression (net.sf.jsqlparser.expression.Expression)4 Parenthesis (net.sf.jsqlparser.expression.Parenthesis)4 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)3 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)3 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)3 ArrayList (java.util.ArrayList)2 LongValue (net.sf.jsqlparser.expression.LongValue)2 NotExpression (net.sf.jsqlparser.expression.NotExpression)2 Multiplication (net.sf.jsqlparser.expression.operators.arithmetic.Multiplication)2 StatementExecutionException (herddb.model.StatementExecutionException)1 CalcitePlanner (herddb.sql.CalcitePlanner)1 BinaryExpressionBuilder (herddb.sql.expressions.CompiledSQLExpression.BinaryExpressionBuilder)1 BuiltinFunctions (herddb.sql.functions.BuiltinFunctions)1 CURRENT_TIMESTAMP (herddb.sql.functions.BuiltinFunctions.CURRENT_TIMESTAMP)1 RawString (herddb.utils.RawString)1 BigDecimal (java.math.BigDecimal)1 AbstractMap (java.util.AbstractMap)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1