Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions engine/src/main/java/com/arcadedb/query/select/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,20 @@ else if (parsedLeft instanceof String string && string.startsWith("#"))
else
throw new IllegalArgumentException("Unsupported value " + parsedLeft);

final SelectOperator parsedOperator = SelectOperator.byName(condition.getString(1));
final String parsedOperatorName = condition.getString(1);
final SelectOperator parsedOperator = SelectOperator.byName(parsedOperatorName);
if (parsedOperator == null)
throw new IllegalArgumentException("Unsupported operator '" + parsedOperatorName + "' in condition " + condition);

// #8059: 'not' IS UNARY - IT NEGATES ITS LEFT OPERAND AND ITS RIGHT IS ALWAYS null (SEE SelectOperator.not) -
// WHILE A JSON CONDITION IS BY CONSTRUCTION THE BINARY TRIPLE [left, operator, right]. THERE IS NO WELL-DEFINED
// READING OF 'X not Y', AND THE TREE THIS USED TO BUILD FOR IT WAS EXECUTED AS A NEGATION OF A LEFT OPERAND WITH
// A RIGHT OPERAND SILENTLY DROPPED. REFUSE IT HERE INSTEAD OF HANDING THE CALLER A QUIETLY WRONG RESULT SET: A
// NEGATION IS EXPRESSED WITH THE COMPLEMENTARY OPERATOR ('<>', 'is null', 'is not null', ...)
if (parsedOperator == SelectOperator.not)
throw new IllegalArgumentException(
"Operator 'not' is unary and cannot be used in a JSON condition, which is always [left, operator, right]. "
+ "Use the complementary operator instead (for example '<>' for '=', 'is null' for 'is not null')");

