Search in sources :

Example 11 with Array

use of org.teiid.query.sql.symbol.Array in project teiid by teiid.

the class DependentProcedureCriteriaProcessor method prepareNextCommand.

protected boolean prepareNextCommand(VariableContext context) throws BlockedException, TeiidComponentException, TeiidProcessingException {
    if (this.critInProgress == null) {
        critInProgress = prepareCriteria();
    }
    for (int j = 0; j < inputReferences.size(); j++) {
        Reference ref = (Reference) inputReferences.get(j);
        context.remove(ref.getExpression());
    }
    if (critInProgress == QueryRewriter.FALSE_CRITERIA) {
        critInProgress = null;
        consumedCriteria();
        return false;
    }
    boolean validRow = true;
    for (Iterator<Criteria> i = Criteria.separateCriteriaByAnd(critInProgress).iterator(); i.hasNext() && validRow; ) {
        Criteria crit = i.next();
        Object value = null;
        boolean nullAllowed = false;
        Reference parameter = null;
        if (crit instanceof IsNullCriteria) {
            parameter = (Reference) ((IsNullCriteria) crit).getExpression();
            nullAllowed = true;
        } else if (crit instanceof CompareCriteria) {
            CompareCriteria compare = (CompareCriteria) crit;
            value = compare.getRightExpression();
            if (compare.getLeftExpression() instanceof Array) {
                Array array = (Array) compare.getLeftExpression();
                if (value instanceof Expression) {
                    value = eval.evaluate((Expression) value, null);
                }
                if (value == null) {
                    validRow = false;
                    break;
                }
                ArrayImpl valueArray = (ArrayImpl) value;
                for (int j = 0; j < array.getExpressions().size(); j++) {
                    validRow = setParam(context, valueArray.getValues()[j], nullAllowed, (Reference) array.getExpressions().get(j));
                    if (!validRow) {
                        break;
                    }
                }
                continue;
            }
            parameter = (Reference) compare.getLeftExpression();
        } else {
            // $NON-NLS-1$
            Assertion.failed("Unknown predicate type");
        }
        validRow = setParam(context, value, nullAllowed, parameter);
    }
    critInProgress = null;
    consumedCriteria();
    if (!validRow) {
        return false;
    }
    for (int j = 0; j < inputReferences.size(); j++) {
        Object defaultValue = inputDefaults.get(j);
        Reference ref = (Reference) inputReferences.get(j);
        if (defaultValue != null && !context.containsVariable(ref.getExpression())) {
            context.setValue(ref.getExpression(), defaultValue);
        }
    }
    return true;
}
Also used : Array(org.teiid.query.sql.symbol.Array) Expression(org.teiid.query.sql.symbol.Expression) Reference(org.teiid.query.sql.symbol.Reference) ArrayImpl(org.teiid.core.types.ArrayImpl) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria)

Example 12 with Array

use of org.teiid.query.sql.symbol.Array in project teiid by teiid.

the class DependentValueSource method getValueIterator.

/**
 * @throws TeiidComponentException
 * @see org.teiid.query.sql.util.ValueIteratorSource#getValueIterator(org.teiid.query.sql.symbol.Expression)
 */
public TupleSourceValueIterator getValueIterator(Expression valueExpression) throws TeiidComponentException {
    IndexedTupleSource its = buffer.createIndexedTupleSource();
    int index = 0;
    if (valueExpression != null) {
        if (valueExpression instanceof Array) {
            final Array array = (Array) valueExpression;
            List<Expression> exprs = array.getExpressions();
            final int[] indexes = new int[exprs.size()];
            for (int i = 0; i < exprs.size(); i++) {
                indexes[i] = getIndex(exprs.get(i));
            }
            return new TupleSourceValueIterator(its, index) {

                @Override
                public Object next() throws TeiidComponentException {
                    List<?> tuple = super.nextTuple();
                    Object[] a = (Object[]) java.lang.reflect.Array.newInstance(array.getComponentType(), indexes.length);
                    for (int i = 0; i < indexes.length; i++) {
                        a[i] = tuple.get(indexes[i]);
                        if (a[i] == null) {
                            // TODO: this is a hack
                            return null;
                        }
                    }
                    return new ArrayImpl(a);
                }
            };
        }
        index = getIndex(valueExpression);
    }
    return new TupleSourceValueIterator(its, index);
}
Also used : Array(org.teiid.query.sql.symbol.Array) Expression(org.teiid.query.sql.symbol.Expression) IndexedTupleSource(org.teiid.common.buffer.IndexedTupleSource) ArrayImpl(org.teiid.core.types.ArrayImpl)

