use of org.teiid.query.sql.symbol.ExpressionSymbol in project teiid by teiid.
the class TestAliasSymbol method testClone.
@Test
public void testClone() {
// $NON-NLS-1$ //$NON-NLS-2$
AliasSymbol a1 = new AliasSymbol("X", new ExpressionSymbol("x", new Constant(1)));
// $NON-NLS-1$
a1.setOutputName("foo");
AliasSymbol clone = (AliasSymbol) a1.clone();
assertEquals(a1, clone);
assertEquals(a1.getOutputName(), clone.getOutputName());
}
use of org.teiid.query.sql.symbol.ExpressionSymbol in project teiid by teiid.
the class TestStaticSymbolMappingVisitor method testVisitSelect4.
public void testVisitSelect4() {
Select select = new Select();
select.addSymbol(new ExpressionSymbol("x", // $NON-NLS-1$ //$NON-NLS-2$
new Function("length", new Expression[] { exampleElement(true, 0) })));
// $NON-NLS-1$
select.addSymbol(new MultipleElementSymbol("abc.*"));
select.addSymbol(exampleElement(true, 1));
helpTest(select, getSymbolMap());
}
use of org.teiid.query.sql.symbol.ExpressionSymbol in project teiid by teiid.
the class TestProjectNode method testProjectExpressionFunctionFails.
@Test
public void testProjectExpressionFunctionFails() throws Exception {
// $NON-NLS-1$
ElementSymbol es1 = new ElementSymbol("e1");
es1.setType(DataTypeManager.DefaultDataClasses.STRING);
List elements = new ArrayList();
elements.add(es1);
// $NON-NLS-1$ //$NON-NLS-2$
Function func = new Function("convert", new Expression[] { es1, new Constant("integer") });
// $NON-NLS-1$
FunctionDescriptor fd = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("convert", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING });
func.setFunctionDescriptor(fd);
func.setType(DataTypeManager.DefaultDataClasses.INTEGER);
// $NON-NLS-1$
ExpressionSymbol expr = new ExpressionSymbol("expr", func);
List projectElements = new ArrayList();
projectElements.add(expr);
List[] data = new List[] { // $NON-NLS-1$
Arrays.asList(new Object[] { "1" }), // $NON-NLS-1$
Arrays.asList(new Object[] { "2x" }) };
// $NON-NLS-1$
String expectedMessage = "TEIID30328 UNABLE TO EVALUATE CONVERT(E1, INTEGER): TEIID30384 ERROR WHILE EVALUATING FUNCTION CONVERT";
helpTestProjectFails(projectElements, data, elements, expectedMessage);
}
use of org.teiid.query.sql.symbol.ExpressionSymbol in project teiid by teiid.
the class TestCapabilitiesUtil method testSupportsFunctionInGroupBy.
/**
* Supports functions in group by is misleading. It should actually
* be called supports expression in group by. Thus the example below
* is not supported.
*/
@Test
public void testSupportsFunctionInGroupBy() throws Exception {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Function f = new Function("concat", new Expression[] { new Constant("a"), new Constant("b") });
// $NON-NLS-1$
ExpressionSymbol expr = new ExpressionSymbol("e", f);
List cols = new ArrayList();
cols.add(expr);
helpTestSupportsAggregates(false, false, cols);
}
use of org.teiid.query.sql.symbol.ExpressionSymbol 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);
}
Aggregations