Search in sources :

Example 21 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TempTableDataManager method registerRequest.

TupleSource registerRequest(final CommandContext context, String modelName, final Command command) throws TeiidComponentException, TeiidProcessingException {
    final TempTableStore contextStore = context.getTempTableStore();
    if (command instanceof Query) {
        Query query = (Query) command;
        if (modelName != null && !modelName.equals(TempMetadataAdapter.TEMP_MODEL.getID())) {
            return null;
        }
        return registerQuery(context, contextStore, query);
    }
    if (command instanceof ProcedureContainer) {
        if (command instanceof StoredProcedure) {
            StoredProcedure proc = (StoredProcedure) command;
            if (CoreConstants.SYSTEM_ADMIN_MODEL.equals(modelName)) {
                TupleSource result = handleSystemProcedures(context, proc);
                if (result != null) {
                    return result;
                }
            } else if (proc.getGroup().isGlobalTable()) {
                return handleCachedProcedure(context, proc);
            }
            // it's not a stored procedure we want to handle
            return null;
        }
        final GroupSymbol group = ((ProcedureContainer) command).getGroup();
        if (!modelName.equals(TempMetadataAdapter.TEMP_MODEL.getID()) || !group.isTempGroupSymbol()) {
            return null;
        }
        return new ProxyTupleSource() {

            @Override
            protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
                final String groupKey = group.getNonCorrelationName();
                final TempTable table = contextStore.getOrCreateTempTable(groupKey, command, bufferManager, true, true, context, group);
                if (command instanceof Insert) {
                    Insert insert = (Insert) command;
                    TupleSource ts = insert.getTupleSource();
                    if (ts == null) {
                        Evaluator eval = new Evaluator(Collections.emptyMap(), TempTableDataManager.this, context);
                        List<Object> values = new ArrayList<Object>(insert.getValues().size());
                        for (Expression expr : (List<Expression>) insert.getValues()) {
                            values.add(eval.evaluate(expr, null));
                        }
                        ts = new CollectionTupleSource(Arrays.asList(values).iterator());
                    }
                    return table.insert(ts, insert.getVariables(), true, insert.isUpsert(), context);
                }
                if (command instanceof Update) {
                    final Update update = (Update) command;
                    final Criteria crit = update.getCriteria();
                    return table.update(crit, update.getChangeList());
                }
                if (command instanceof Delete) {
                    final Delete delete = (Delete) command;
                    final Criteria crit = delete.getCriteria();
                    if (crit == null) {
                        // TODO: we'll add a real truncate later
                        long rows = table.truncate(false);
                        return CollectionTupleSource.createUpdateCountTupleSource((int) Math.min(Integer.MAX_VALUE, rows));
                    }
                    return table.delete(crit);
                }
                // $NON-NLS-1$
                throw new AssertionError("unknown command " + command);
            }
        };
    }
    if (command instanceof Create) {
        Create create = (Create) command;
        String tempTableName = create.getTable().getName();
        if (contextStore.hasTempTable(tempTableName, true)) {
            throw new QueryProcessingException(QueryPlugin.Event.TEIID30229, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30229, tempTableName));
        }
        if (create.getTableMetadata() != null) {
            contextStore.addForeignTempTable(tempTableName, create);
        } else {
            contextStore.addTempTable(tempTableName, create, bufferManager, true, context);
        }
        return CollectionTupleSource.createUpdateCountTupleSource(0);
    }
    if (command instanceof Drop) {
        String tempTableName = ((Drop) command).getTable().getName();
        contextStore.removeTempTableByName(tempTableName, context);
        return CollectionTupleSource.createUpdateCountTupleSource(0);
    }
    if (command instanceof AlterTempTable) {
        // non longer used, was part of xml logic
        AlterTempTable att = (AlterTempTable) command;
        TempTable tt = contextStore.getTempTable(att.getTempTable());
        // $NON-NLS-1$
        Assertion.isNotNull(tt, "Table doesn't exist");
        tt.setUpdatable(false);
        if (att.getIndexColumns() != null && tt.getRowCount() > 2 * tt.getTree().getPageSize(true)) {
            for (List<ElementSymbol> cols : att.getIndexColumns()) {
                tt.addIndex(cols, false);
            }
        }
        return CollectionTupleSource.createUpdateCountTupleSource(0);
    }
    return null;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ArrayList(java.util.ArrayList) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Evaluator(org.teiid.query.eval.Evaluator) Expression(org.teiid.query.sql.symbol.Expression) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Example 22 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TempTableDataManager method updateMatviewRows.

