Search in sources :

Example 91 with Expression

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

the class TempTable method matchesPkColumn.

@Override
public Object matchesPkColumn(int pkIndex, Expression ex) {
    if (rowId != null) {
        return false;
    }
    if (ex instanceof Array) {
        Array array = (Array) ex;
        List<Expression> exprs = array.getExpressions();
        int toIndex = Math.min(this.getPkLength(), exprs.size());
        int[] indexes = new int[toIndex];
        for (int i = pkIndex; i < toIndex; i++) {
            int index = exprs.indexOf(this.columns.get(i));
            indexes[i] = index;
            if (index == -1) {
                if (i == pkIndex) {
                    return false;
                }
                break;
            }
        }
        return indexes;
    }
    return columns.get(pkIndex).equals(ex);
}
Also used : Array(org.teiid.query.sql.symbol.Array) Expression(org.teiid.query.sql.symbol.Expression) CacheHint(org.teiid.query.sql.lang.CacheHint)

Example 92 with Expression

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

the class TempTable method update.

public TupleSource update(Criteria crit, final SetClauseList update) throws TeiidComponentException, ExpressionEvaluationException, TeiidProcessingException {
    final boolean primaryKeyChangePossible = canChangePrimaryKey(update);
    final TupleBrowser browser = createTupleBrower(crit, OrderBy.ASC);
    UpdateProcessor up = new UpdateProcessor(crit, browser, true) {

        protected TupleBuffer changeSet;

        protected UpdateProcessor changeSetProcessor;

        @Override
        protected void tuplePassed(List tuple) throws BlockedException, TeiidComponentException, TeiidProcessingException {
            List<Object> newTuple = new ArrayList<Object>(tuple);
            for (Map.Entry<ElementSymbol, Expression> entry : update.getClauseMap().entrySet()) {
                newTuple.set(columnMap.get(entry.getKey()), eval.evaluate(entry.getValue(), tuple));
            }
            validateNotNull(newTuple);
            if (primaryKeyChangePossible) {
                browser.removed();
                deleteTuple(tuple);
                if (changeSet == null) {
                    changeSet = bm.createTupleBuffer(columns, sessionID, TupleSourceType.PROCESSOR);
                }
                changeSet.addTuple(newTuple);
            } else {
                browser.update(newTuple);
            }
        }

        @Override
        protected void undo(List<?> tuple) throws TeiidComponentException, TeiidProcessingException {
            if (primaryKeyChangePossible) {
                insertTuple(tuple, false, true);
            } else {
                updateTuple(tuple);
            }
        }

        @Override
        void success() throws TeiidComponentException, ExpressionEvaluationException, TeiidProcessingException {
            // changeSet contains possible updates
            if (primaryKeyChangePossible) {
                changeSet.close();
                if (changeSetProcessor == null) {
                    changeSetProcessor = new InsertUpdateProcessor(changeSet.createIndexedTupleSource(true), false, null, true, false);
                }
                // when this returns, we're up to date
                changeSetProcessor.process();
            }
        }

        @Override
        public void close() {
            super.close();
            changeSetProcessor = null;
            if (changeSet != null) {
                changeSet.remove();
                changeSet = null;
            }
        }
    };
    long updateCount = up.process();
    tid.getTableData().dataModified(updateCount);
    return CollectionTupleSource.createUpdateCountTupleSource((int) Math.min(Integer.MAX_VALUE, updateCount));
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TupleBuffer(org.teiid.common.buffer.TupleBuffer) ArrayList(java.util.ArrayList) Expression(org.teiid.query.sql.symbol.Expression) SetClauseList(org.teiid.query.sql.lang.SetClauseList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TupleBrowser(org.teiid.common.buffer.TupleBrowser)

Example 93 with Expression

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

the class UpdateValidator method internalValidate.

private void internalValidate(Command command, List<ElementSymbol> viewSymbols) throws QueryMetadataException, TeiidComponentException {
    if (!(command instanceof Query)) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0001"), true, true, true);
        return;
    }
    Query query = (Query) command;
    if (query.getFrom() == null || query.getInto() != null) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0001"), true, true, true);
        return;
    }
    if (query.getWith() != null) {
        // $NON-NLS-1$
        String warning = QueryPlugin.Util.getString("ERR.015.012.0002");
        updateReport.handleValidationWarning(warning);
        deleteReport.handleValidationWarning(warning);
        updateInfo.isSimple = false;
    }
    if (query.hasAggregates()) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0006"), true, true, true);
        return;
    }
    if (query.getLimit() != null) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0013"), true, true, true);
        return;
    }
    if (query.getSelect().isDistinct()) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0008"), true, true, true);
        return;
    }
    updateInfo.view = query;
    List<Expression> projectedSymbols = query.getSelect().getProjectedSymbols();
    for (int i = 0; i < projectedSymbols.size(); i++) {
        Expression symbol = projectedSymbols.get(i);
        Expression ex = SymbolMap.getExpression(symbol);
        if (!metadata.elementSupports(viewSymbols.get(i).getMetadataID(), SupportConstants.Element.UPDATE)) {
            continue;
        }
        if (ex instanceof ElementSymbol) {
            ElementSymbol es = (ElementSymbol) ex;
            String groupName = es.getGroupSymbol().getName();
            UpdateMapping info = updateInfo.updatableGroups.get(groupName);
            if (es.getGroupSymbol().getDefinition() != null) {
                ElementSymbol clone = es.clone();
                clone.setOutputName(null);
                clone.getGroupSymbol().setName(clone.getGroupSymbol().getNonCorrelationName());
                clone.getGroupSymbol().setDefinition(null);
                es = clone;
            }
            if (info == null) {
                info = new UpdateMapping();
                info.group = es.getGroupSymbol();
                info.correlatedName = ((ElementSymbol) ex).getGroupSymbol();
                updateInfo.updatableGroups.put(groupName, info);
            }
            // TODO: warn if mapped twice
            info.updatableViewSymbols.put(viewSymbols.get(i), es);
        } else {
            // TODO: look for reversable widening conversions
            // $NON-NLS-1$
            report.handleValidationWarning(QueryPlugin.Util.getString("ERR.015.012.0007", viewSymbols.get(i), symbol));
        }
    }
    if (query.getFrom().getClauses().size() > 1 || (!(query.getFrom().getClauses().get(0) instanceof UnaryFromClause))) {
        // $NON-NLS-1$
        String warning = QueryPlugin.Util.getString("ERR.015.012.0009", query.getFrom());
        updateReport.handleValidationWarning(warning);
        deleteReport.handleValidationWarning(warning);
        updateInfo.isSimple = false;
    }
    List<GroupSymbol> allGroups = query.getFrom().getGroups();
    HashSet<GroupSymbol> keyPreservingGroups = new HashSet<GroupSymbol>();
    ResolverUtil.findKeyPreserved(query, keyPreservingGroups, metadata);
    for (GroupSymbol groupSymbol : keyPreservingGroups) {
        setUpdateFlags(groupSymbol);
    }
    allGroups.removeAll(keyPreservingGroups);
    if (updateInfo.isSimple) {
        if (!allGroups.isEmpty()) {
            setUpdateFlags(allGroups.iterator().next());
        }
    } else {
        for (GroupSymbol groupSymbol : allGroups) {
            UpdateMapping info = updateInfo.updatableGroups.get(groupSymbol.getName());
            if (info == null) {
                // not projected
                continue;
            }
            // $NON-NLS-1$
            String warning = QueryPlugin.Util.getString("ERR.015.012.0004", info.correlatedName);
            report.handleValidationWarning(warning);
        }
    }
    boolean updatable = false;
    boolean insertable = false;
    for (UpdateMapping info : updateInfo.updatableGroups.values()) {
        if (info.updateAllowed) {
            if (!updatable) {
                this.updateInfo.deleteTarget = info;
            } else if (!info.getGroup().equals(this.updateInfo.deleteTarget.getGroup())) {
                // TODO: warning about multiple
                this.updateInfo.deleteTarget = null;
            }
        }
        updatable |= info.updateAllowed;
        insertable |= info.insertAllowed;
    }
    if ((this.updateInfo.insertType == UpdateType.INHERENT && !insertable)) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0015"), false, true, false);
    }
    if (this.updateInfo.updateType == UpdateType.INHERENT && !updatable) {
        // $NON-NLS-1$
        handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0005"), true, false, false);
    }
    if (this.updateInfo.deleteType == UpdateType.INHERENT) {
        if (this.updateInfo.deleteTarget == null) {
            if (this.updateInfo.isSimple && updatable) {
                this.updateInfo.deleteTarget = this.updateInfo.updatableGroups.values().iterator().next();
            } else {
                // $NON-NLS-1$
                handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0014"), false, false, true);
            }
        }
        if (this.updateInfo.deleteTarget != null) {
            GroupSymbol group = this.updateInfo.deleteTarget.group;
            if (!this.updateInfo.isSimple && metadata.getPrimaryKey(group.getMetadataID()) == null && metadata.getUniqueKeysInGroup(group.getMetadataID()).isEmpty()) {
                handleValidationError(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31267, viewSymbols.iterator().next().getGroupSymbol(), group), false, false, true);
            }
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) SetQuery(org.teiid.query.sql.lang.SetQuery) Query(org.teiid.query.sql.lang.Query) UnaryFromClause(org.teiid.query.sql.lang.UnaryFromClause) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) HashSet(java.util.HashSet)

