Search in sources :

Example 1 with EqualsTo

use of net.sf.jsqlparser.expression.operators.relational.EqualsTo in project herddb by diennea.

the class SQLExpressionCompiler method compileSpecialBinaryExpression.

private static CompiledSQLExpression compileSpecialBinaryExpression(String validatedTableAlias, Expression exp) {
    BinaryExpression be = (BinaryExpression) exp;
    // MOST frequent expressions "TABLE.COLUMNNAME OPERATOR ?", we can hardcode the access to the column and to the JDBC parameter
    if (be.getLeftExpression() instanceof net.sf.jsqlparser.schema.Column) {
        net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) be.getLeftExpression();
        if (validatedTableAlias != null) {
            if (c.getTable() != null && c.getTable().getName() != null && !c.getTable().getName().equals(validatedTableAlias)) {
                return null;
            }
        }
        String columnName = c.getColumnName();
        switch(columnName) {
            case BuiltinFunctions.BOOLEAN_TRUE:
                return null;
            case BuiltinFunctions.BOOLEAN_FALSE:
                return null;
            default:
                // OK !
                break;
        }
        if (be.getRightExpression() instanceof JdbcParameter) {
            JdbcParameter jdbcParam = (JdbcParameter) be.getRightExpression();
            int jdbcIndex = jdbcParam.getIndex() - 1;
            if (be instanceof EqualsTo) {
                return new ColumnEqualsJdbcParameter(be.isNot(), columnName, jdbcIndex);
            } else if (be instanceof NotEqualsTo) {
                return new ColumnNotEqualsJdbcParameter(be.isNot(), columnName, jdbcIndex);
            } else if (be instanceof GreaterThanEquals) {
                return new ColumnGreaterThanEqualsJdbcParameter(be.isNot(), columnName, jdbcIndex);
            } else if (be instanceof GreaterThan) {
                return new ColumnGreaterThanJdbcParameter(be.isNot(), columnName, jdbcIndex);
            } else if (be instanceof MinorThan) {
                return new ColumnMinorThanJdbcParameter(be.isNot(), columnName, jdbcIndex);
            } else if (be instanceof MinorThanEquals) {
                return new ColumnMinorThanEqualsJdbcParameter(be.isNot(), columnName, jdbcIndex);
            }
        }
    // TODO handle "TABLE.COLUMNNAME OPERATOR CONSTANT"
    }
    return null;
}
Also used : MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) RawString(herddb.utils.RawString) GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo)

Example 2 with EqualsTo

use of net.sf.jsqlparser.expression.operators.relational.EqualsTo 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 3 with EqualsTo

use of net.sf.jsqlparser.expression.operators.relational.EqualsTo in project herddb by diennea.

the class SQLParserExpressionCompiler method compileBinaryExpression.

private static CompiledSQLExpression compileBinaryExpression(BinaryExpression expression, OpSchema tableSchema) {
    CompiledSQLExpression left = compileExpression(expression.getLeftExpression(), tableSchema);
    CompiledSQLExpression right = compileExpression(expression.getRightExpression(), tableSchema);
    if (expression instanceof EqualsTo) {
        EqualsTo eq = (EqualsTo) expression;
        checkSupported(eq.getOldOracleJoinSyntax() == EqualsTo.NO_ORACLE_JOIN);
        checkSupported(eq.getOraclePriorPosition() == EqualsTo.NO_ORACLE_PRIOR);
        return new CompiledEqualsExpression(left, right);
    } else if (expression instanceof AndExpression) {
        return new CompiledAndExpression(left, right);
    } else if (expression instanceof OrExpression) {
        return new CompiledOrExpression(left, right);
    } else if (expression instanceof GreaterThan) {
        return new CompiledGreaterThanExpression(left, right);
    } else if (expression instanceof GreaterThanEquals) {
        return new CompiledGreaterThanEqualsExpression(left, right);
    } else if (expression instanceof MinorThan) {
        return new CompiledMinorThanExpression(left, right);
    } else if (expression instanceof MinorThanEquals) {
        return new CompiledMinorThanEqualsExpression(left, right);
    } else if (expression instanceof Addition) {
        return new CompiledAddExpression(left, right);
    } else if (expression instanceof Division) {
        return new CompiledDivideExpression(left, right);
    } else if (expression instanceof Multiplication) {
        return new CompiledMultiplyExpression(left, right);
    } else if (expression instanceof LikeExpression) {
        LikeExpression eq = (LikeExpression) expression;
        if (eq.isCaseInsensitive()) {
            // this is not supported by Calcite, do not support it with jSQLParser
            throw new StatementExecutionException("ILIKE is not supported");
        }
        CompiledSQLExpression res;
        if (eq.getEscape() != null) {
            CompiledSQLExpression escape = new ConstantExpression(eq.getEscape(), ColumnTypes.NOTNULL_STRING);
            res = new CompiledLikeExpression(left, right, escape);
        } else {
            res = new CompiledLikeExpression(left, right);
        }
        if (eq.isNot()) {
            res = new CompiledNotExpression(res);
        }
        return res;
    } else if (expression instanceof Modulo) {
        return new CompiledModuloExpression(left, right);
    } else if (expression instanceof Subtraction) {
        return new CompiledSubtractExpression(left, right);
    } else if (expression instanceof NotEqualsTo) {
        NotEqualsTo eq = (NotEqualsTo) expression;
        checkSupported(eq.getOldOracleJoinSyntax() == EqualsTo.NO_ORACLE_JOIN);
        checkSupported(eq.getOraclePriorPosition() == EqualsTo.NO_ORACLE_PRIOR);
        return new CompiledNotEqualsExpression(left, right);
    } else {
        throw new StatementExecutionException("not implemented expression type " + expression.getClass() + ": " + expression);
    }
}
Also used : MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) Multiplication(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) StatementExecutionException(herddb.model.StatementExecutionException) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) Division(net.sf.jsqlparser.expression.operators.arithmetic.Division) MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) Modulo(net.sf.jsqlparser.expression.operators.arithmetic.Modulo) 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) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo)

