Search in sources :

Example 11 with ParseInfo

use of org.teiid.query.parser.ParseInfo 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 12 with ParseInfo

use of org.teiid.query.parser.ParseInfo in project teiid by teiid.

the class CommandContext method putPlan.

public void putPlan(String key, PreparedPlan plan, Determinism determinismLevel) {
    if (this.globalState.planCache == null) {
        return;
    }
    CacheID id = new CacheID(new ParseInfo(), key, getVdbName(), getVdbVersion(), getConnectionId(), getUserName());
    this.globalState.planCache.put(id, determinismLevel, plan, null);
}
Also used : CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) ParseInfo(org.teiid.query.parser.ParseInfo)

Example 13 with ParseInfo

use of org.teiid.query.parser.ParseInfo in project teiid by teiid.

the class TestSessionAwareCache method testVDBRemoval.

@Test
public void testVDBRemoval() {
    SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>("resultset", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.RESULTSET, 0);
    CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    Cachable result = Mockito.mock(Cachable.class);
    Mockito.stub(result.prepare((BufferManager) anyObject())).toReturn(true);
    Mockito.stub(result.restore((BufferManager) anyObject())).toReturn(true);
    id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    cache.put(id, Determinism.VDB_DETERMINISTIC, result, null);
    Object c = cache.get(id);
    assertTrue(result == c);
    cache.clearForVDB("vdb-name", "1");
    assertNull(cache.get(id));
}
Also used : CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) ParseInfo(org.teiid.query.parser.ParseInfo) Cachable(org.teiid.cache.Cachable) Test(org.junit.Test)

Example 14 with ParseInfo

use of org.teiid.query.parser.ParseInfo in project teiid by teiid.

the class TestSessionAwareCache method testRemove.

@Test
public void testRemove() {
    SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>("resultset", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.RESULTSET, 0);
    CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    Cachable result = Mockito.mock(Cachable.class);
    Mockito.stub(result.prepare((BufferManager) anyObject())).toReturn(true);
    Mockito.stub(result.restore((BufferManager) anyObject())).toReturn(true);
    id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    cache.put(id, Determinism.VDB_DETERMINISTIC, result, null);
    Object c = cache.get(id);
    assertTrue(result == c);
    assertTrue(cache.remove(id, Determinism.VDB_DETERMINISTIC) != null);
    assertNull(cache.get(id));
    // session scope
    cache.put(id, Determinism.SESSION_DETERMINISTIC, result, null);
    assertTrue(cache.get(id) != null);
    assertTrue(cache.remove(id, Determinism.SESSION_DETERMINISTIC) != null);
    assertNull(cache.get(id));
}
Also used : CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) ParseInfo(org.teiid.query.parser.ParseInfo) Cachable(org.teiid.cache.Cachable) Test(org.junit.Test)

Example 15 with ParseInfo

use of org.teiid.query.parser.ParseInfo in project teiid by teiid.

the class TestODataSQLBuilder method helpTest.

public QueryState helpTest(String url, String sqlExpected, Integer skip, Integer top, Boolean count) throws Exception {
    QueryState state = setup(url);
    Client client = state.client;
    ArgumentCaptor<Query> arg1 = ArgumentCaptor.forClass(Query.class);
    ArgumentCaptor<EntityCollectionResponse> arg6 = ArgumentCaptor.forClass(EntityCollectionResponse.class);
    List<SQLParameter> parameters = new ArrayList<SQLParameter>();
    if (sqlExpected != null) {
        Query actualCommand = (Query) QueryParser.getQueryParser().parseCommand(sqlExpected, new ParseInfo());
        Mockito.verify(client).executeSQL(arg1.capture(), Mockito.eq(parameters), Mockito.eq(count), (Integer) Mockito.eq(skip), (Integer) Mockito.eq(top), (String) Mockito.eq(null), Mockito.anyInt(), arg6.capture());
        Assert.assertEquals(actualCommand.toString(), arg1.getValue().toString());
    }
    state.parameters = parameters;
    state.arg1 = arg1;
    state.arg6 = arg6;
    return state;
}
Also used : Query(org.teiid.query.sql.lang.Query) EntityCollectionResponse(org.teiid.olingo.service.EntityCollectionResponse) SQLParameter(org.teiid.odata.api.SQLParameter) ArrayList(java.util.ArrayList) ParseInfo(org.teiid.query.parser.ParseInfo) Client(org.teiid.odata.api.Client)

Aggregations

ParseInfo (org.teiid.query.parser.ParseInfo)16 CacheID (org.teiid.dqp.internal.process.SessionAwareCache.CacheID)11 Test (org.junit.Test)6 Cachable (org.teiid.cache.Cachable)6 Command (org.teiid.query.sql.lang.Command)4 ArrayList (java.util.ArrayList)3 Client (org.teiid.odata.api.Client)2 BatchCollector (org.teiid.query.processor.BatchCollector)2 Reference (org.teiid.query.sql.symbol.Reference)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 BlockedException (org.teiid.common.buffer.BlockedException)1 TupleBatch (org.teiid.common.buffer.TupleBatch)1 TupleBuffer (org.teiid.common.buffer.TupleBuffer)1 TeiidComponentException (org.teiid.core.TeiidComponentException)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 TransformationException (org.teiid.core.types.TransformationException)1 CachedResults (org.teiid.dqp.internal.process.CachedResults)1 PreparedPlan (org.teiid.dqp.internal.process.PreparedPlan)1 TransactionContext (org.teiid.dqp.service.TransactionContext)1