Search in sources :

Example 1 with Query

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

the class TestSubqueryCompareCriteriaImpl method helpExample.

public static SubqueryCompareCriteria helpExample() {
    // $NON-NLS-1$ //$NON-NLS-2$
    ElementSymbol element = TestElementImpl.helpExample("g1", "e1");
    Query query = TestQueryImpl.helpExample(true);
    SubqueryCompareCriteria scc = new SubqueryCompareCriteria(element, query, AbstractCompareCriteria.GT, SubqueryCompareCriteria.ANY);
    return scc;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) SubqueryCompareCriteria(org.teiid.query.sql.lang.SubqueryCompareCriteria) Query(org.teiid.query.sql.lang.Query)

Example 2 with Query

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

the class TestSubqueryInCriteriaImpl method helpExample.

public static SubquerySetCriteria helpExample() {
    // $NON-NLS-1$ //$NON-NLS-2$
    ElementSymbol element = TestElementImpl.helpExample("g1", "e1");
    Query query = TestQueryImpl.helpExample(true);
    SubquerySetCriteria ssc = new SubquerySetCriteria(element, query);
    ssc.setNegated(true);
    return ssc;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Query(org.teiid.query.sql.lang.Query) SubquerySetCriteria(org.teiid.query.sql.lang.SubquerySetCriteria)

Example 3 with Query

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

the class TeiidServiceHandler method upsertEntity.

@Override
public void upsertEntity(DataRequest request, Entity entity, boolean merge, String entityETag, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
    final ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, true, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
    visitor.visit(request.getUriInfo());
    final EntityCollectionResponse queryResponse;
    try {
        Query query = visitor.selectQuery();
        queryResponse = (EntityCollectionResponse) executeQuery(request, request.isCountRequest(), visitor, query);
    } catch (Exception e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    if (!queryResponse.getEntities().isEmpty()) {
        updateEntity(request, entity, merge, entityETag, response);
    } else {
        createEntity(request, entity, response);
    }
}
Also used : Query(org.teiid.query.sql.lang.Query) URISyntaxException(java.net.URISyntaxException) TransformationException(org.teiid.core.types.TransformationException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) ODataLibraryException(org.apache.olingo.server.api.ODataLibraryException) TeiidException(org.teiid.core.TeiidException) EdmPrimitiveTypeException(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException) SerializerException(org.apache.olingo.server.api.serializer.SerializerException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException)

Example 4 with Query

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

use of org.teiid.query.sql.lang.Query 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)

Aggregations

Query (org.teiid.query.sql.lang.Query)97 Test (org.junit.Test)58 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)31 Select (org.teiid.query.sql.lang.Select)30 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)26 From (org.teiid.query.sql.lang.From)25 Constant (org.teiid.query.sql.symbol.Constant)22 SetQuery (org.teiid.query.sql.lang.SetQuery)21 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)20 UnaryFromClause (org.teiid.query.sql.lang.UnaryFromClause)13 Limit (org.teiid.query.sql.lang.Limit)10 ArrayList (java.util.ArrayList)9 Expression (org.teiid.query.sql.symbol.Expression)9 SQLException (java.sql.SQLException)7 Reference (org.teiid.query.sql.symbol.Reference)7 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)6 ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)5 List (java.util.List)4 ODataLibraryException (org.apache.olingo.server.api.ODataLibraryException)4 TeiidProcessingException (org.teiid.core.TeiidProcessingException)4