Search in sources :

Example 26 with AsterixConstantValue

use of org.apache.asterix.om.constants.AsterixConstantValue in project asterixdb by apache.

the class InvertedIndexAccessMethod method isEditDistanceFuncSelectOptimizable.

private boolean isEditDistanceFuncSelectOptimizable(Index index, IOptimizableFuncExpr optFuncExpr) throws AlgebricksException {
    // Check for panic in selection query.
    // TODO: Panic also depends on prePost which is currently hardcoded to be true.
    AsterixConstantValue listOrStrConstVal = (AsterixConstantValue) ((ConstantExpression) optFuncExpr.getConstantExpr(0)).getValue();
    IAObject listOrStrObj = listOrStrConstVal.getObject();
    ATypeTag typeTag = listOrStrObj.getType().getTypeTag();
    if (!isEditDistanceFuncCompatible(typeTag, index.getIndexType())) {
        return false;
    }
    AsterixConstantValue intConstVal = (AsterixConstantValue) ((ConstantExpression) optFuncExpr.getConstantExpr(1)).getValue();
    IAObject intObj = intConstVal.getObject();
    AInt32 edThresh = null;
    // Apply type casting based on numeric types of the input to INTEGER type.
    try {
        edThresh = (AInt32) ATypeHierarchy.convertNumericTypeObject(intObj, ATypeTag.INTEGER);
    } catch (HyracksDataException e) {
        throw new AlgebricksException(e);
    }
    int mergeThreshold = 0;
    if (typeTag == ATypeTag.STRING) {
        AString astr = (AString) listOrStrObj;
        // Compute merge threshold depending on the query grams contain pre- and postfixing
        if (optFuncExpr.containsPartialField()) {
            mergeThreshold = (astr.getStringValue().length() - index.getGramLength() + 1) - edThresh.getIntegerValue() * index.getGramLength();
        } else {
            mergeThreshold = (astr.getStringValue().length() + index.getGramLength() - 1) - edThresh.getIntegerValue() * index.getGramLength();
        }
    }
    if ((typeTag == ATypeTag.ARRAY) && (index.getIndexType() == IndexType.SINGLE_PARTITION_WORD_INVIX || index.getIndexType() == IndexType.LENGTH_PARTITIONED_WORD_INVIX)) {
        IACollection alist = (IACollection) listOrStrObj;
        // Compute merge threshold.
        mergeThreshold = alist.size() - edThresh.getIntegerValue();
    }
    if (mergeThreshold <= 0) {
        // We cannot use index to optimize expr.
        return false;
    }
    return true;
}
Also used : AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ATypeTag(org.apache.asterix.om.types.ATypeTag) IAObject(org.apache.asterix.om.base.IAObject) IACollection(org.apache.asterix.om.base.IACollection) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AString(org.apache.asterix.om.base.AString) AInt32(org.apache.asterix.om.base.AInt32) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 27 with AsterixConstantValue

use of org.apache.asterix.om.constants.AsterixConstantValue in project asterixdb by apache.

the class InvertedIndexAccessMethod method addFunctionSpecificArgs.

private void addFunctionSpecificArgs(IOptimizableFuncExpr optFuncExpr, InvertedIndexJobGenParams jobGenParams) {
    if (optFuncExpr.getFuncExpr().getFunctionIdentifier() == BuiltinFunctions.STRING_CONTAINS) {
        jobGenParams.setSearchModifierType(SearchModifierType.CONJUNCTIVE);
        jobGenParams.setSimilarityThreshold(new AsterixConstantValue(AMissing.MISSING));
        return;
    }
    if (optFuncExpr.getFuncExpr().getFunctionIdentifier() == BuiltinFunctions.SIMILARITY_JACCARD_CHECK) {
        jobGenParams.setSearchModifierType(SearchModifierType.JACCARD);
        // Add the similarity threshold which, by convention, is the last constant value.
        jobGenParams.setSimilarityThreshold(((ConstantExpression) optFuncExpr.getConstantExpr(optFuncExpr.getNumConstantExpr() - 1)).getValue());
        return;
    }
    if (optFuncExpr.getFuncExpr().getFunctionIdentifier() == BuiltinFunctions.EDIT_DISTANCE_CHECK || optFuncExpr.getFuncExpr().getFunctionIdentifier() == BuiltinFunctions.EDIT_DISTANCE_CONTAINS) {
        if (optFuncExpr.containsPartialField()) {
            jobGenParams.setSearchModifierType(SearchModifierType.CONJUNCTIVE_EDIT_DISTANCE);
        } else {
            jobGenParams.setSearchModifierType(SearchModifierType.EDIT_DISTANCE);
        }
        // Add the similarity threshold which, by convention, is the last constant value.
        jobGenParams.setSimilarityThreshold(((ConstantExpression) optFuncExpr.getConstantExpr(optFuncExpr.getNumConstantExpr() - 1)).getValue());
        return;
    }
    if (optFuncExpr.getFuncExpr().getFunctionIdentifier() == BuiltinFunctions.FULLTEXT_CONTAINS || optFuncExpr.getFuncExpr().getFunctionIdentifier() == BuiltinFunctions.FULLTEXT_CONTAINS_WO_OPTION) {
        // Let the job Gen pass the full-text search information.
        jobGenParams.setIsFullTextSearch(true);
        // We check the last argument of the given full-text search to see whether conjunctive or disjunctive
        // search parameter is given. This is the last argument of the function call expression.
        AbstractFunctionCallExpression funcExpr = optFuncExpr.getFuncExpr();
        jobGenParams.setSearchModifierType(getFullTextOption(funcExpr));
        jobGenParams.setSimilarityThreshold(new AsterixConstantValue(ANull.NULL));
    }
}
Also used : AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)

