Search in sources :

Example 31 with IVariableTypeEnvironment

use of org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment in project asterixdb by apache.

the class StreamLimitPOperator method contributeRuntimeOperator.

@Override
public void contributeRuntimeOperator(IHyracksJobBuilder builder, JobGenContext context, ILogicalOperator op, IOperatorSchema propagatedSchema, IOperatorSchema[] inputSchemas, IOperatorSchema outerPlanSchema) throws AlgebricksException {
    LimitOperator limit = (LimitOperator) op;
    IExpressionRuntimeProvider expressionRuntimeProvider = context.getExpressionRuntimeProvider();
    IVariableTypeEnvironment env = context.getTypeEnvironment(op);
    IScalarEvaluatorFactory maxObjectsFact = expressionRuntimeProvider.createEvaluatorFactory(limit.getMaxObjects().getValue(), env, inputSchemas, context);
    ILogicalExpression offsetExpr = limit.getOffset().getValue();
    IScalarEvaluatorFactory offsetFact = (offsetExpr == null) ? null : expressionRuntimeProvider.createEvaluatorFactory(offsetExpr, env, inputSchemas, context);
    RecordDescriptor recDesc = JobGenHelper.mkRecordDescriptor(context.getTypeEnvironment(op), propagatedSchema, context);
    StreamLimitRuntimeFactory runtime = new StreamLimitRuntimeFactory(maxObjectsFact, offsetFact, null, context.getBinaryIntegerInspectorFactory());
    builder.contributeMicroOperator(limit, runtime, recDesc);
    // and contribute one edge from its child
    ILogicalOperator src = limit.getInputs().get(0).getValue();
    builder.contributeGraphEdge(src, 0, limit, 0);
}
Also used : IExpressionRuntimeProvider(org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionRuntimeProvider) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) LimitOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LimitOperator) RecordDescriptor(org.apache.hyracks.api.dataflow.value.RecordDescriptor) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) StreamLimitRuntimeFactory(org.apache.hyracks.algebricks.runtime.operators.std.StreamLimitRuntimeFactory)

Example 32 with IVariableTypeEnvironment

use of org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment in project asterixdb by apache.

the class TypeComputerTest method testTypeComputer.

private boolean testTypeComputer(Class<? extends IResultTypeComputer> c) throws Exception {
    // Mocks the type environment.
    IVariableTypeEnvironment mockTypeEnv = mock(IVariableTypeEnvironment.class);
    // Mocks the metadata provider.
    IMetadataProvider<?, ?> mockMetadataProvider = mock(IMetadataProvider.class);
    // Mocks function expression.
    AbstractFunctionCallExpression mockExpr = mock(AbstractFunctionCallExpression.class);
    FunctionIdentifier fid = mock(FunctionIdentifier.class);
    when(mockExpr.getFunctionIdentifier()).thenReturn(fid);
    when(fid.getName()).thenReturn("testFunction");
    // A function at most has six argument.
    List<Mutable<ILogicalExpression>> argRefs = new ArrayList<>();
    for (int argIndex = 0; argIndex < 6; ++argIndex) {
        ILogicalExpression mockArg = mock(ILogicalExpression.class);
        argRefs.add(new MutableObject<>(mockArg));
        when(mockTypeEnv.getType(mockArg)).thenReturn(BuiltinType.ANY);
    }
    // Sets up arguments for the mocked expression.
    when(mockExpr.getArguments()).thenReturn(argRefs);
    // Sets up required/actual types of the mocked expression.
    Object[] opaqueParameters = new Object[2];
    opaqueParameters[0] = BuiltinType.ANY;
    opaqueParameters[1] = BuiltinType.ANY;
    when(mockExpr.getOpaqueParameters()).thenReturn(opaqueParameters);
    // Tests the return type. It should be either ANY or NULLABLE/MISSABLE.
    IResultTypeComputer instance = (IResultTypeComputer) c.getField("INSTANCE").get(null);
    IAType resultType = instance.computeType(mockExpr, mockTypeEnv, mockMetadataProvider);
    ATypeTag typeTag = resultType.getTypeTag();
    if (typeTag == ATypeTag.ANY) {
        return true;
    }
    if (typeTag == ATypeTag.UNION) {
        AUnionType unionType = (AUnionType) resultType;
        return unionType.isMissableType() && unionType.isNullableType();
    }
    return false;
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) AUnionType(org.apache.asterix.om.types.AUnionType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ATypeTag(org.apache.asterix.om.types.ATypeTag) MutableObject(org.apache.commons.lang3.mutable.MutableObject) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) IAType(org.apache.asterix.om.types.IAType)

Example 33 with IVariableTypeEnvironment

use of org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment 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)

Example 34 with IVariableTypeEnvironment

use of org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment in project asterixdb by apache.

the class CheckFilterExpressionTypeRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.SELECT) {
        return false;
    }
    SelectOperator select = (SelectOperator) op;
    ILogicalExpression condition = select.getCondition().getValue();
    IVariableTypeEnvironment env = select.computeOutputTypeEnvironment(context);
    IAType condType = (IAType) env.getType(condition);
    if (condType.getTypeTag() != ATypeTag.BOOLEAN && condType.getTypeTag() != ATypeTag.ANY && !isPossibleBoolean(condType)) {
        throw new AlgebricksException("The select condition " + condition.toString() + " should be of the boolean type.");
    }
    return false;
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) IAType(org.apache.asterix.om.types.IAType)

Example 35 with IVariableTypeEnvironment

use of org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment in project asterixdb by apache.

the class RTreeSearchPOperator method contributeRuntimeOperator.

@Override
public void contributeRuntimeOperator(IHyracksJobBuilder builder, JobGenContext context, ILogicalOperator op, IOperatorSchema opSchema, IOperatorSchema[] inputSchemas, IOperatorSchema outerPlanSchema) throws AlgebricksException {
    AbstractUnnestMapOperator unnestMap = (AbstractUnnestMapOperator) op;
    ILogicalExpression unnestExpr = unnestMap.getExpressionRef().getValue();
    if (unnestExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        throw new IllegalStateException();
    }
    AbstractFunctionCallExpression unnestFuncExpr = (AbstractFunctionCallExpression) unnestExpr;
    FunctionIdentifier funcIdent = unnestFuncExpr.getFunctionIdentifier();
    if (!funcIdent.equals(BuiltinFunctions.INDEX_SEARCH)) {
        return;
    }
    RTreeJobGenParams jobGenParams = new RTreeJobGenParams();
    jobGenParams.readFromFuncArgs(unnestFuncExpr.getArguments());
    int[] keyIndexes = getKeyIndexes(jobGenParams.getKeyVarList(), inputSchemas);
    int[] minFilterFieldIndexes = getKeyIndexes(unnestMap.getMinFilterVars(), inputSchemas);
    int[] maxFilterFieldIndexes = getKeyIndexes(unnestMap.getMaxFilterVars(), inputSchemas);
    MetadataProvider mp = (MetadataProvider) context.getMetadataProvider();
    Dataset dataset = mp.findDataset(jobGenParams.getDataverseName(), jobGenParams.getDatasetName());
    IVariableTypeEnvironment typeEnv = context.getTypeEnvironment(unnestMap);
    List<LogicalVariable> outputVars = unnestMap.getVariables();
    if (jobGenParams.getRetainInput()) {
        outputVars = new ArrayList<LogicalVariable>();
        VariableUtilities.getLiveVariables(unnestMap, outputVars);
    }
    boolean retainNull = false;
    if (op.getOperatorTag() == LogicalOperatorTag.LEFT_OUTER_UNNEST_MAP) {
        // By nature, LEFT_OUTER_UNNEST_MAP should generate null values for non-matching tuples.
        retainNull = true;
    }
    Pair<IOperatorDescriptor, AlgebricksPartitionConstraint> rtreeSearch = mp.buildRtreeRuntime(builder.getJobSpec(), outputVars, opSchema, typeEnv, context, jobGenParams.getRetainInput(), retainNull, dataset, jobGenParams.getIndexName(), keyIndexes, minFilterFieldIndexes, maxFilterFieldIndexes);
    builder.contributeHyracksOperator(unnestMap, rtreeSearch.first);
    builder.contributeAlgebricksPartitionConstraint(rtreeSearch.first, rtreeSearch.second);
    ILogicalOperator srcExchange = unnestMap.getInputs().get(0).getValue();
    builder.contributeGraphEdge(srcExchange, 0, unnestMap, 0);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Dataset(org.apache.asterix.metadata.entities.Dataset) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) RTreeJobGenParams(org.apache.asterix.optimizer.rules.am.RTreeJobGenParams) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) IOperatorDescriptor(org.apache.hyracks.api.dataflow.IOperatorDescriptor) AbstractUnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractUnnestMapOperator) AlgebricksPartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksPartitionConstraint) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)

Aggregations

IVariableTypeEnvironment (org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)51 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)24 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)23 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)22 IAType (org.apache.asterix.om.types.IAType)15 RecordDescriptor (org.apache.hyracks.api.dataflow.value.RecordDescriptor)14 ArrayList (java.util.ArrayList)13 Mutable (org.apache.commons.lang3.mutable.Mutable)13 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)12 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)12 IOperatorDescriptor (org.apache.hyracks.api.dataflow.IOperatorDescriptor)11 IBinaryComparatorFactory (org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory)11 AlgebricksPartitionConstraint (org.apache.hyracks.algebricks.common.constraints.AlgebricksPartitionConstraint)10 Pair (org.apache.hyracks.algebricks.common.utils.Pair)9 IBinaryComparatorFactoryProvider (org.apache.hyracks.algebricks.data.IBinaryComparatorFactoryProvider)9 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)8 INormalizedKeyComputerFactory (org.apache.hyracks.api.dataflow.value.INormalizedKeyComputerFactory)8 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)7 IMetadataProvider (org.apache.hyracks.algebricks.core.algebra.metadata.IMetadataProvider)7 INormalizedKeyComputerFactoryProvider (org.apache.hyracks.algebricks.data.INormalizedKeyComputerFactoryProvider)7