-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use CQL Order By for non ANN sort (#1635)
- Loading branch information
Showing
42 changed files
with
1,300 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/io/stargate/sgv2/jsonapi/exception/WithWarnings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package io.stargate.sgv2.jsonapi.exception; | ||
|
||
import com.google.common.base.Preconditions; | ||
import io.stargate.sgv2.jsonapi.service.operation.OperationAttempt; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Re-usable class for holding an object and the {@link WarningException}'s that have been generated | ||
* for it. | ||
* | ||
* <p>This is usually used when analysing part of a query and generating warnings for the user. | ||
* | ||
* <p>Is a {@link Consumer} of {@link OperationAttempt} so that it can add the warnings to the | ||
* attempt, and so multiple instances can be chained together using {@link | ||
* Consumer#andThen(Consumer)} | ||
* | ||
* @param <T> Type of the target object the warnings are about. | ||
*/ | ||
public class WithWarnings<T> implements Consumer<OperationAttempt<?, ?>> { | ||
|
||
private final T target; | ||
private final List<WarningException> warnings; | ||
|
||
public WithWarnings(T target, List<WarningException> warnings) { | ||
Preconditions.checkNotNull(target, "target must not be null"); | ||
this.target = target; | ||
this.warnings = warnings == null ? new ArrayList<>() : warnings; | ||
} | ||
|
||
/** | ||
* The target object the warnings are about. | ||
* | ||
* @return The target object. | ||
*/ | ||
public T target() { | ||
return target; | ||
} | ||
|
||
/** | ||
* The warnings generated for the target object. | ||
* | ||
* <p>This is a mutable, so you can add more warnings to it. | ||
* | ||
* @return The list of warnings, never null. | ||
*/ | ||
public List<WarningException> warnings() { | ||
return warnings; | ||
} | ||
|
||
/** Returns true if there are no warnings. */ | ||
public boolean isEmpty() { | ||
return warnings.isEmpty(); | ||
} | ||
|
||
/* | ||
* Constructor an instance with no warnings. | ||
* @param target the target object that has no warnings | ||
* @return an instance with no warnings | ||
*/ | ||
public static <T> WithWarnings<T> of(T target) { | ||
return new WithWarnings<>(target, new ArrayList<>()); | ||
} | ||
|
||
/** | ||
* Constructor an instance with a single warning. | ||
* | ||
* @param target the target object that has the warning | ||
* @param warning the warning to add | ||
* @return An instance with the warning | ||
* @param <T> Type of the target object the warnings are about. | ||
*/ | ||
public static <T> WithWarnings<T> of(T target, WarningException warning) { | ||
Objects.requireNonNull(warning, "warning is required"); | ||
var warnings = new ArrayList<WarningException>(); | ||
warnings.add(warning); | ||
return new WithWarnings<>(target, warnings); | ||
} | ||
|
||
/** | ||
* Adds all the warnings to the {@link OperationAttempt} | ||
* | ||
* @param operationAttempt the {@link OperationAttempt} to add the warnings to | ||
*/ | ||
@Override | ||
public void accept(OperationAttempt<?, ?> operationAttempt) { | ||
Objects.requireNonNull(operationAttempt, "operationAttempt must not be null"); | ||
warnings.forEach(operationAttempt::addWarning); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...main/java/io/stargate/sgv2/jsonapi/service/operation/tables/TableOrderByANNCqlClause.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.stargate.sgv2.jsonapi.service.operation.tables; | ||
|
||
import com.datastax.oss.driver.api.core.data.CqlVector; | ||
import com.datastax.oss.driver.api.querybuilder.select.Select; | ||
import io.stargate.sgv2.jsonapi.service.operation.query.OrderByCqlClause; | ||
import io.stargate.sgv2.jsonapi.service.schema.tables.ApiColumnDef; | ||
import io.stargate.sgv2.jsonapi.service.schema.tables.ApiTypeName; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A CQL clause that adds an ORDER BY clause to a SELECT statement to ANN sort. | ||
* | ||
* <p>Note: Only supports sorting on vector columns a single column, if there is a secondary sort | ||
* that would be in memory sorting. | ||
*/ | ||
public class TableOrderByANNCqlClause implements OrderByCqlClause { | ||
|
||
private final ApiColumnDef apiColumnDef; | ||
private final CqlVector<Float> vector; | ||
|
||
public TableOrderByANNCqlClause(ApiColumnDef apiColumnDef, CqlVector<Float> vector) { | ||
this.apiColumnDef = Objects.requireNonNull(apiColumnDef, "apiColumnDef must not be null"); | ||
this.vector = Objects.requireNonNull(vector, "vector must not be null"); | ||
|
||
// sanity check | ||
if (apiColumnDef.type().typeName() != ApiTypeName.VECTOR) { | ||
throw new IllegalArgumentException( | ||
"ApiColumnDef must be a vector type, got: %s".formatted(apiColumnDef)); | ||
} | ||
} | ||
|
||
@Override | ||
public Select apply(Select select) { | ||
return select.orderByAnnOf(apiColumnDef.name(), vector); | ||
} | ||
|
||
@Override | ||
public boolean inMemorySortNeeded() { | ||
return false; | ||
} | ||
} |
Oops, something went wrong.