use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression in project asterixdb by apache.
the class RecordRemoveFieldsTypeComputer method getListFromExpression.
private List<String> getListFromExpression(String funcName, ILogicalExpression expression) throws AlgebricksException {
AbstractFunctionCallExpression funcExp = (AbstractFunctionCallExpression) expression;
List<Mutable<ILogicalExpression>> args = funcExp.getArguments();
List<String> list = new ArrayList<>();
for (Mutable<ILogicalExpression> arg : args) {
// At this point all elements has to be a constant
// Input list has only one level of nesting (list of list or list of strings)
ConstantExpression ce = (ConstantExpression) arg.getValue();
if (!(ce.getValue() instanceof AsterixConstantValue)) {
throw new InvalidExpressionException(funcName, 1, ce, LogicalExpressionTag.CONSTANT);
}
IAObject item = ((AsterixConstantValue) ce.getValue()).getObject();
ATypeTag type = item.getType().getTypeTag();
if (type == ATypeTag.STRING) {
list.add(((AString) item).getStringValue());
} else {
throw new UnsupportedTypeException(funcName, type);
}
}
return list;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression 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.ConstantExpression in project asterixdb by apache.
the class FullTextContainsParameterCheckRule method setDefaultValueForThirdParameter.
/**
* Sets the default option value(s) when a user doesn't provide any option.
*/
void setDefaultValueForThirdParameter(List<Mutable<ILogicalExpression>> newArgs) throws AlgebricksException {
// Sets the search mode option: the default option is conjunctive search.
ILogicalExpression searchModeOptionExpr = new ConstantExpression(new AsterixConstantValue(new AString(FullTextContainsDescriptor.SEARCH_MODE_OPTION)));
ILogicalExpression searchModeValExpr = new ConstantExpression(new AsterixConstantValue(new AString(FullTextContainsDescriptor.CONJUNCTIVE_SEARCH_MODE_OPTION)));
// Add this option as arguments to the ftcontains().
newArgs.add(new MutableObject<ILogicalExpression>(searchModeOptionExpr));
newArgs.add(new MutableObject<ILogicalExpression>(searchModeValExpr));
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression 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.ConstantExpression 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;
}
Aggregations