Search in sources :

Example 6 with Comparison

use of org.teiid.language.Comparison in project teiid by teiid.

the class TestDependentJoins method helpTestPushdown.

private void helpTestPushdown(boolean supportsArrayType) {
    // Create query
    // $NON-NLS-1$
    String sql = "SELECT pm1.g1.e1 FROM /*+ MAKEIND */ pm1.g1, pm2.g1 WHERE pm1.g1.e1 = pm2.g1.e1 AND pm1.g1.e2=pm2.g1.e2 order by pm1.g1.e1";
    // Create expected results
    List[] expected = new List[] { // $NON-NLS-1$
    Arrays.asList(new Object[] { "a" }) };
    // Construct data manager with data
    HardcodedDataManager dataManager = new HardcodedDataManager(RealMetadataFactory.example1Cached());
    dataManager.addData("SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM g1 AS g_0 ORDER BY c_0, c_1", new List[] { Arrays.asList("a", 1), Arrays.asList("b", 2) });
    if (supportsArrayType) {
        dataManager.addData("SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM g1 AS g_0 WHERE (g_0.e1, g_0.e2) = (?, ?) ORDER BY c_0, c_1", new List[] { Arrays.asList("a", 1) });
    } else {
        dataManager.addData("SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM g1 AS g_0 WHERE g_0.e1 = ? AND g_0.e2 = ? ORDER BY c_0, c_1", new List[] { Arrays.asList("a", 1) });
    }
    BasicSourceCapabilities bsc = TestOptimizer.getTypicalCapabilities();
    bsc.setCapabilitySupport(Capability.DEPENDENT_JOIN, true);
    bsc.setCapabilitySupport(Capability.ARRAY_TYPE, supportsArrayType);
    bsc.setSourceProperty(Capability.MAX_DEPENDENT_PREDICATES, 1);
    bsc.setSourceProperty(Capability.MAX_IN_CRITERIA_SIZE, 1);
    DefaultCapabilitiesFinder dcf = new DefaultCapabilitiesFinder(bsc);
    // Plan query
    ProcessorPlan plan = TestProcessor.helpGetPlan(sql, RealMetadataFactory.example1Cached(), dcf);
    TestOptimizer.checkDependentJoinCount(plan, 1);
    // Run query
    TestProcessor.helpProcess(plan, dataManager, expected);
    Select s = (Select) dataManager.getPushdownCommands().get(1);
    assertEquals(1, s.getDependentValues().size());
    List<? extends List<?>> vals = s.getDependentValues().values().iterator().next();
    assertEquals(2, vals.size());
    if (supportsArrayType) {
        Comparison comp = (Comparison) s.getWhere();
        Parameter p = (Parameter) ((Array) comp.getRightExpression()).getExpressions().get(0);
        assertEquals(0, p.getValueIndex());
        assertNotNull(s.getDependentValues().get(p.getDependentValueId()));
    }
}
Also used : Array(org.teiid.language.Array) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) Comparison(org.teiid.language.Comparison) Select(org.teiid.language.Select) Parameter(org.teiid.language.Parameter) List(java.util.List) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)

Example 7 with Comparison

use of org.teiid.language.Comparison in project teiid by teiid.

the class CoherenceVisitor method visit.

public void visit(Comparison obj) {
    // $NON-NLS-1$
    LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Parsing Comparison criteria.");
    try {
        Comparison.Operator op = ((Comparison) obj).getOperator();
        Expression lhs = ((Comparison) obj).getLeftExpression();
        Expression rhs = ((Comparison) obj).getRightExpression();
        String lhsString = getExpressionString(lhs);
        String rhsString = getExpressionString(rhs);
        if (lhsString == null || rhsString == null) {
            // $NON-NLS-1$
            final String msg = CoherencePlugin.Util.getString("CoherenceVisitor.missingComparisonExpression");
            exception = new TranslatorException(msg);
        }
        if (rhs instanceof Literal || lhs instanceof Literal) {
            if (rhs instanceof Literal) {
                Literal literal = (Literal) rhs;
                addCompareCriteria(lhsString, literal.getValue(), op, literal.getType());
            // filter = CoherenceFilterUtil.createCompareFilter(lhsString, literal.getValue(), op, literal.getType() );
            } else {
                Literal literal = (Literal) lhs;
                addCompareCriteria(rhsString, literal.getValue(), op, literal.getType());
            // filter = CoherenceFilterUtil.createCompareFilter(rhsString, literal.getValue(), op, literal.getType() );
            }
        }
    } catch (TranslatorException t) {
        exception = t;
    }
}
Also used : Comparison(org.teiid.language.Comparison) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) Operator(org.teiid.language.Comparison.Operator)

Example 8 with Comparison

use of org.teiid.language.Comparison in project teiid by teiid.

the class TestOracleTranslator method testArrayComparison.

@Test
public void testArrayComparison() throws Exception {
    // $NON-NLS-1$
    String input = "select intkey from bqt1.smalla where intkey = 5";
    // $NON-NLS-1$
    String output = "SELECT g_0.IntKey FROM SmallA g_0 WHERE (g_0.IntKey, g_0.IntKey) = ((5, 2))";
    CommandBuilder commandBuilder = new CommandBuilder(RealMetadataFactory.exampleBQTCached());
    Select obj = (Select) commandBuilder.getCommand(input, true, true);
    Comparison comp = (Comparison) obj.getWhere();
    // modify to an array comparison, since there is not yet parsing support
    comp.setLeftExpression(new Array(comp.getLeftExpression().getType(), Arrays.asList(comp.getLeftExpression(), comp.getLeftExpression())));
    comp.setRightExpression(new Array(comp.getLeftExpression().getType(), Arrays.asList(comp.getRightExpression(), new Literal(2, TypeFacility.RUNTIME_TYPES.INTEGER))));
    TranslationHelper.helpTestVisitor(output, TRANSLATOR, obj);
}
Also used : Array(org.teiid.language.Array) Comparison(org.teiid.language.Comparison) Literal(org.teiid.language.Literal) Select(org.teiid.language.Select) CommandBuilder(org.teiid.cdk.CommandBuilder) Test(org.junit.Test)

