Search in sources :

Example 11 with Expression

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

the class InsertResolver method resolveList.

private void resolveList(Collection elements, TempMetadataAdapter metadata, GroupContext externalGroups, Set<GroupSymbol> groups) throws TeiidComponentException, QueryResolverException {
    for (Iterator i = elements.iterator(); i.hasNext(); ) {
        Expression expr = (Expression) i.next();
        ResolverVisitor.resolveLanguageObject(expr, groups, externalGroups, metadata);
    }
}
Also used : Expression(org.teiid.query.sql.symbol.Expression) Iterator(java.util.Iterator)

Example 12 with Expression

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

the class SetQueryResolver method setProjectedTypes.

private void setProjectedTypes(SetQuery setQuery, List<Class<?>> firstProjectTypes, QueryMetadataInterface metadata) throws QueryResolverException {
    for (QueryCommand subCommand : setQuery.getQueryCommands()) {
        if (!(subCommand instanceof SetQuery)) {
            continue;
        }
        SetQuery child = (SetQuery) subCommand;
        List projectedSymbols = child.getProjectedSymbols();
        if (child.getOrderBy() != null) {
            for (int j = 0; j < projectedSymbols.size(); j++) {
                Expression ses = (Expression) projectedSymbols.get(j);
                Class<?> targetType = firstProjectTypes.get(j);
                if (ses.getType() != targetType && orderByContainsVariable(child.getOrderBy(), ses, j)) {
                    String sourceTypeName = DataTypeManager.getDataTypeName(ses.getType());
                    String targetTypeName = DataTypeManager.getDataTypeName(targetType);
                    throw new QueryResolverException(// $NON-NLS-1$
                    QueryPlugin.Util.getString(// $NON-NLS-1$
                    "UnionQueryResolver.type_conversion", new Object[] { ses, sourceTypeName, targetTypeName }));
                }
            }
        }
        child.setProjectedTypes(firstProjectTypes, metadata);
        setProjectedTypes(child, firstProjectTypes, metadata);
    }
}
Also used : SetQuery(org.teiid.query.sql.lang.SetQuery) Expression(org.teiid.query.sql.symbol.Expression) QueryCommand(org.teiid.query.sql.lang.QueryCommand) ArrayList(java.util.ArrayList) List(java.util.List) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 13 with Expression

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

the class SetQueryResolver method checkSymbolTypes.

static void checkSymbolTypes(List<Class<?>> firstProjectTypes, List<Expression> projSymbols) throws QueryResolverException {
    for (int j = 0; j < projSymbols.size(); j++) {
        Class<?> firstProjType = firstProjectTypes.get(j);
        Expression projSymbol = projSymbols.get(j);
        Class<?> projType = projSymbol.getType();
        if (firstProjType == null) {
            if (projType != null) {
                firstProjectTypes.set(j, projType);
            }
            continue;
        }
        if (firstProjType.equals(projType)) {
            continue;
        }
        String sourceType = DataTypeManager.getDataTypeName(firstProjType);
        String targetType = DataTypeManager.getDataTypeName(projType);
        String commonType = ResolverUtil.getCommonRuntimeType(new String[] { sourceType, targetType });
        if (commonType == null) {
            commonType = DataTypeManager.DefaultDataTypes.OBJECT;
        }
        firstProjectTypes.set(j, DataTypeManager.getDataTypeClass(commonType));
    }
}
Also used : Expression(org.teiid.query.sql.symbol.Expression)

Example 14 with Expression

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

the class UpdateProcedureResolver method resolveBlock.

