use of org.teiid.query.sql.lang.Select in project teiid by teiid.
the class RulePushAggregates method addUnionGroupBy.
private void addUnionGroupBy(List<Expression> groupingExpressions, LinkedHashSet<AggregateSymbol> aggregates, SymbolMap parentMap, QueryMetadataInterface metadata, CapabilitiesFinder capFinder, GroupSymbol group, boolean first, PlanNode planNode, boolean viewOnly, boolean partitioned) throws QueryMetadataException, TeiidComponentException, QueryPlannerException, QueryResolverException {
List<Expression> groupingColumns = LanguageObject.Util.deepClone(groupingExpressions, Expression.class);
// branches other than the first need to have their projected column names updated
if (!first) {
PlanNode sortNode = NodeEditor.findNodePreOrder(planNode, NodeConstants.Types.SORT, NodeConstants.Types.SOURCE);
List<Expression> sortOrder = null;
OrderBy orderBy = null;
if (sortNode != null) {
orderBy = (OrderBy) sortNode.getProperty(Info.SORT_ORDER);
sortOrder = orderBy.getSortKeys();
}
List<Expression> projectCols = FrameUtil.findTopCols(planNode);
List<ElementSymbol> virtualElements = parentMap.getKeys();
for (int i = 0; i < virtualElements.size(); i++) {
ElementSymbol virtualElem = virtualElements.get(i);
Expression projectedSymbol = projectCols.get(i);
if (!Symbol.getShortName(projectedSymbol).equals(Symbol.getShortName(virtualElem))) {
if (sortOrder != null) {
int sortIndex = sortOrder.indexOf(projectedSymbol);
if (sortIndex > -1) {
updateSymbolName(sortOrder, sortIndex, virtualElem, sortOrder.get(sortIndex));
orderBy.getOrderByItems().get(sortIndex).setSymbol(sortOrder.get(sortIndex));
}
}
updateSymbolName(projectCols, i, virtualElem, projectedSymbol);
}
}
}
PlanNode view = RuleDecomposeJoin.createSource(group, planNode, parentMap);
PlanNode projectPlanNode = NodeFactory.getNewNode(NodeConstants.Types.PROJECT);
Select allSymbols = new Select();
for (Expression expr : groupingColumns) {
// $NON-NLS-1$
allSymbols.addSymbol(new ExpressionSymbol("expr", expr));
}
if (viewOnly) {
for (AggregateSymbol agg : aggregates) {
agg = (AggregateSymbol) agg.clone();
if (agg.getAggregateFunction() == Type.COUNT) {
if (isCountStar(agg)) {
// $NON-NLS-1$
allSymbols.addSymbol(new ExpressionSymbol("stagedAgg", new Constant(1)));
} else {
SearchedCaseExpression count = new SearchedCaseExpression(Arrays.asList(new IsNullCriteria(agg.getArg(0))), Arrays.asList(new Constant(Integer.valueOf(0))));
count.setElseExpression(new Constant(Integer.valueOf(1)));
count.setType(DataTypeManager.DefaultDataClasses.INTEGER);
// $NON-NLS-1$
allSymbols.addSymbol(new ExpressionSymbol("stagedAgg", count));
}
} else {
// prior canStage should ensure this is true
assert agg.getArgs().length == 1;
Expression ex = agg.getArg(0);
ex = ResolverUtil.convertExpression(ex, DataTypeManager.getDataTypeName(agg.getType()), metadata);
// $NON-NLS-1$
allSymbols.addSymbol(new ExpressionSymbol("stagedAgg", ex));
}
}
} else {
allSymbols.addSymbols(aggregates);
}
if (first) {
QueryRewriter.makeSelectUnique(allSymbols, false);
}
projectPlanNode.setProperty(NodeConstants.Info.PROJECT_COLS, allSymbols.getSymbols());
projectPlanNode.addGroups(view.getGroups());
view.addAsParent(projectPlanNode);
if (!viewOnly) {
addGroupBy(view, groupingColumns, aggregates, metadata, projectPlanNode.getParent(), capFinder, true, groupingColumns.isEmpty() && (partitioned || containsNullDependent(aggregates)));
}
}
use of org.teiid.query.sql.lang.Select in project teiid by teiid.
the class FrameUtil method convertNode.
// If newGroup == null, this will be performing a straight symbol swap - that is,
// an oldGroup is undergoing a name change and that is the only difference in the
// symbols. In that case, some additional work can be done because we can assume
// that an oldElement isn't being replaced by an expression using elements from
// multiple new groups.
static void convertNode(PlanNode node, GroupSymbol oldGroup, Set<GroupSymbol> newGroups, Map symbolMap, QueryMetadataInterface metadata, boolean rewrite) throws QueryPlannerException {
if (node.getType() == NodeConstants.Types.GROUP) {
correctSymbolMap(symbolMap, node);
}
// Convert expressions from correlated subquery references;
List<SymbolMap> refMaps = node.getAllReferences();
LinkedList<Expression> correlatedExpression = new LinkedList<Expression>();
for (SymbolMap refs : refMaps) {
for (Map.Entry<ElementSymbol, Expression> ref : refs.asUpdatableMap().entrySet()) {
Expression expr = ref.getValue();
Expression convertedExpr = convertExpression(expr, symbolMap);
ref.setValue(convertedExpr);
correlatedExpression.add(convertedExpr);
}
}
// Update groups for current node
Set<GroupSymbol> groups = node.getGroups();
boolean hasOld = groups.remove(oldGroup);
int type = node.getType();
boolean singleMapping = newGroups != null && newGroups.size() == 1;
if (singleMapping) {
if (!hasOld) {
return;
}
groups.addAll(newGroups);
} else if ((type & (NodeConstants.Types.ACCESS | NodeConstants.Types.JOIN | NodeConstants.Types.SOURCE)) == type) {
if (newGroups != null) {
groups.addAll(newGroups);
}
} else {
groups.clear();
}
groups.addAll(GroupsUsedByElementsVisitor.getGroups(correlatedExpression));
if (type == NodeConstants.Types.SELECT) {
Criteria crit = (Criteria) node.getProperty(NodeConstants.Info.SELECT_CRITERIA);
crit = convertCriteria(crit, symbolMap, metadata, rewrite);
node.setProperty(NodeConstants.Info.SELECT_CRITERIA, crit);
if (!singleMapping) {
GroupsUsedByElementsVisitor.getGroups(crit, groups);
}
} else if (type == NodeConstants.Types.PROJECT) {
List<Expression> projectedSymbols = (List<Expression>) node.getProperty(NodeConstants.Info.PROJECT_COLS);
Select select = new Select(projectedSymbols);
ExpressionMappingVisitor.mapExpressions(select, symbolMap);
if (rewrite) {
for (LanguageObject expr : select.getSymbols()) {
rewriteSingleElementSymbol(metadata, (Expression) expr);
}
}
node.setProperty(NodeConstants.Info.PROJECT_COLS, select.getSymbols());
if (!singleMapping) {
GroupsUsedByElementsVisitor.getGroups(select, groups);
}
} else if (type == NodeConstants.Types.JOIN) {
// Convert join criteria property
List<Criteria> joinCrits = (List<Criteria>) node.getProperty(NodeConstants.Info.JOIN_CRITERIA);
if (joinCrits != null && !joinCrits.isEmpty()) {
Criteria crit = new CompoundCriteria(joinCrits);
crit = convertCriteria(crit, symbolMap, metadata, rewrite);
if (crit instanceof CompoundCriteria && ((CompoundCriteria) crit).getOperator() == CompoundCriteria.AND) {
node.setProperty(NodeConstants.Info.JOIN_CRITERIA, ((CompoundCriteria) crit).getCriteria());
} else {
joinCrits = new ArrayList<Criteria>();
joinCrits.add(crit);
node.setProperty(NodeConstants.Info.JOIN_CRITERIA, joinCrits);
}
}
convertAccessPatterns(symbolMap, node);
} else if (type == NodeConstants.Types.SORT) {
OrderBy orderBy = (OrderBy) node.getProperty(NodeConstants.Info.SORT_ORDER);
ExpressionMappingVisitor.mapExpressions(orderBy, symbolMap);
if (rewrite) {
for (OrderByItem item : orderBy.getOrderByItems()) {
rewriteSingleElementSymbol(metadata, item.getSymbol());
}
}
if (!singleMapping) {
GroupsUsedByElementsVisitor.getGroups(orderBy, groups);
}
} else if (type == NodeConstants.Types.GROUP) {
List<Expression> groupCols = (List<Expression>) node.getProperty(NodeConstants.Info.GROUP_COLS);
if (groupCols != null) {
GroupBy groupBy = new GroupBy(groupCols);
ExpressionMappingVisitor.mapExpressions(groupBy, symbolMap);
node.setProperty(NodeConstants.Info.GROUP_COLS, groupBy.getSymbols());
if (!singleMapping) {
GroupsUsedByElementsVisitor.getGroups(groupBy, groups);
}
}
if (!singleMapping) {
// add back the anon group
SymbolMap property = (SymbolMap) node.getProperty(Info.SYMBOL_MAP);
if (!property.asMap().isEmpty()) {
groups.add(property.asMap().keySet().iterator().next().getGroupSymbol());
}
}
} else if (type == NodeConstants.Types.SOURCE || type == NodeConstants.Types.ACCESS) {
convertAccessPatterns(symbolMap, node);
}
}
use of org.teiid.query.sql.lang.Select in project teiid by teiid.
the class TestRulePlaceAccess method testAddAccessPatterns2.
/**
* Tests that any access patterns (a Collection of Collections of
* Object element ids) for a physical group will be found and added
* as a property of an ACCESS node.
*/
public void testAddAccessPatterns2() throws Exception {
Query query = new Query();
From from = new From();
// $NON-NLS-1$
GroupSymbol group = new GroupSymbol("pm4.g2");
from.addGroup(group);
query.setFrom(from);
Select select = new Select();
select.addSymbol(new MultipleElementSymbol());
query.setSelect(select);
// $NON-NLS-1$
group.setMetadataID(METADATA.getGroupID("pm4.g2"));
PlanNode n1 = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
n1.setProperty(NodeConstants.Info.ATOMIC_REQUEST, query);
n1.addGroup(group);
RulePlaceAccess.addAccessPatternsProperty(n1, METADATA);
Collection accessPatterns = (Collection) n1.getProperty(NodeConstants.Info.ACCESS_PATTERNS);
assertNotNull(accessPatterns);
// $NON-NLS-1$
assertTrue("Expected two access patterns, got " + accessPatterns.size(), accessPatterns.size() == 2);
}
use of org.teiid.query.sql.lang.Select in project teiid by teiid.
the class TestLimitParsing method testSetQueryLimit.
@Test
public void testSetQueryLimit() {
Query query = new Query();
Select select = new Select(Arrays.asList(new MultipleElementSymbol()));
// $NON-NLS-1$
From from = new From(Arrays.asList(new UnaryFromClause(new GroupSymbol("a"))));
query.setSelect(select);
query.setFrom(from);
SetQuery setQuery = new SetQuery(Operation.UNION, true, query, query);
setQuery.setLimit(new Limit(new Reference(0), new Reference(1)));
// $NON-NLS-1$ //$NON-NLS-2$
helpTest("Select * from a union all Select * from a limit ?,?", "SELECT * FROM a UNION ALL SELECT * FROM a LIMIT ?, ?", setQuery);
}
use of org.teiid.query.sql.lang.Select in project teiid by teiid.
the class TestLimitParsing method testLimit.
@Test
public void testLimit() {
Query query = new Query();
Select select = new Select(Arrays.asList(new MultipleElementSymbol()));
// $NON-NLS-1$
From from = new From(Arrays.asList(new UnaryFromClause(new GroupSymbol("a"))));
query.setSelect(select);
query.setFrom(from);
query.setLimit(new Limit(null, new Constant(new Integer(100))));
// $NON-NLS-1$ //$NON-NLS-2$
helpTest("Select * from a limit 100", "SELECT * FROM a LIMIT 100", query);
}
Aggregations