Skip to content

Commit 51a058b

Browse files
fix CONTAINSANY index use for lists of embedded documents (#3051)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 9878a9f commit 51a058b

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

engine/src/main/java/com/arcadedb/query/sql/parser/ContainsAnyCondition.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,31 @@ protected SimpleNode[] getCacheableElements() {
206206

207207
@Override
208208
public boolean isIndexAware(final IndexSearchInfo info) {
209+
// For nested properties like "tags.id", we need to handle the case where isBaseIdentifier() returns false
210+
// due to the presence of a suffix/modifier. We'll check both cases:
211+
// 1. Simple identifiers: left.isBaseIdentifier() == true (e.g., "tags")
212+
// 2. Nested identifiers: Compare the full string representation (e.g., "tags.id")
213+
214+
String fieldName = null;
215+
209216
if (left.isBaseIdentifier()) {
210-
if (info.getField().equals(left.getDefaultAlias().getStringValue())) {
211-
return right.isEarlyCalculated(info.getContext());
217+
// Simple identifier - use default alias
218+
fieldName = left.getDefaultAlias().getStringValue();
219+
} else {
220+
// Might be a nested property - try using the string representation
221+
String leftStr = left.toString();
222+
// Check if this looks like a simple property path (alphanumeric with dots)
223+
if (leftStr.matches("[a-zA-Z_][a-zA-Z0-9_.]*")) {
224+
fieldName = leftStr;
212225
}
213226
}
227+
228+
if (fieldName != null && info.getField().equals(fieldName)) {
229+
// CONTAINSANY operator only works with BY-ITEM indexes, not regular list indexes
230+
if (info.isIndexByItem() && right != null)
231+
return right.isEarlyCalculated(info.getContext());
232+
}
233+
214234
return false;
215235
}
216236

engine/src/test/java/com/arcadedb/index/EmbeddedListIndexByItemTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void listOfEmbeddedDocumentIndexByItem() {
150150
database.command("sql", "INSERT INTO doc SET nums = [{'@type':'num','a':1},{'@type':'num','a':2}]");
151151
});
152152

153-
// Query the indexed data
153+
// Query the indexed data via CONTAINS
154154
database.transaction(() -> {
155155
ResultSet result = database.query("sql", "SELECT FROM doc WHERE nums.a CONTAINS 1");
156156
long count = result.stream().count();
@@ -161,5 +161,17 @@ void listOfEmbeddedDocumentIndexByItem() {
161161
.next().getProperty("executionPlan").toString();
162162
assertThat(explain).contains("FETCH FROM INDEX");
163163
});
164+
165+
// Query the indexed data via CONTAINSANY
166+
database.transaction(() -> {
167+
ResultSet result = database.query("sql", "SELECT FROM doc WHERE nums.a CONTAINSANY [1]");
168+
long count = result.stream().count();
169+
assertThat(count).isEqualTo(1);
170+
171+
// Verify index is being used
172+
String explain = database.query("sql", "EXPLAIN SELECT FROM doc WHERE nums.a CONTAINSANY [1]")
173+
.next().getProperty("executionPlan").toString();
174+
assertThat(explain).contains("FETCH FROM INDEX");
175+
});
164176
}
165177
}

0 commit comments

Comments
 (0)