Search in sources :

Example 21 with TempMetadataID

use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.

the class TempTable method createIndexTable.

private TempTable createIndexTable(List<ElementSymbol> indexColumns, boolean unique) {
    List<ElementSymbol> allColumns = new ArrayList<ElementSymbol>(indexColumns);
    for (ElementSymbol elementSymbol : columns.subList(0, tree.getKeyLength())) {
        if (allColumns.indexOf(elementSymbol) < 0) {
            allColumns.add(elementSymbol);
        }
    }
    // $NON-NLS-1$
    TempTable indexTable = new TempTable(new TempMetadataID("idx", Collections.EMPTY_LIST), this.bm, allColumns, allColumns.size(), this.sessionID);
    indexTable.setPreferMemory(this.tree.isPreferMemory());
    indexTable.lock = this.lock;
    if (unique) {
        indexTable.uniqueColIndex = indexColumns.size();
    }
    if (indexTables == null) {
        indexTables = new LinkedHashMap<List<ElementSymbol>, TempTable>();
        indexTables.put(indexColumns, indexTable);
    }
    indexTable.setUpdatable(this.updatable);
    return indexTable;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ArrayList(java.util.ArrayList) TempMetadataID(org.teiid.query.metadata.TempMetadataID) SetClauseList(org.teiid.query.sql.lang.SetClauseList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 22 with TempMetadataID

use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.

the class TempTableDataManager method registerQuery.

private TupleSource registerQuery(final CommandContext context, final TempTableStore contextStore, final Query query) {
    final GroupSymbol group = query.getFrom().getGroups().get(0);
    if (!group.isTempGroupSymbol()) {
        return null;
    }
    final String tableName = group.getNonCorrelationName();
    if (group.isGlobalTable()) {
        TempMetadataID matTableId = (TempMetadataID) group.getMetadataID();
        final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
        final MatTableInfo info = globalStore.getMatTableInfo(tableName);
        return new ProxyTupleSource() {

            Future<Void> moreWork = null;

            TupleSource loadingTupleSource;

            DQPWorkContext newWorkContext;

            @Override
            protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
                if (loadingTupleSource != null) {
                    load();
                } else {
                    boolean load = false;
                    if (!info.isUpToDate()) {
                        boolean invalidate = shouldInvalidate(context.getVdb());
                        load = globalStore.needsLoading(tableName, globalStore.getAddress(), true, false, info.isValid() && invalidate);
                        if (load) {
                            load = globalStore.needsLoading(tableName, globalStore.getAddress(), false, false, info.isValid() && invalidate);
                        }
                        if (!load) {
                            synchronized (info) {
                                if (!info.isUpToDate()) {
                                    RequestWorkItem workItem = context.getWorkItem();
                                    info.addWaiter(workItem);
                                    if (moreWork != null) {
                                        moreWork.cancel(false);
                                    }
                                    // fail-safe - attempt again in 10 seconds
                                    moreWork = workItem.scheduleWork(10000);
                                    // $NON-NLS-1$
                                    throw BlockedException.block("Blocking on mat view load", tableName);
                                }
                            }
                        } else {
                            if (!info.isValid() || executor == null) {
                                // TODO: we should probably do all loads using a temp session
                                if (info.getVdbMetaData() != null && context.getDQPWorkContext() != null && !info.getVdbMetaData().getFullName().equals(context.getDQPWorkContext().getVDB().getFullName())) {
                                    assert executor != null;
                                    // load with by pretending we're in the imported vdb
                                    newWorkContext = createWorkContext(context, info.getVdbMetaData());
                                    CommandContext newContext = context.clone();
                                    newContext.setNewVDBState(newWorkContext);
                                    loadingTupleSource = loadGlobalTable(newContext, group, tableName, newContext.getGlobalTableStore());
                                } else {
                                    loadingTupleSource = loadGlobalTable(context, group, tableName, globalStore);
                                }
                                load();
                            } else {
                                loadViaRefresh(context, tableName, context.getDQPWorkContext().getVDB(), info);
                            }
                        }
                    }
                }
                TempTable table = globalStore.getTempTable(tableName);
                context.accessedDataObject(group.getMetadataID());
                if (context.isParallel() && query.getCriteria() == null && query.getOrderBy() != null && table.getRowCount() > MIN_ASYNCH_SIZE) {
                    return new AsyncTupleSource(new Callable<TupleSource>() {

                        @Override
                        public TupleSource call() throws Exception {
                            synchronized (this) {
                                TupleSource result = table.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
                                cancelMoreWork();
                                return result;
                            }
                        }
                    }, context);
                }
                TupleSource result = table.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
                cancelMoreWork();
                return result;
            }

            private void load() throws TeiidComponentException, TeiidProcessingException {
                try {
                    if (newWorkContext != null) {
                        newWorkContext.runInContext(new Callable<Void>() {

                            @Override
                            public Void call() throws Exception {
                                loadingTupleSource.nextTuple();
                                return null;
                            }
                        });
                    } else {
                        loadingTupleSource.nextTuple();
                    }
                } catch (Throwable e) {
                    rethrow(e);
                }
            }

            private void cancelMoreWork() {
                if (moreWork != null) {
                    moreWork.cancel(false);
                    moreWork = null;
                }
            }

            @Override
            public void closeSource() {
                if (loadingTupleSource != null) {
                    loadingTupleSource.closeSource();
                }
                super.closeSource();
                cancelMoreWork();
            }
        };
    }
    // it's not expected for a blocked exception to bubble up from here, so return a tuplesource to perform getOrCreateTempTable
    return new ProxyTupleSource() {

        @Override
        protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
            TempTableStore tts = contextStore;
            TempTable tt = tts.getOrCreateTempTable(tableName, query, bufferManager, true, false, context, group);
            if (context.getDataObjects() != null) {
                Object id = RelationalPlanner.getTrackableGroup(group, context.getMetadata());
                if (id != null) {
                    context.accessedDataObject(id);
                }
            }
            if (context.isParallel() && query.getCriteria() == null && query.getOrderBy() != null && tt.getRowCount() > MIN_ASYNCH_SIZE) {
                return new AsyncTupleSource(new Callable<TupleSource>() {

                    @Override
                    public TupleSource call() throws Exception {
                        synchronized (this) {
                            return tt.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
                        }
                    }
                }, context);
            }
            return tt.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
        }
    };
}
Also used : DQPWorkContext(org.teiid.dqp.internal.process.DQPWorkContext) RequestWorkItem(org.teiid.dqp.internal.process.RequestWorkItem) CommandContext(org.teiid.query.util.CommandContext) TempMetadataID(org.teiid.query.metadata.TempMetadataID) 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) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) MatTableInfo(org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo) Future(java.util.concurrent.Future)

