From de45c856680f3d95d2c5acffca5dd167c8ae2ac4 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Sat, 2 Nov 2024 14:16:49 +0100 Subject: [PATCH] [bugfix] Cleanup and fix issues with setting the parent Expression of an Expression --- .../main/java/org/exist/xquery/BinaryOp.java | 17 ++++++++----- .../main/java/org/exist/xquery/Function.java | 25 ++++++++----------- .../org/exist/xquery/GeneralComparison.java | 8 +++--- .../java/org/exist/xquery/LocationStep.java | 12 ++++++++- .../main/java/org/exist/xquery/LogicalOp.java | 21 ++++++++-------- .../main/java/org/exist/xquery/OpNumeric.java | 13 +++++++++- .../main/java/org/exist/xquery/Optimizer.java | 11 +++++++- .../main/java/org/exist/xquery/PathExpr.java | 11 +++++++- .../main/java/org/exist/xquery/Predicate.java | 19 +++++++------- .../modules/range/OptimizeFieldPragma.java | 11 +++++++- .../modules/range/RangeQueryRewriter.java | 13 ++++++++-- 11 files changed, 109 insertions(+), 52 deletions(-) diff --git a/exist-core/src/main/java/org/exist/xquery/BinaryOp.java b/exist-core/src/main/java/org/exist/xquery/BinaryOp.java index 6bdbf047a4c..c3bc1734af1 100644 --- a/exist-core/src/main/java/org/exist/xquery/BinaryOp.java +++ b/exist-core/src/main/java/org/exist/xquery/BinaryOp.java @@ -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 * @@ -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; diff --git a/exist-core/src/main/java/org/exist/xquery/Function.java b/exist-core/src/main/java/org/exist/xquery/Function.java index 617af471d30..69f4907be0b 100644 --- a/exist-core/src/main/java/org/exist/xquery/Function.java +++ b/exist-core/src/main/java/org/exist/xquery/Function.java @@ -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 * @@ -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). @@ -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. * @@ -394,6 +388,7 @@ protected Tuple2 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(); diff --git a/exist-core/src/main/java/org/exist/xquery/GeneralComparison.java b/exist-core/src/main/java/org/exist/xquery/GeneralComparison.java index 9b1eebe7fb6..bac95406dcc 100644 --- a/exist-core/src/main/java/org/exist/xquery/GeneralComparison.java +++ b/exist-core/src/main/java/org/exist/xquery/GeneralComparison.java @@ -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 ); diff --git a/exist-core/src/main/java/org/exist/xquery/LocationStep.java b/exist-core/src/main/java/org/exist/xquery/LocationStep.java index bc779a6a481..5da7236a84a 100644 --- a/exist-core/src/main/java/org/exist/xquery/LocationStep.java +++ b/exist-core/src/main/java/org/exist/xquery/LocationStep.java @@ -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 * @@ -1192,7 +1201,8 @@ protected DocumentSet getDocumentSet(final NodeSet contextSet) { * * @return the parent expression */ - public Expression getParentExpression() { + @Override + public Expression getParent() { return this.parent; } diff --git a/exist-core/src/main/java/org/exist/xquery/LogicalOp.java b/exist-core/src/main/java/org/exist/xquery/LogicalOp.java index c82a2a1c363..b7c098b7bd6 100644 --- a/exist-core/src/main/java/org/exist/xquery/LogicalOp.java +++ b/exist-core/src/main/java/org/exist/xquery/LogicalOp.java @@ -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 * @@ -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); @@ -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 @@ -102,10 +107,6 @@ public int getDependencies() { else {return Dependency.CONTEXT_SET;} } - - public Expression getParent() { - return this.parent; - } public boolean isRewritable() { return rewritable; diff --git a/exist-core/src/main/java/org/exist/xquery/OpNumeric.java b/exist-core/src/main/java/org/exist/xquery/OpNumeric.java index eefaf3e48a1..7004a4ef943 100644 --- a/exist-core/src/main/java/org/exist/xquery/OpNumeric.java +++ b/exist-core/src/main/java/org/exist/xquery/OpNumeric.java @@ -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 * @@ -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); } diff --git a/exist-core/src/main/java/org/exist/xquery/Optimizer.java b/exist-core/src/main/java/org/exist/xquery/Optimizer.java index 7905e27c6dc..53b4f334eae 100644 --- a/exist-core/src/main/java/org/exist/xquery/Optimizer.java +++ b/exist-core/src/main/java/org/exist/xquery/Optimizer.java @@ -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 * @@ -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 diff --git a/exist-core/src/main/java/org/exist/xquery/PathExpr.java b/exist-core/src/main/java/org/exist/xquery/PathExpr.java index 818a30c5a29..c84352c185f 100644 --- a/exist-core/src/main/java/org/exist/xquery/PathExpr.java +++ b/exist-core/src/main/java/org/exist/xquery/PathExpr.java @@ -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 * @@ -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); } } diff --git a/exist-core/src/main/java/org/exist/xquery/Predicate.java b/exist-core/src/main/java/org/exist/xquery/Predicate.java index 715e44b7caa..48298e5a6e3 100644 --- a/exist-core/src/main/java/org/exist/xquery/Predicate.java +++ b/exist-core/src/main/java/org/exist/xquery/Predicate.java @@ -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 * @@ -64,8 +73,6 @@ public enum ExecutionMode { private int outerContextId; - private Expression parent; - public Predicate(final XQueryContext context) { super(context); } @@ -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); @@ -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; } @@ -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); diff --git a/extensions/indexes/range/src/main/java/org/exist/xquery/modules/range/OptimizeFieldPragma.java b/extensions/indexes/range/src/main/java/org/exist/xquery/modules/range/OptimizeFieldPragma.java index 2bf981842a5..e739eaee45e 100644 --- a/extensions/indexes/range/src/main/java/org/exist/xquery/modules/range/OptimizeFieldPragma.java +++ b/extensions/indexes/range/src/main/java/org/exist/xquery/modules/range/OptimizeFieldPragma.java @@ -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 * @@ -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; } diff --git a/extensions/indexes/range/src/main/java/org/exist/xquery/modules/range/RangeQueryRewriter.java b/extensions/indexes/range/src/main/java/org/exist/xquery/modules/range/RangeQueryRewriter.java index b9b050690e4..51c3f173015 100644 --- a/extensions/indexes/range/src/main/java/org/exist/xquery/modules/range/RangeQueryRewriter.java +++ b/extensions/indexes/range/src/main/java/org/exist/xquery/modules/range/RangeQueryRewriter.java @@ -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 * @@ -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 @@ -255,7 +264,7 @@ protected static NodePath toNodePath(List steps) { } protected static List getPrecedingSteps(LocationStep current) { - Expression parentExpr = current.getParentExpression(); + Expression parentExpr = current.getParent(); if (!(parentExpr instanceof RewritableExpression)) { return null; }