Skip to content

Commit

Permalink
[bugfix] Cleanup and fix issues with setting the parent Expression of…
Browse files Browse the repository at this point in the history
… an Expression
  • Loading branch information
adamretter committed Dec 1, 2024
1 parent 5e8d3a3 commit de45c85
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 52 deletions.
17 changes: 11 additions & 6 deletions exist-core/src/main/java/org/exist/xquery/BinaryOp.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/*
* Copyright (C) 2014 Evolved Binary Ltd
*
* Changes made by Evolved Binary are proprietary and are not Open Source.
*
* NOTE: Parts of this file contain code from The eXist-db Authors.
* The original license header is included below.
*
* ----------------------------------------------------------------------------
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -60,12 +69,8 @@ public void setContextDocSet(DocumentSet contextSet) {
getRight().setContextDocSet(contextSet);
}

/*
* (non-Javadoc)
*
* @see org.exist.xquery.PathExpr#analyze(org.exist.xquery.Expression)
*/
public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
@Override
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
inPredicate = (contextInfo.getFlags() & IN_PREDICATE) != 0;
contextId = contextInfo.getContextId();
inWhereClause = (contextInfo.getFlags() & IN_WHERE_CLAUSE) != 0;
Expand Down
25 changes: 10 additions & 15 deletions exist-core/src/main/java/org/exist/xquery/Function.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/*
* Copyright (C) 2014 Evolved Binary Ltd
*
* Changes made by Evolved Binary are proprietary and are not Open Source.
*
* NOTE: Parts of this file contain code from The eXist-db Authors.
* The original license header is included below.
*
* ----------------------------------------------------------------------------
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -69,11 +78,6 @@ public abstract class Function extends PathExpr {
*/
private FunctionSignature mySignature;

/**
* The parent expression from which this function is called.
*/
private Expression parent;

/**
* Flag to indicate if argument types are statically checked.
* This is set to true by default (meaning: no further checks needed).
Expand Down Expand Up @@ -193,16 +197,6 @@ public void setParent(final Expression parent) {
this.parent = parent;
}

/**
* Returns the expression from which this function gets called.
*
* @return the parent expression
*/
@Override
public Expression getParent() {
return parent;
}