Example 23 with TempMetadataID

use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.

the class TempTableDataManager method handleSystemProcedures.

private TupleSource handleSystemProcedures(final CommandContext context, StoredProcedure proc) throws TeiidComponentException, QueryMetadataException, QueryProcessingException, QueryResolverException, QueryValidatorException, TeiidProcessingException, ExpressionEvaluationException {
    final QueryMetadataInterface metadata = context.getMetadata();
    if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEW)) {
        Object groupID = validateMatView(metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
        TempMetadataID matTableId = context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
        final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
        String matViewName = metadata.getFullName(groupID);
        String matTableName = metadata.getFullName(matTableId);
        // $NON-NLS-1$
        LogManager.logDetail(LogConstants.CTX_MATVIEWS, "processing refreshmatview for", matViewName);
        boolean invalidate = Boolean.TRUE.equals(((Constant) proc.getParameter(3).getExpression()).getValue());
        boolean needsLoading = globalStore.getMatTableInfo(matTableName).getAndClearAsynch();
        if (!needsLoading) {
            needsLoading = globalStore.needsLoading(matTableName, globalStore.getAddress(), true, true, invalidate);
            if (needsLoading) {
                needsLoading = globalStore.needsLoading(matTableName, globalStore.getAddress(), false, false, invalidate);
            }
        }
        if (!needsLoading) {
            return CollectionTupleSource.createUpdateCountTupleSource(-1);
        }
        GroupSymbol matTable = new GroupSymbol(matTableName);
        matTable.setMetadataID(matTableId);
        return loadGlobalTable(context, matTable, matTableName, globalStore);
    } else if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEWROWS)) {
        final Object groupID = validateMatView(metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
        TempMetadataID matTableId = context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
        final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
        Object pk = metadata.getPrimaryKey(groupID);
        String matViewName = metadata.getFullName(groupID);
        if (pk == null) {
            throw new QueryProcessingException(QueryPlugin.Event.TEIID30230, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30230, matViewName));
        }
        List<?> ids = metadata.getElementIDsInKey(pk);
        Object[][] params = (Object[][]) ((ArrayImpl) ((Constant) proc.getParameter(3).getExpression()).getValue()).getValues();
        return updateMatviewRows(context, metadata, groupID, globalStore, matViewName, ids, params);
    } else if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEWROW)) {
        final Object groupID = validateMatView(metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
        TempMetadataID matTableId = context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
        final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
        Object pk = metadata.getPrimaryKey(groupID);
        final String matViewName = metadata.getFullName(groupID);
        if (pk == null) {
            throw new QueryProcessingException(QueryPlugin.Event.TEIID30230, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30230, matViewName));
        }
        List<?> ids = metadata.getElementIDsInKey(pk);
        Constant key = (Constant) proc.getParameter(3).getExpression();
        Object initialValue = key.getValue();
        SPParameter keyOther = proc.getParameter(4);
        Object[] param = null;
        if (keyOther != null) {
            Object[] otherCols = ((ArrayImpl) ((Constant) keyOther.getExpression()).getValue()).getValues();
            if (otherCols != null) {
                param = new Object[1 + otherCols.length];
                param[0] = initialValue;
                for (int i = 0; i < otherCols.length; i++) {
                    param[i + 1] = otherCols[i];
                }
            }
        }
        if (param == null) {
            param = new Object[] { initialValue };
        }
        Object[][] params = new Object[][] { param };
        return updateMatviewRows(context, metadata, groupID, globalStore, matViewName, ids, params);
    }
    return null;
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) ArrayImpl(org.teiid.core.types.ArrayImpl) TempMetadataID(org.teiid.query.metadata.TempMetadataID) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Example 24 with TempMetadataID

