Skip to content

Commit

Permalink
disambiguate the BM25 error message when the index isn't analyzed
Browse files Browse the repository at this point in the history
  • Loading branch information
jbellis committed Dec 12, 2024
1 parent 3d17e2f commit 907a2ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class StatementRestrictions
"Restriction on partition key column %s must not be nested under OR operator";

public static final String GEO_DISTANCE_REQUIRES_INDEX_MESSAGE = "GEO_DISTANCE requires the vector column to be indexed";
public static final String BM25_ORDERING_REQUIRES_ANALYZED_INDEX_MESSAGE = "BM25 ordering on column %s requires an analyzed index";
public static final String NON_CLUSTER_ORDERING_REQUIRES_INDEX_MESSAGE = "Ordering on non-clustering column %s requires the column to be indexed";
public static final String NON_CLUSTER_ORDERING_REQUIRES_ALL_RESTRICTED_NON_PARTITION_KEY_COLUMNS_INDEXED_MESSAGE =
"Ordering on non-clustering column requires each restricted column to be indexed except for fully-specified partition keys";
Expand Down Expand Up @@ -696,8 +697,12 @@ else if (indexOrderings.size() == 1)
throw new InvalidRequestException(String.format("SAI based ordering on column %s of type %s is not supported",
restriction.getFirstColumn(),
restriction.getFirstColumn().type.asCQL3Type()));
throw new InvalidRequestException(String.format(NON_CLUSTER_ORDERING_REQUIRES_INDEX_MESSAGE,
restriction.getFirstColumn()));
if (ordering.expression instanceof Ordering.Bm25)
throw new InvalidRequestException(String.format(BM25_ORDERING_REQUIRES_ANALYZED_INDEX_MESSAGE,
restriction.getFirstColumn()));
else
throw new InvalidRequestException(String.format(NON_CLUSTER_ORDERING_REQUIRES_INDEX_MESSAGE,
restriction.getFirstColumn()));
}
receiver.addRestriction(restriction, false);
}
Expand Down
15 changes: 15 additions & 0 deletions test/unit/org/apache/cassandra/index/sai/cql/BM25Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import org.junit.Test;

import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.index.sai.SAITester;
import org.apache.cassandra.index.sai.plan.QueryController;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

public class BM25Test extends SAITester
Expand All @@ -43,6 +45,19 @@ public void testMatchingAllowed() throws Throwable
});
}

@Test
public void testTwoIndexes() throws Throwable
{
// create un-analyzed index
createTable("CREATE TABLE %s (k int PRIMARY KEY, v text)");
createIndex("CREATE CUSTOM INDEX ON %s(v) USING 'org.apache.cassandra.index.sai.StorageAttachedIndex'");

execute("INSERT INTO %s (k, v) VALUES (1, 'apple')");
assertThatThrownBy(() -> execute("SELECT k FROM %s WHERE v : 'apple' ORDER BY v BM25 OF 'apple' LIMIT 3"))
.isInstanceOf(InvalidRequestException.class)
.hasMessage("BM25 ordering on column v requires an analyzed index");
}

@Test
public void testTermFrequencyOrdering() throws Throwable
{
Expand Down

0 comments on commit 907a2ee

Please sign in to comment.