Search in sources :

Example 1 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class SubqueryAwareEvaluator method evaluatePushdown.

/**
 * Implements must pushdown function handling if supported by the source.
 *
 * The basic strategy is to create a dummy subquery to represent the evaluation
 */
@Override
protected Object evaluatePushdown(Function function, List<?> tuple, Object[] values) throws TeiidComponentException, TeiidProcessingException {
    final FunctionDescriptor fd = function.getFunctionDescriptor();
    if (fd.getMethod() == null) {
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID30341, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30341, fd.getFullName()));
    }
    String schema = null;
    if (fd.getMethod().getParent() == null || !fd.getMethod().getParent().isPhysical()) {
        // find a suitable target
        // TODO: do better than a linear search
        VDBMetaData vdb = this.context.getVdb();
        CapabilitiesFinder capabiltiesFinder = this.context.getQueryProcessorFactory().getCapabiltiesFinder();
        for (ModelMetaData mmd : vdb.getModelMetaDatas().values()) {
            if (!mmd.isSource()) {
                continue;
            }
            SourceCapabilities caps = capabiltiesFinder.findCapabilities(mmd.getName());
            if (caps.supportsCapability(Capability.SELECT_WITHOUT_FROM) && caps.supportsFunction(fd.getMethod().getFullName())) {
                schema = mmd.getName();
                break;
            }
        }
        if (schema == null) {
            throw new FunctionExecutionException(QueryPlugin.Event.TEIID30341, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30341, fd.getFullName()));
        }
    } else {
        if (!CapabilitiesUtil.supports(Capability.SELECT_WITHOUT_FROM, fd.getMethod().getParent(), context.getMetadata(), context.getQueryProcessorFactory().getCapabiltiesFinder())) {
            if (elements != null) {
                Integer index = (Integer) elements.get(function);
                if (index != null) {
                    return tuple.get(index.intValue());
                }
            }
            throw new FunctionExecutionException(QueryPlugin.Event.TEIID30341, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30341, fd.getFullName()));
        }
        schema = fd.getSchema();
    }
    ScalarSubquery ss = null;
    if (functionState != null) {
        ss = functionState.get(function);
    }
    Expression[] functionArgs = new Expression[values.length];
    for (int i = 0; i < values.length; i++) {
        functionArgs[i] = new Constant(values[i]);
    }
    if (ss == null) {
        final Query command = new Query();
        Select select = new Select();
        command.setSelect(select);
        Function f = new Function(function.getName(), functionArgs);
        f.setType(function.getType());
        f.setFunctionDescriptor(fd);
        select.addSymbol(f);
        ss = new ScalarSubquery(command);
        SymbolMap correlatedReferences = new SymbolMap();
        Collection<ElementSymbol> elements = ElementCollectorVisitor.getElements(function, true);
        if (!elements.isEmpty()) {
            for (ElementSymbol es : elements) {
                correlatedReferences.addMapping(es, es);
            }
            command.setCorrelatedReferences(correlatedReferences);
        }
        command.setProcessorPlan(new SimpleProcessorPlan(command, schema, fd, Arrays.asList(new Constant(null, fd.getReturnType()))));
    } else {
        ((Function) ((ExpressionSymbol) ss.getCommand().getProjectedSymbols().get(0)).getExpression()).setArgs(functionArgs);
    }
    if (functionState == null) {
        this.functionState = new HashMap<Function, ScalarSubquery>(2);
    }
    functionState.put(function, ss);
    return internalEvaluate(ss, tuple);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery) Query(org.teiid.query.sql.lang.Query) Constant(org.teiid.query.sql.symbol.Constant) SymbolMap(org.teiid.query.sql.util.SymbolMap) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Function(org.teiid.query.sql.symbol.Function) CapabilitiesFinder(org.teiid.query.optimizer.capabilities.CapabilitiesFinder) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) Expression(org.teiid.query.sql.symbol.Expression) VDBMetaData(org.teiid.adminapi.impl.VDBMetaData) Select(org.teiid.query.sql.lang.Select) SourceCapabilities(org.teiid.query.optimizer.capabilities.SourceCapabilities)

Example 2 with Select

use of org.teiid.query.sql.lang.Select 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));
            }
        }
    }
}
Also used : Query(org.teiid.query.sql.lang.Query) Constant(org.teiid.query.sql.symbol.Constant) OrderByItem(org.teiid.query.sql.lang.OrderByItem) Expression(org.teiid.query.sql.symbol.Expression) Select(org.teiid.query.sql.lang.Select)

Example 3 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class TestStaticSymbolMappingVisitor method testVisitSelect2.

public void testVisitSelect2() {
    Select select = new Select();
    MultipleElementSymbol all = new MultipleElementSymbol();
    select.addSymbol(all);
    helpTest(select, getSymbolMap());
}
Also used : MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) Select(org.teiid.query.sql.lang.Select)

Example 4 with Select

use of org.teiid.query.sql.lang.Select 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 5 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class TestStaticSymbolMappingVisitor method testVisitSelect1.

public void testVisitSelect1() {
    Select select = new Select();
    helpTest(select, getSymbolMap());
}
Also used : Select(org.teiid.query.sql.lang.Select)

Aggregations

Select (org.teiid.query.sql.lang.Select)50 Query (org.teiid.query.sql.lang.Query)30 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)26 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)26 From (org.teiid.query.sql.lang.From)25 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)24 Test (org.junit.Test)17 SetQuery (org.teiid.query.sql.lang.SetQuery)17 Constant (org.teiid.query.sql.symbol.Constant)15 UnaryFromClause (org.teiid.query.sql.lang.UnaryFromClause)11 Limit (org.teiid.query.sql.lang.Limit)10 ArrayList (java.util.ArrayList)5 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)5 Reference (org.teiid.query.sql.symbol.Reference)5 Element (org.jdom.Element)4 Expression (org.teiid.query.sql.symbol.Expression)4 List (java.util.List)3 Attribute (org.jdom.Attribute)3 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)3 LanguageObject (org.teiid.query.sql.LanguageObject)3