Search in sources :

Example 6 with ScalarSubquery

use of org.teiid.query.sql.symbol.ScalarSubquery in project teiid by teiid.

the class RuleMergeCriteria method findSubquery.

public PlannedResult findSubquery(Criteria crit, boolean unnest) throws TeiidComponentException, QueryMetadataException {
    PlannedResult result = new PlannedResult();
    if (crit instanceof SubquerySetCriteria) {
        // convert to the quantified form
        SubquerySetCriteria ssc = (SubquerySetCriteria) crit;
        if (ssc.getSubqueryHint().isNoUnnest()) {
            return result;
        }
        result.not = ssc.isNegated();
        result.type = ssc.getClass();
        crit = new SubqueryCompareCriteria(ssc.getExpression(), ssc.getCommand(), SubqueryCompareCriteria.EQ, SubqueryCompareCriteria.SOME);
        ((SubqueryCompareCriteria) crit).setSubqueryHint(ssc.getSubqueryHint());
    } else if (crit instanceof CompareCriteria) {
        CompareCriteria cc = (CompareCriteria) crit;
        if (cc.getRightExpression() instanceof ScalarSubquery) {
            ScalarSubquery ss = (ScalarSubquery) cc.getRightExpression();
            if (ss.getSubqueryHint().isNoUnnest()) {
                return result;
            }
            result.type = ss.getClass();
            // we can only use a semi-join if we know that 1 row will be present
            if (ss.getCommand() instanceof Query) {
                Query query = (Query) ss.getCommand();
                if (query.getGroupBy() == null && query.hasAggregates()) {
                    crit = new SubqueryCompareCriteria(cc.getLeftExpression(), ss.getCommand(), cc.getOperator(), SubqueryCompareCriteria.SOME);
                    ((SubqueryCompareCriteria) crit).setSubqueryHint(ss.getSubqueryHint());
                }
            }
        }
    }
    if (crit instanceof SubqueryCompareCriteria) {
        SubqueryCompareCriteria scc = (SubqueryCompareCriteria) crit;
        if (scc.getSubqueryHint().isNoUnnest()) {
            return result;
        }
        if (scc.getPredicateQuantifier() != SubqueryCompareCriteria.SOME || // TODO: could add an inline view if not a query
        !(scc.getCommand() instanceof Query)) {
            return result;
        }
        Query query = (Query) scc.getCommand();
        Expression rightExpr = SymbolMap.getExpression(query.getProjectedSymbols().get(0));
        if (result.not && !isNonNull(query, rightExpr)) {
            return result;
        }
        if (result.type == null) {
            result.type = scc.getClass();
        }
        result.mergeJoin = scc.getSubqueryHint().isMergeJoin();
        if (!unnest && !result.mergeJoin) {
            return result;
        }
        result.makeInd = scc.getSubqueryHint().isDepJoin();
        result.query = query;
        result.additionalCritieria = (Criteria) new CompareCriteria(scc.getLeftExpression(), scc.getOperator(), rightExpr).clone();
    }
    if (crit instanceof ExistsCriteria) {
        ExistsCriteria exists = (ExistsCriteria) crit;
        if (exists.getSubqueryHint().isNoUnnest()) {
            return result;
        }
        if (!(exists.getCommand() instanceof Query)) {
            return result;
        }
        result.type = crit.getClass();
        result.not = exists.isNegated();
        // the correlations can only be in where (if no group by or aggregates) or having
        result.mergeJoin = exists.getSubqueryHint().isMergeJoin();
        result.makeInd = exists.getSubqueryHint().isDepJoin();
        if (!unnest && !result.mergeJoin) {
            return result;
        }
        result.query = (Query) exists.getCommand();
    }
    return result;
}
Also used : ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery) Expression(org.teiid.query.sql.symbol.Expression)

Example 7 with ScalarSubquery

use of org.teiid.query.sql.symbol.ScalarSubquery in project teiid by teiid.

the class ODataExpressionToSQLVisitor method visit.

@Override
public void visit(UriResourceNavigation info) {
    try {
        DocumentNode navigationResource = DocumentNode.build((EdmEntityType) info.getType(), info.getKeyPredicates(), this.metadata, this.odata, this.nameGenerator, true, getUriInfo(), this.parseService);
        Query query = new Query();
        query.setSelect(new Select(Arrays.asList(new AggregateSymbol(AggregateSymbol.Type.COUNT.name(), false, null))));
        query.setFrom(new From(Arrays.asList(navigationResource.getFromClause())));
        Criteria criteria = this.ctxQuery.buildJoinCriteria(navigationResource, info.getProperty());
        if (criteria == null) {
            throw new TeiidException(ODataPlugin.Event.TEIID16037, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16037));
        }
        query.setCriteria(criteria);
        this.stack.add(new ScalarSubquery(query));
    } catch (TeiidException e) {
        throw new TeiidRuntimeException(e);
    }
}
Also used : AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TeiidException(org.teiid.core.TeiidException)

Aggregations

ScalarSubquery (org.teiid.query.sql.symbol.ScalarSubquery)7 Expression (org.teiid.query.sql.symbol.Expression)3 AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)2 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)1 VDBMetaData (org.teiid.adminapi.impl.VDBMetaData)1 FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)1 TeiidException (org.teiid.core.TeiidException)1 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)1 Column (org.teiid.metadata.Column)1 ProjectedColumn (org.teiid.olingo.ProjectedColumn)1 FunctionDescriptor (org.teiid.query.function.FunctionDescriptor)1 CapabilitiesFinder (org.teiid.query.optimizer.capabilities.CapabilitiesFinder)1 SourceCapabilities (org.teiid.query.optimizer.capabilities.SourceCapabilities)1 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)1 AccessNode (org.teiid.query.processor.relational.AccessNode)1 SubqueryHint (org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint)1 Query (org.teiid.query.sql.lang.Query)1