private TupleSource updateMatviewRows(final CommandContext context, final QueryMetadataInterface metadata, final Object groupID, final GlobalTableStore globalStore, final String matViewName, List<?> ids, Object[][] params) throws QueryProcessingException, TeiidComponentException, QueryMetadataException, TransformationException {
    final String matTableName = RelationalPlanner.MAT_PREFIX + matViewName.toUpperCase();
    MatTableInfo info = globalStore.getMatTableInfo(matTableName);
    if (!info.isValid()) {
        return CollectionTupleSource.createUpdateCountTupleSource(-1);
    }
    TempTable tempTable = globalStore.getTempTable(matTableName);
    if (!tempTable.isUpdatable()) {
        throw new QueryProcessingException(QueryPlugin.Event.TEIID30232, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30232, matViewName));
    }
    List<Object[]> converted = new ArrayList<Object[]>();
    for (Object[] param : params) {
        if (param == null || ids.size() != param.length) {
            throw new QueryProcessingException(QueryPlugin.Event.TEIID30231, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30231, matViewName, ids.size(), param == null ? 0 : param.length));
        }
        final Object[] vals = new Object[param.length];
        for (int i = 0; i < ids.size(); i++) {
            Object value = param[i];
            String targetTypeName = metadata.getElementRuntimeTypeName(ids.get(i));
            value = DataTypeManager.transformValue(value, DataTypeManager.getDataTypeClass(targetTypeName));
            vals[i] = value;
        }
        converted.add(vals);
    }
    final Iterator<Object[]> paramIter = converted.iterator();
    Iterator<?> iter = ids.iterator();
    StringBuilder criteria = new StringBuilder();
    for (int i = 0; i < ids.size(); i++) {
        Object id = iter.next();
        if (i != 0) {
            // $NON-NLS-1$
            criteria.append(" AND ");
        }
        // $NON-NLS-1$
        criteria.append(metadata.getFullName(id)).append(" = ?");
    }
    final String queryString = // $NON-NLS-1$
    Reserved.SELECT + " * " + Reserved.FROM + ' ' + matViewName + ' ' + Reserved.WHERE + ' ' + criteria.toString() + ' ' + Reserved.OPTION + ' ' + Reserved.NOCACHE;
    return new ProxyTupleSource() {

        private QueryProcessor qp;

        private TupleSource ts;

        private Object[] params;

        private int count;

        @Override
        protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
            while (true) {
                if (qp == null) {
                    params = paramIter.next();
                    LogManager.logInfo(LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30012, matViewName, Arrays.toString(params)));
                    qp = context.getQueryProcessorFactory().createQueryProcessor(queryString, matViewName.toUpperCase(), context, params);
                    ts = new BatchCollector.BatchProducerTupleSource(qp);
                }
                List<?> tuple = ts.nextTuple();
                boolean delete = false;
                if (tuple == null) {
                    delete = true;
                    tuple = Arrays.asList(params);
                } else {
                    // ensure the list is serializable
                    tuple = new ArrayList<Object>(tuple);
                }
                List<?> result = globalStore.updateMatViewRow(matTableName, tuple, delete);
                if (result != null) {
                    count++;
                }
                if (eventDistributor != null) {
                    eventDistributor.updateMatViewRow(context.getVdbName(), context.getVdbVersion(), metadata.getName(metadata.getModelID(groupID)), metadata.getName(groupID), tuple, delete);
                }
                qp.closeProcessing();
                qp = null;
                ts = null;
                if (!paramIter.hasNext()) {
                    break;
                }
            }
            return CollectionTupleSource.createUpdateCountTupleSource(count);
        }

        @Override
        public void closeSource() {
            super.closeSource();
            if (qp != null) {
                qp.closeProcessing();
            }
        }
    };
}
Also used : ArrayList(java.util.ArrayList) QueryProcessor(org.teiid.query.processor.QueryProcessor) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) MatTableInfo(org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo) BatchCollector(org.teiid.query.processor.BatchCollector) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Example 23 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TempTableDataManager method loadGlobalTable.