if (parsedOperator.logicOperator)
setLogic(parsedOperator);
Expand Down Expand Up @@ -353,14 +366,27 @@ SelectWhereLeftBlock setLogic(final SelectOperator newLogicOperator) {
lastTreeElement.getParent().setRight(newNode);
lastTreeElement = newTreeElement;
} else {
// OR+ OPERATOR
// OR+ OPERATOR: THE NEW OPERATOR DOES NOT BIND MORE TIGHTLY THAN THE ONE ALREADY THERE, SO THE CURRENT
// SUBTREE IS PUSHED DOWN AND BECOMES THE LEFT CHILD OF A NODE CARRYING THE NEW OPERATOR
final SelectTreeNode currentParent = lastTreeElement.getParent();
// #8047: READ THE GRANDPARENT *BEFORE* THE SelectTreeNode CONSTRUCTOR RE-PARENTS currentParent UNDER newNode.
// THE CONSTRUCTOR ALREADY DOES TWO OF THE THREE THINGS THIS BRANCH NEEDS - IT SETS currentParent.parent TO
// newNode AND, THROUGH SelectTreeNode.setParent, MOVES THE GRANDPARENT'S OWN CHILD POINTER OVER TO newNode -
// SO THE ONLY THING LEFT IS newNode'S UPWARD LINK. READING currentParent.getParent() *AFTER* THE CONSTRUCTOR
// HANDED BACK newNode ITSELF, SO THE CALL REDUCED TO newNode.setParent(newNode) AND INSTALLED A SELF-PARENT
// CYCLE. THAT CYCLE THEN DEFEATED THE *NEXT* RE-PARENTING - setParent REWIRES THE GRANDPARENT BY TESTING
// this.parent.left/right == this, AND ON A SELF-PARENTED NODE BOTH TESTS COMPARE THE NODE AGAINST ITS OWN
// CHILDREN AND FAIL - SO THE NEXT NODE, AND EVERYTHING APPENDED TO IT, DANGLED OFF THE TREE rootTreeElement
// REFERS TO AND THE EXECUTOR NEVER SAW IT. FROM THE FIFTH CONDITION ON, AN 'OR' FOLLOWED BY THREE OR MORE
// 'AND's SILENTLY DISCARDED EVERY REMAINING CONDITION AND THE QUERY RETURNED MORE ROWS THAN THE PREDICATE
// ALLOWS
final SelectTreeNode grandParent = currentParent.getParent();
currentParent.setRight(newTreeElement);
final SelectTreeNode newNode = new SelectTreeNode(currentParent, newLogicOperator, null);
if (rootTreeElement.equals(currentParent))
rootTreeElement = newNode;
else
newNode.setParent(currentParent.getParent());
newNode.setParent(grandParent);
lastTreeElement = currentParent;
}
}
Expand Down
80 changes: 58 additions & 22 deletions engine/src/main/java/com/arcadedb/query/select/SelectExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -687,27 +687,9 @@ private void filterWithIndexesFinalNode(final SelectTreeNode node, final List<In
if (!CURSOR_BUILDABLE_OPERATORS.contains(node.operator))
return;

if (node.getParent().operator == SelectOperator.not)
// #6575: A LEAF DIRECTLY UNDER not WOULD OTHERWISE BUILD A "POSITIVE" CURSOR FOR THE UN-NEGATED CONDITION
// (E.G. AN eq CURSOR FOR NOT a = 'x' YIELDS THE RECORDS WHERE a = 'x' IS TRUE) - evaluateWhere() THEN
// REJECTS EVERY ONE OF THOSE CANDIDATES SINCE THEY ALL SATISFY THE POSITIVE CONDITION BY CONSTRUCTION, SO
// THE QUERY WOULD SILENTLY RETURN ZERO ROWS INSTEAD OF "EVERY RECORD WHERE a != 'x'". isTheNodeFullyIndexed()
// STILL SETS node.index HERE (SEE ITS not BRANCH), SO REFUSE THE CURSOR HERE INSTEAD, THE SAME WAY
// is_null/is_not_null LEAVES ARE ALREADY EXCLUDED THERE. NEITHER A FLUENT .not() NOR Select.json() REACH THIS
// TODAY, BUT THE DEFENSIVE POSTURE MUST HOLD THE MOMENT EITHER PATH OPENS UP.
if (!isCursorSoundOnPath(node))
return;

if (node.getParent().operator == SelectOperator.or) {
// UNDER AN 'OR' OPERATOR: BOTH SIDES MUST BE INDEXED, OTHERWISE CANNOT USE INDEXES
if (node != node.getParent().right) {
if (!isTheNodeFullyIndexed((SelectTreeNode) node.getParent().right))
return;
} else {
if (node.getParent().left instanceof SelectTreeNode leftNode && !isTheNodeFullyIndexed(leftNode))
return;
}
}

final Object rightValue;
if (node.right instanceof SelectParameterValue value)
rightValue = value.eval(null);
Expand Down Expand Up @@ -779,6 +761,50 @@ else if (node.operator == SelectOperator.le)
cursors.add(cursor);
}