Example 4 with EqualsTo

use of net.sf.jsqlparser.expression.operators.relational.EqualsTo in project JSqlParser by JSQLParser.

the class SelectUtilsTest method testAddJoin.

@Test
public void testAddJoin() throws JSQLParserException {
    Select select = (Select) CCJSqlParserUtil.parse("select a from mytable");
    final EqualsTo equalsTo = new EqualsTo();
    equalsTo.setLeftExpression(new Column("a"));
    equalsTo.setRightExpression(new Column("b"));
    Join addJoin = SelectUtils.addJoin(select, new Table("mytable2"), equalsTo);
    addJoin.setLeft(true);
    assertEquals("SELECT a FROM mytable LEFT JOIN mytable2 ON a = b", select.toString());
}
Also used : Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) Join(net.sf.jsqlparser.statement.select.Join) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) Test(org.junit.Test)

Example 5 with EqualsTo

use of net.sf.jsqlparser.expression.operators.relational.EqualsTo in project JSqlParser by JSQLParser.

the class SelectUtilsTest method testTableAliasIssue311.

@Test
public void testTableAliasIssue311() {
    Table table1 = new Table("mytable1");
    table1.setAlias(new Alias("tab1"));
    Table table2 = new Table("mytable2");
    table2.setAlias(new Alias("tab2"));
    List<? extends Expression> colunas = Arrays.asList(new Column(table1, "col1"), new Column(table1, "col2"), new Column(table1, "col3"), new Column(table2, "b1"), new Column(table2, "b2"));
    Select select = SelectUtils.buildSelectFromTableAndExpressions(table1, colunas.toArray(new Expression[colunas.size()]));
    final EqualsTo equalsTo = new EqualsTo();
    equalsTo.setLeftExpression(new Column(table1, "col1"));
    equalsTo.setRightExpression(new Column(table2, "b1"));
    Join addJoin = SelectUtils.addJoin(select, table2, equalsTo);
    addJoin.setLeft(true);
    assertEquals("SELECT tab1.col1, tab1.col2, tab1.col3, tab2.b1, tab2.b2 FROM mytable1 AS tab1 LEFT JOIN mytable2 AS tab2 ON tab1.col1 = tab2.b1", select.toString());
}
Also used : Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression) Alias(net.sf.jsqlparser.expression.Alias) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) Join(net.sf.jsqlparser.statement.select.Join) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) Test(org.junit.Test)

Aggregations

EqualsTo (net.sf.jsqlparser.expression.operators.relational.EqualsTo)5 GreaterThan (net.sf.jsqlparser.expression.operators.relational.GreaterThan)3 GreaterThanEquals (net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals)3 MinorThan (net.sf.jsqlparser.expression.operators.relational.MinorThan)3 MinorThanEquals (net.sf.jsqlparser.expression.operators.relational.MinorThanEquals)3 NotEqualsTo (net.sf.jsqlparser.expression.operators.relational.NotEqualsTo)3 StatementExecutionException (herddb.model.StatementExecutionException)2 RawString (herddb.utils.RawString)2 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)2 Expression (net.sf.jsqlparser.expression.Expression)2 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)2 Addition (net.sf.jsqlparser.expression.operators.arithmetic.Addition)2 Division (net.sf.jsqlparser.expression.operators.arithmetic.Division)2 Multiplication (net.sf.jsqlparser.expression.operators.arithmetic.Multiplication)2 Subtraction (net.sf.jsqlparser.expression.operators.arithmetic.Subtraction)2 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)2 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)2 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)2 Column (net.sf.jsqlparser.schema.Column)2 Table (net.sf.jsqlparser.schema.Table)2