private TupleSource loadGlobalTable(final CommandContext context, final GroupSymbol group, final String tableName, final GlobalTableStore globalStore) throws TeiidComponentException, TeiidProcessingException {
    LogManager.logInfo(LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30013, tableName));
    final QueryMetadataInterface metadata = context.getMetadata();
    final List<ElementSymbol> allColumns = ResolverUtil.resolveElementsInGroup(group, metadata);
    final TempTable table = globalStore.createMatTable(tableName, group);
    table.setUpdatable(false);
    return new ProxyTupleSource() {

        TupleSource insertTupleSource;

        boolean success;

        QueryProcessor qp;

        boolean closed;

        boolean errored;

        @Override
        protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
            long rowCount = -1;
            try {
                if (insertTupleSource == null) {
                    String fullName = metadata.getFullName(group.getMetadataID());
                    String transformation = metadata.getVirtualPlan(group.getMetadataID()).getQuery();
                    qp = context.getQueryProcessorFactory().createQueryProcessor(transformation, fullName, context);
                    insertTupleSource = new BatchCollector.BatchProducerTupleSource(qp);
                }
                table.insert(insertTupleSource, allColumns, false, false, null);
                table.getTree().compact();
                rowCount = table.getRowCount();
                Determinism determinism = qp.getContext().getDeterminismLevel();
                context.setDeterminismLevel(determinism);
                // TODO: could pre-process indexes to remove overlap
                for (Object index : metadata.getIndexesInGroup(group.getMetadataID())) {
                    List<ElementSymbol> columns = GlobalTableStoreImpl.resolveIndex(metadata, allColumns, index);
                    table.addIndex(columns, false);
                }
                for (Object key : metadata.getUniqueKeysInGroup(group.getMetadataID())) {
                    List<ElementSymbol> columns = GlobalTableStoreImpl.resolveIndex(metadata, allColumns, key);
                    table.addIndex(columns, true);
                }
                CacheHint hint = table.getCacheHint();
                if (hint != null && table.getPkLength() > 0) {
                    table.setUpdatable(hint.isUpdatable(false));
                }
                if (determinism.compareTo(Determinism.VDB_DETERMINISTIC) < 0 && (hint == null || hint.getScope() == null || Scope.VDB.compareTo(hint.getScope()) <= 0)) {
                    // $NON-NLS-1$
                    LogManager.logInfo(LogConstants.CTX_DQP, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31143, determinism, tableName));
                }
                globalStore.loaded(tableName, table);
                success = true;
                LogManager.logInfo(LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30014, tableName, rowCount));
                return CollectionTupleSource.createUpdateCountTupleSource((int) Math.min(Integer.MAX_VALUE, rowCount));
            } catch (BlockedException e) {
                throw e;
            } catch (Exception e) {
                errored = true;
                if (executor == null || !executor.isShutdown()) {
                    // if we're shutting down, no need to log
                    LogManager.logError(LogConstants.CTX_MATVIEWS, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30015, tableName));
                }
                closeSource();
                rethrow(e);
                throw new AssertionError();
            }
        }

        @Override
        public void closeSource() {
            if (closed) {
                return;
            }
            if (!errored && !success) {
                LogManager.logInfo(LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31153, tableName));
            }
            closed = true;
            if (!success) {
                globalStore.failedLoad(tableName);
                table.remove();
            }
            if (qp != null) {
                qp.closeProcessing();
            }
            super.closeSource();
        }
    };
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Determinism(org.teiid.metadata.FunctionMethod.Determinism) BlockedException(org.teiid.common.buffer.BlockedException) QueryProcessor(org.teiid.query.processor.QueryProcessor) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException) TeiidComponentException(org.teiid.core.TeiidComponentException) TransformationException(org.teiid.core.types.TransformationException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) BlockedException(org.teiid.common.buffer.BlockedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) BatchCollector(org.teiid.query.processor.BatchCollector)

Example 24 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TestDataTierManager method testCancelWithCaching.

