use of org.teiid.metadata.FunctionMethod in project teiid by teiid.
the class ODataFilterVisitor method visit.
@Override
public void visit(Function obj) {
if (this.ef.getFunctionModifiers().containsKey(obj.getName())) {
List<?> parts = this.ef.getFunctionModifiers().get(obj.getName()).translate(obj);
if (parts != null) {
// $NON-NLS-1$
throw new AssertionError("not supported");
}
}
String name = obj.getName();
List<Expression> args = obj.getParameters();
if (isInfixFunction(name)) {
this.filter.append(Tokens.LPAREN);
if (args != null) {
for (int i = 0; i < args.size(); i++) {
append(args.get(i));
if (i < (args.size() - 1)) {
this.filter.append(Tokens.SPACE);
this.filter.append(infixFunctions.get(name));
this.filter.append(Tokens.SPACE);
}
}
}
this.filter.append(Tokens.RPAREN);
} else {
FunctionMethod method = obj.getMetadataObject();
if (name.startsWith(method.getCategory())) {
name = name.substring(method.getCategory().length() + 1);
}
this.filter.append(name).append(Tokens.LPAREN);
if (name.equals("cast")) {
append(args.get(0));
this.filter.append(Tokens.COMMA);
Literal literal = (Literal) args.get(1);
String type = ODataTypeManager.odataType((String) literal.getValue()).getFullQualifiedName().getFullQualifiedNameAsString();
this.filter.append(type);
} else {
if (args != null && args.size() != 0) {
if (SourceSystemFunctions.ENDSWITH.equalsIgnoreCase(name)) {
append(args.get(1));
this.filter.append(Tokens.COMMA);
append(args.get(0));
} else {
BaseColumn old = currentExpression;
for (int i = 0; i < args.size(); i++) {
currentExpression = method.getInputParameters().get(Math.min(i, method.getInputParameters().size() - 1));
append(args.get(i));
if (i < args.size() - 1) {
this.filter.append(Tokens.COMMA);
}
}
currentExpression = old;
}
}
}
this.filter.append(Tokens.RPAREN);
}
}
use of org.teiid.metadata.FunctionMethod in project teiid by teiid.
the class TestVisitors method exampleSalesforce.
public static QueryMetadataInterface exampleSalesforce() {
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("SalesforceModel");
MetadataFactory mf = new MetadataFactory("sf", 1, SystemMetadata.getInstance().getRuntimeTypeMap(), mmd);
mf.setParser(new QueryParser());
// load the metadata as captured from 8.9 on 9/3/2014
mf.parse(new FileReader(UnitTestUtil.getTestDataFile("sf.ddl")));
SalesForceExecutionFactory factory = new SalesForceExecutionFactory();
factory.start();
for (FunctionMethod func : factory.getPushDownFunctions()) {
mf.addFunction(func);
}
SalesForceMetadataProcessor.addProcedrues(mf);
// Create Contacts group - which has different name in sources
// $NON-NLS-1$
Table contactTable = RealMetadataFactory.createPhysicalGroup("Contacts", mf.getSchema());
// $NON-NLS-1$
contactTable.setNameInSource("Contact");
// $NON-NLS-1$
contactTable.setProperty("Supports Query", Boolean.TRUE.toString());
// Create Contact Columns
String[] elemNames = new String[] { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"ContactID", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"Name", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"AccountId", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"InitialContact", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"LastTime" };
String[] elemTypes = new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.TIMESTAMP, DataTypeManager.DefaultDataTypes.TIME };
List<Column> contactCols = RealMetadataFactory.createElements(contactTable, elemNames, elemTypes);
// Set name in source on each column
String[] contactNameInSource = new String[] { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"id", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"ContactName", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"accountid", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"InitialContact", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"LastTime" };
for (int i = 0; i < 2; i++) {
Column obj = contactCols.get(i);
obj.setNameInSource(contactNameInSource[i]);
}
// add a procedure with a native query property
List<ProcedureParameter> params = new LinkedList<ProcedureParameter>();
params.add(RealMetadataFactory.createParameter("x", SPParameter.IN, TypeFacility.RUNTIME_NAMES.STRING));
Procedure nativeProc = RealMetadataFactory.createStoredProcedure("foo", mf.getSchema(), params);
nativeProc.setProperty(SQLStringVisitor.TEIID_NATIVE_QUERY, "search;select accountname from account where accountid = $1");
nativeProc.setResultSet(RealMetadataFactory.createResultSet("rs", new String[] { "accountname" }, new String[] { TypeFacility.RUNTIME_NAMES.STRING }));
TransformationMetadata tm = RealMetadataFactory.createTransformationMetadata(mf.asMetadataStore(), "x");
ValidatorReport report = new MetadataValidator().validate(tm.getVdbMetaData(), tm.getMetadataStore());
if (report.hasItems()) {
throw new RuntimeException(report.getFailureMessage());
}
return tm;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.teiid.metadata.FunctionMethod in project teiid by teiid.
the class FunctionLibrary method getBuiltInAggregateFunctions.
/**
* Return a list of the most general forms of built-in aggregate functions.
* <br/>count(*) - is not included
* <br/>textagg - is not included due to its non standard syntax
*
* @param includeAnalytic - true to include analytic functions that must be windowed
* @return
*/
public List<FunctionMethod> getBuiltInAggregateFunctions(boolean includeAnalytic) {
ArrayList<FunctionMethod> result = new ArrayList<FunctionMethod>();
if (this.systemFunctions != null) {
FunctionDescriptor stExtent = this.systemFunctions.getFunction(SourceSystemFunctions.ST_EXTENT, new Class[] { DataTypeManager.DefaultDataClasses.GEOMETRY });
result.add(stExtent.getMethod());
}
for (Type type : AggregateSymbol.Type.values()) {
AggregateAttributes aa = new AggregateAttributes();
String returnType = null;
String[] argTypes = null;
aa.setAllowsDistinct(true);
switch(type) {
case TEXTAGG:
case USER_DEFINED:
continue;
case DENSE_RANK:
case RANK:
case ROW_NUMBER:
if (!includeAnalytic) {
continue;
}
aa.setAllowsDistinct(false);
aa.setAnalytic(true);
returnType = DataTypeManager.DefaultDataTypes.INTEGER;
argTypes = new String[] {};
break;
case ANY:
case SOME:
case EVERY:
returnType = DataTypeManager.DefaultDataTypes.BOOLEAN;
argTypes = new String[] { DataTypeManager.DefaultDataTypes.BOOLEAN };
break;
case COUNT:
returnType = DataTypeManager.DefaultDataTypes.INTEGER;
argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT };
break;
case MAX:
case MIN:
case AVG:
case SUM:
returnType = DataTypeManager.DefaultDataTypes.OBJECT;
argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT };
break;
case STDDEV_POP:
case STDDEV_SAMP:
case VAR_POP:
case VAR_SAMP:
returnType = DataTypeManager.DefaultDataTypes.DOUBLE;
argTypes = new String[] { DataTypeManager.DefaultDataTypes.DOUBLE };
break;
case STRING_AGG:
returnType = DataTypeManager.DefaultDataTypes.OBJECT;
argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT };
aa.setAllowsOrderBy(true);
break;
case ARRAY_AGG:
returnType = DataTypeManager.DefaultDataTypes.OBJECT;
argTypes = new String[] { DataTypeManager.getDataTypeName(DataTypeManager.getArrayType(DataTypeManager.DefaultDataClasses.OBJECT)) };
aa.setAllowsOrderBy(true);
aa.setAllowsDistinct(false);
break;
case JSONARRAY_AGG:
returnType = DataTypeManager.DefaultDataTypes.CLOB;
argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT };
aa.setAllowsOrderBy(true);
aa.setAllowsDistinct(false);
break;
case XMLAGG:
returnType = DataTypeManager.DefaultDataTypes.XML;
argTypes = new String[] { DataTypeManager.DefaultDataTypes.XML };
aa.setAllowsOrderBy(true);
aa.setAllowsDistinct(false);
break;
case FIRST_VALUE:
case LAST_VALUE:
if (!includeAnalytic) {
continue;
}
aa.setAllowsDistinct(false);
aa.setAnalytic(true);
returnType = DataTypeManager.DefaultDataTypes.OBJECT;
argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT };
break;
case LEAD:
case LAG:
if (!includeAnalytic) {
continue;
}
aa.setAllowsDistinct(false);
aa.setAnalytic(true);
returnType = DataTypeManager.DefaultDataTypes.OBJECT;
argTypes = new String[] { DataTypeManager.DefaultDataTypes.OBJECT, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.OBJECT };
break;
}
FunctionMethod fm = FunctionMethod.createFunctionMethod(type.name(), type.name(), FunctionCategoryConstants.AGGREGATE, returnType, argTypes);
fm.setAggregateAttributes(aa);
result.add(fm);
}
return result;
}
use of org.teiid.metadata.FunctionMethod in project teiid by teiid.
the class FunctionLibrary method determineNecessaryConversions.
/**
* Get the conversions that are needed to call the named function with arguments
* of the given type. In the case of an exact match, the list will contain all nulls.
* In other cases the list will contain one or more non-null values where the value
* is a conversion function that can be used to convert to the proper types for
* executing the function.
* @param name Name of function
* @param returnType
* @param args
* @param types Existing types passed to the function
* @throws InvalidFunctionException
* @throws QueryResolverException
*/
public ConversionResult determineNecessaryConversions(String name, Class<?> returnType, Expression[] args, Class<?>[] types, boolean hasUnknownType) throws InvalidFunctionException {
// First find existing functions with same name and same number of parameters
final Collection<FunctionMethod> functionMethods = new LinkedList<FunctionMethod>();
functionMethods.addAll(this.systemFunctions.findFunctionMethods(name, types.length));
if (this.userFunctions != null) {
for (FunctionTree tree : this.userFunctions) {
functionMethods.addAll(tree.findFunctionMethods(name, types.length));
}
}
// Score each match, reject any where types can not be converted implicitly
// Score of current method (lower score means better match with less converts
// Current best score (lower score is best. Higher score results in more implicit conversions
int bestScore = Integer.MAX_VALUE;
boolean ambiguous = false;
FunctionMethod result = null;
boolean isSystem = false;
boolean narrowing = false;
outer: for (FunctionMethod nextMethod : functionMethods) {
int currentScore = 0;
boolean nextNarrowing = false;
final List<FunctionParameter> methodTypes = nextMethod.getInputParameters();
// no implicit conversion is possible
for (int i = 0; i < types.length; i++) {
final String tmpTypeName = methodTypes.get(Math.min(i, methodTypes.size() - 1)).getRuntimeType();
Class<?> targetType = DataTypeManager.getDataTypeClass(tmpTypeName);
Class<?> sourceType = types[i];
if (sourceType == null) {
currentScore++;
continue;
}
if (sourceType.isArray() && targetType.isArray() && sourceType.getComponentType().equals(targetType.getComponentType())) {
currentScore++;
continue;
}
if (sourceType.isArray()) {
if (isVarArgArrayParam(nextMethod, types, i, targetType)) {
// vararg array parameter
continue;
}
// treat the array as object type until proper type handling is added
sourceType = DataTypeManager.DefaultDataClasses.OBJECT;
}
try {
Transform t = getConvertFunctionDescriptor(sourceType, targetType);
if (t != null) {
if (t.isExplicit()) {
if (!(args[i] instanceof Constant) || ResolverUtil.convertConstant(DataTypeManager.getDataTypeName(sourceType), tmpTypeName, (Constant) args[i]) == null) {
continue outer;
}
nextNarrowing = true;
currentScore++;
} else {
currentScore++;
}
}
} catch (InvalidFunctionException e) {
continue outer;
}
}
// If the method is valid match and it is the current best score, capture those values as current best match
if (currentScore > bestScore) {
continue;
}
if (hasUnknownType) {
if (returnType != null) {
try {
Transform t = getConvertFunctionDescriptor(DataTypeManager.getDataTypeClass(nextMethod.getOutputParameter().getRuntimeType()), returnType);
if (t != null) {
if (t.isExplicit()) {
// there still may be a common type, but use any other valid conversion over this one
currentScore += types.length + 1;
nextNarrowing = true;
} else {
currentScore++;
}
}
} catch (InvalidFunctionException e) {
// there still may be a common type, but use any other valid conversion over this one
currentScore += (types.length * types.length);
}
}
}
if (nextNarrowing && result != null && !narrowing) {
continue;
}
boolean useNext = false;
if (!nextNarrowing && narrowing) {
useNext = true;
}
boolean isSystemNext = nextMethod.getParent() == null || INTERNAL_SCHEMAS.contains(nextMethod.getParent().getName());
if ((isSystem && isSystemNext) || (!isSystem && !isSystemNext && result != null)) {
int partCount = partCount(result.getName());
int nextPartCount = partCount(nextMethod.getName());
if (partCount < nextPartCount) {
// this makes us more consistent with the table resolving logic
continue outer;
}
if (nextPartCount < partCount) {
useNext = true;
}
} else if (isSystemNext) {
useNext = true;
}
if (currentScore == bestScore && !useNext) {
ambiguous = true;
boolean useCurrent = false;
List<FunctionParameter> bestParams = result.getInputParameters();
for (int j = 0; j < types.length; j++) {
String t1 = bestParams.get(Math.min(j, bestParams.size() - 1)).getRuntimeType();
String t2 = methodTypes.get((Math.min(j, methodTypes.size() - 1))).getRuntimeType();
if (types[j] == null || t1.equals(t2)) {
continue;
}
String commonType = ResolverUtil.getCommonRuntimeType(new String[] { t1, t2 });
if (commonType == null) {
// still ambiguous
continue outer;
}
if (commonType.equals(t1)) {
if (!useCurrent) {
useNext = true;
}
} else if (commonType.equals(t2)) {
if (!useNext) {
useCurrent = true;
}
} else {
continue outer;
}
}
if (useCurrent) {
// prefer narrower
ambiguous = false;
} else {
String sysName = result.getProperty(FunctionMethod.SYSTEM_NAME, false);
String sysNameOther = nextMethod.getProperty(FunctionMethod.SYSTEM_NAME, false);
if (sysName != null && sysName.equalsIgnoreCase(sysNameOther)) {
ambiguous = false;
}
}
}
if (currentScore < bestScore || useNext) {
ambiguous = false;
if (currentScore == 0 && isSystemNext) {
return new ConversionResult(nextMethod);
}
bestScore = currentScore;
result = nextMethod;
isSystem = isSystemNext;
narrowing = nextNarrowing;
}
}
if (ambiguous) {
throw GENERIC_EXCEPTION;
}
ConversionResult cr = new ConversionResult(result);
if (result != null) {
cr.needsConverion = (bestScore != 0);
}
return cr;
}
use of org.teiid.metadata.FunctionMethod in project teiid by teiid.
the class FunctionTree method createFunctionDescriptor.
private FunctionDescriptor createFunctionDescriptor(FunctionMetadataSource source, FunctionMethod method, Class<?>[] types, boolean system) {
// Get return type
FunctionParameter outputParam = method.getOutputParameter();
Class<?> outputType = null;
if (outputParam != null) {
outputType = DataTypeManager.getDataTypeClass(outputParam.getRuntimeType());
}
List<Class<?>> inputTypes = new ArrayList<Class<?>>(Arrays.asList(types));
boolean hasWrappedArg = false;
if (!system) {
for (int i = 0; i < types.length; i++) {
if (types[i] == DataTypeManager.DefaultDataClasses.VARBINARY) {
hasWrappedArg = true;
inputTypes.set(i, byte[].class);
}
}
}
if (method.isVarArgs()) {
inputTypes.set(inputTypes.size() - 1, DataTypeManager.getArrayType(inputTypes.get(inputTypes.size() - 1)));
}
Method invocationMethod = method.getMethod();
boolean requiresContext = false;
// Defect 20007 - Ignore the invocation method if pushdown is not required.
if (validateClass && (method.getPushdown() == PushDown.CAN_PUSHDOWN || method.getPushdown() == PushDown.CANNOT_PUSHDOWN)) {
if (invocationMethod == null) {
if (method.getInvocationClass() == null || method.getInvocationMethod() == null) {
throw new MetadataException(QueryPlugin.Event.TEIID31123, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31123, method.getName()));
}
try {
Class<?> methodClass = source.getInvocationClass(method.getInvocationClass());
ReflectionHelper helper = new ReflectionHelper(methodClass);
try {
invocationMethod = helper.findBestMethodWithSignature(method.getInvocationMethod(), inputTypes);
} catch (NoSuchMethodException e) {
inputTypes.add(0, CommandContext.class);
invocationMethod = helper.findBestMethodWithSignature(method.getInvocationMethod(), inputTypes);
requiresContext = true;
}
} catch (ClassNotFoundException e) {
throw new MetadataException(QueryPlugin.Event.TEIID30387, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30387, method.getName(), method.getInvocationClass()));
} catch (NoSuchMethodException e) {
throw new MetadataException(QueryPlugin.Event.TEIID30388, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30388, method, method.getInvocationClass(), method.getInvocationMethod()));
} catch (Exception e) {
throw new MetadataException(QueryPlugin.Event.TEIID30389, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30389, method, method.getInvocationClass(), method.getInvocationMethod()));
}
} else {
requiresContext = (invocationMethod.getParameterTypes().length > 0 && org.teiid.CommandContext.class.isAssignableFrom(invocationMethod.getParameterTypes()[0]));
}
if (invocationMethod != null) {
// Check return type is non void
Class<?> methodReturn = invocationMethod.getReturnType();
if (method.getAggregateAttributes() == null && methodReturn.equals(Void.TYPE)) {
throw new MetadataException(QueryPlugin.Event.TEIID30390, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30390, method.getName(), invocationMethod));
}
// Check that method is public
int modifiers = invocationMethod.getModifiers();
if (!Modifier.isPublic(modifiers)) {
throw new MetadataException(QueryPlugin.Event.TEIID30391, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30391, method.getName(), invocationMethod));
}
// Check that method is static
if (!Modifier.isStatic(modifiers)) {
if (method.getAggregateAttributes() == null) {
throw new MetadataException(QueryPlugin.Event.TEIID30392, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30392, method.getName(), invocationMethod));
}
} else if (method.getAggregateAttributes() != null) {
throw new MetadataException(QueryPlugin.Event.TEIID30600, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30600, method.getName(), invocationMethod));
}
if (method.getAggregateAttributes() != null && !(UserDefinedAggregate.class.isAssignableFrom(invocationMethod.getDeclaringClass()))) {
throw new MetadataException(QueryPlugin.Event.TEIID30601, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30601, method.getName(), method.getInvocationClass(), UserDefinedAggregate.class.getName()));
}
try {
// turn off access checks for a small performance boost
invocationMethod.setAccessible(true);
} catch (SecurityException e) {
// just ignore
}
method.setMethod(invocationMethod);
}
}
FunctionDescriptor result = new FunctionDescriptor(method, types, outputType, invocationMethod, requiresContext, source.getClassLoader());
if (validateClass && method.getAggregateAttributes() != null && (method.getPushdown() == PushDown.CAN_PUSHDOWN || method.getPushdown() == PushDown.CANNOT_PUSHDOWN)) {
try {
result.newInstance();
} catch (FunctionExecutionException e) {
// should only happen if the method is null / not found
throw new MetadataException(QueryPlugin.Event.TEIID30387, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30387, method.getName(), method.getInvocationClass()));
}
}
result.setHasWrappedArgs(hasWrappedArg);
return result;
}
Aggregations