Search in sources :

Example 16 with MetadataProvider

use of org.apache.asterix.metadata.declared.MetadataProvider 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)

Example 17 with MetadataProvider

use of org.apache.asterix.metadata.declared.MetadataProvider in project asterixdb by apache.

the class BTreeSearchPOperator 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;
    }
    BTreeJobGenParams jobGenParams = new BTreeJobGenParams();
    jobGenParams.readFromFuncArgs(unnestFuncExpr.getArguments());
    int[] lowKeyIndexes = getKeyIndexes(jobGenParams.getLowKeyVarList(), inputSchemas);
    int[] highKeyIndexes = getKeyIndexes(jobGenParams.getHighKeyVarList(), inputSchemas);
    int[] minFilterFieldIndexes = getKeyIndexes(unnestMap.getMinFilterVars(), inputSchemas);
    int[] maxFilterFieldIndexes = getKeyIndexes(unnestMap.getMaxFilterVars(), inputSchemas);
    MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
    Dataset dataset = metadataProvider.findDataset(jobGenParams.getDataverseName(), jobGenParams.getDatasetName());
    IVariableTypeEnvironment typeEnv = context.getTypeEnvironment(op);
    // By nature, LEFT_OUTER_UNNEST_MAP should generate null values for non-matching tuples.
    boolean retainMissing = op.getOperatorTag() == LogicalOperatorTag.LEFT_OUTER_UNNEST_MAP;
    Pair<IOperatorDescriptor, AlgebricksPartitionConstraint> btreeSearch = metadataProvider.buildBtreeRuntime(builder.getJobSpec(), opSchema, typeEnv, context, jobGenParams.getRetainInput(), retainMissing, dataset, jobGenParams.getIndexName(), lowKeyIndexes, highKeyIndexes, jobGenParams.isLowKeyInclusive(), jobGenParams.isHighKeyInclusive(), minFilterFieldIndexes, maxFilterFieldIndexes);
    builder.contributeHyracksOperator(unnestMap, btreeSearch.first);
    builder.contributeAlgebricksPartitionConstraint(btreeSearch.first, btreeSearch.second);
    ILogicalOperator srcExchange = unnestMap.getInputs().get(0).getValue();
    builder.contributeGraphEdge(srcExchange, 0, unnestMap, 0);
}
Also used : Dataset(org.apache.asterix.metadata.entities.Dataset) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) BTreeJobGenParams(org.apache.asterix.optimizer.rules.am.BTreeJobGenParams) 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) 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)

Example 18 with MetadataProvider

use of org.apache.asterix.metadata.declared.MetadataProvider in project asterixdb by apache.

the class ExternalDataLookupPOperator method contributeRuntimeOperator.

@Override
public void contributeRuntimeOperator(IHyracksJobBuilder builder, JobGenContext context, ILogicalOperator op, IOperatorSchema opSchema, IOperatorSchema[] inputSchemas, IOperatorSchema outerPlanSchema) throws AlgebricksException {
    UnnestMapOperator unnestMap = (UnnestMapOperator) op;
    ILogicalExpression expr = unnestMap.getExpressionRef().getValue();
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        throw new IllegalStateException();
    }
    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
    FunctionIdentifier funcIdent = funcExpr.getFunctionIdentifier();
    if (!funcIdent.equals(BuiltinFunctions.EXTERNAL_LOOKUP)) {
        return;
    }
    int[] ridIndexes = getKeyIndexes(ridVarList, inputSchemas);
    IVariableTypeEnvironment typeEnv = context.getTypeEnvironment(op);
    MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
    Pair<IOperatorDescriptor, AlgebricksPartitionConstraint> externalLoopup = metadataProvider.buildExternalDataLookupRuntime(builder.getJobSpec(), dataset, ridIndexes, retainInput, typeEnv, opSchema, context, metadataProvider, retainMissing);
    builder.contributeHyracksOperator(unnestMap, externalLoopup.first);
    builder.contributeAlgebricksPartitionConstraint(externalLoopup.first, externalLoopup.second);
    ILogicalOperator srcExchange = unnestMap.getInputs().get(0).getValue();
    builder.contributeGraphEdge(srcExchange, 0, unnestMap, 0);
}
Also used : FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) UnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestMapOperator) IOperatorDescriptor(org.apache.hyracks.api.dataflow.IOperatorDescriptor) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AlgebricksPartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksPartitionConstraint) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)

Example 19 with MetadataProvider

use of org.apache.asterix.metadata.declared.MetadataProvider in project asterixdb by apache.

the class PushFieldAccessRule method isAccessToIndexedField.

