use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.
the class RemoveUnusedOneToOneEquiJoinRule method isEquiJoin.
private boolean isEquiJoin(Mutable<ILogicalExpression> conditionExpr) {
AbstractLogicalExpression expr = (AbstractLogicalExpression) conditionExpr.getValue();
if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
FunctionIdentifier funcIdent = funcExpr.getFunctionIdentifier();
if (funcIdent != AlgebricksBuiltinFunctions.AND && funcIdent != AlgebricksBuiltinFunctions.EQ) {
return false;
}
}
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.
the class RemoveRedundantListifyRule method appliesForReverseCase.
private boolean appliesForReverseCase(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> varUsedAbove, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
if (op1.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
return false;
}
AggregateOperator agg = (AggregateOperator) op1;
if (agg.getVariables().size() > 1 || agg.getVariables().size() <= 0) {
return false;
}
LogicalVariable aggVar = agg.getVariables().get(0);
ILogicalExpression aggFun = agg.getExpressions().get(0).getValue();
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) aggFun;
if (!BuiltinFunctions.LISTIFY.equals(f.getFunctionIdentifier())) {
return false;
}
if (f.getArguments().size() != 1) {
return false;
}
ILogicalExpression arg0 = f.getArguments().get(0).getValue();
if (((AbstractLogicalExpression) arg0).getExpressionTag() != LogicalExpressionTag.VARIABLE) {
return false;
}
LogicalVariable aggInputVar = ((VariableReferenceExpression) arg0).getVariableReference();
if (varUsedAbove.contains(aggInputVar)) {
return false;
}
if (agg.getInputs().size() == 0) {
return false;
}
AbstractLogicalOperator op2 = (AbstractLogicalOperator) agg.getInputs().get(0).getValue();
if (op2.getOperatorTag() != LogicalOperatorTag.UNNEST) {
return false;
}
UnnestOperator unnest = (UnnestOperator) op2;
if (unnest.getPositionalVariable() != null) {
return false;
}
if (!unnest.getVariable().equals(aggInputVar)) {
return false;
}
ILogicalExpression unnestArg = unnest.getExpressionRef().getValue();
if (unnestArg.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
AbstractFunctionCallExpression scanFunc = (AbstractFunctionCallExpression) unnestArg;
if (scanFunc.getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
return false;
}
if (scanFunc.getArguments().size() != 1) {
return false;
}
List<LogicalVariable> assgnVars = new ArrayList<>(1);
assgnVars.add(aggVar);
AssignOperator assign = new AssignOperator(assgnVars, scanFunc.getArguments());
assign.getInputs().add(unnest.getInputs().get(0));
context.computeAndSetTypeEnvironmentForOperator(assign);
opRef.setValue(assign);
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression 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;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression 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;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.
the class InvertedIndexAccessMethod method getNonConstArgFieldExprPair.
private LogicalVariable getNonConstArgFieldExprPair(ILogicalExpression nonConstArg, AbstractFunctionCallExpression funcExpr, List<AbstractLogicalOperator> assignsAndUnnests, int matchedAssignOrUnnestIndex) {
LogicalVariable fieldVar = null;
// Analyze nonConstArg depending on similarity function.
if (funcExpr.getFunctionIdentifier() == BuiltinFunctions.SIMILARITY_JACCARD_CHECK) {
AbstractFunctionCallExpression nonConstFuncExpr = funcExpr;
if (nonConstArg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
nonConstFuncExpr = (AbstractFunctionCallExpression) nonConstArg;
// TODO: Currently, we're only looking for word and gram tokens (non hashed).
if (nonConstFuncExpr.getFunctionIdentifier() != BuiltinFunctions.WORD_TOKENS && nonConstFuncExpr.getFunctionIdentifier() != BuiltinFunctions.GRAM_TOKENS) {
return null;
}
// Find the variable that is being tokenized.
nonConstArg = nonConstFuncExpr.getArguments().get(0).getValue();
}
}
if (funcExpr.getFunctionIdentifier() == BuiltinFunctions.EDIT_DISTANCE_CHECK || funcExpr.getFunctionIdentifier() == BuiltinFunctions.EDIT_DISTANCE_CONTAINS) {
while (nonConstArg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
AbstractFunctionCallExpression nonConstFuncExpr = (AbstractFunctionCallExpression) nonConstArg;
if (nonConstFuncExpr.getFunctionIdentifier() != BuiltinFunctions.WORD_TOKENS && nonConstFuncExpr.getFunctionIdentifier() != BuiltinFunctions.SUBSTRING && nonConstFuncExpr.getFunctionIdentifier() != BuiltinFunctions.SUBSTRING_BEFORE && nonConstFuncExpr.getFunctionIdentifier() != BuiltinFunctions.SUBSTRING_AFTER) {
return null;
}
// Find the variable whose substring is used in the similarity function
nonConstArg = nonConstFuncExpr.getArguments().get(0).getValue();
}
}
if (nonConstArg.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
fieldVar = ((VariableReferenceExpression) nonConstArg).getVariableReference();
}
return fieldVar;
}
Aggregations