Search in sources :

Example 1 with ExpressionSymbol

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());
}
Also used : AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Constant(org.teiid.query.sql.symbol.Constant) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) Test(org.junit.Test)

Example 2 with ExpressionSymbol

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());
}
Also used : Function(org.teiid.query.sql.symbol.Function) MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) Select(org.teiid.query.sql.lang.Select) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol)

Example 3 with ExpressionSymbol

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);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Function(org.teiid.query.sql.symbol.Function) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) Test(org.junit.Test)

Example 4 with ExpressionSymbol

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);
}
Also used : Function(org.teiid.query.sql.symbol.Function) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) Test(org.junit.Test)

Example 5 with ExpressionSymbol

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) OrderBy(org.teiid.query.sql.lang.OrderBy) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) ArrayList(java.util.ArrayList) SymbolMap(org.teiid.query.sql.util.SymbolMap) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) Criteria(org.teiid.query.sql.lang.Criteria) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Expression(org.teiid.query.sql.symbol.Expression) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) SymbolMap(org.teiid.query.sql.util.SymbolMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

ExpressionSymbol (org.teiid.query.sql.symbol.ExpressionSymbol)16 Constant (org.teiid.query.sql.symbol.Constant)10 Expression (org.teiid.query.sql.symbol.Expression)9 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Test (org.junit.Test)6 AliasSymbol (org.teiid.query.sql.symbol.AliasSymbol)5 Function (org.teiid.query.sql.symbol.Function)5 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)4 LinkedHashSet (java.util.LinkedHashSet)3 FunctionDescriptor (org.teiid.query.function.FunctionDescriptor)3 OrderBy (org.teiid.query.sql.lang.OrderBy)3 AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)3 Map (java.util.Map)2 WindowFunction (org.teiid.query.sql.symbol.WindowFunction)2 SymbolMap (org.teiid.query.sql.util.SymbolMap)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1