public void resolveBlock(CreateProcedureCommand command, Block block, GroupContext originalExternalGroups, TempMetadataAdapter original) throws QueryResolverException, QueryMetadataException, TeiidComponentException {
    // $NON-NLS-1$
    LogManager.logTrace(org.teiid.logging.LogConstants.CTX_QUERY_RESOLVER, new Object[] { "Resolving block", block });
    // create a new variable and metadata context for this block so that discovered metadata is not visible else where
    TempMetadataStore store = original.getMetadataStore().clone();
    TempMetadataAdapter metadata = new TempMetadataAdapter(original.getMetadata(), store);
    GroupContext externalGroups = new GroupContext(originalExternalGroups, null);
    // create a new variables group for this block
    GroupSymbol variables = ProcedureContainerResolver.addScalarGroup(ProcedureReservedWords.VARIABLES, store, externalGroups, new LinkedList<Expression>());
    for (Statement statement : block.getStatements()) {
        resolveStatement(command, statement, externalGroups, variables, metadata);
    }
    if (block.getExceptionGroup() != null) {
        // create a new variable and metadata context for this block so that discovered metadata is not visible else where
        store = original.getMetadataStore().clone();
        metadata = new TempMetadataAdapter(original.getMetadata(), store);
        externalGroups = new GroupContext(originalExternalGroups, null);
        // create a new variables group for this block
        variables = ProcedureContainerResolver.addScalarGroup(ProcedureReservedWords.VARIABLES, store, externalGroups, new LinkedList<Expression>());
        isValidGroup(metadata, block.getExceptionGroup());
        if (block.getExceptionStatements() != null) {
            ProcedureContainerResolver.addScalarGroup(block.getExceptionGroup(), store, externalGroups, exceptionGroup, false);
            for (Statement statement : block.getExceptionStatements()) {
                resolveStatement(command, statement, externalGroups, variables, metadata);
            }
        }
    }
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) GroupContext(org.teiid.query.sql.lang.GroupContext) LinkedList(java.util.LinkedList)

Example 15 with Expression

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

the class SubqueryAwareEvaluator method evaluateSubquery.

