Skip to content

Commit

Permalink
Merge pull request #3947 from kiss034/compound_index_search_fix
Browse files Browse the repository at this point in the history
Compound index search fix
  • Loading branch information
grandinj committed Dec 15, 2023
2 parents 1f1ad76 + 4203ba1 commit 5b62710
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 12 deletions.
23 changes: 23 additions & 0 deletions h2/src/main/org/h2/expression/ExpressionList.java
Expand Up @@ -6,6 +6,7 @@
package org.h2.expression;

import org.h2.engine.SessionLocal;
import org.h2.message.DbException;
import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.value.ExtTypeInfoRow;
Expand Down Expand Up @@ -137,4 +138,26 @@ public boolean isArray() {
return isArray;
}

/**
* Creates a copy of this expression list but the new instance will contain the subexpressions according to
* {@code newOrder}.<br />
* E.g.: ROW (?1, ?2).cloneWithOrder([1, 0]) returns ROW (?2, ?1)
* @param newOrder array of indexes to create the new subexpression array
*/
public ExpressionList cloneWithOrder(int[] newOrder) {
int length = list.length;
if (newOrder.length != list.length) {
throw DbException.getInternalError("Length of the new orders is different than list size.");
}

Expression[] newList = new Expression[length];
for (int i = 0; i < length; i++) {
newList[i] = list[newOrder[i]];
}

ExpressionList clone = new ExpressionList(newList, isArray);
clone.initializeType();
return clone;
}

}
17 changes: 13 additions & 4 deletions h2/src/main/org/h2/index/IndexCondition.java
Expand Up @@ -550,10 +550,19 @@ public IndexCondition cloneWithIndexColumns(Index index) {

List<Expression> newList = new ArrayList<>(length);
for (Expression expression: expressionList) {
ValueExpression valueExpression = (ValueExpression) expression;
ValueRow valueRow = (ValueRow) valueExpression.getValue(null);
ValueRow newRow = valueRow.cloneWithOrder(newOrder);
newList.add(ValueExpression.get(newRow));
if (expression instanceof ValueExpression) {
ValueExpression valueExpression = (ValueExpression) expression;
ValueRow currentRow = (ValueRow) valueExpression.getValue(null);
ValueRow newRow = currentRow.cloneWithOrder(newOrder);
newList.add(ValueExpression.get(newRow));
} else if (expression instanceof ExpressionList) {
ExpressionList currentRow = (ExpressionList) expression;
ExpressionList newRow = currentRow.cloneWithOrder(newOrder);
newList.add(newRow);
}
else {
throw DbException.getInternalError("Unexpected expression type: " + expression.getClass());
}
}

return new IndexCondition(Comparison.IN_LIST, null, newColumns, null, newList, null);
Expand Down
2 changes: 2 additions & 0 deletions h2/src/test/org/h2/test/TestAll.java
Expand Up @@ -35,6 +35,7 @@
import org.h2.test.db.TestCompatibility;
import org.h2.test.db.TestCompatibilityOracle;
import org.h2.test.db.TestCompatibilitySQLServer;
import org.h2.test.db.TestCompoundIndexParamSearch;
import org.h2.test.db.TestCompoundIndexSearch;
import org.h2.test.db.TestCsv;
import org.h2.test.db.TestDateStorage;
Expand Down Expand Up @@ -734,6 +735,7 @@ private void test() throws SQLException {
addTest(new TestIndex());
addTest(new TestIndexHints());
addTest(new TestCompoundIndexSearch());
addTest(new TestCompoundIndexParamSearch());
addTest(new TestLargeBlob());
addTest(new TestLinkedTable());
addTest(new TestListener());
Expand Down

0 comments on commit 5b62710

Please sign in to comment.