Skip to content

Commit

Permalink
fix: operators in, contains, containsany and containsall (#1796)
Browse files Browse the repository at this point in the history
* fix: operators in, contains, containsany and containsall

* Fixed wrong casting
  • Loading branch information
lvca authored Oct 29, 2024
1 parent 6ff9cfc commit a11b782
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,31 @@ public ContainsAllCondition(final int id) {
public boolean execute(Object left, Object right) {
if (left instanceof Collection) {
if (right instanceof Collection)
return ((Collection) left).containsAll((Collection) right);
return ((Collection<?>) left).containsAll((Collection<?>) right);

if (right instanceof Iterable)
right = ((Iterable) right).iterator();
right = ((Iterable<?>) right).iterator();

if (right instanceof Iterator) {
final Iterator iterator = (Iterator) right;
if (right instanceof Iterator<?> iterator) {
while (iterator.hasNext()) {
final Object next = iterator.next();
if (!((Collection) left).contains(next)) {
if (!((Collection<?>) left).contains(next))
return false;
}

}
}
return ((Collection) left).contains(right);
}
if (left instanceof Iterable)
left = ((Iterable) left).iterator();
left = ((Iterable<?>) left).iterator();

if (left instanceof Iterator) {
if (left instanceof Iterator<?> leftIterator) {
if (!(right instanceof Iterable))
right = Collections.singleton(right);

right = ((Iterable) right).iterator();
right = ((Iterable<?>) right).iterator();

final Iterator leftIterator = (Iterator) left;
final Iterator rightIterator = (Iterator) right;
final Iterator<?> rightIterator = (Iterator<?>) right;
while (rightIterator.hasNext()) {
final Object leftItem = rightIterator.next();
boolean found = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,35 @@ public ContainsAnyCondition(final int id) {

public boolean execute(Object left, Object right) {
if (left instanceof Collection) {
if (right instanceof Iterable) {
right = ((Iterable) right).iterator();
}
if (right instanceof Iterator) {
final Iterator iterator = (Iterator) right;
if (right instanceof Iterable)
right = ((Iterable<?>) right).iterator();

if (right instanceof Iterator<?> iterator) {
while (iterator.hasNext()) {
final Object next = iterator.next();
if (((Collection) left).contains(next)) {
if (((Collection<?>) left).contains(next))
return true;
}
}
}
return false;
return ((Collection<?>) left).contains(right);
}
if (left instanceof Iterable) {
left = ((Iterable) left).iterator();
}
if (left instanceof Iterator) {
if (!(right instanceof Iterable)) {

if (left instanceof Iterable)
left = ((Iterable<?>) left).iterator();

if (left instanceof Iterator<?> leftIterator) {
if (!(right instanceof Iterable))
right = Collections.singleton(right);
}
right = ((Iterable) right).iterator();

final Iterator leftIterator = (Iterator) left;
final Iterator rightIterator = (Iterator) right;
right = ((Iterable<?>) right).iterator();

final Iterator<?> rightIterator = (Iterator<?>) right;
while (rightIterator.hasNext()) {
final Object leftItem = rightIterator.next();
while (leftIterator.hasNext()) {
final Object rightItem = leftIterator.next();
if (leftItem != null && leftItem.equals(rightItem)) {
if (leftItem != null && leftItem.equals(rightItem))
return true;
}
}
}
}
Expand All @@ -85,23 +82,20 @@ public Boolean evaluate(final Identifiable currentRecord, final CommandContext c
final Object rightValue = right.execute(currentRecord, context);
return execute(leftValue, rightValue);
} else {
if (!MultiValue.isMultiValue(leftValue)) {
if (!MultiValue.isMultiValue(leftValue))
return false;
}

final Iterator<Object> iter = MultiValue.getMultiValueIterator(leftValue);
while (iter.hasNext()) {
final Object item = iter.next();
if (item instanceof Identifiable) {
if (!rightBlock.evaluate((Identifiable) item, context)) {
if (!rightBlock.evaluate((Identifiable) item, context))
return false;
}
} else if (item instanceof Result) {
if (!rightBlock.evaluate((Result) item, context)) {
if (!rightBlock.evaluate((Result) item, context))
return false;
}
} else if (!rightBlock.evaluate(new ResultInternal(item), context)) {
} else if (!rightBlock.evaluate(new ResultInternal(item), context))
return false;
}
}
return true;
}
Expand All @@ -114,23 +108,20 @@ public Boolean evaluate(final Result currentRecord, final CommandContext context
final Object rightValue = right.execute(currentRecord, context);
return execute(leftValue, rightValue);
} else {
if (!MultiValue.isMultiValue(leftValue)) {
if (!MultiValue.isMultiValue(leftValue))
return false;
}

final Iterator<Object> iter = MultiValue.getMultiValueIterator(leftValue);
while (iter.hasNext()) {
final Object item = iter.next();
if (item instanceof Identifiable) {
if (!rightBlock.evaluate((Identifiable) item, context)) {
if (!rightBlock.evaluate((Identifiable) item, context))
return false;
}
} else if (item instanceof Result) {
if (!rightBlock.evaluate((Result) item, context)) {
if (!rightBlock.evaluate((Result) item, context))
return false;
}
} else if (!rightBlock.evaluate(new ResultInternal(item), context)) {
} else if (!rightBlock.evaluate(new ResultInternal(item), context))
return false;
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,59 +43,56 @@ public ContainsCondition(final int id) {
public boolean execute(final Object left, Object right) {
if (left instanceof Collection) {
if (right instanceof Collection) {
if (((Collection) right).size() == 1) {
Object item = ((Collection) right).iterator().next();
if (((Collection<?>) right).size() == 1) {
Object item = ((Collection<?>) right).iterator().next();
if (item instanceof Result && ((Result) item).getPropertyNames().size() == 1) {
final Object propValue = ((Result) item).getProperty(((Result) item).getPropertyNames().iterator().next());
if (((Collection) left).contains(propValue)) {
if (((Collection<?>) left).contains(propValue))
return true;
}
}
if (((Collection) left).contains(item)) {
if (((Collection<?>) left).contains(item))
return true;
}

if (item instanceof Result)
item = ((Result) item).getElement().orElse(null);

if (item instanceof Identifiable && ((Collection) left).contains(item))
if (item instanceof Identifiable && ((Collection<?>) left).contains(item))
return true;
}

return MultiValue.contains(left, right);
}
if (right instanceof Iterable) {
right = ((Iterable) right).iterator();
}
if (right instanceof Iterator) {
final Iterator iterator = (Iterator) right;

if (right instanceof Iterable)
right = ((Iterable<?>) right).iterator();

if (right instanceof Iterator<?> iterator) {
while (iterator.hasNext()) {
final Object next = iterator.next();
if (!((Collection) left).contains(next)) {
if (!((Collection<?>) left).contains(next))
return false;
}
}
}
for (final Object o : (Collection) left) {
if (equalsInContainsSpace(o, right)) {
for (final Object o : (Collection<?>) left) {
if (equalsInContainsSpace(o, right))
return true;
}
}
return false;
}

Iterator leftIterator = null;
if (left instanceof Iterable) {
leftIterator = ((Iterable) left).iterator();
} else if (left instanceof Iterator) {
leftIterator = (Iterator) left;
}
Iterator<?> leftIterator = null;
if (left instanceof Iterable)
leftIterator = ((Iterable<?>) left).iterator();
else if (left instanceof Iterator)
leftIterator = (Iterator<?>) left;

if (leftIterator != null) {
if (!(right instanceof Iterable)) {
if (!(right instanceof Iterable))
right = Collections.singleton(right);
}
right = ((Iterable) right).iterator();

final Iterator rightIterator = (Iterator) right;
right = ((Iterable<?>) right).iterator();

final Iterator<?> rightIterator = (Iterator<?>) right;
while (rightIterator.hasNext()) {
final Object leftItem = rightIterator.next();
boolean found = false;
Expand All @@ -107,29 +104,26 @@ public boolean execute(final Object left, Object right) {
}
}

if (!found) {
if (!found)
return false;
}

// here left iterator should go from beginning, that can be done only for iterable
// if left at input is iterator result can be invalid
// TODO what if left is Iterator!!!???, should we make temporary Collection , to be able to
// iterate from beginning
if (left instanceof Iterable) {
leftIterator = ((Iterable) left).iterator();
}
if (left instanceof Iterable)
leftIterator = ((Iterable<?>) left).iterator();
}
return true;
}
return false;
}

private boolean equalsInContainsSpace(final Object left, final Object right) {
if (left == null && right == null) {
if (left == null && right == null)
return true;
} else {
else
return QueryOperatorEquals.equals(left, right);
}
}

@Override
Expand All @@ -139,9 +133,9 @@ public Boolean evaluate(final Identifiable currentRecord, final CommandContext c
final Object rightValue = right.execute(currentRecord, context);
return execute(leftValue, rightValue);
} else {
if (!MultiValue.isMultiValue(leftValue)) {
if (!MultiValue.isMultiValue(leftValue))
return false;
}

final Iterator<Object> iter = MultiValue.getMultiValueIterator(leftValue);
while (iter.hasNext()) {
final Object item = iter.next();
Expand All @@ -163,9 +157,9 @@ public Boolean evaluate(final Result currentRecord, final CommandContext context
final Object rightValue = right.execute(currentRecord, context);
return execute(leftValue, rightValue);
} else {
if (!MultiValue.isMultiValue(leftValue)) {
if (!MultiValue.isMultiValue(leftValue))
return false;
}

final Iterator<Object> iter = MultiValue.getMultiValueIterator(leftValue);
while (iter.hasNext()) {
final Object item = iter.next();
Expand Down Expand Up @@ -226,7 +220,7 @@ public List<String> getMatchPatternInvolvedAliases() {
final List<String> rightX = right == null ? null : right.getMatchPatternInvolvedAliases();
final List<String> conditionX = condition == null ? null : condition.getMatchPatternInvolvedAliases();

final List<String> result = new ArrayList<String>();
final List<String> result = new ArrayList<>();
if (leftX != null)
result.addAll(leftX);

Expand All @@ -247,9 +241,8 @@ protected SimpleNode[] getCacheableElements() {
public boolean isIndexAware(final IndexSearchInfo info) {
if (left.isBaseIdentifier()) {
if (info.getField().equals(left.getDefaultAlias().getStringValue())) {
if (right != null) {
if (right != null)
return right.isEarlyCalculated(info.getContext());
}
}
}
return false;
Expand Down
44 changes: 20 additions & 24 deletions engine/src/main/java/com/arcadedb/query/sql/parser/InOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,32 @@ public InOperator(final int id) {

@Override
public boolean execute(final DatabaseInternal database, Object left, Object right) {
if (left == null) {
return false;
}
if (right instanceof Collection) {
if (left instanceof Collection) {
return ((Collection) right).containsAll((Collection) left);
}
if (left instanceof Iterable) {
left = ((Iterable) left).iterator();
}
if (left instanceof Iterator) {
final Iterator iterator = (Iterator) left;
if (left instanceof Collection<?>)
return ((Collection<?>) right).containsAll((Collection<?>) left);

if (left instanceof Iterable)
left = ((Iterable<?>) left).iterator();

if (left instanceof Iterator<?> iterator) {
while (iterator.hasNext()) {
final Object next = iterator.next();
if (!((Collection) right).contains(next)) {
if (!((Collection<?>) right).contains(next)) {
return false;
}
}
}
return ((Collection) right).contains(left);
}
if (right instanceof Iterable) {
right = ((Iterable) right).iterator();
return ((Collection<?>) right).contains(left);
}
if (right instanceof Iterator) {
if (left instanceof Iterable) {
left = ((Iterable) left).iterator();
}
final Iterator leftIterator = (Iterator) left;
final Iterator rightIterator = (Iterator) right;

if (right instanceof Iterable)
right = ((Iterable<?>) right).iterator();

if (right instanceof Iterator<?> rightIterator) {
if (left instanceof Iterable)
left = ((Iterable<?>) left).iterator();

final Iterator<?> leftIterator = (Iterator<?>) left;
while (leftIterator.hasNext()) {
final Object leftItem = leftIterator.next();
boolean found = false;
Expand All @@ -71,9 +67,9 @@ public boolean execute(final DatabaseInternal database, Object left, Object righ
break;
}
}
if (!found) {
if (!found)
return false;
}

}
return true;
}
Expand Down
Loading

0 comments on commit a11b782

Please sign in to comment.