@Override
protected ValueIterator evaluateSubquery(SubqueryContainer<?> container, List<?> tuple) throws TeiidProcessingException, BlockedException, TeiidComponentException {
    ContextReference ref = (ContextReference) container;
    String key = ref.getContextSymbol();
    SubqueryState state = this.subqueries.get(key);
    if (state == null) {
        String otherKey = commands.get(container.getCommand());
        if (otherKey != null) {
            state = this.subqueries.get(otherKey);
            if (state != null) {
                key = otherKey;
            }
        }
    }
    if (state == null) {
        state = new SubqueryState();
        state.plan = container.getCommand().getProcessorPlan().clone();
        if (container.getCommand().getCorrelatedReferences() != null) {
            for (ElementSymbol es : container.getCommand().getCorrelatedReferences().getKeys()) {
                if (DataTypeManager.isNonComparable(DataTypeManager.getDataTypeName(es.getType()))) {
                    state.comparable = false;
                    break;
                }
            }
        }
        this.subqueries.put(key, state);
        this.commands.put(container.getCommand(), key);
    }
    SymbolMap correlatedRefs = container.getCommand().getCorrelatedReferences();
    VariableContext currentContext = null;
    boolean shouldClose = false;
    boolean deterministic = true;
    if (state.processor != null && correlatedRefs != null) {
        Determinism determinism = state.processor.getContext().getDeterminismLevel();
        deterministic = Determinism.COMMAND_DETERMINISTIC.compareTo(determinism) <= 0;
    }
    boolean removeBuffer = true;
    if (correlatedRefs != null) {
        currentContext = new VariableContext();
        for (Map.Entry<ElementSymbol, Expression> entry : container.getCommand().getCorrelatedReferences().asMap().entrySet()) {
            currentContext.setValue(entry.getKey(), evaluate(entry.getValue(), tuple));
        }
        List<Object> refValues = currentContext.getLocalValues();
        if (!refValues.equals(state.refValues)) {
            if (state.comparable && deterministic) {
                if (state.processor != null) {
                    // cache the old value
                    TupleBuffer tb = state.collector.collectTuples();
                    // recheck determinism as the plan may not have been fully processed by the initial check
                    Determinism determinism = state.processor.getContext().getDeterminismLevel();
                    deterministic = Determinism.COMMAND_DETERMINISTIC.compareTo(determinism) <= 0;
                    if (deterministic) {
                        // allowed to track up to 4x the maximum results size
                        maxTuples = Math.max((int) Math.min(Integer.MAX_VALUE, tb.getRowCount() << 2), maxTuples);
                        ArrayList<Object> cacheKey = new ArrayList<Object>(state.refValues);
                        cacheKey.add(key);
                        // ensure that we aren't leaving large last batches in memory
                        tb.saveBatch();
                        this.cache.put(cacheKey, tb);
                        removeBuffer = false;
                        this.currentTuples += tb.getRowCount();
                        while (this.currentTuples > maxTuples && !cache.isEmpty()) {
                            Iterator<Map.Entry<List<?>, TupleBuffer>> i = this.cache.entrySet().iterator();
                            Map.Entry<List<?>, TupleBuffer> entry = i.next();
                            TupleBuffer buffer = entry.getValue();
                            if (buffer.getRowCount() <= 2) {
                                this.smallCache.put(entry.getKey(), buffer);
                            } else {
                                buffer.remove();
                            }
                            this.currentTuples -= buffer.getRowCount();
                            i.remove();
                        }
                    }
                }
                // find if we have cached values
                List<Object> cacheKey = new ArrayList<Object>(refValues);
                cacheKey.add(key);
                TupleBuffer cachedResult = cache.get(cacheKey);
                if (cachedResult == null) {
                    cachedResult = smallCache.get(cacheKey);
                }
                if (cachedResult != null) {
                    state.close(false);
                    return new TupleSourceValueIterator(cachedResult.createIndexedTupleSource(), 0);
                }
            }
            state.refValues = refValues;
            shouldClose = true;
        }
    }
    if (shouldClose || (!deterministic && !state.blocked)) {
        state.close(removeBuffer);
    }
    state.blocked = true;
    if (state.processor == null) {
        CommandContext subContext = context.clone();
        state.plan.reset();
        state.processor = new QueryProcessor(state.plan, subContext, manager, this.dataMgr);
        if (currentContext != null) {
            state.processor.getContext().pushVariableContext(currentContext);
        }
        state.collector = state.processor.createBatchCollector();
    }
    TupleSourceValueIterator iter = new TupleSourceValueIterator(state.collector.collectTuples().createIndexedTupleSource(), 0);
    state.blocked = false;
    return iter;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Determinism(org.teiid.metadata.FunctionMethod.Determinism) CommandContext(org.teiid.query.util.CommandContext) ContextReference(org.teiid.query.sql.symbol.ContextReference) TupleBuffer(org.teiid.common.buffer.TupleBuffer) ArrayList(java.util.ArrayList) SymbolMap(org.teiid.query.sql.util.SymbolMap) VariableContext(org.teiid.query.sql.util.VariableContext) QueryProcessor(org.teiid.query.processor.QueryProcessor) Expression(org.teiid.query.sql.symbol.Expression) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) SymbolMap(org.teiid.query.sql.util.SymbolMap)

Aggregations

Expression (org.teiid.query.sql.symbol.Expression)257 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)104 ArrayList (java.util.ArrayList)74 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)54 Test (org.junit.Test)53 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)50 List (java.util.List)49 Constant (org.teiid.query.sql.symbol.Constant)38 SymbolMap (org.teiid.query.sql.util.SymbolMap)37 HashMap (java.util.HashMap)22 LinkedList (java.util.LinkedList)22 Criteria (org.teiid.query.sql.lang.Criteria)22 Map (java.util.Map)21 ClobType (org.teiid.core.types.ClobType)16 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)16 HashSet (java.util.HashSet)13 LinkedHashSet (java.util.LinkedHashSet)13 AliasSymbol (org.teiid.query.sql.symbol.AliasSymbol)13 SearchedCaseExpression (org.teiid.query.sql.symbol.SearchedCaseExpression)13 OrderBy (org.teiid.query.sql.lang.OrderBy)12