use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.
the class InvertedIndexAccessMethod method createPrimaryKeysEqJoinCondition.
private Mutable<ILogicalExpression> createPrimaryKeysEqJoinCondition(List<LogicalVariable> originalSubTreePKs, List<LogicalVariable> surrogateSubTreePKs) {
List<Mutable<ILogicalExpression>> eqExprs = new ArrayList<Mutable<ILogicalExpression>>();
int numPKVars = originalSubTreePKs.size();
for (int i = 0; i < numPKVars; i++) {
List<Mutable<ILogicalExpression>> args = new ArrayList<Mutable<ILogicalExpression>>();
args.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(surrogateSubTreePKs.get(i))));
args.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(originalSubTreePKs.get(i))));
ILogicalExpression eqFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(AlgebricksBuiltinFunctions.EQ), args);
eqExprs.add(new MutableObject<ILogicalExpression>(eqFunc));
}
if (eqExprs.size() == 1) {
return eqExprs.get(0);
} else {
ILogicalExpression andFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(AlgebricksBuiltinFunctions.AND), eqExprs);
return new MutableObject<ILogicalExpression>(andFunc);
}
}
use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.
the class IntroduceGroupByForSubplanRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op0 = (AbstractLogicalOperator) opRef.getValue();
if (op0.getOperatorTag() != LogicalOperatorTag.SUBPLAN) {
return false;
}
SubplanOperator subplan = (SubplanOperator) op0;
Iterator<ILogicalPlan> plansIter = subplan.getNestedPlans().iterator();
ILogicalPlan p = null;
while (plansIter.hasNext()) {
p = plansIter.next();
}
if (p == null) {
return false;
}
if (p.getRoots().size() != 1) {
return false;
}
Mutable<ILogicalOperator> subplanRoot = p.getRoots().get(0);
AbstractLogicalOperator op1 = (AbstractLogicalOperator) subplanRoot.getValue();
Mutable<ILogicalOperator> botRef = subplanRoot;
AbstractLogicalOperator op2;
// Project is optional
if (op1.getOperatorTag() != LogicalOperatorTag.PROJECT) {
op2 = op1;
} else {
ProjectOperator project = (ProjectOperator) op1;
botRef = project.getInputs().get(0);
op2 = (AbstractLogicalOperator) botRef.getValue();
}
if (op2.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
return false;
}
AggregateOperator aggregate = (AggregateOperator) op2;
Set<LogicalVariable> free = new HashSet<LogicalVariable>();
VariableUtilities.getUsedVariables(aggregate, free);
Mutable<ILogicalOperator> op3Ref = aggregate.getInputs().get(0);
AbstractLogicalOperator op3 = (AbstractLogicalOperator) op3Ref.getValue();
while (op3.getInputs().size() == 1) {
Set<LogicalVariable> prod = new HashSet<LogicalVariable>();
VariableUtilities.getProducedVariables(op3, prod);
free.removeAll(prod);
VariableUtilities.getUsedVariables(op3, free);
botRef = op3Ref;
op3Ref = op3.getInputs().get(0);
op3 = (AbstractLogicalOperator) op3Ref.getValue();
}
if (op3.getOperatorTag() != LogicalOperatorTag.INNERJOIN && op3.getOperatorTag() != LogicalOperatorTag.LEFTOUTERJOIN) {
return false;
}
AbstractBinaryJoinOperator join = (AbstractBinaryJoinOperator) op3;
if (join.getCondition().getValue() == ConstantExpression.TRUE) {
return false;
}
VariableUtilities.getUsedVariables(join, free);
AbstractLogicalOperator b0 = (AbstractLogicalOperator) join.getInputs().get(0).getValue();
// see if there's an NTS at the end of the pipeline
NestedTupleSourceOperator outerNts = getNts(b0);
if (outerNts == null) {
AbstractLogicalOperator b1 = (AbstractLogicalOperator) join.getInputs().get(1).getValue();
outerNts = getNts(b1);
if (outerNts == null) {
return false;
}
}
Set<LogicalVariable> pkVars = computeGbyVars(outerNts, free, context);
if (pkVars == null || pkVars.size() < 1) {
// there is no non-trivial primary key, group-by keys are all live variables
// that were produced by descendant or self
ILogicalOperator subplanInput = subplan.getInputs().get(0).getValue();
pkVars = new HashSet<LogicalVariable>();
//get live variables
VariableUtilities.getLiveVariables(subplanInput, pkVars);
//get produced variables
Set<LogicalVariable> producedVars = new HashSet<LogicalVariable>();
VariableUtilities.getProducedVariablesInDescendantsAndSelf(subplanInput, producedVars);
//retain the intersection
pkVars.retainAll(producedVars);
}
AlgebricksConfig.ALGEBRICKS_LOGGER.fine("Found FD for introducing group-by: " + pkVars);
Mutable<ILogicalOperator> rightRef = join.getInputs().get(1);
LogicalVariable testForNull = null;
AbstractLogicalOperator right = (AbstractLogicalOperator) rightRef.getValue();
switch(right.getOperatorTag()) {
case UNNEST:
{
UnnestOperator innerUnnest = (UnnestOperator) right;
// Select [ $y != null ]
testForNull = innerUnnest.getVariable();
break;
}
case RUNNINGAGGREGATE:
{
ILogicalOperator inputToRunningAggregate = right.getInputs().get(0).getValue();
Set<LogicalVariable> producedVars = new ListSet<LogicalVariable>();
VariableUtilities.getProducedVariables(inputToRunningAggregate, producedVars);
if (!producedVars.isEmpty()) {
// Select [ $y != null ]
testForNull = producedVars.iterator().next();
}
break;
}
case DATASOURCESCAN:
{
DataSourceScanOperator innerScan = (DataSourceScanOperator) right;
// Select [ $y != null ]
if (innerScan.getVariables().size() == 1) {
testForNull = innerScan.getVariables().get(0);
}
break;
}
default:
break;
}
if (testForNull == null) {
testForNull = context.newVar();
AssignOperator tmpAsgn = new AssignOperator(testForNull, new MutableObject<ILogicalExpression>(ConstantExpression.TRUE));
tmpAsgn.getInputs().add(new MutableObject<ILogicalOperator>(rightRef.getValue()));
rightRef.setValue(tmpAsgn);
context.computeAndSetTypeEnvironmentForOperator(tmpAsgn);
}
IFunctionInfo finfoEq = context.getMetadataProvider().lookupFunction(AlgebricksBuiltinFunctions.IS_MISSING);
ILogicalExpression isNullTest = new ScalarFunctionCallExpression(finfoEq, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(testForNull)));
IFunctionInfo finfoNot = context.getMetadataProvider().lookupFunction(AlgebricksBuiltinFunctions.NOT);
ScalarFunctionCallExpression nonNullTest = new ScalarFunctionCallExpression(finfoNot, new MutableObject<ILogicalExpression>(isNullTest));
SelectOperator selectNonNull = new SelectOperator(new MutableObject<ILogicalExpression>(nonNullTest), false, null);
GroupByOperator g = new GroupByOperator();
Mutable<ILogicalOperator> newSubplanRef = new MutableObject<ILogicalOperator>(subplan);
NestedTupleSourceOperator nts = new NestedTupleSourceOperator(new MutableObject<ILogicalOperator>(g));
opRef.setValue(g);
selectNonNull.getInputs().add(new MutableObject<ILogicalOperator>(nts));
List<Mutable<ILogicalOperator>> prodInpList = botRef.getValue().getInputs();
prodInpList.clear();
prodInpList.add(new MutableObject<ILogicalOperator>(selectNonNull));
ILogicalPlan gPlan = new ALogicalPlanImpl(new MutableObject<ILogicalOperator>(subplanRoot.getValue()));
g.getNestedPlans().add(gPlan);
subplanRoot.setValue(op3Ref.getValue());
g.getInputs().add(newSubplanRef);
HashSet<LogicalVariable> underVars = new HashSet<LogicalVariable>();
VariableUtilities.getLiveVariables(subplan.getInputs().get(0).getValue(), underVars);
underVars.removeAll(pkVars);
Map<LogicalVariable, LogicalVariable> mappedVars = buildVarExprList(pkVars, context, g, g.getGroupByList());
context.updatePrimaryKeys(mappedVars);
for (LogicalVariable uv : underVars) {
g.getDecorList().add(new Pair<LogicalVariable, Mutable<ILogicalExpression>>(null, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(uv))));
}
OperatorPropertiesUtil.typeOpRec(subplanRoot, context);
OperatorPropertiesUtil.typeOpRec(gPlan.getRoots().get(0), context);
context.computeAndSetTypeEnvironmentForOperator(g);
return true;
}
use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.
the class NestedSubplanToJoinRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
if (context.checkIfInDontApplySet(this, opRef.getValue()))
return false;
context.addToDontApplySet(this, opRef.getValue());
ILogicalOperator op1 = opRef.getValue();
if (op1.getInputs().size() == 0) {
return false;
}
boolean rewritten = false;
for (int index = 0; index < op1.getInputs().size(); index++) {
AbstractLogicalOperator child = (AbstractLogicalOperator) op1.getInputs().get(index).getValue();
if (child.getOperatorTag() != LogicalOperatorTag.SUBPLAN) {
continue;
}
AbstractOperatorWithNestedPlans subplan = (AbstractOperatorWithNestedPlans) child;
Set<LogicalVariable> freeVars = new HashSet<LogicalVariable>();
OperatorPropertiesUtil.getFreeVariablesInSubplans(subplan, freeVars);
if (!freeVars.isEmpty()) {
/**
* the subplan is correlated with the outer plan, other rules can deal with it
*/
continue;
}
/** get the input operator of the subplan operator */
ILogicalOperator subplanInput = subplan.getInputs().get(0).getValue();
AbstractLogicalOperator subplanInputOp = (AbstractLogicalOperator) subplanInput;
/** If the other join branch is a trivial plan, do not do the rewriting. */
if (subplanInputOp.getOperatorTag() == LogicalOperatorTag.EMPTYTUPLESOURCE) {
continue;
}
/** get all nested top operators */
List<ILogicalPlan> nestedPlans = subplan.getNestedPlans();
List<Mutable<ILogicalOperator>> nestedRoots = new ArrayList<Mutable<ILogicalOperator>>();
for (ILogicalPlan nestedPlan : nestedPlans) {
nestedRoots.addAll(nestedPlan.getRoots());
}
if (nestedRoots.size() == 0) {
/** there is no nested top operators */
continue;
}
/**
* Expends the input and roots into a DAG of nested loop joins.
* Though joins should be left-outer joins, a left-outer join with condition TRUE is equivalent to an inner join.
**/
Mutable<ILogicalExpression> expr = new MutableObject<ILogicalExpression>(ConstantExpression.TRUE);
Mutable<ILogicalOperator> nestedRootRef = nestedRoots.get(0);
ILogicalOperator join = new InnerJoinOperator(expr, new MutableObject<ILogicalOperator>(subplanInput), nestedRootRef);
/** rewrite the nested tuple source to be empty tuple source */
rewriteNestedTupleSource(nestedRootRef, context);
for (int i = 1; i < nestedRoots.size(); i++) {
join = new InnerJoinOperator(expr, new MutableObject<ILogicalOperator>(join), nestedRoots.get(i));
}
op1.getInputs().get(index).setValue(join);
context.computeAndSetTypeEnvironmentForOperator(join);
rewritten = true;
}
return rewritten;
}
use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project oap by oaplatform.
the class SchemaPath method traverse.
public static Result traverse(SchemaAST root, String path) {
SchemaAST schemaAST = root;
val additionalProperties = new MutableObject<Boolean>(null);
final Supplier<Result> empty = () -> new Result(Optional.empty(), Optional.ofNullable(additionalProperties.getValue()));
for (val item : StringUtils.split(path, '.')) {
if (schemaAST instanceof ObjectSchemaAST) {
val objectSchemaAST = (ObjectSchemaAST) schemaAST;
schemaAST = objectSchemaAST.properties.get(item);
objectSchemaAST.additionalProperties.ifPresent(additionalProperties::setValue);
if (schemaAST == null)
return empty.get();
} else if (schemaAST instanceof ArraySchemaAST) {
if (!"items".equals(item))
return empty.get();
schemaAST = ((ArraySchemaAST) schemaAST).items;
} else {
return empty.get();
}
}
return new Result(Optional.of(schemaAST), Optional.ofNullable(additionalProperties.getValue()));
}
use of org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject in project oap by oaplatform.
the class JavaCTemplate method addPathOr.
private void addPathOr(Class<T> clazz, String delimiter, StringBuilder c, AtomicInteger num, FieldStack fields, boolean last, AtomicInteger tab, String[] orPath, int orIndex, TLine line) throws NoSuchFieldException, NoSuchMethodException {
val currentPath = orPath[orIndex].trim();
val m = mapper.get(currentPath);
if (m != null) {
printDefaultValue(tab(c, tab), m.get(), line);
} else {
int sp = 0;
StringBuilder newPath = new StringBuilder("s.");
MutableObject<Type> lc = new MutableObject<>(clazz);
AtomicInteger psp = new AtomicInteger(0);
AtomicInteger opts = new AtomicInteger(0);
while (sp >= 0) {
sp = currentPath.indexOf('.', sp + 1);
if (sp > 0) {
Pair<Type, String> pnp = fields.computeIfAbsent(currentPath.substring(0, sp), Try.map((key) -> {
String prefix = StringUtils.trim(psp.get() > 1 ? key.substring(0, psp.get() - 1) : "");
String suffix = StringUtils.trim(key.substring(psp.get()));
boolean optional = isOptional(lc.getValue());
Type declaredFieldType = getDeclaredFieldOrFunctionType(optional ? getOptionalArgumentType(lc.getValue()) : lc.getValue(), suffix);
String classType = toJavaType(declaredFieldType);
String field = "field" + num.incrementAndGet();
Optional<Pair<Type, String>> rfP = Optional.ofNullable(fields.get(prefix));
String rf = rfP.map(p -> p._2 + (optional ? ".get()" : "") + "." + suffix).orElse("s." + key);
if (optional) {
tab(c, tab).append("if( ").append(rfP.map(p -> p._2).orElse("s")).append(".isPresent() ) {\n");
opts.incrementAndGet();
tabInc(tab);
fields.up();
}
tab(c, tab).append(" ").append(classType).append(" ").append(field).append(" = ").append(rf).append(";\n");
lc.setValue(declaredFieldType);
return __(lc.getValue(), field);
}));
newPath = new StringBuilder(pnp._2 + ".");
lc.setValue(pnp._1);
psp.set(sp + 1);
} else {
newPath.append(currentPath.substring(psp.get()));
}
}
int in = newPath.toString().lastIndexOf('.');
String pField = in > 0 ? newPath.substring(0, in) : newPath.toString();
String cField = newPath.substring(in + 1);
boolean isJoin = cField.startsWith("{");
String[] cFields = isJoin ? StringUtils.split(cField.substring(1, cField.length() - 1), ',') : new String[] { cField };
Type parentClass = lc.getValue();
boolean isOptionalParent = isOptional(parentClass) && !cField.startsWith("isPresent");
String optField = null;
if (isOptionalParent) {
opts.incrementAndGet();
tab(c, tab).append("if( ").append(pField).append(".isPresent() ) {\n");
fields.up();
tabInc(tab);
parentClass = getOptionalArgumentType(parentClass);
optField = "opt" + num.incrementAndGet();
tab(c, tab).append(" ").append(toJavaType(parentClass)).append(" ").append(optField).append(" = ").append(pField).append(".get();\n");
}
for (int i = 0; i < cFields.length; i++) {
cField = StringUtils.trim(cFields[i]);
Optional<Join> join = isJoin ? Optional.of(new Join(i, cFields.length)) : Optional.empty();
if (cField.startsWith("\"")) {
tab(c.append("\n"), tab);
map.map(c, String.class, line, cField, delimiter, join);
} else {
if (isOptionalParent) {
newPath = new StringBuilder(in > 0 ? optField + "." + cField : cField);
} else {
newPath = new StringBuilder(in > 0 ? pField + "." + cField : "s." + cField);
}
Type cc = in > 0 ? getDeclaredFieldOrFunctionType(parentClass, cField) : parentClass;
add(c, num, newPath.toString(), cc, parentClass, true, tab, orPath, orIndex, clazz, delimiter, fields, last || (i < cFields.length - 1), line, join);
}
}
c.append("\n");
for (int i = 0; i < opts.get(); i++) {
fields.down();
tabDec(tab);
tab(c, tab).append("} else {\n");
fields.up();
tabInc(tab);
if (orIndex + 1 < orPath.length) {
addPathOr(clazz, delimiter, c, num, fields, last, new AtomicInteger(tab.get() + 2), orPath, orIndex + 1, line);
} else {
printDefaultValue(c, line.defaultValue, line);
if (!map.ignoreDefaultValue())
printDelimiter(delimiter, c, last, tab);
}
tabDec(tab);
fields.down();
tab(c, tab).append("}\n");
}
}
}
Aggregations