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