@Test
public void testCancelWithCaching() throws Exception {
    QueryMetadataInterface metadata = RealMetadataFactory.exampleBQTCached();
    CacheDirective cd = new CacheDirective();
    this.connectorManager.cacheDirective = cd;
    helpSetupDataTierManager();
    Command command = helpSetupRequest("SELECT stringkey from bqt1.smalla", 1, metadata).getCommand();
    this.context.getSession().setSessionId("different");
    RegisterRequestParameter rrp = new RegisterRequestParameter();
    rrp.connectorBindingId = "x";
    TupleSource ts = dtm.registerRequest(context, command, "foo", rrp);
    assertTrue(ts instanceof CachingTupleSource);
    assertEquals(4, pullTuples(ts, 4));
    ((CachingTupleSource) ts).item.requestCancel("");
    assertEquals(1, connectorManager.getExecuteCount().get());
    assertFalse(rrp.doNotCache);
    // should force read all
    ts.closeSource();
    assertFalse(((CachingTupleSource) ts).dtts.errored);
    assertNull(((CachingTupleSource) ts).dtts.scope);
    assertEquals(0, this.rm.getRsCache().getCachePutCount());
}
Also used : CacheDirective(org.teiid.translator.CacheDirective) Command(org.teiid.query.sql.lang.Command) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) TupleSource(org.teiid.common.buffer.TupleSource) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) RegisterRequestParameter(org.teiid.query.processor.RegisterRequestParameter) Test(org.junit.Test)

Example 25 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TestProcessor method examineResults.

/**
 * @param expectedResults
 * @param bufferMgr
 * @param tsID
 * @throws TeiidComponentException
 * @throws TeiidProcessingException
 * @throws IOException
 * @since 4.3
 */
static void examineResults(List[] expectedResults, BufferManager bufferMgr, TupleBuffer tsID) throws TeiidComponentException, SQLException, TeiidProcessingException, IOException {
    // Create QueryResults from TupleSource
    TupleSource ts = tsID.createIndexedTupleSource();
    long count = tsID.getRowCount();
    // Compare actual to expected row count
    // $NON-NLS-1$
    assertEquals("Did not get expected row count: ", expectedResults.length, count);
    // Walk results and compare
    for (int i = 0; i < count; i++) {
        List record = ts.nextTuple();
        // handle xml
        if (record.size() == 1 && expectedResults[i].size() == 1) {
            Object cellValue = record.get(0);
            if (cellValue instanceof XMLType) {
                XMLType id = (XMLType) cellValue;
                String actualDoc = id.getString();
                if (expectedResults[i].size() == 1) {
                    compareDocuments((String) expectedResults[i].get(0), actualDoc);
                    continue;
                }
            } else if (cellValue instanceof Clob) {
                Clob id = (Clob) cellValue;
                String actualDoc = ClobType.getString(id);
                if (expectedResults[i].size() == 1) {
                    compareDocuments((String) expectedResults[i].get(0), actualDoc);
                    continue;
                }
            }
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        assertEquals("Row " + i + " does not match expected: ", expectedResults[i], record);
    }
    ts.closeSource();
}
Also used : XMLType(org.teiid.core.types.XMLType) TupleSource(org.teiid.common.buffer.TupleSource) Clob(java.sql.Clob)

Aggregations

TupleSource (org.teiid.common.buffer.TupleSource)31 CollectionTupleSource (org.teiid.query.processor.CollectionTupleSource)11 Test (org.junit.Test)10 TupleBuffer (org.teiid.common.buffer.TupleBuffer)10 CommandContext (org.teiid.query.util.CommandContext)10 List (java.util.List)9 BlockedException (org.teiid.common.buffer.BlockedException)9 TeiidProcessingException (org.teiid.core.TeiidProcessingException)9 Command (org.teiid.query.sql.lang.Command)9 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)9 ArrayList (java.util.ArrayList)6 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)6 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)6 RegisterRequestParameter (org.teiid.query.processor.RegisterRequestParameter)6 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)6 BufferManager (org.teiid.common.buffer.BufferManager)5 TeiidComponentException (org.teiid.core.TeiidComponentException)5 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)4 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)4 BufferManagerImpl (org.teiid.common.buffer.impl.BufferManagerImpl)3