use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class ODataExpressionToSQLVisitor method visit.
@Override
public void visit(Literal expr) {
try {
Object value = null;
if (expr.getText() != null && !expr.getText().equalsIgnoreCase("null")) {
String type = expr.getType().getFullQualifiedName().getFullQualifiedNameAsString();
value = ODataTypeManager.parseLiteral(type, expr.getText());
}
if (this.prepared) {
if (value == null) {
this.stack.add(new Constant(value));
} else {
Function ref = new Function(CONVERT, new org.teiid.query.sql.symbol.Expression[] { new Reference(this.params.size()), new Constant(DataTypeManager.getDataTypeName(value.getClass())) });
stack.add(ref);
this.params.add(new SQLParameter(value, JDBCSQLTypeInfo.getSQLTypeFromClass(value.getClass().getName())));
}
} else {
this.stack.add(new Constant(value));
}
} catch (TeiidException e) {
throw new TeiidRuntimeException(e);
}
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class ODataExpressionToSQLVisitor method visit.
@Override
public void visit(Unary expr) {
accept(expr.getOperand());
org.teiid.query.sql.symbol.Expression teiidExpr = this.stack.pop();
switch(expr.getOperator()) {
case MINUS:
this.stack.push(new Function(SourceSystemFunctions.MULTIPLY_OP, new org.teiid.query.sql.symbol.Expression[] { new Constant(-1), teiidExpr }));
break;
case NOT:
this.stack.push(new NotCriteria(new ExpressionCriteria(teiidExpr)));
break;
}
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class ODataExpressionToSQLVisitor method visit.
@Override
public void visit(UriResourceIt info) {
if (info.getType() instanceof SingletonPrimitiveType) {
org.teiid.query.sql.symbol.Expression ex = null;
if (this.ctxQuery.getIterator() == null) {
String group = this.nameGenerator.getNextGroup();
GroupSymbol groupSymbol = new GroupSymbol(group);
StoredProcedure procedure = new StoredProcedure();
procedure.setProcedureName("arrayiterate");
// the projected should only be the collection property at this point
// we may need more checks here to ensure that is valid
Collection<ProjectedColumn> values = this.ctxQuery.getProjectedColumns().values();
Assertion.assertTrue(values.size() == 1);
ProjectedColumn projectedColumn = values.iterator().next();
ElementSymbol projectedEs = (ElementSymbol) projectedColumn.getExpression();
List<SPParameter> params = new ArrayList<SPParameter>();
SPParameter param = new SPParameter(1, SPParameter.IN, "val");
param.setExpression(projectedEs);
params.add(param);
procedure.setParameter(param);
SubqueryFromClause fromClause = new SubqueryFromClause(group, procedure);
fromClause.setLateral(true);
ElementSymbol es = new ElementSymbol("col", groupSymbol);
String type = ODataTypeManager.teiidType((SingletonPrimitiveType) info.getType(), false);
Function castFunction = new Function(CAST, new org.teiid.query.sql.symbol.Expression[] { es, new Constant(type) });
DocumentNode itResource = new DocumentNode();
org.teiid.query.sql.symbol.Expression clone = (org.teiid.query.sql.symbol.Expression) castFunction.clone();
AggregateSymbol symbol = new AggregateSymbol(AggregateSymbol.Type.ARRAY_AGG.name(), false, clone);
AliasSymbol expression = new AliasSymbol(projectedEs.getShortName(), symbol);
itResource.setFromClause(fromClause);
itResource.setGroupSymbol(groupSymbol);
itResource.addProjectedColumn(expression, info.getType(), projectedColumn.getProperty(), true);
this.ctxQuery.getProjectedColumns().remove(projectedColumn.getExpression());
this.ctxQuery.setIterator(itResource);
ex = castFunction;
} else {
GroupSymbol groupSymbol = this.ctxQuery.getIterator().getGroupSymbol();
ElementSymbol es = new ElementSymbol("col", groupSymbol);
String type = ODataTypeManager.teiidType((SingletonPrimitiveType) info.getType(), false);
ex = new Function(CAST, new org.teiid.query.sql.symbol.Expression[] { es, new Constant(type) });
}
this.stack.push(ex);
} else {
boolean ex = true;
if (this.ctxQuery instanceof ExpandDocumentNode) {
ExpandDocumentNode node = (ExpandDocumentNode) this.ctxQuery;
DocumentNode parent = node.getCollectionContext();
if (parent != null) {
this.ctxExpression = parent;
ex = false;
}
}
if (ex) {
throw new TeiidRuntimeException(new TeiidNotImplementedException(ODataPlugin.Event.TEIID16010, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16010)));
}
}
}
use of org.teiid.query.sql.symbol.Function 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.symbol.Function in project teiid by teiid.
the class TestStaticSymbolMappingVisitor method testFunction3.
public void testFunction3() {
// $NON-NLS-1$
Function f1 = new Function("concat", new Expression[] { exampleElement(true, 0), exampleElement(true, 1) });
// $NON-NLS-1$
Function f2 = new Function("length", new Expression[] { f1 });
helpTest(f2, getSymbolMap());
}
Aggregations