use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class PlanTranslationUtil method prepareVarAndExpression.
public static void prepareVarAndExpression(List<String> field, LogicalVariable resVar, List<LogicalVariable> vars, List<Mutable<ILogicalExpression>> assignExpressions, List<Mutable<ILogicalExpression>> varRefs, IVariableContext context) {
ScalarFunctionCallExpression f = createFieldAccessExpression(new VariableReferenceExpression(DUMMY_VAR), field);
f.substituteVar(DUMMY_VAR, resVar);
assignExpressions.add(new MutableObject<ILogicalExpression>(f));
LogicalVariable v = context.newVar();
vars.add(v);
if (varRefs != null) {
varRefs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(v)));
}
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class DisjunctivePredicateToJoinRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
MetadataProvider metadataProvider = (MetadataProvider) context.getMetadataProvider();
if (metadataProvider.isBlockingOperatorDisabled()) {
return false;
}
SelectOperator select;
if ((select = asSelectOperator(opRef)) == null) {
return false;
}
AbstractFunctionCallExpression condEx;
if ((condEx = asFunctionCallExpression(select.getCondition(), AlgebricksBuiltinFunctions.OR)) == null) {
return false;
}
List<Mutable<ILogicalExpression>> args = condEx.getArguments();
VariableReferenceExpression varEx = null;
IAType valType = null;
HashSet<AsterixConstantValue> values = new HashSet<AsterixConstantValue>();
for (Mutable<ILogicalExpression> arg : args) {
AbstractFunctionCallExpression fctCall;
if ((fctCall = asFunctionCallExpression(arg, AlgebricksBuiltinFunctions.EQ)) == null) {
return false;
}
boolean haveConst = false;
boolean haveVar = false;
List<Mutable<ILogicalExpression>> fctArgs = fctCall.getArguments();
for (Mutable<ILogicalExpression> fctArg : fctArgs) {
final ILogicalExpression argExpr = fctArg.getValue();
switch(argExpr.getExpressionTag()) {
case CONSTANT:
haveConst = true;
AsterixConstantValue value = (AsterixConstantValue) ((ConstantExpression) argExpr).getValue();
if (valType == null) {
valType = value.getObject().getType();
} else if (!isCompatible(valType, value.getObject().getType())) {
return false;
}
values.add(value);
break;
case VARIABLE:
haveVar = true;
final VariableReferenceExpression varArg = (VariableReferenceExpression) argExpr;
if (varEx == null) {
varEx = varArg;
} else if (!varEx.getVariableReference().equals(varArg.getVariableReference())) {
return false;
}
break;
default:
return false;
}
}
if (!(haveVar && haveConst)) {
return false;
}
}
AOrderedList list = new AOrderedList(new AOrderedListType(valType, "orderedlist"));
for (AsterixConstantValue value : values) {
list.add(value.getObject());
}
EmptyTupleSourceOperator ets = new EmptyTupleSourceOperator();
context.computeAndSetTypeEnvironmentForOperator(ets);
ILogicalExpression cExp = new ConstantExpression(new AsterixConstantValue(list));
Mutable<ILogicalExpression> mutCExp = new MutableObject<>(cExp);
IFunctionInfo scanFctInfo = BuiltinFunctions.getAsterixFunctionInfo(BuiltinFunctions.SCAN_COLLECTION);
UnnestingFunctionCallExpression scanExp = new UnnestingFunctionCallExpression(scanFctInfo, mutCExp);
LogicalVariable scanVar = context.newVar();
UnnestOperator unn = new UnnestOperator(scanVar, new MutableObject<>(scanExp));
unn.getInputs().add(new MutableObject<>(ets));
context.computeAndSetTypeEnvironmentForOperator(unn);
IFunctionInfo eqFctInfo = BuiltinFunctions.getAsterixFunctionInfo(AlgebricksBuiltinFunctions.EQ);
AbstractFunctionCallExpression eqExp = new ScalarFunctionCallExpression(eqFctInfo);
eqExp.getArguments().add(new MutableObject<>(new VariableReferenceExpression(scanVar)));
eqExp.getArguments().add(new MutableObject<>(varEx.cloneExpression()));
eqExp.getAnnotations().put(IndexedNLJoinExpressionAnnotation.INSTANCE, IndexedNLJoinExpressionAnnotation.INSTANCE);
BroadcastExpressionAnnotation bcast = new BroadcastExpressionAnnotation();
// Broadcast the OR predicates branch.
bcast.setObject(BroadcastExpressionAnnotation.BroadcastSide.LEFT);
eqExp.getAnnotations().put(BroadcastExpressionAnnotation.BROADCAST_ANNOTATION_KEY, bcast);
InnerJoinOperator jOp = new InnerJoinOperator(new MutableObject<>(eqExp));
jOp.getInputs().add(new MutableObject<>(unn));
jOp.getInputs().add(select.getInputs().get(0));
opRef.setValue(jOp);
context.computeAndSetTypeEnvironmentForOperator(jOp);
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class FuzzyEqRule method expandFuzzyEq.
private boolean expandFuzzyEq(Mutable<ILogicalExpression> expRef, IOptimizationContext context, IVariableTypeEnvironment env, MetadataProvider metadataProvider) throws AlgebricksException {
ILogicalExpression exp = expRef.getValue();
if (exp.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
boolean expanded = false;
AbstractFunctionCallExpression funcExp = (AbstractFunctionCallExpression) exp;
FunctionIdentifier fi = funcExp.getFunctionIdentifier();
if (fi.equals(BuiltinFunctions.FUZZY_EQ)) {
List<Mutable<ILogicalExpression>> inputExps = funcExp.getArguments();
String simFuncName = FuzzyUtils.getSimFunction(metadataProvider);
ArrayList<Mutable<ILogicalExpression>> similarityArgs = new ArrayList<Mutable<ILogicalExpression>>();
for (int i = 0; i < inputExps.size(); ++i) {
Mutable<ILogicalExpression> inputExpRef = inputExps.get(i);
similarityArgs.add(inputExpRef);
}
FunctionIdentifier simFunctionIdentifier = FuzzyUtils.getFunctionIdentifier(simFuncName);
ScalarFunctionCallExpression similarityExp = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(simFunctionIdentifier), similarityArgs);
// Add annotations from the original fuzzy-eq function.
similarityExp.getAnnotations().putAll(funcExp.getAnnotations());
ArrayList<Mutable<ILogicalExpression>> cmpArgs = new ArrayList<Mutable<ILogicalExpression>>();
cmpArgs.add(new MutableObject<ILogicalExpression>(similarityExp));
IAObject simThreshold = FuzzyUtils.getSimThreshold(metadataProvider, simFuncName);
cmpArgs.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(simThreshold))));
ScalarFunctionCallExpression cmpExpr = FuzzyUtils.getComparisonExpr(simFuncName, cmpArgs);
expRef.setValue(cmpExpr);
return true;
} else if (fi.equals(AlgebricksBuiltinFunctions.AND) || fi.equals(AlgebricksBuiltinFunctions.OR)) {
for (int i = 0; i < 2; i++) {
if (expandFuzzyEq(funcExp.getArguments().get(i), context, env, metadataProvider)) {
expanded = true;
}
}
}
return expanded;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class AddEquivalenceClassForRecordConstructorRule method propagateEquivalenceClassesForRecordConstructor.
@SuppressWarnings("unchecked")
private boolean propagateEquivalenceClassesForRecordConstructor(LogicalVariable recordVar, AbstractFunctionCallExpression funcExpr, AssignOperator assignOp, IOptimizationContext context) {
List<Mutable<ILogicalExpression>> argRefs = funcExpr.getArguments();
boolean changed = false;
// Only odd position arguments are field value expressions.
for (int parameterIndex = 1; parameterIndex < argRefs.size(); parameterIndex += 2) {
ILogicalExpression fieldExpr = argRefs.get(parameterIndex).getValue();
// Adds equivalent classes if a field is from a variable reference.
if (fieldExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
VariableReferenceExpression varExpr = (VariableReferenceExpression) fieldExpr;
LogicalVariable fieldVar = varExpr.getVariableReference();
Map<LogicalVariable, EquivalenceClass> ecs = context.getEquivalenceClassMap(assignOp);
if (ecs == null) {
ecs = new HashMap<LogicalVariable, EquivalenceClass>();
context.putEquivalenceClassMap(assignOp, ecs);
}
ILogicalExpression expr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_INDEX), new MutableObject<ILogicalExpression>(new VariableReferenceExpression(recordVar)), new MutableObject<ILogicalExpression>(// Every two parameters corresponds to a field.
new ConstantExpression(new AsterixConstantValue(new AInt32(parameterIndex / 2)))));
EquivalenceClass equivClass = new EquivalenceClass(Collections.singletonList(fieldVar), fieldVar, Collections.singletonList(expr));
ecs.put(fieldVar, equivClass);
changed = true;
}
}
return changed;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression in project asterixdb by apache.
the class ByNameToByHandleFieldAccessRule method byNameToByHandle.
private static void byNameToByHandle(AssignOperator fieldAccessOp, IOptimizationContext context) {
Mutable<ILogicalOperator> opUnder = fieldAccessOp.getInputs().get(0);
AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) fieldAccessOp.getExpressions().get(0).getValue();
ILogicalExpression a1 = fce.getArguments().get(0).getValue();
VariableReferenceExpression x;
if (a1.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
x = (VariableReferenceExpression) a1;
} else {
LogicalVariable var1 = context.newVar();
ArrayList<LogicalVariable> varArray = new ArrayList<LogicalVariable>(1);
varArray.add(var1);
ArrayList<Mutable<ILogicalExpression>> exprArray = new ArrayList<Mutable<ILogicalExpression>>(1);
exprArray.add(new MutableObject<ILogicalExpression>(a1));
AssignOperator assignVar = new AssignOperator(varArray, exprArray);
x = new VariableReferenceExpression(var1);
assignVar.getInputs().add(opUnder);
opUnder = new MutableObject<ILogicalOperator>(assignVar);
}
// let $t := type-of(x)
LogicalVariable t = context.newVar();
AbstractFunctionCallExpression typeOf = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.TYPE_OF));
typeOf.getArguments().add(new MutableObject<ILogicalExpression>(x));
AssignOperator typAssign = new AssignOperator(t, new MutableObject<ILogicalExpression>(typeOf));
typAssign.getInputs().add(opUnder);
// let $w := get-handle($t, path-expression)
LogicalVariable w = context.newVar();
AbstractFunctionCallExpression getHandle = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.GET_HANDLE));
getHandle.getArguments().add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(t)));
// the accessed field
getHandle.getArguments().add(new MutableObject<ILogicalExpression>(fce.getArguments().get(1).getValue()));
AssignOperator handleAssign = new AssignOperator(w, new MutableObject<ILogicalExpression>(getHandle));
handleAssign.getInputs().add(new MutableObject<ILogicalOperator>(typAssign));
// let $y := get-data(x, $w)
AbstractFunctionCallExpression getData = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.GET_DATA));
VariableReferenceExpression ref2 = new VariableReferenceExpression(x.getVariableReference());
getData.getArguments().add(new MutableObject<ILogicalExpression>(ref2));
getData.getArguments().add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(w)));
fieldAccessOp.getExpressions().get(0).setValue(getData);
List<Mutable<ILogicalOperator>> faInputs = fieldAccessOp.getInputs();
faInputs.clear();
faInputs.add(new MutableObject<ILogicalOperator>(handleAssign));
// fieldAccess.setAnnotation(OperatorAnnotation.FIELD_ACCESS,
// fce.getArguments().get(0));
fieldAccessOp.removeAnnotation(OperatorAnnotation.PUSHED_FIELD_ACCESS);
}
Aggregations