Search in sources :

Example 11 with QueryProcessingException

use of org.teiid.api.exception.query.QueryProcessingException 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 12 with QueryProcessingException

use of org.teiid.api.exception.query.QueryProcessingException 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)

Aggregations

QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)12 List (java.util.List)6 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)4 Insert (org.teiid.query.sql.lang.Insert)4 Expression (org.teiid.query.sql.symbol.Expression)4 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)4 Test (org.junit.Test)3 TupleSource (org.teiid.common.buffer.TupleSource)3 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)3 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)3 Command (org.teiid.query.sql.lang.Command)3 Constant (org.teiid.query.sql.symbol.Constant)3 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)3 BlockedException (org.teiid.common.buffer.BlockedException)2 Evaluator (org.teiid.query.eval.Evaluator)2 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)2 TempMetadataID (org.teiid.query.metadata.TempMetadataID)2 CollectionTupleSource (org.teiid.query.processor.CollectionTupleSource)2 Create (org.teiid.query.sql.lang.Create)2