Search in sources :

Example 26 with Reference

use of org.teiid.query.sql.symbol.Reference 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 27 with Reference

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

the class TempTableDataManager method handleCachedProcedure.

private TupleSource handleCachedProcedure(final CommandContext context, StoredProcedure proc) throws TeiidComponentException, QueryMetadataException, TeiidProcessingException {
    String fullName = context.getMetadata().getFullName(proc.getProcedureID());
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_DQP, "processing cached procedure request for", fullName);
    LinkedList<Object> vals = new LinkedList<Object>();
    for (SPParameter param : proc.getInputParameters()) {
        vals.add(((Constant) param.getExpression()).getValue());
    }
    // collapse the hash to single byte for the key to restrict the possible results to 256
    int hash = vals.hashCode();
    hash |= (hash >>> 16);
    hash |= (hash >>> 8);
    hash &= 0x000000ff;
    final CacheID cid = new CacheID(new ParseInfo(), fullName + hash, context.getVdbName(), context.getVdbVersion(), context.getConnectionId(), context.getUserName());
    cid.setParameters(vals);
    CachedResults results = cache.get(cid);
    if (results != null) {
        TupleBuffer buffer = results.getResults();
        return buffer.createIndexedTupleSource();
    }
    // construct a query with a no cache hint
    final CacheHint hint = proc.getCacheHint();
    proc.setCacheHint(null);
    Option option = new Option();
    option.setNoCache(true);
    option.addNoCacheGroup(fullName);
    proc.setOption(option);
    StoredProcedure cloneProc = (StoredProcedure) proc.clone();
    int i = 0;
    for (SPParameter param : cloneProc.getInputParameters()) {
        param.setExpression(new Reference(i++));
    }
    final QueryProcessor qp = context.getQueryProcessorFactory().createQueryProcessor(cloneProc.toString(), fullName.toUpperCase(), context, vals.toArray());
    final BatchCollector bc = qp.createBatchCollector();
    return new ProxyTupleSource() {

        boolean success = false;

        @Override
        protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
            TupleBuffer tb = bc.collectTuples();
            CachedResults cr = new CachedResults();
            cr.setResults(tb, qp.getProcessorPlan());
            Determinism determinismLevel = qp.getContext().getDeterminismLevel();
            if (hint != null && hint.getDeterminism() != null) {
                // $NON-NLS-1$ //$NON-NLS-2$
                LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Cache hint modified the query determinism from ", determinismLevel, " to ", hint.getDeterminism() });
                determinismLevel = hint.getDeterminism();
            }
            cache.put(cid, determinismLevel, cr, hint != null ? hint.getTtl() : null);
            context.setDeterminismLevel(determinismLevel);
            success = true;
            return tb.createIndexedTupleSource();
        }

        @Override
        public void closeSource() {
            super.closeSource();
            qp.closeProcessing();
            if (!success && bc.getTupleBuffer() != null) {
                bc.getTupleBuffer().remove();
            }
        }
    };
}
Also used : Determinism(org.teiid.metadata.FunctionMethod.Determinism) Reference(org.teiid.query.sql.symbol.Reference) TupleBuffer(org.teiid.common.buffer.TupleBuffer) ParseInfo(org.teiid.query.parser.ParseInfo) LinkedList(java.util.LinkedList) CachedResults(org.teiid.dqp.internal.process.CachedResults) QueryProcessor(org.teiid.query.processor.QueryProcessor) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) BatchCollector(org.teiid.query.processor.BatchCollector)

Example 28 with Reference

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

the class CommandBuilder method getCommand.

