Search in sources :

Example 1 with AggregateSymbol

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

the class ODataSQLBuilder method buildAggregateQuery.

private void buildAggregateQuery(DocumentNode node, Query outerQuery, ExpandDocumentNode expandResource, OrderBy expandOrder, Query query, EdmNavigationProperty navigationProperty) throws TeiidException {
    Select select = query.getSelect();
    Array array = new Array(Object.class, new ArrayList<Expression>(select.getSymbols()));
    select.getSymbols().clear();
    AggregateSymbol symbol = new AggregateSymbol(AggregateSymbol.Type.ARRAY_AGG.name(), false, array);
    select.addSymbol(symbol);
    symbol.setOrderBy(expandOrder);
    Criteria crit = node.buildJoinCriteria(expandResource, navigationProperty);
    if (crit != null) {
        query.setCriteria(Criteria.combineCriteria(crit, query.getCriteria()));
    }
    // else assertion error?
    expandResource.setColumnIndex(outerQuery.getSelect().getCount() + 1);
    ScalarSubquery agg = new ScalarSubquery(query);
    SubqueryHint subqueryHint = new SubqueryHint();
    subqueryHint.setMergeJoin(true);
    agg.setSubqueryHint(subqueryHint);
    outerQuery.getSelect().addSymbol(agg);
}
Also used : Array(org.teiid.query.sql.symbol.Array) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery) Expression(org.teiid.query.sql.symbol.Expression) SubqueryHint(org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint)

Example 2 with AggregateSymbol

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

the class ODataSQLBuilder method selectQuery.

public Query selectQuery() throws TeiidException {
    if (!this.exceptions.isEmpty()) {
        throw this.exceptions.get(0);
    }
    Query query = this.context.buildQuery();
    if (this.countQuery) {
        AggregateSymbol aggregateSymbol = new AggregateSymbol(AggregateSymbol.Type.COUNT.name(), false, null);
        Select select = new Select(Arrays.asList(aggregateSymbol));
        query.setSelect(select);
    } else if (this.orderBy != null) {
        if (this.context.getIterator() != null) {
            // currently this doesn't matter as the ordering can only be based upon the parent entity
            ((AggregateSymbol) ((AliasSymbol) query.getSelect().getSymbol(query.getSelect().getProjectedSymbols().size() - 1)).getSymbol()).setOrderBy(this.orderBy);
        } else {
            query.setOrderBy(this.orderBy);
        }
    }
    if (this.expandOption != null) {
        processExpandOption(this.expandOption, this.context, query, 1, null);
    }
    return query;
}
Also used : AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol)

Example 3 with AggregateSymbol

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

the class ODataExpressionToSQLVisitor method visit.

@Override
public void visit(UriResourceIt info) {
    if (info.getType() instanceof SingletonPrimitiveType) {
        org.teiid.query.sql.symbol.Expression ex = null;
        if (this.ctxQuery.getIterator() == null) {
            String group = this.nameGenerator.getNextGroup();
            GroupSymbol groupSymbol = new GroupSymbol(group);
            StoredProcedure procedure = new StoredProcedure();
            procedure.setProcedureName("arrayiterate");
            // the projected should only be the collection property at this point
            // we may need more checks here to ensure that is valid
            Collection<ProjectedColumn> values = this.ctxQuery.getProjectedColumns().values();
            Assertion.assertTrue(values.size() == 1);
            ProjectedColumn projectedColumn = values.iterator().next();
            ElementSymbol projectedEs = (ElementSymbol) projectedColumn.getExpression();
            List<SPParameter> params = new ArrayList<SPParameter>();
            SPParameter param = new SPParameter(1, SPParameter.IN, "val");
            param.setExpression(projectedEs);
            params.add(param);
            procedure.setParameter(param);
            SubqueryFromClause fromClause = new SubqueryFromClause(group, procedure);
            fromClause.setLateral(true);
            ElementSymbol es = new ElementSymbol("col", groupSymbol);
            String type = ODataTypeManager.teiidType((SingletonPrimitiveType) info.getType(), false);
            Function castFunction = new Function(CAST, new org.teiid.query.sql.symbol.Expression[] { es, new Constant(type) });
            DocumentNode itResource = new DocumentNode();
            org.teiid.query.sql.symbol.Expression clone = (org.teiid.query.sql.symbol.Expression) castFunction.clone();
            AggregateSymbol symbol = new AggregateSymbol(AggregateSymbol.Type.ARRAY_AGG.name(), false, clone);
            AliasSymbol expression = new AliasSymbol(projectedEs.getShortName(), symbol);
            itResource.setFromClause(fromClause);
            itResource.setGroupSymbol(groupSymbol);
            itResource.addProjectedColumn(expression, info.getType(), projectedColumn.getProperty(), true);
            this.ctxQuery.getProjectedColumns().remove(projectedColumn.getExpression());
            this.ctxQuery.setIterator(itResource);
            ex = castFunction;
        } else {
            GroupSymbol groupSymbol = this.ctxQuery.getIterator().getGroupSymbol();
            ElementSymbol es = new ElementSymbol("col", groupSymbol);
            String type = ODataTypeManager.teiidType((SingletonPrimitiveType) info.getType(), false);
            ex = new Function(CAST, new org.teiid.query.sql.symbol.Expression[] { es, new Constant(type) });
        }
        this.stack.push(ex);
    } else {
        boolean ex = true;
        if (this.ctxQuery instanceof ExpandDocumentNode) {
            ExpandDocumentNode node = (ExpandDocumentNode) this.ctxQuery;
            DocumentNode parent = node.getCollectionContext();
            if (parent != null) {
                this.ctxExpression = parent;
                ex = false;
            }
        }
        if (ex) {
            throw new TeiidRuntimeException(new TeiidNotImplementedException(ODataPlugin.Event.TEIID16010, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16010)));
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ProjectedColumn(org.teiid.olingo.ProjectedColumn) Function(org.teiid.query.sql.symbol.Function) SingletonPrimitiveType(org.apache.olingo.commons.core.edm.primitivetype.SingletonPrimitiveType) ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol)

Example 4 with AggregateSymbol

use of org.teiid.query.sql.symbol.AggregateSymbol 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)

Example 5 with AggregateSymbol

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

the class TestAggregateSymbol method testParser3.

public void testParser3() {
    // $NON-NLS-1$
    AggregateSymbol as = new AggregateSymbol(NonReserved.MIN, false, sampleConstant());
    // $NON-NLS-1$
    helpParser(as, "MIN(5)");
}
Also used : AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol)

Aggregations

AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)53 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)27 Test (org.junit.Test)21 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)16 ArrayList (java.util.ArrayList)12 List (java.util.List)11 Expression (org.teiid.query.sql.symbol.Expression)9 OrderBy (org.teiid.query.sql.lang.OrderBy)8 CommandContext (org.teiid.query.util.CommandContext)6 BufferManager (org.teiid.common.buffer.BufferManager)5 SymbolMap (org.teiid.query.sql.util.SymbolMap)5 BigDecimal (java.math.BigDecimal)4 FakeTupleSource (org.teiid.query.processor.FakeTupleSource)4 Constant (org.teiid.query.sql.symbol.Constant)4 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)3 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)3 AliasSymbol (org.teiid.query.sql.symbol.AliasSymbol)3 ExpressionSymbol (org.teiid.query.sql.symbol.ExpressionSymbol)3 Map (java.util.Map)2 AggregateFunction (org.teiid.query.function.aggregate.AggregateFunction)2