Search in sources :

Example 6 with IResultTypeComputer

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);
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ArrayList(java.util.ArrayList) IAType(org.apache.asterix.om.types.IAType)

Example 7 with IResultTypeComputer

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);
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) IMetadataProvider(org.apache.hyracks.algebricks.core.algebra.metadata.IMetadataProvider) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) Dataset(org.apache.asterix.metadata.entities.Dataset) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) IAType(org.apache.asterix.om.types.IAType) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Aggregations

IResultTypeComputer (org.apache.asterix.om.typecomputer.base.IResultTypeComputer)7 IAType (org.apache.asterix.om.types.IAType)4 FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)4 ArrayList (java.util.ArrayList)3 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)3 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)3 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)3 IVariableTypeEnvironment (org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)3 ATypeTag (org.apache.asterix.om.types.ATypeTag)2 Mutable (org.apache.commons.lang3.mutable.Mutable)2 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 Test (org.junit.Test)2 Reflections (org.reflections.Reflections)2 SubTypesScanner (org.reflections.scanners.SubTypesScanner)2 HashSet (java.util.HashSet)1 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)1 MetadataProvider (org.apache.asterix.metadata.declared.MetadataProvider)1 Dataset (org.apache.asterix.metadata.entities.Dataset)1 AUnionType (org.apache.asterix.om.types.AUnionType)1 Pair (org.apache.hyracks.algebricks.common.utils.Pair)1