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;
}
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);
}
}
Aggregations