Search in sources :

Example 21 with Expression

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

the class AccessNode method minimizeProject.

public void minimizeProject(Command atomicCommand) {
    if (!(atomicCommand instanceof Query)) {
        return;
    }
    Query query = (Query) atomicCommand;
    Select select = query.getSelect();
    List<Expression> symbols = select.getSymbols();
    if (symbols.size() == 1) {
        return;
    }
    boolean shouldProject = false;
    LinkedHashMap<Expression, Integer> uniqueSymbols = new LinkedHashMap<Expression, Integer>();
    projection = new Object[symbols.size()];
    this.originalSelect = new ArrayList<Expression>(query.getSelect().getSymbols());
    int i = 0;
    int j = 0;
    for (Iterator<Expression> iter = symbols.iterator(); iter.hasNext(); ) {
        Expression ss = iter.next();
        Expression ex = SymbolMap.getExpression(ss);
        if (ex instanceof Constant) {
            projection[i] = ex;
            if (iter.hasNext() || j != 0) {
                iter.remove();
                shouldProject = true;
            } else {
                projection[i] = j++;
            }
        } else {
            Integer index = uniqueSymbols.get(ex);
            if (index == null) {
                uniqueSymbols.put(ex, j);
                index = j++;
            } else {
                iter.remove();
                shouldProject = true;
            }
            projection[i] = index;
        }
        i++;
    }
    if (!shouldProject) {
        this.projection = NO_PROJECTION;
    } else if (query.getOrderBy() != null) {
        for (OrderByItem item : query.getOrderBy().getOrderByItems()) {
            Integer index = uniqueSymbols.get(SymbolMap.getExpression(item.getSymbol()));
            if (index != null) {
                item.setExpressionPosition(index);
                item.setSymbol(select.getSymbols().get(index));
            }
        }
    }
}
Also used : Query(org.teiid.query.sql.lang.Query) Constant(org.teiid.query.sql.symbol.Constant) OrderByItem(org.teiid.query.sql.lang.OrderByItem) Expression(org.teiid.query.sql.symbol.Expression) Select(org.teiid.query.sql.lang.Select)

Example 22 with Expression

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

the class DependentProcedureCriteriaProcessor method setParam.

private boolean setParam(VariableContext context, Object value, boolean nullAllowed, Reference parameter) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
    if (value instanceof Expression) {
        value = eval.evaluate((Expression) value, null);
    }
    if (value == null && !nullAllowed) {
        return false;
    }
    ElementSymbol parameterSymbol = parameter.getExpression();
    if (context.containsVariable(parameterSymbol)) {
        Object existingValue = context.getValue(parameterSymbol);
        if ((value != null && !value.equals(existingValue)) || (value == null && existingValue != null)) {
            return false;
        }
    }
    context.setValue(parameterSymbol, value);
    return true;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Expression(org.teiid.query.sql.symbol.Expression)

Example 23 with Expression

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

the class GroupingNode method initAccumulator.

static AggregateFunction initAccumulator(AggregateSymbol aggSymbol, RelationalNode node, LinkedHashMap<Expression, Integer> expressionIndexes) {
    int[] argIndexes = new int[aggSymbol.getArgs().length];
    AggregateFunction result = null;
    Expression[] args = aggSymbol.getArgs();
    Class<?>[] inputTypes = new Class[args.length];
    for (int j = 0; j < args.length; j++) {
        inputTypes[j] = args[j].getType();
        argIndexes[j] = getIndex(args[j], expressionIndexes);
    }
    Type function = aggSymbol.getAggregateFunction();
    switch(function) {
        case RANK:
        case DENSE_RANK:
            result = new RankingFunction(function);
            break;
        // same as count(*)
        case ROW_NUMBER:
        case COUNT:
            result = new Count();
            break;
        case SUM:
            result = new Sum();
            break;
        case AVG:
            result = new Avg();
            break;
        case MIN:
            result = new Min();
            break;
        case MAX:
            result = new Max();
            break;
        case XMLAGG:
            result = new XMLAgg();
            break;
        case ARRAY_AGG:
            result = new ArrayAgg();
            break;
        case JSONARRAY_AGG:
            result = new JSONArrayAgg();
            break;
        case TEXTAGG:
            result = new TextAgg((TextLine) args[0]);
            break;
        case STRING_AGG:
            result = new StringAgg(aggSymbol.getType() == DataTypeManager.DefaultDataClasses.BLOB);
            break;
        case FIRST_VALUE:
            result = new FirstLastValue(aggSymbol.getType(), true);
            break;
        case LAST_VALUE:
            result = new FirstLastValue(aggSymbol.getType(), false);
            break;
        case LEAD:
        case LAG:
            result = new LeadLagValue();
            break;
        case USER_DEFINED:
            try {
                result = new UserDefined(aggSymbol.getFunctionDescriptor());
            } catch (FunctionExecutionException e) {
                throw new TeiidRuntimeException(e);
            }
            break;
        default:
            result = new StatsFunction(function);
    }
    if (aggSymbol.getOrderBy() != null) {
        int numOrderByItems = aggSymbol.getOrderBy().getOrderByItems().size();
        List<OrderByItem> orderByItems = new ArrayList<OrderByItem>(numOrderByItems);
        List<ElementSymbol> schema = createSortSchema(result, inputTypes);
        argIndexes = Arrays.copyOf(argIndexes, argIndexes.length + numOrderByItems);
        for (ListIterator<OrderByItem> iterator = aggSymbol.getOrderBy().getOrderByItems().listIterator(); iterator.hasNext(); ) {
            OrderByItem item = iterator.next();
            argIndexes[args.length + iterator.previousIndex()] = getIndex(item.getSymbol(), expressionIndexes);
            ElementSymbol element = new ElementSymbol(String.valueOf(iterator.previousIndex()));
            element.setType(item.getSymbol().getType());
            schema.add(element);
            OrderByItem newItem = item.clone();
            newItem.setSymbol(element);
            orderByItems.add(newItem);
        }
        SortingFilter filter = new SortingFilter(result, node.getBufferManager(), node.getConnectionID(), aggSymbol.isDistinct());
        filter.setElements(schema);
        filter.setSortItems(orderByItems);
        result = filter;
    } else if (aggSymbol.isDistinct()) {
        SortingFilter filter = new SortingFilter(result, node.getBufferManager(), node.getConnectionID(), true);
        List<ElementSymbol> elements = createSortSchema(result, inputTypes);
        filter.setElements(elements);
        result = filter;
    }
    result.setArgIndexes(argIndexes);
    if (aggSymbol.getCondition() != null) {
        result.setConditionIndex(getIndex(aggSymbol.getCondition(), expressionIndexes));
    }
    result.initialize(aggSymbol.getType(), inputTypes);
    return result;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) OrderByItem(org.teiid.query.sql.lang.OrderByItem) ArrayList(java.util.ArrayList) List(java.util.List) TextLine(org.teiid.query.sql.symbol.TextLine) Type(org.teiid.query.sql.symbol.AggregateSymbol.Type) Expression(org.teiid.query.sql.symbol.Expression)

