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