@SuppressWarnings("unchecked")
private boolean isAccessToIndexedField(AssignOperator assign, IOptimizationContext context) throws AlgebricksException {
    AbstractFunctionCallExpression accessFun = (AbstractFunctionCallExpression) assign.getExpressions().get(0).getValue();
    ILogicalExpression e0 = accessFun.getArguments().get(0).getValue();
    if (e0.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    LogicalVariable var = ((VariableReferenceExpression) e0).getVariableReference();
    if (context.findPrimaryKey(var) == null) {
        // not referring to a dataset record
        return false;
    }
    AbstractLogicalOperator op = assign;
    while (op.getInputs().size() == 1 && op.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN) {
        op = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
    }
    if (op.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN) {
        return false;
    }
    DataSourceScanOperator scan = (DataSourceScanOperator) op;
    LogicalVariable recVar = scan.getVariables().get(scan.getVariables().size() - 1);
    if (recVar != var) {
        return false;
    }
    MetadataProvider mp = (MetadataProvider) context.getMetadataProvider();
    DataSourceId asid = ((IDataSource<DataSourceId>) scan.getDataSource()).getId();
    Dataset dataset = mp.findDataset(asid.getDataverseName(), asid.getDatasourceName());
    if (dataset == null) {
        throw new AlgebricksException("Dataset " + asid.getDatasourceName() + " not found.");
    }
    if (dataset.getDatasetType() != DatasetType.INTERNAL) {
        return false;
    }
    final Integer pos = ConstantExpressionUtil.getIntConstant(accessFun.getArguments().get(1).getValue());
    if (pos != null) {
        String tName = dataset.getItemTypeName();
        IAType t = mp.findType(dataset.getItemTypeDataverseName(), tName);
        if (t.getTypeTag() != ATypeTag.OBJECT) {
            return false;
        }
        ARecordType rt = (ARecordType) t;
        if (pos >= rt.getFieldNames().length) {
            return false;
        }
    }
    List<Index> datasetIndexes = mp.getDatasetIndexes(dataset.getDataverseName(), dataset.getDatasetName());
    boolean hasSecondaryIndex = false;
    for (Index index : datasetIndexes) {
        if (index.isSecondaryIndex()) {
            hasSecondaryIndex = true;
            break;
        }
    }
    return hasSecondaryIndex;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) Dataset(org.apache.asterix.metadata.entities.Dataset) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) Index(org.apache.asterix.metadata.entities.Index) AString(org.apache.asterix.om.base.AString) IDataSource(org.apache.hyracks.algebricks.core.algebra.metadata.IDataSource) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) DataSourceScanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DataSourceScanOperator) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ARecordType(org.apache.asterix.om.types.ARecordType) DataSourceId(org.apache.asterix.metadata.declared.DataSourceId) IAType(org.apache.asterix.om.types.IAType)

Example 20 with MetadataProvider

use of org.apache.asterix.metadata.declared.MetadataProvider in project asterixdb by apache.

the class ResolveVariableRule method resolveInternal.

// Resolves an undefined name to a dataset or a fully qualified variable/field-access path
// based on the given information of dataset matches and candidate paths.
private boolean resolveInternal(Mutable<ILogicalExpression> funcRef, boolean hasMatchedDataset, Collection<Pair<ILogicalExpression, List<String>>> varAccessCandidates, String unresolvedVarName, Triple<Boolean, String, String> fullyQualifiedDatasetPathCandidateFromParent, Mutable<ILogicalExpression> parentFuncRef, IOptimizationContext context) throws AlgebricksException {
    AbstractFunctionCallExpression func = (AbstractFunctionCallExpression) funcRef.getValue();
    int numVarCandidates = varAccessCandidates.size();
    // The resolution order: 1. field-access 2. datasets (standalone-name or fully-qualified)
    if (numVarCandidates > 0) {
        if (numVarCandidates == 1) {
            resolveAsFieldAccess(funcRef, varAccessCandidates.iterator().next());
        } else {
            // More than one possibilities.
            throw new AlgebricksException("Cannot resolve ambiguous alias reference for undefined identifier " + unresolvedVarName);
        }
    } else if (hasMatchedDataset) {
        // Rewrites the "resolve" function to a "dataset" function and only keep the dataset name argument.
        func.setFunctionInfo(FunctionUtil.getFunctionInfo(BuiltinFunctions.DATASET));
        Mutable<ILogicalExpression> datasetNameExpression = func.getArguments().get(0);
        func.getArguments().clear();
        func.getArguments().add(datasetNameExpression);
    } else if (fullyQualifiedDatasetPathCandidateFromParent.first) {
        // Rewrites the parent "field-access" function to a "dataset" function.
        AbstractFunctionCallExpression parentFunc = (AbstractFunctionCallExpression) parentFuncRef.getValue();
        parentFunc.setFunctionInfo(FunctionUtil.getFunctionInfo(BuiltinFunctions.DATASET));
        parentFunc.getArguments().clear();
        parentFunc.getArguments().add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AString(fullyQualifiedDatasetPathCandidateFromParent.second + "." + fullyQualifiedDatasetPathCandidateFromParent.third)))));
    } else {
        MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
        // Cannot find any resolution.
        throw new AlgebricksException("Cannot find dataset " + unresolvedVarName + " in dataverse " + metadataProvider.getDefaultDataverseName() + " nor an alias with name " + unresolvedVarName);
    }
    return true;
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AString(org.apache.asterix.om.base.AString)

Aggregations

MetadataProvider (org.apache.asterix.metadata.declared.MetadataProvider)30 Dataset (org.apache.asterix.metadata.entities.Dataset)16 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)16 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)13 ArrayList (java.util.ArrayList)10 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)10 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)10 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)10 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)10 List (java.util.List)9 IAType (org.apache.asterix.om.types.IAType)9 ARecordType (org.apache.asterix.om.types.ARecordType)8 Index (org.apache.asterix.metadata.entities.Index)7 Mutable (org.apache.commons.lang3.mutable.Mutable)7 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)7 FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)7 AString (org.apache.asterix.om.base.AString)5 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)5 AlgebricksPartitionConstraint (org.apache.hyracks.algebricks.common.constraints.AlgebricksPartitionConstraint)4 IVariableTypeEnvironment (org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)4