use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.

the class TempTableDataManager method lookupCodeValue.

public Object lookupCodeValue(CommandContext context, String codeTableName, String returnElementName, String keyElementName, Object keyValue) throws BlockedException, TeiidComponentException, TeiidProcessingException {
    // we are not using a resolved form of a lookup, so we canonicallize with upper case
    codeTableName = codeTableName.toUpperCase();
    keyElementName = keyElementName.toUpperCase();
    returnElementName = returnElementName.toUpperCase();
    String matTableName = CODE_PREFIX + codeTableName + ElementSymbol.SEPARATOR + keyElementName + ElementSymbol.SEPARATOR + returnElementName;
    TupleSource ts = context.getCodeLookup(matTableName, keyValue);
    if (ts == null) {
        QueryMetadataInterface metadata = context.getMetadata();
        TempMetadataID id = context.getGlobalTableStore().getCodeTableMetadataId(codeTableName, returnElementName, keyElementName, matTableName);
        ElementSymbol keyElement = new ElementSymbol(keyElementName, new GroupSymbol(matTableName));
        ElementSymbol returnElement = new ElementSymbol(returnElementName, new GroupSymbol(matTableName));
        keyElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + keyElementName))));
        returnElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + returnElementName))));
        Query query = RelationalPlanner.createMatViewQuery(id, matTableName, Arrays.asList(returnElement), true);
        query.setCriteria(new CompareCriteria(keyElement, CompareCriteria.EQ, new Constant(keyValue)));
        ts = registerQuery(context, context.getTempTableStore(), query);
    }
    try {
        List<?> row = ts.nextTuple();
        Object result = null;
        if (row != null) {
            result = row.get(0);
        }
        ts.closeSource();
        return result;
    } catch (BlockedException e) {
        context.putCodeLookup(matTableName, keyValue, ts);
        throw e;
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) Constant(org.teiid.query.sql.symbol.Constant) TempMetadataID(org.teiid.query.metadata.TempMetadataID) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) BlockedException(org.teiid.common.buffer.BlockedException)