Example 94 with Expression

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

the class CommandBuilder method expandAllSymbol.

/**
 * Convert the "*" in "select * from..." to the list of column names for the data source.
 */
protected void expandAllSymbol(Command command) {
    if (command instanceof Query) {
        Query query = (Query) command;
        Select select = query.getSelect();
        List<Expression> originalSymbols = select.getSymbols();
        List<Expression> expandedSymbols = new ArrayList<Expression>();
        for (Iterator<Expression> i = originalSymbols.iterator(); i.hasNext(); ) {
            Expression next = i.next();
            if (next instanceof MultipleElementSymbol) {
                MultipleElementSymbol allSymbol = (MultipleElementSymbol) next;
                expandedSymbols.addAll(allSymbol.getElementSymbols());
            } else {
                expandedSymbols.add(next);
            }
        }
        select.setSymbols(expandedSymbols);
    }
}
Also used : MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) Query(org.teiid.query.sql.lang.Query) Expression(org.teiid.query.sql.symbol.Expression) Select(org.teiid.query.sql.lang.Select) ArrayList(java.util.ArrayList)

Example 95 with Expression

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

the class SymbolMap method createSymbolMap.

public static final SymbolMap createSymbolMap(List<ElementSymbol> virtualElements, List<? extends Expression> mappedCols) {
    Assertion.assertTrue(virtualElements.size() == mappedCols.size());
    SymbolMap symbolMap = new SymbolMap();
    Iterator<ElementSymbol> keyIter = virtualElements.iterator();
    for (Expression symbol : mappedCols) {
        symbolMap.addMapping(keyIter.next(), symbol);
    }
    return symbolMap;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Expression(org.teiid.query.sql.symbol.Expression)

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