use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.
the class MetaDataProcessor method createProjectedSymbolMetadata.
private Map<Integer, Object>[] createProjectedSymbolMetadata(Command originalCommand) throws TeiidComponentException {
Map<Integer, Object>[] columnMetadata;
// Allow command to use temporary metadata
TempMetadataStore tempMetadata = originalCommand.getTemporaryMetadata();
if (tempMetadata != null && tempMetadata.getData().size() > 0) {
TempMetadataAdapter tempFacade = new TempMetadataAdapter(this.metadata, tempMetadata);
this.metadata = tempFacade;
}
List<Expression> projectedSymbols = originalCommand.getProjectedSymbols();
columnMetadata = new Map[projectedSymbols.size()];
Iterator<Expression> symbolIter = projectedSymbols.iterator();
for (int i = 0; symbolIter.hasNext(); i++) {
Expression symbol = symbolIter.next();
String shortColumnName = Symbol.getShortName(Symbol.getOutputName(symbol));
if (symbol instanceof AliasSymbol) {
symbol = ((AliasSymbol) symbol).getSymbol();
}
try {
columnMetadata[i] = createColumnMetadata(shortColumnName, symbol);
} catch (QueryMetadataException e) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30559, e);
}
}
return columnMetadata;
}
use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.
the class LanguageBridgeFactory method translate.
/* Query */
Select translate(Query query) {
With with = translate(query.getWith());
List<Expression> symbols = query.getSelect().getSymbols();
List<DerivedColumn> translatedSymbols = new ArrayList<DerivedColumn>(symbols.size());
for (Iterator<Expression> i = symbols.iterator(); i.hasNext(); ) {
Expression symbol = i.next();
String alias = null;
if (symbol instanceof AliasSymbol) {
alias = ((AliasSymbol) symbol).getOutputName();
symbol = ((AliasSymbol) symbol).getSymbol();
}
org.teiid.language.Expression iExp = translate(symbol);
DerivedColumn selectSymbol = new DerivedColumn(alias, iExp);
translatedSymbols.add(selectSymbol);
}
List<TableReference> items = null;
if (query.getFrom() != null) {
List<FromClause> clauses = query.getFrom().getClauses();
items = new ArrayList<TableReference>(clauses.size());
for (Iterator<FromClause> i = clauses.iterator(); i.hasNext(); ) {
items.add(translate(i.next()));
}
}
Select q = new Select(translatedSymbols, query.getSelect().isDistinct(), items, translate(query.getCriteria()), translate(query.getGroupBy()), translate(query.getHaving()), translate(query.getOrderBy(), false));
q.setLimit(translate(query.getLimit()));
q.setWith(with);
return q;
}
use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.
the class SetQuery method getTypedProjectedSymbols.
public static List<Expression> getTypedProjectedSymbols(List<? extends Expression> acutal, List<Class<?>> projectedTypes, QueryMetadataInterface metadata) {
List<Expression> newProject = new ArrayList<Expression>();
for (int i = 0; i < acutal.size(); i++) {
Expression originalSymbol = acutal.get(i);
Expression symbol = originalSymbol;
Class<?> type = projectedTypes.get(i);
if (symbol.getType() != type) {
symbol = SymbolMap.getExpression(originalSymbol);
try {
symbol = ResolverUtil.convertExpression(symbol, DataTypeManager.getDataTypeName(type), metadata);
} catch (QueryResolverException err) {
throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30447, err);
}
if (originalSymbol instanceof Symbol) {
symbol = new AliasSymbol(Symbol.getShortName(originalSymbol), symbol);
}
}
newProject.add(symbol);
}
return newProject;
}
use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.
the class RuleAssignOutputElements method collectRequiredInputSymbols.
/**
* Collect all required input symbols for a given node. Input symbols
* are any symbols that are required in the processing of this node,
* for instance to create a new element symbol or sort on it, etc.
* @param node Node to collect for
* @param metadata
* @param capFinder
* @throws TeiidComponentException
* @throws QueryMetadataException
*/
private List<Expression> collectRequiredInputSymbols(PlanNode node, QueryMetadataInterface metadata, CapabilitiesFinder capFinder) throws QueryMetadataException, TeiidComponentException {
Set<Expression> requiredSymbols = new LinkedHashSet<Expression>();
Set<Expression> createdSymbols = new HashSet<Expression>();
List<Expression> outputCols = (List<Expression>) node.getProperty(NodeConstants.Info.OUTPUT_COLS);
switch(node.getType()) {
case NodeConstants.Types.PROJECT:
{
List<Expression> projectCols = (List<Expression>) node.getProperty(NodeConstants.Info.PROJECT_COLS);
PlanNode accessParent = NodeEditor.findParent(node, NodeConstants.Types.ACCESS);
PlanNode accessNode = null;
if (accessParent == null) {
// find the direct access node
accessNode = NodeEditor.findNodePreOrder(node, NodeConstants.Types.ACCESS, NodeConstants.Types.SOURCE | NodeConstants.Types.JOIN | NodeConstants.Types.SET_OP | NodeConstants.Types.GROUP);
}
for (Expression ss : projectCols) {
if (ss instanceof AliasSymbol) {
createdSymbols.add(ss);
ss = ((AliasSymbol) ss).getSymbol();
}
if (ss instanceof WindowFunction || ss instanceof ExpressionSymbol) {
createdSymbols.add(ss);
}
if (!pushProjection(node, metadata, capFinder, requiredSymbols, accessParent, accessNode, ss)) {
ElementCollectorVisitor.getElements(ss, requiredSymbols);
}
}
break;
}
case NodeConstants.Types.SELECT:
Criteria selectCriteria = (Criteria) node.getProperty(NodeConstants.Info.SELECT_CRITERIA);
ElementCollectorVisitor.getElements(selectCriteria, requiredSymbols);
break;
case NodeConstants.Types.JOIN:
List<Criteria> crits = (List) node.getProperty(NodeConstants.Info.JOIN_CRITERIA);
if (crits != null) {
for (Criteria joinCriteria : crits) {
ElementCollectorVisitor.getElements(joinCriteria, requiredSymbols);
}
}
break;
case NodeConstants.Types.GROUP:
List<Expression> groupCols = (List<Expression>) node.getProperty(NodeConstants.Info.GROUP_COLS);
PlanNode accessParent = NodeEditor.findParent(node, NodeConstants.Types.ACCESS);
PlanNode accessNode = null;
if (accessParent == null) {
// find the direct access node
accessNode = NodeEditor.findNodePreOrder(node.getFirstChild(), NodeConstants.Types.ACCESS, NodeConstants.Types.SOURCE | NodeConstants.Types.JOIN | NodeConstants.Types.SET_OP | NodeConstants.Types.GROUP);
}
if (groupCols != null) {
for (Expression expression : groupCols) {
if (!pushProjection(node, metadata, capFinder, requiredSymbols, accessParent, accessNode, expression)) {
ElementCollectorVisitor.getElements(expression, requiredSymbols);
}
}
}
SymbolMap symbolMap = (SymbolMap) node.getProperty(NodeConstants.Info.SYMBOL_MAP);
Set<ElementSymbol> usedAggregates = new HashSet<ElementSymbol>();
// Take credit for creating any aggregates that are needed above
for (Expression outputSymbol : outputCols) {
if (!(outputSymbol instanceof ElementSymbol)) {
continue;
}
createdSymbols.add(outputSymbol);
Expression ex = symbolMap.getMappedExpression((ElementSymbol) outputSymbol);
if (ex instanceof AggregateSymbol) {
AggregateSymbol agg = (AggregateSymbol) ex;
Expression[] aggExprs = agg.getArgs();
for (Expression expression : aggExprs) {
if (!pushProjection(node, metadata, capFinder, requiredSymbols, accessParent, accessNode, expression)) {
ElementCollectorVisitor.getElements(expression, requiredSymbols);
}
}
OrderBy orderBy = agg.getOrderBy();
if (orderBy != null) {
ElementCollectorVisitor.getElements(orderBy, requiredSymbols);
}
Expression condition = agg.getCondition();
if (condition != null) {
ElementCollectorVisitor.getElements(condition, requiredSymbols);
}
usedAggregates.add((ElementSymbol) outputSymbol);
}
}
// update the aggs in the symbolmap
for (Map.Entry<ElementSymbol, Expression> entry : new ArrayList<Map.Entry<ElementSymbol, Expression>>(symbolMap.asMap().entrySet())) {
if (entry.getValue() instanceof AggregateSymbol && !usedAggregates.contains(entry.getKey())) {
symbolMap.asUpdatableMap().remove(entry.getKey());
}
}
if (requiredSymbols.isEmpty() && usedAggregates.isEmpty()) {
node.setProperty(Info.IS_OPTIONAL, true);
}
break;
}
// Gather elements from correlated subquery references;
for (SymbolMap refs : node.getAllReferences()) {
for (Expression expr : refs.asMap().values()) {
ElementCollectorVisitor.getElements(expr, requiredSymbols);
}
}
// Add any columns to required that are in this node's output but were not created here
for (Expression currentOutputSymbol : outputCols) {
if (!createdSymbols.contains(currentOutputSymbol) && (finalRun || node.getType() != NodeConstants.Types.PROJECT || currentOutputSymbol instanceof ElementSymbol)) {
requiredSymbols.add(currentOutputSymbol);
}
}
// TODO: this should depend upon whether the expressions are deterministic
if (node.getType() == NodeConstants.Types.PROJECT) {
Set<Expression> expressions = new HashSet<Expression>();
for (Iterator<Expression> iterator = requiredSymbols.iterator(); iterator.hasNext(); ) {
Expression ses = iterator.next();
if (!expressions.add(SymbolMap.getExpression(ses))) {
iterator.remove();
}
}
}
return new ArrayList<Expression>(requiredSymbols);
}
use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.
the class AliasGenerator method visit.
public void visit(Select obj) {
List<Expression> selectSymbols = obj.getSymbols();
LinkedHashMap<Expression, String> symbols = new LinkedHashMap<Expression, String>(selectSymbols.size());
for (int i = 0; i < selectSymbols.size(); i++) {
Expression symbol = selectSymbols.get(i);
visitNode(symbol);
boolean needsAlias = visitor.namingContext.aliasColumns;
// $NON-NLS-1$
String newAlias = "c_" + i;
Expression newSymbol = SymbolMap.getExpression(symbol);
if (newSymbol instanceof ElementSymbol) {
if (!needsAlias) {
newAlias = ((ElementSymbol) newSymbol).getShortName();
} else {
needsAlias &= needsAlias(newAlias, (ElementSymbol) newSymbol);
}
}
symbols.put(symbol, newAlias);
if (visitor.namingContext.aliasColumns && needsAlias) {
newSymbol = new AliasSymbol(Symbol.getShortName(symbol), newSymbol);
((AliasSymbol) newSymbol).setShortName(newAlias);
}
selectSymbols.set(i, newSymbol);
}
visitor.namingContext.currentSymbols = symbols;
}
Aggregations