/**
* Decides whether an index cursor built for {@code leaf} is SOUND, by looking at the whole path from the leaf up
* to the root rather than only at the leaf's immediate sibling.
* <p>
* The union of the cursors {@link #filterWithIndexesFinalNode} builds is the ONLY candidate set the query ever
* evaluates ({@link #lookForIndexes} feeds it straight into {@link #buildIterator}), so a cursor is admissible
* only when every record that could satisfy the whole WHERE is guaranteed to be inside that union. Two ancestors
* can break that guarantee, and #8048 showed both were only checked one level up:
* <ul>
* <li><b>{@code or}</b> - a branch of an {@code or} may only contribute a cursor when the OTHER branch also
* contributes one covering all of its own matches, otherwise the other branch's records are never offered to
* {@link #evaluateWhere}. Checking only {@code leaf.getParent()} missed every leaf nested deeper: in
* {@code b = 1 and a = 0 or b = 2} the indexed leaf {@code a = 0} has the {@code and} as its parent, so the
* gate never fired, the candidate set collapsed to "records with a = 0" and the whole {@code b = 2} branch was
* silently dropped - 2 rows instead of 7, and only once an index on {@code a} existed.</li>
* <li><b>{@code not}</b> - a cursor under a negation yields exactly the records the negation REJECTS (#6575),
* so the query would answer with zero rows. The old check looked at the direct parent alone, which leaves
* {@code not (a = 1 and b = 2)} exposed: the leaf's parent is the {@code and}, the {@code not} is one level
* further up, and the positive cursor was built anyway.</li>
* </ul>
* A {@code run} ancestor (the synthetic root {@link Select#compile} adds) and an {@code and} ancestor impose no
* condition: an {@code and} narrows, so a cursor covering one conjunct is always a superset of the conjunction.
*
* @return {@code true} when no ancestor forbids the cursor
*/
private boolean isCursorSoundOnPath(final SelectTreeNode leaf) {
SelectTreeNode child = leaf;
for (SelectTreeNode ancestor = leaf.getParent(); ancestor != null; ancestor = ancestor.getParent()) {
if (ancestor.operator == SelectOperator.not)
return false;

if (ancestor.operator == SelectOperator.or) {
// THE SIBLING IS THE CHILD THAT IS NOT THE ONE WE CAME UP THROUGH. A NON-node SIBLING (OR A MISSING ONE)
// CAN NEVER BE ANSWERED BY AN INDEX, SO IT DISQUALIFIES THIS BRANCH JUST AS AN UN-INDEXED ONE WOULD
final Object sibling = ancestor.right == child ? ancestor.left : ancestor.right;
if (!(sibling instanceof SelectTreeNode siblingNode) || !isTheNodeFullyIndexed(siblingNode))
return false;
}

child = ancestor;
}
return true;
}

/**
* Considers a fully indexed node when both properties are indexed or only one with an AND operator.
*/
Expand Down Expand Up @@ -808,12 +834,22 @@ private boolean isTheNodeFullyIndexed(final SelectTreeNode node) {
final boolean rightIsIndexed = isTheNodeFullyIndexed((SelectTreeNode) node.right);

if (node.operator.equals(SelectOperator.and))
// AND: ONE OR BOTH MEANS INDEXED
// AND: ONE OR BOTH MEANS INDEXED - THE CURSOR FOR EITHER CONJUNCT ALREADY CONTAINS EVERY RECORD SATISFYING
// THE CONJUNCTION, AND evaluateWhere() NARROWS IT DOWN
return leftIsIndexed || rightIsIndexed;
else if (node.operator.equals(SelectOperator.or))
return leftIsIndexed || rightIsIndexed;
// #8048: OR NEEDS *BOTH*. THIS ARM USED TO CARRY THE SAME EXPRESSION AS THE and ARM ABOVE, WHOSE "ONE OR
// BOTH MEANS INDEXED" COMMENT IS THE RULE FOR AND ONLY. WITH ||, A COMPOSITE SIBLING LIKE (b = 1 or a = 2)
// REPORTED ITSELF FULLY INDEXED ON THE STRENGTH OF a = 2 ALONE, THE LEAF ON THE OTHER SIDE OF THE OUTER OR
// GOT ITS CURSOR, AND EVERY RECORD MATCHING b = 1 ONLY WAS NEVER EVEN LOOKED AT
return leftIsIndexed && rightIsIndexed;
else if (node.operator.equals(SelectOperator.not))
return leftIsIndexed;
// #8048/#8059: NEVER. A CURSOR UNDER A NEGATION IS THE SET OF RECORDS THE NEGATION REJECTS (#6575), SO
// filterWithIndexesFinalNode() REFUSES TO BUILD ONE FOR ANY LEAF BELOW A not - AND A BRANCH THAT CONTRIBUTES
// NO CURSOR IS EXACTLY WHAT AN or SIBLING MUST NOT BE TOLD IS "INDEXED". REPORTING leftIsIndexed HERE WAS
// THE #6577 DEFECT IN A DIFFERENT SHAPE: or(a = 1, not (b = 2)) DROPPED THE WHOLE NEGATED BRANCH. THE
// RECURSION ABOVE STILL RAN, SO THE LEAF UNDERNEATH KEEPS THE node.index SIDE EFFECT IT ALWAYS HAD
return false;
}
return false;
}
Expand Down
69 changes: 59 additions & 10 deletions engine/src/main/java/com/arcadedb/query/select/SelectOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,22 @@ Object eval(final Document record, final Object left, final Object right) {
}
},