Example 24 with Expression

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

the class GroupingNode method getDescriptionProperties.

public PlanNode getDescriptionProperties() {
    // Default implementation - should be overridden
    PlanNode props = super.getDescriptionProperties();
    if (orderBy != null) {
        int elements = orderBy.size();
        List<String> groupCols = new ArrayList<String>(elements);
        for (int i = 0; i < elements; i++) {
            groupCols.add(this.orderBy.get(i).toString());
        }
        props.addProperty(PROP_GROUP_COLS, groupCols);
    }
    if (outputMapping != null) {
        List<String> groupCols = new ArrayList<String>(outputMapping.asMap().size());
        for (Map.Entry<ElementSymbol, Expression> entry : outputMapping.asMap().entrySet()) {
            groupCols.add(entry.toString());
        }
        props.addProperty(PROP_GROUP_MAPPING, groupCols);
    }
    props.addProperty(PROP_SORT_MODE, String.valueOf(this.removeDuplicates));
    if (rollup) {
        props.addProperty(PROP_ROLLUP, Boolean.TRUE.toString());
    }
    return props;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) PlanNode(org.teiid.client.plan.PlanNode) Expression(org.teiid.query.sql.symbol.Expression) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SymbolMap(org.teiid.query.sql.util.SymbolMap)

Example 25 with Expression

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

the class GroupingNode method initialize.

@Override
public void initialize(CommandContext context, BufferManager bufferManager, ProcessorDataManager dataMgr) {
    super.initialize(context, bufferManager, dataMgr);
    if (this.functions != null) {
        return;
    }
    // Incoming elements and lookup map for evaluating expressions
    List<? extends Expression> sourceElements = this.getChildren()[0].getElements();
    this.elementMap = createLookupMap(sourceElements);
    this.collectedExpressions = new LinkedHashMap<Expression, Integer>();
    // List should contain all grouping columns / expressions as we need those for sorting
    if (this.orderBy != null) {
        for (OrderByItem item : this.orderBy) {
            Expression ex = SymbolMap.getExpression(item.getSymbol());
            getIndex(ex, this.collectedExpressions);
        }
        if (removeDuplicates) {
            for (Expression ses : sourceElements) {
                getIndex(ses, collectedExpressions);
            }
            distinctCols = collectedExpressions.size();
        }
    }
    // Construct aggregate function state accumulators
    functions = new AggregateFunction[getElements().size()][];
    for (int i = 0; i < getElements().size(); i++) {
        Expression symbol = getElements().get(i);
        if (this.outputMapping != null) {
            symbol = outputMapping.getMappedExpression((ElementSymbol) symbol);
        }
        Class<?> outputType = symbol.getType();
        if (symbol instanceof AggregateSymbol) {
            AggregateSymbol aggSymbol = (AggregateSymbol) symbol;
            functions[i] = new AggregateFunction[rollup ? orderBy.size() + 1 : 1];
            for (int j = 0; j < functions[i].length; j++) {
                functions[i][j] = initAccumulator(aggSymbol, this, this.collectedExpressions);
            }
        } else {
            AggregateFunction af = new ConstantFunction();
            af.setArgIndexes(new int[] { this.collectedExpressions.get(symbol) });
            af.initialize(outputType, new Class<?>[] { symbol.getType() });
            functions[i] = new AggregateFunction[] { af };
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) OrderByItem(org.teiid.query.sql.lang.OrderByItem) 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