public org.teiid.language.Command getCommand(String queryString, boolean generateAliases, boolean supportsGroupAlias) {
    Command command = null;
    try {
        command = QueryParser.getQueryParser().parseCommand(queryString);
        QueryResolver.resolveCommand(command, metadata);
        command = QueryRewriter.rewrite(command, metadata, null);
        expandAllSymbol(command);
        if (generateAliases) {
            command = (Command) command.clone();
            command.acceptVisitor(new AliasGenerator(supportsGroupAlias));
        }
        // the language bridge doesn't expect References
        ValueIteratorProviderCollectorVisitor v = new ValueIteratorProviderCollectorVisitor();
        v.setCollectLateral(true);
        PreOrderNavigator.doVisit(command, v);
        for (SubqueryContainer<?> container : v.getValueIteratorProviders()) {
            ExpressionMappingVisitor visitor = new ExpressionMappingVisitor(null) {

                @Override
                public Expression replaceExpression(Expression element) {
                    if (element instanceof Reference) {
                        return ((Reference) element).getExpression();
                    }
                    return element;
                }
            };
            DeepPostOrderNavigator.doVisit(command, visitor);
        }
        return languageBridgeFactory.translate(command);
    } catch (TeiidException e) {
        throw new TeiidRuntimeException(e);
    }
}
Also used : Command(org.teiid.query.sql.lang.Command) AliasGenerator(org.teiid.query.optimizer.relational.AliasGenerator) Expression(org.teiid.query.sql.symbol.Expression) Reference(org.teiid.query.sql.symbol.Reference) ValueIteratorProviderCollectorVisitor(org.teiid.query.sql.visitor.ValueIteratorProviderCollectorVisitor) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor) TeiidException(org.teiid.core.TeiidException)

Example 29 with Reference

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

the class TestLimitParsing method testSetQueryLimit.

@Test
public void testSetQueryLimit() {
    Query query = new Query();
    Select select = new Select(Arrays.asList(new MultipleElementSymbol()));
    // $NON-NLS-1$
    From from = new From(Arrays.asList(new UnaryFromClause(new GroupSymbol("a"))));
    query.setSelect(select);
    query.setFrom(from);
    SetQuery setQuery = new SetQuery(Operation.UNION, true, query, query);
    setQuery.setLimit(new Limit(new Reference(0), new Reference(1)));
    // $NON-NLS-1$ //$NON-NLS-2$
    helpTest("Select * from a union all Select * from a limit ?,?", "SELECT * FROM a UNION ALL SELECT * FROM a LIMIT ?, ?", setQuery);
}
Also used : MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) SetQuery(org.teiid.query.sql.lang.SetQuery) Query(org.teiid.query.sql.lang.Query) SetQuery(org.teiid.query.sql.lang.SetQuery) UnaryFromClause(org.teiid.query.sql.lang.UnaryFromClause) Reference(org.teiid.query.sql.symbol.Reference) Select(org.teiid.query.sql.lang.Select) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) From(org.teiid.query.sql.lang.From) Limit(org.teiid.query.sql.lang.Limit) Test(org.junit.Test)

Example 30 with Reference

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

the class TestLimitParsing method testOffset.

@Test
public void testOffset() {
    Query query = new Query();
    Select select = new Select(Arrays.asList(new MultipleElementSymbol()));
    // $NON-NLS-1$
    From from = new From(Arrays.asList(new UnaryFromClause(new GroupSymbol("a"))));
    query.setSelect(select);
    query.setFrom(from);
    query.setLimit(new Limit(new Reference(0), null));
    // $NON-NLS-1$ //$NON-NLS-2$
    helpTest("Select * from a offset ? rows", "SELECT * FROM a OFFSET ? ROWS", query);
}
Also used : MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) Query(org.teiid.query.sql.lang.Query) SetQuery(org.teiid.query.sql.lang.SetQuery) UnaryFromClause(org.teiid.query.sql.lang.UnaryFromClause) Reference(org.teiid.query.sql.symbol.Reference) Select(org.teiid.query.sql.lang.Select) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) From(org.teiid.query.sql.lang.From) Limit(org.teiid.query.sql.lang.Limit) Test(org.junit.Test)

Aggregations

Reference (org.teiid.query.sql.symbol.Reference)42 Test (org.junit.Test)15 Constant (org.teiid.query.sql.symbol.Constant)14 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)14 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)12 Expression (org.teiid.query.sql.symbol.Expression)11 Limit (org.teiid.query.sql.lang.Limit)7 Query (org.teiid.query.sql.lang.Query)7 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)7 ArrayList (java.util.ArrayList)6 Criteria (org.teiid.query.sql.lang.Criteria)6 LinkedList (java.util.LinkedList)5 From (org.teiid.query.sql.lang.From)5 Select (org.teiid.query.sql.lang.Select)5 SetQuery (org.teiid.query.sql.lang.SetQuery)5 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)5 UnaryFromClause (org.teiid.query.sql.lang.UnaryFromClause)5 Column (org.teiid.metadata.Column)4 Command (org.teiid.query.sql.lang.Command)4 SPParameter (org.teiid.query.sql.lang.SPParameter)4