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