Example 9 with Comparison

use of org.teiid.language.Comparison in project teiid by teiid.

the class TestOracleTranslator method testDependentJoin.

@Test
public void testDependentJoin() throws Exception {
    CommandBuilder commandBuilder = new CommandBuilder(getOracleSpecificMetadata());
    Select command = (Select) commandBuilder.getCommand("select id from smalla where description = 'a'");
    Parameter param = new Parameter();
    param.setType(TypeFacility.RUNTIME_TYPES.STRING);
    param.setDependentValueId("x");
    param.setValueIndex(0);
    Map<String, List<? extends List<?>>> dependentValues = new HashMap<String, List<? extends List<?>>>();
    dependentValues.put("x", Arrays.asList(Arrays.asList("a"), Arrays.asList("b")));
    command.setDependentValues(dependentValues);
    ((Comparison) command.getWhere()).setRightExpression(param);
    Connection connection = Mockito.mock(Connection.class);
    Statement statement = Mockito.mock(Statement.class);
    Mockito.stub(connection.createStatement()).toReturn(statement);
    PreparedStatement ps = Mockito.mock(PreparedStatement.class);
    Mockito.stub(ps.executeBatch()).toReturn(new int[] { -2, -2 });
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("INSERT INTO TEIID_DKJ1 (COL1) VALUES (?)")).toReturn(ps);
    // we won't bother to retrieve the results, but we expect the following join query
    PreparedStatement ps1 = Mockito.mock(PreparedStatement.class);
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("SELECT SmallishA.ID FROM TEIID_DKJ1, SmallishA WHERE SmallishA.description = TEIID_DKJ1.COL1")).toReturn(ps1);
    OracleExecutionFactory ef = new OracleExecutionFactory() {

        public String getTemporaryTableName(String prefix) {
            // don't use random for testing
            return prefix;
        }
    };
    ef.setDatabaseVersion(Version.DEFAULT_VERSION);
    ef.start();
    JDBCQueryExecution e = new JDBCQueryExecution(command, connection, new FakeExecutionContextImpl(), ef);
    e.execute();
    Mockito.verify(statement, Mockito.times(1)).execute("DECLARE PRAGMA AUTONOMOUS_TRANSACTION; BEGIN EXECUTE IMMEDIATE 'create global temporary table TEIID_DKJ1 (COL1 varchar2(100 char)) on commit delete rows; END;");
    Mockito.verify(ps, Mockito.times(1)).setObject(1, "a", Types.VARCHAR);
    Mockito.verify(ps, Mockito.times(1)).setObject(1, "b", Types.VARCHAR);
    Mockito.verify(ps, Mockito.times(2)).addBatch();
    Mockito.verify(ps, Mockito.times(1)).executeBatch();
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Comparison(org.teiid.language.Comparison) Select(org.teiid.language.Select) Parameter(org.teiid.language.Parameter) SPParameter(org.teiid.query.sql.lang.SPParameter) ProcedureParameter(org.teiid.metadata.ProcedureParameter) List(java.util.List) JDBCQueryExecution(org.teiid.translator.jdbc.JDBCQueryExecution) CommandBuilder(org.teiid.cdk.CommandBuilder) Test(org.junit.Test)

Example 10 with Comparison

use of org.teiid.language.Comparison in project teiid by teiid.

the class ODataQuery method parseKeySegmentFromCondition.

protected Condition parseKeySegmentFromCondition(Condition obj) throws TranslatorException {
    List<Condition> crits = LanguageUtil.separateCriteriaByAnd(obj);
    if (!crits.isEmpty()) {
        boolean modified = false;
        for (Iterator<Condition> iter = crits.iterator(); iter.hasNext(); ) {
            Condition crit = iter.next();
            if (crit instanceof Comparison) {
                Comparison left = (Comparison) crit;
                boolean leftAdded = parseKeySegmentFromComparison(left);
                if (leftAdded) {
                    iter.remove();
                    modified = true;
                }
            }
        }
        if (modified) {
            return LanguageUtil.combineCriteria(crits);
        }
    }
    return obj;
}
Also used : Condition(org.teiid.language.Condition) Comparison(org.teiid.language.Comparison)

Aggregations

Comparison (org.teiid.language.Comparison)15 Literal (org.teiid.language.Literal)8 Test (org.junit.Test)5 Select (org.teiid.language.Select)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Expression (org.teiid.language.Expression)3 Function (org.teiid.language.Function)3 CommandBuilder (org.teiid.cdk.CommandBuilder)2 Array (org.teiid.language.Array)2 ColumnReference (org.teiid.language.ColumnReference)2 NamedTable (org.teiid.language.NamedTable)2 Parameter (org.teiid.language.Parameter)2 SearchedCase (org.teiid.language.SearchedCase)2 SearchedWhenClause (org.teiid.language.SearchedWhenClause)2 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)2 DefaultCapabilitiesFinder (org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)2 CallableStatement (java.sql.CallableStatement)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1