/**
* UNARY: NEGATES ITS LEFT OPERAND, {@code right} IS ALWAYS {@code null} (SEE
* {@code new SelectTreeNode(operand, SelectOperator.not, null)}, THE SHAPE EVERY IN-TREE USE BUILDS).
* <p>
* #8059: THE BODY USED TO BE {@code left == Boolean.FALSE} - A REFERENCE COMPARISON AGAINST THE *UNEVALUATED*
* OPERAND, WHICH IN A REAL TREE IS A SelectTreeNode AND NEVER Boolean.FALSE, SO THE WHOLE EXPRESSION ANSWERED
* false FOR EVERY RECORD AND THE QUERY RETURNED NOTHING. EVERY OTHER OPERATOR ROUTES ITS OPERANDS THROUGH
* SelectExecutor.evaluateValue(); THIS ONE DID NOT.
*/
not("not", true, 2) {
@Override
Object eval(final Document record, final Object left, final Object right) {
return left == Boolean.FALSE;
final Object leftValue = SelectExecutor.evaluateValue(record, left);
if (leftValue instanceof Boolean booleanValue)
return !booleanValue;
throw new IllegalArgumentException("A boolean operand was expected by 'not' but '" + leftValue + "' was returned");
}
},

Expand All @@ -76,35 +88,66 @@ Object eval(final Document record, final Object left, final Object right) {
}
},

/**
* #8049: AN ORDERING COMPARISON AGAINST A MISSING OR NULL OPERAND IS false, NEVER true.
* <p>
* BinaryComparator.compareTo() IS A *SORT* ORDER AND SORTS null FIRST: compareTo(null, x) IS -1 AND
* compareTo(x, null) IS 1. READ AS A PREDICATE THAT MADE A RECORD WITHOUT THE PROPERTY SATISFY EVERY
* {@code < } AND {@code <=} ON IT, SO {@code a < 5} MATCHED RECORDS THAT CARRY NO {@code a} AT ALL - AND ONLY
* WHILE NO INDEX EXISTED ON {@code a}, BECAUSE AN INDEX HOLDS NO ENTRY FOR A RECORD THAT LACKS THE PROPERTY AND
* SelectExecutor.filterWithIndexesFinalNode() ANSWERS THE LEAF FROM A RANGE SCAN. THE SAME SELECT OVER THE SAME
* DATA THEREFORE RETURNED A DIFFERENT NUMBER OF ROWS DEPENDING ON THE PHYSICAL SCHEMA, WHICH IS THE ONE THING
* ADDING AN INDEX MUST NEVER DO.
* <p>
* REJECTING null ON *EITHER* SIDE IS SQL'S OWN RULE (A COMPARISON WITH NULL IS UNKNOWN, AND UNKNOWN DOES NOT
* MATCH), IT IS WHAT {@code between} ALREADY DID ON ITS LEFT OPERAND, AND IT IS WHAT THE INDEX PATH DOES BY
* CONSTRUCTION - SO ALL FOUR DIRECTIONS, THE INDEX PLAN AND THE FULL SCAN NOW AGREE. USE {@code is null} /
* {@code is not null} TO ASK ABOUT ABSENCE.
*/
lt("<", false, 1) {
@Override
Object eval(final Document record, final Object left, final Object right) {
return BinaryComparator.compareTo(SelectExecutor.evaluateValue(record, left), SelectExecutor.evaluateValue(record, right))
< 0;
final Object leftValue = SelectExecutor.evaluateValue(record, left);
final Object rightValue = SelectExecutor.evaluateValue(record, right);
if (leftValue == null || rightValue == null)
return false;
return BinaryComparator.compareTo(leftValue, rightValue) < 0;
}
},