Example 25 with TempMetadataID

use of org.teiid.query.metadata.TempMetadataID in project teiid by teiid.

the class TempTableStore method getOrCreateTempTable.

TempTable getOrCreateTempTable(String tempTableID, Command command, BufferManager buffer, boolean delegate, boolean forUpdate, CommandContext context, GroupSymbol group) throws TeiidProcessingException, BlockedException, TeiidComponentException {
    if (!(group.getMetadataID() instanceof TempMetadataID)) {
        // TODO: use a proper metadata
        TempTableStore tts = context.getSessionTempTableStore();
        context.setDeterminismLevel(Determinism.SESSION_DETERMINISTIC);
        if (tts.getTempTable(tempTableID) == null) {
            // implicitly create global (session scoped) temp table
            // $NON-NLS-1$
            LogManager.logDetail(LogConstants.CTX_DQP, "binding global temp table to session", group);
            QueryMetadataInterface metadata = context.getMetadata();
            Create create = GlobalTableStoreImpl.getCreateCommand(group, false, metadata);
            tts.addTempTable(tempTableID, create, buffer, true, context);
        }
        return getTempTable(tempTableID, command, buffer, delegate, forUpdate, context);
    }
    TempTable tempTable = getTempTable(tempTableID, command, buffer, delegate, forUpdate, context);
    if (tempTable != null) {
        if (processors != null) {
            TableProcessor withProcessor = processors.get(tempTableID);
            if (withProcessor != null) {
                TempTable tt = withProcessor.process(tempTable);
                if (tt != tempTable) {
                    return tt;
                }
                processors.remove(tempTableID);
            }
        }
        return tempTable;
    }
    // allow implicit temp group definition
    List<ElementSymbol> columns = null;
    if (command instanceof Insert) {
        Insert insert = (Insert) command;
        if (group.isImplicitTempGroupSymbol()) {
            columns = insert.getVariables();
        }
    }
    if (columns == null) {
        if (processors != null) {
            TableProcessor withProcessor = processors.get(tempTableID);
            if (withProcessor != null) {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, "Creating temporary table for with clause", tempTableID);
                Create create = new Create();
                create.setTable(new GroupSymbol(tempTableID));
                create.setElementSymbolsAsColumns(withProcessor.columns);
                withProcessor.alterCreate(create);
                tempTable = addTempTable(tempTableID, create, buffer, true, context);
                TempTable tt = withProcessor.process(tempTable);
                if (tt != tempTable) {
                    return tt;
                }
                processors.remove(tempTableID);
                return tempTable;
            }
        }
        if (delegate && this.parentTempTableStore != null) {
            // may be a cte from a higher scope that needs to have creation triggered
            return parentTempTableStore.getOrCreateTempTable(tempTableID, command, buffer, delegate, forUpdate, context, group);
        }
        throw new QueryProcessingException(QueryPlugin.Event.TEIID30226, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30226, tempTableID));
    }
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_DQP, "Creating temporary table", tempTableID);
    Create create = new Create();
    create.setTable(new GroupSymbol(tempTableID));
    create.setElementSymbolsAsColumns(columns);
    return addTempTable(tempTableID, create, buffer, true, context);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Create(org.teiid.query.sql.lang.Create) TempMetadataID(org.teiid.query.metadata.TempMetadataID) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) Insert(org.teiid.query.sql.lang.Insert) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Aggregations

TempMetadataID (org.teiid.query.metadata.TempMetadataID)48 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)21 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)16 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)12 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Test (org.junit.Test)7 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)6 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)6 HashSet (java.util.HashSet)5 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)5 TeiidComponentException (org.teiid.core.TeiidComponentException)5 TeiidProcessingException (org.teiid.core.TeiidProcessingException)5 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)5 LanguageObject (org.teiid.query.sql.LanguageObject)4 CacheHint (org.teiid.query.sql.lang.CacheHint)4 Expression (org.teiid.query.sql.symbol.Expression)4 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)3 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)3