Example 28 with AsterixConstantValue

use of org.apache.asterix.om.constants.AsterixConstantValue in project asterixdb by apache.

the class ResolveVariableRule method resolveAsFieldAccess.

// Resolves a "resolve" function call as a field access.
private void resolveAsFieldAccess(Mutable<ILogicalExpression> funcRef, Pair<ILogicalExpression, List<String>> varAndPath) {
    // Rewrites to field-access-by-names.
    ILogicalExpression expr = varAndPath.first;
    List<String> path = varAndPath.second;
    Mutable<ILogicalExpression> firstArgRef = new MutableObject<>(expr);
    ILogicalExpression newFunc = null;
    for (String fieldName : path) {
        List<Mutable<ILogicalExpression>> args = new ArrayList<>();
        args.add(firstArgRef);
        args.add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AString(fieldName)))));
        newFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_NAME), args);
        firstArgRef = new MutableObject<>(newFunc);
    }
    funcRef.setValue(newFunc);
}
Also used : ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) AString(org.apache.asterix.om.base.AString) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 29 with AsterixConstantValue

use of org.apache.asterix.om.constants.AsterixConstantValue in project asterixdb by apache.

the class SimilarityCheckRule method replaceWithVariableArg.

private boolean replaceWithVariableArg(Mutable<ILogicalExpression> expRef, FunctionIdentifier normFuncIdent, AsterixConstantValue constVal, VariableReferenceExpression varRefExpr, List<AssignOperator> assigns, IOptimizationContext context) throws AlgebricksException {
    // Find variable in assigns to determine its originating function.
    LogicalVariable var = varRefExpr.getVariableReference();
    Mutable<ILogicalExpression> simFuncExprRef = null;
    ScalarFunctionCallExpression simCheckFuncExpr = null;
    AssignOperator matchingAssign = null;
    for (int i = 0; i < assigns.size(); i++) {
        AssignOperator assign = assigns.get(i);
        for (int j = 0; j < assign.getVariables().size(); j++) {
            // Check if variables match.
            if (var != assign.getVariables().get(j)) {
                continue;
            }
            // Check if corresponding expr is a function call.
            if (assign.getExpressions().get(j).getValue().getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
                continue;
            }
            simFuncExprRef = assign.getExpressions().get(j);
            // Analyze function expression and get equivalent similarity check function.
            simCheckFuncExpr = getSimilarityCheckExpr(normFuncIdent, constVal, (AbstractFunctionCallExpression) simFuncExprRef.getValue());
            matchingAssign = assign;
            break;
        }
        if (simCheckFuncExpr != null) {
            break;
        }
    }
    // Only non-null if we found that varRefExpr refers to an optimizable similarity function call.
    if (simCheckFuncExpr != null) {
        // Create a new assign under matchingAssign which assigns the result of our similarity-check function to a variable.
        LogicalVariable newVar = context.newVar();
        AssignOperator newAssign = new AssignOperator(newVar, new MutableObject<ILogicalExpression>(simCheckFuncExpr));
        // Hook up inputs.
        newAssign.getInputs().add(new MutableObject<ILogicalOperator>(matchingAssign.getInputs().get(0).getValue()));
        matchingAssign.getInputs().get(0).setValue(newAssign);
        // Replace select condition with a get-item on newVarFromExpression.
        List<Mutable<ILogicalExpression>> selectGetItemArgs = new ArrayList<Mutable<ILogicalExpression>>();
        // First arg is a variable reference expr on newVarFromExpression.
        selectGetItemArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(newVar)));
        // Second arg is the item index to be accessed, here 0.
        selectGetItemArgs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AInt32(0)))));
        ILogicalExpression selectGetItemExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.GET_ITEM), selectGetItemArgs);
        // Replace the old similarity function call with the new getItemExpr.
        expRef.setValue(selectGetItemExpr);
        // Replace expr corresponding to original variable in the original assign with a get-item on
        // newVarFromExpression.
        List<Mutable<ILogicalExpression>> assignGetItemArgs = new ArrayList<Mutable<ILogicalExpression>>();
        // First arg is a variable reference expr on newVarFromExpression.
        assignGetItemArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(newVar)));
        // Second arg is the item index to be accessed, here 1.
        assignGetItemArgs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AInt32(1)))));
        ILogicalExpression assignGetItemExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.GET_ITEM), assignGetItemArgs);
        // Replace the original assign expr with the get-item expr.
        simFuncExprRef.setValue(assignGetItemExpr);
        context.computeAndSetTypeEnvironmentForOperator(newAssign);
        context.computeAndSetTypeEnvironmentForOperator(matchingAssign);
        return true;
    }
    return false;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) AInt32(org.apache.asterix.om.base.AInt32) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 30 with AsterixConstantValue

use of org.apache.asterix.om.constants.AsterixConstantValue 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

AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)49 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)41 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)34 AString (org.apache.asterix.om.base.AString)23 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)23 Mutable (org.apache.commons.lang3.mutable.Mutable)21 ArrayList (java.util.ArrayList)20 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)20 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)19 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)18 IAObject (org.apache.asterix.om.base.IAObject)17 AInt32 (org.apache.asterix.om.base.AInt32)15 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)14 MutableObject (org.apache.commons.lang3.mutable.MutableObject)13 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)11 Pair (org.apache.hyracks.algebricks.common.utils.Pair)10 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)10 AOrderedList (org.apache.asterix.om.base.AOrderedList)8 IAType (org.apache.asterix.om.types.IAType)8 ATypeTag (org.apache.asterix.om.types.ATypeTag)7