/** #8049: see {@link #lt}. */
le("<=", false, 1) {
@Override
Object eval(final Document record, final Object left, final Object right) {
return BinaryComparator.compareTo(SelectExecutor.evaluateValue(record, left), SelectExecutor.evaluateValue(record, right))
<= 0;
final Object leftValue = SelectExecutor.evaluateValue(record, left);
final Object rightValue = SelectExecutor.evaluateValue(record, right);
if (leftValue == null || rightValue == null)
return false;
return BinaryComparator.compareTo(leftValue, rightValue) <= 0;
}
},

/** #8049: see {@link #lt}. A null LEFT was already rejected by the sort order; a null RIGHT was not. */
gt(">", false, 1) {
@Override
Object eval(final Document record, final Object left, final Object right) {
return BinaryComparator.compareTo(SelectExecutor.evaluateValue(record, left), SelectExecutor.evaluateValue(record, right))
> 0;
final Object leftValue = SelectExecutor.evaluateValue(record, left);
final Object rightValue = SelectExecutor.evaluateValue(record, right);
if (leftValue == null || rightValue == null)
return false;
return BinaryComparator.compareTo(leftValue, rightValue) > 0;
}
},

/** #8049: see {@link #lt} and {@link #gt}. */
ge(">=", false, 1) {
@Override
Object eval(final Document record, final Object left, final Object right) {
return BinaryComparator.compareTo(SelectExecutor.evaluateValue(record, left), SelectExecutor.evaluateValue(record, right))
>= 0;
final Object leftValue = SelectExecutor.evaluateValue(record, left);
final Object rightValue = SelectExecutor.evaluateValue(record, right);
if (leftValue == null || rightValue == null)
return false;
return BinaryComparator.compareTo(leftValue, rightValue) >= 0;
}
},

Expand Down Expand Up @@ -149,8 +192,14 @@ Object eval(final Document record, final Object left, final Object right) {
Object eval(final Document record, final Object left, final Object right) {
final Object leftValue = SelectExecutor.evaluateValue(record, left);
final Object rightValue = SelectExecutor.evaluateValue(record, right);
if (rightValue instanceof Object[] range && range.length == 2)
if (rightValue instanceof Object[] range && range.length == 2) {
// #8049: SAME RULE AS lt/le/gt/ge. THE LOWER-BOUND COMPARE ALREADY REJECTED A null LEFT FOR EVERY NON-null
// BOUND, SO THIS ONLY CLOSES THE "BOTH THE VALUE AND THE BOUND ARE null" CORNER, BUT IT MAKES THE RULE ONE
// RULE RATHER THAN AN ACCIDENT OF THE SORT ORDER
if (leftValue == null || range[0] == null || range[1] == null)
return false;
return BinaryComparator.compareTo(leftValue, range[0]) >= 0 && BinaryComparator.compareTo(leftValue, range[1]) <= 0;
}
throw new IllegalArgumentException("BETWEEN requires a range of two values");
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public void setParent(final SelectTreeNode newParent) {
if (this.parent == newParent)
return;

// #8047: BELT AND BRACES. A SELF-PARENTED NODE IS NOT MERELY REDUNDANT, IT IS CORRUPTING: THE REWIRING BELOW
// TESTS this.parent.left/right == this, WHICH ON SUCH A NODE COMPARES IT AGAINST ITS OWN CHILDREN AND SILENTLY
// SKIPS THE GRANDPARENT UPDATE - THAT IS HOW Select.setLogic() LOST EVERY CONDITION AFTER THE FOURTH. IT ALSO
// MAKES ANY getParent() WALK NON-TERMINATING, WHICH SelectExecutor's OR/NOT ANCESTOR CHECK (#8048) NOW DOES.
// THE GUARD ON THE LINE ABOVE PROTECTS AGAINST setParent(this.parent), NOT AGAINST setParent(this)
if (newParent == this)
throw new IllegalArgumentException("A condition node cannot be its own parent");

if (this.parent != null) {
if (this.parent.left == this) {
this.parent.left = newParent;
Expand Down
Loading
Loading