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);
}
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;
}
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)));
}
}
}
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 };
}
}
}
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)");
}
Aggregations