/**
* Set the (static) arguments for this function from a list of expressions.
*
Expand Down Expand Up @@ -394,6 +388,7 @@ protected Tuple2<Expression, Integer> strictCheckArgumentType(Expression argumen

@Override
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
this.parent = contextInfo.getParent();
inPredicate = (contextInfo.getFlags() & IN_PREDICATE) > 0;
unordered = (contextInfo.getFlags() & UNORDERED) > 0;
contextId = contextInfo.getContextId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,9 @@ public GeneralComparison( XQueryContext context, Expression left, Expression rig
}
}

/* (non-Javadoc)
* @see org.exist.xquery.BinaryOp#analyze(org.exist.xquery.AnalyzeContextInfo)
*/
public void analyze( AnalyzeContextInfo contextInfo ) throws XPathException
{
@Override
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
this.parent = contextInfo.getParent();
contextInfo.addFlag( NEED_INDEX_INFO );
contextInfo.setParent( this );
super.analyze( contextInfo );
Expand Down
12 changes: 11 additions & 1 deletion exist-core/src/main/java/org/exist/xquery/LocationStep.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/*
* Copyright (C) 2014 Evolved Binary Ltd
*
* Changes made by Evolved Binary are proprietary and are not Open Source.
*
* NOTE: Parts of this file contain code from The eXist-db Authors.
* The original license header is included below.
*
* ----------------------------------------------------------------------------
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -1192,7 +1201,8 @@ protected DocumentSet getDocumentSet(final NodeSet contextSet) {
*
* @return the parent expression
*/
public Expression getParentExpression() {
@Override
public Expression getParent() {
return this.parent;
}

Expand Down
21 changes: 11 additions & 10 deletions exist-core/src/main/java/org/exist/xquery/LogicalOp.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/*
* Copyright (C) 2014 Evolved Binary Ltd
*
* Changes made by Evolved Binary are proprietary and are not Open Source.
*
* NOTE: Parts of this file contain code from The eXist-db Authors.
* The original license header is included below.
*
* ----------------------------------------------------------------------------
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -41,8 +50,6 @@ public abstract class LogicalOp extends BinaryOp {
*/
protected boolean optimize = false;
protected boolean rewritable = false;

protected Expression parent;

public LogicalOp(XQueryContext context) {
super(context);
Expand All @@ -56,10 +63,8 @@ public abstract Sequence eval(
Item contextItem)
throws XPathException;

/* (non-Javadoc)
* @see org.exist.xquery.BinaryOp#analyze(org.exist.xquery.Expression, int)
*/
public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
@Override
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
this.parent = contextInfo.getParent();
super.analyze(contextInfo);
//To optimize, we want nodes
Expand Down Expand Up @@ -102,10 +107,6 @@ public int getDependencies() {
else
{return Dependency.CONTEXT_SET;}
}

public Expression getParent() {
return this.parent;
}

public boolean isRewritable() {
return rewritable;
Expand Down
13 changes: 12 additions & 1 deletion exist-core/src/main/java/org/exist/xquery/OpNumeric.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/*
* Copyright (C) 2014 Evolved Binary Ltd
*
* Changes made by Evolved Binary are proprietary and are not Open Source.
*
* NOTE: Parts of this file contain code from The eXist-db Authors.
* The original license header is included below.
*
* ----------------------------------------------------------------------------
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -98,7 +107,9 @@ public int returnsType() {
return returnType;
}

public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
@Override
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
this.parent = contextInfo.getParent();
super.analyze(contextInfo);
contextInfo.setStaticReturnType(returnType);
}
Expand Down
11 changes: 10 additions & 1 deletion exist-core/src/main/java/org/exist/xquery/Optimizer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/*
* Copyright (C) 2014 Evolved Binary Ltd
*
* Changes made by Evolved Binary are proprietary and are not Open Source.
*
* NOTE: Parts of this file contain code from The eXist-db Authors.
* The original license header is included below.
*
* ----------------------------------------------------------------------------
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -114,7 +123,7 @@ public void visitLocationStep(final LocationStep locationStep) {
}
}

final Expression parent = locationStep.getParentExpression();
final Expression parent = locationStep.getParent();

if (optimize) {
// we found at least one Optimizable. Rewrite the whole expression and
Expand Down
11 changes: 10 additions & 1 deletion exist-core/src/main/java/org/exist/xquery/PathExpr.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/*
* Copyright (C) 2014 Evolved Binary Ltd
*
* Changes made by Evolved Binary are proprietary and are not Open Source.
*
* NOTE: Parts of this file contain code from The eXist-db Authors.
* The original license header is included below.
*
* ----------------------------------------------------------------------------
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -182,7 +191,7 @@ public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException
if (i > 1) {
contextInfo.setContextStep(steps.get(i - 1));
}
contextInfo.setParent(this);
contextInfo.setParent(this); // TODO(AR) it seems we have to repeatedly mutate the parent contextInfo here... it would be better to have some immutable structure!
expr.analyze(contextInfo);
}
}
Expand Down
19 changes: 10 additions & 9 deletions exist-core/src/main/java/org/exist/xquery/Predicate.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/*
* Copyright (C) 2014 Evolved Binary Ltd
*
* Changes made by Evolved Binary are proprietary and are not Open Source.
*
* NOTE: Parts of this file contain code from The eXist-db Authors.
* The original license header is included below.
*
* ----------------------------------------------------------------------------
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -64,8 +73,6 @@ public enum ExecutionMode {

private int outerContextId;

private Expression parent;

public Predicate(final XQueryContext context) {
super(context);
}
Expand All @@ -92,7 +99,6 @@ public int getDependencies() {

@Override
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
parent = contextInfo.getParent();
AnalyzeContextInfo newContextInfo = createContext(contextInfo);
super.analyze(newContextInfo);
final Expression inner = getSubExpression(0);
Expand Down Expand Up @@ -134,7 +140,7 @@ private AnalyzeContextInfo createContext(final AnalyzeContextInfo contextInfo) {
outerContextId = newContextInfo.getContextId();
newContextInfo.setContextId(getExpressionId());
newContextInfo.setStaticType(contextInfo.getStaticType());
newContextInfo.setParent(this);
newContextInfo.setParent(contextInfo.getParent());
return newContextInfo;
}

Expand Down Expand Up @@ -636,11 +642,6 @@ public void resetState(final boolean postOptimization) {
}
}

@Override
public Expression getParent() {
return parent;
}

@Override
public void accept(final ExpressionVisitor visitor) {
visitor.visitPredicate(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/*
* Copyright (C) 2014 Evolved Binary Ltd
*
* Changes made by Evolved Binary are proprietary and are not Open Source.
*
* NOTE: Parts of this file contain code from The eXist-db Authors.
* The original license header is included below.
*
* ----------------------------------------------------------------------------
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -73,7 +82,7 @@ public void before(final XQueryContext context, final Expression expression, fin
final LocationStep locationStep = (LocationStep) expression;
@Nullable final Predicate[] preds = locationStep.getPredicates();
if (preds != null) {
final Expression parentExpr = locationStep.getParentExpression();
final Expression parentExpr = locationStep.getParent();
if (!(parentExpr instanceof RewritableExpression)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/*
* Copyright (C) 2014 Evolved Binary Ltd
*
* Changes made by Evolved Binary are proprietary and are not Open Source.
*
* NOTE: Parts of this file contain code from The eXist-db Authors.
* The original license header is included below.
*
* ----------------------------------------------------------------------------
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -51,7 +60,7 @@ public Pragma rewriteLocationStep(final LocationStep locationStep) throws XPathE

@Nullable final Predicate[] preds = locationStep.getPredicates();
if (preds != null) {
final Expression parentExpr = locationStep.getParentExpression();
final Expression parentExpr = locationStep.getParent();
if ((parentExpr instanceof RewritableExpression)) {
// Step 1: replace all optimizable expressions within predicates with
// calls to the range functions. If those functions are used or not will
Expand Down Expand Up @@ -255,7 +264,7 @@ protected static NodePath toNodePath(List<LocationStep> steps) {
}

protected static List<LocationStep> getPrecedingSteps(LocationStep current) {
Expression parentExpr = current.getParentExpression();
Expression parentExpr = current.getParent();
if (!(parentExpr instanceof RewritableExpression)) {
return null;
}
Expand Down

0 comments on commit de45c85

Please sign in to comment.