use of org.apache.asterix.om.typecomputer.base.IResultTypeComputer in project asterixdb by apache.
the class ExternalFunctionCompilerUtil method getScalarFunctionInfo.
private static IFunctionInfo getScalarFunctionInfo(MetadataTransactionContext txnCtx, Function function) throws MetadataException {
FunctionIdentifier fid = new FunctionIdentifier(function.getDataverseName(), function.getName(), function.getArity());
IResultTypeComputer typeComputer = getResultTypeComputer(txnCtx, function);
List<IAType> arguments = new ArrayList<IAType>();
IAType returnType = null;
List<String> paramTypes = function.getParams();
for (String paramType : paramTypes) {
arguments.add(getTypeInfo(paramType, txnCtx, function));
}
returnType = getTypeInfo(function.getReturnType(), txnCtx, function);
return new ExternalScalarFunctionInfo(fid.getNamespace(), fid.getName(), fid.getArity(), returnType, function.getFunctionBody(), function.getLanguage(), arguments, typeComputer);
}
use of org.apache.asterix.om.typecomputer.base.IResultTypeComputer in project asterixdb by apache.
the class MetadataBuiltinFunctions method addMetadataBuiltinFunctions.
public static void addMetadataBuiltinFunctions() {
BuiltinFunctions.addFunction(BuiltinFunctions.DATASET, new IResultTypeComputer() {
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
if (f.getArguments().size() != 1) {
throw new AlgebricksException("dataset arity is 1, not " + f.getArguments().size());
}
ILogicalExpression a1 = f.getArguments().get(0).getValue();
IAType t1 = (IAType) env.getType(a1);
if (t1.getTypeTag() == ATypeTag.ANY) {
return BuiltinType.ANY;
}
if (t1.getTypeTag() != ATypeTag.STRING) {
throw new AlgebricksException("Illegal type " + t1 + " for dataset() argument.");
}
String datasetArg = ConstantExpressionUtil.getStringConstant(a1);
if (datasetArg == null) {
return BuiltinType.ANY;
}
MetadataProvider metadata = (MetadataProvider) mp;
Pair<String, String> datasetInfo = getDatasetInfo(metadata, datasetArg);
String dataverseName = datasetInfo.first;
String datasetName = datasetInfo.second;
if (dataverseName == null) {
throw new AlgebricksException("Unspecified dataverse!");
}
Dataset dataset = metadata.findDataset(dataverseName, datasetName);
if (dataset == null) {
throw new AlgebricksException("Could not find dataset " + datasetName + " in dataverse " + dataverseName);
}
String tn = dataset.getItemTypeName();
IAType t2 = metadata.findType(dataset.getItemTypeDataverseName(), tn);
if (t2 == null) {
throw new AlgebricksException("No type for dataset " + datasetName);
}
return t2;
}
}, true);
BuiltinFunctions.addPrivateFunction(BuiltinFunctions.FEED_COLLECT, new IResultTypeComputer() {
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
if (f.getArguments().size() != BuiltinFunctions.FEED_COLLECT.getArity()) {
throw new AlgebricksException("Incorrect number of arguments -> arity is " + BuiltinFunctions.FEED_COLLECT.getArity() + ", not " + f.getArguments().size());
}
ILogicalExpression a1 = f.getArguments().get(5).getValue();
IAType t1 = (IAType) env.getType(a1);
if (t1.getTypeTag() == ATypeTag.ANY) {
return BuiltinType.ANY;
}
if (t1.getTypeTag() != ATypeTag.STRING) {
throw new AlgebricksException("Illegal type " + t1 + " for feed-ingest argument.");
}
String typeArg = ConstantExpressionUtil.getStringConstant(a1);
if (typeArg == null) {
return BuiltinType.ANY;
}
MetadataProvider metadata = (MetadataProvider) mp;
Pair<String, String> argInfo = getDatasetInfo(metadata, typeArg);
String dataverseName = argInfo.first;
String typeName = argInfo.second;
if (dataverseName == null) {
throw new AlgebricksException("Unspecified dataverse!");
}
IAType t2 = metadata.findType(dataverseName, typeName);
if (t2 == null) {
throw new AlgebricksException("Unknown type " + typeName);
}
return t2;
}
}, true);
BuiltinFunctions.addFunction(BuiltinFunctions.FEED_INTERCEPT, new IResultTypeComputer() {
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
if (f.getArguments().size() != 1) {
throw new AlgebricksException("dataset arity is 1, not " + f.getArguments().size());
}
ILogicalExpression a1 = f.getArguments().get(0).getValue();
IAType t1 = (IAType) env.getType(a1);
if (t1.getTypeTag() == ATypeTag.ANY) {
return BuiltinType.ANY;
}
if (t1.getTypeTag() != ATypeTag.STRING) {
throw new AlgebricksException("Illegal type " + t1 + " for dataset() argument.");
}
String datasetArg = ConstantExpressionUtil.getStringConstant(a1);
if (datasetArg == null) {
return BuiltinType.ANY;
}
MetadataProvider metadata = (MetadataProvider) mp;
Pair<String, String> datasetInfo = getDatasetInfo(metadata, datasetArg);
String dataverseName = datasetInfo.first;
String datasetName = datasetInfo.second;
if (dataverseName == null) {
throw new AlgebricksException("Unspecified dataverse!");
}
Dataset dataset = metadata.findDataset(dataverseName, datasetName);
if (dataset == null) {
throw new AlgebricksException("Could not find dataset " + datasetName + " in dataverse " + dataverseName);
}
String tn = dataset.getItemTypeName();
IAType t2 = metadata.findType(dataset.getItemTypeDataverseName(), tn);
if (t2 == null) {
throw new AlgebricksException("No type for dataset " + datasetName);
}
return t2;
}
}, true);
}
Aggregations