Example 13 with Array

use of org.teiid.query.sql.symbol.Array in project teiid by teiid.

the class TestArrayProcessing method testArrayParsing.

@Test
public void testArrayParsing() throws Exception {
    TestParser.helpTestExpression("()", "()", new Array(new ArrayList<Expression>()));
    TestParser.helpTestExpression("(,)", "()", new Array(new ArrayList<Expression>()));
    TestParser.helpTestExpression("(1,)", "(1,)", new Array(Arrays.asList((Expression) new Constant(1))));
    TestParser.helpTestExpression("(1,2)", "(1, 2)", new Array(Arrays.asList((Expression) new Constant(1), (Expression) new Constant(2))));
    TestParser.helpTestExpression("(1,2,)", "(1, 2)", new Array(Arrays.asList((Expression) new Constant(1), (Expression) new Constant(2))));
}
Also used : Array(org.teiid.query.sql.symbol.Array) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 14 with Array

use of org.teiid.query.sql.symbol.Array in project teiid by teiid.

the class TestProcedureResolving method testVarArgs1.

@Test
public void testVarArgs1() throws Exception {
    String ddl = "create foreign procedure proc (VARIADIC z integer) returns (x string);\n";
    TransformationMetadata tm = createMetadata(ddl);
    // $NON-NLS-1$
    String sql = "call proc ()";
    StoredProcedure sp = (StoredProcedure) TestResolver.helpResolve(sql, tm);
    assertEquals("EXEC proc()", sp.toString());
    assertEquals(new Array(DataTypeManager.DefaultDataClasses.INTEGER, new ArrayList<Expression>(0)), sp.getParameter(1).getExpression());
    sp = (StoredProcedure) QueryRewriter.evaluateAndRewrite(sp, new Evaluator(null, null, null), null, tm);
    LanguageBridgeFactory lbf = new LanguageBridgeFactory(tm);
    Call call = (Call) lbf.translate(sp);
    assertEquals("EXEC proc()", call.toString());
    // we pass to the translator level flattened, so no argument
    assertEquals(0, call.getArguments().size());
}
Also used : Array(org.teiid.query.sql.symbol.Array) Call(org.teiid.language.Call) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) ArrayList(java.util.ArrayList) Evaluator(org.teiid.query.eval.Evaluator) LanguageBridgeFactory(org.teiid.dqp.internal.datamgr.LanguageBridgeFactory) Test(org.junit.Test)

Aggregations

Array (org.teiid.query.sql.symbol.Array)14 Expression (org.teiid.query.sql.symbol.Expression)9 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)5 Constant (org.teiid.query.sql.symbol.Constant)5 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)4 ArrayImpl (org.teiid.core.types.ArrayImpl)3 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)3 LanguageBridgeFactory (org.teiid.dqp.internal.datamgr.LanguageBridgeFactory)2 Call (org.teiid.language.Call)2 Evaluator (org.teiid.query.eval.Evaluator)2 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)2 Criteria (org.teiid.query.sql.lang.Criteria)2 DependentSetCriteria (org.teiid.query.sql.lang.DependentSetCriteria)2 Reference (org.teiid.query.sql.symbol.Reference)2 HashSet (java.util.HashSet)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)1 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)1