-
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.
POC for table filters with codecs for findOne (#1313)
Co-authored-by: Yuqi Du <istimdu@gmail.com>
- Loading branch information
Showing
30 changed files
with
811 additions
and
152 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
19 changes: 12 additions & 7 deletions
19
src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/executor/TableSchemaObject.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
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
36 changes: 0 additions & 36 deletions
36
...main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/ColumnTableFilter.java
This file was deleted.
Oops, something went wrong.
129 changes: 129 additions & 0 deletions
129
.../java/io/stargate/sgv2/jsonapi/service/operation/filters/table/NativeTypeTableFilter.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,129 @@ | ||
package io.stargate.sgv2.jsonapi.service.operation.filters.table; | ||
|
||
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.bindMarker; | ||
|
||
import com.datastax.oss.driver.api.querybuilder.relation.Relation; | ||
import com.datastax.oss.driver.api.querybuilder.select.Select; | ||
import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.ValueComparisonOperator; | ||
import io.stargate.sgv2.jsonapi.exception.ErrorCode; | ||
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.TableSchemaObject; | ||
import io.stargate.sgv2.jsonapi.service.operation.builder.BuiltCondition; | ||
import io.stargate.sgv2.jsonapi.service.operation.builder.BuiltConditionPredicate; | ||
import io.stargate.sgv2.jsonapi.service.operation.filters.table.codecs.FromJavaCodecException; | ||
import io.stargate.sgv2.jsonapi.service.operation.filters.table.codecs.JSONCodecRegistry; | ||
import java.util.List; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A DB Filter that can be applied on columns in a CQL Tables that use the `native-type` 's as | ||
* defined in the CQL specification. | ||
* | ||
* <pre> | ||
* <native-type> ::= ascii | ||
* | bigint | ||
* | blob | ||
* | boolean | ||
* | counter | ||
* | date | ||
* | decimal | ||
* | double | ||
* | duration | ||
* | float | ||
* | inet | ||
* | int | ||
* | smallint | ||
* | text | ||
* | time | ||
* | timestamp | ||
* | timeuuid | ||
* | tinyint | ||
* | uuid | ||
* | varchar | ||
* | varint | ||
* </pre> | ||
* | ||
* @param <T> The JSON Type , BigDecimal, String etc | ||
*/ | ||
public abstract class NativeTypeTableFilter<T> extends TableFilter { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(NativeTypeTableFilter.class); | ||
|
||
/** | ||
* The operations that can be performed to filter a column TIDY: we have operations defined in | ||
* multiple places, once we have refactored the collection operations we should centralize these | ||
* operator definitions | ||
*/ | ||
public enum Operator { | ||
// TODO, other operators like NE, IN etc. | ||
EQ(BuiltConditionPredicate.EQ), | ||
LT(BuiltConditionPredicate.LT), | ||
GT(BuiltConditionPredicate.GT), | ||
LTE(BuiltConditionPredicate.LTE), | ||
GTE(BuiltConditionPredicate.GTE); | ||
|
||
final BuiltConditionPredicate predicate; | ||
|
||
Operator(BuiltConditionPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
public static Operator from(ValueComparisonOperator operator) { | ||
return switch (operator) { | ||
case EQ -> EQ; | ||
case GT -> GT; | ||
case GTE -> GTE; | ||
case LT -> LT; | ||
case LTE -> LTE; | ||
default -> throw new IllegalArgumentException("Unsupported operator: " + operator); | ||
}; | ||
} | ||
} | ||
|
||
protected final Operator operator; | ||
protected final T columnValue; | ||
|
||
protected NativeTypeTableFilter(String path, Operator operator, T columnValue) { | ||
super(path); | ||
this.columnValue = columnValue; | ||
this.operator = operator; | ||
} | ||
|
||
@Override | ||
public BuiltCondition get() { | ||
throw new UnsupportedOperationException( | ||
"No supported - will be modified when we migrate collections filters java driver"); | ||
} | ||
|
||
@Override | ||
public Select apply( | ||
TableSchemaObject tableSchemaObject, Select select, List<Object> positionalValues) { | ||
|
||
// TODO: AARON return the correct errors, this is POC work now | ||
// TODO: Checking for valid column should be part of request deserializer or to be done in | ||
// resolver. Should not be left till operation classes. | ||
var column = | ||
tableSchemaObject | ||
.tableMetadata | ||
.getColumn(path) | ||
.orElseThrow(() -> new IllegalArgumentException("Column not found: " + path)); | ||
|
||
var codec = | ||
JSONCodecRegistry.codecFor(column.getType(), columnValue) | ||
.orElseThrow( | ||
() -> | ||
ErrorCode.ERROR_APPLYING_CODEC.toApiException( | ||
"No Codec for a value of type %s with table column %s it has CQL type %s", | ||
columnValue.getClass(), | ||
column.getName(), | ||
column.getType().asCql(true, false))); | ||
|
||
try { | ||
positionalValues.add(codec.apply(columnValue)); | ||
} catch (FromJavaCodecException e) { | ||
throw ErrorCode.ERROR_APPLYING_CODEC.toApiException(e, "Error applying codec"); | ||
} | ||
|
||
return select.where(Relation.column(path).build(operator.predicate.cql, bindMarker())); | ||
} | ||
} |
7 changes: 5 additions & 2 deletions
7
...main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/NumberTableFilter.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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package io.stargate.sgv2.jsonapi.service.operation.filters.table; | ||
|
||
public class NumberTableFilter<T extends Number> extends ColumnTableFilter<T> { | ||
import java.math.BigDecimal; | ||
|
||
public NumberTableFilter(String path, Operator operator, T value) { | ||
/** Filter to use any JSON number against a table column. */ | ||
public class NumberTableFilter extends NativeTypeTableFilter<BigDecimal> { | ||
|
||
public NumberTableFilter(String path, Operator operator, BigDecimal value) { | ||
super(path, operator, value); | ||
} | ||
} |
36 changes: 33 additions & 3 deletions
36
src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/TableFilter.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 |
---|---|---|
@@ -1,11 +1,41 @@ | ||
package io.stargate.sgv2.jsonapi.service.operation.filters.table; | ||
|
||
import com.datastax.oss.driver.api.querybuilder.select.Select; | ||
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.IndexUsage; | ||
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.TableSchemaObject; | ||
import io.stargate.sgv2.jsonapi.service.operation.filters.DBFilterBase; | ||
import java.util.List; | ||
|
||
/** | ||
* A {@link DBFilterBase} that is applied to a table (i.e. not a Collection) to filter the rows to | ||
* read. | ||
*/ | ||
public abstract class TableFilter extends DBFilterBase { | ||
// TODO- the path is the column name here, maybe rename ? | ||
protected TableFilter(String path) { | ||
super(path, IndexUsage.NO_OP); | ||
|
||
protected TableFilter(String column) { | ||
super(column, IndexUsage.NO_OP); | ||
} | ||
|
||
/** | ||
* Call to have the filter applied to the select statement using the {@link | ||
* com.datastax.oss.driver.api.querybuilder.QueryBuilder} from the Java driver. | ||
* | ||
* <p>NOTE: use this method rather than {@link DBFilterBase#get()} which is build to work with the | ||
* old gRPC bridge query builder. | ||
* | ||
* <p>TIDY: Refactor DBFilterBase to use this method when we move collection filters to use the | ||
* java driver. | ||
* | ||
* @param tableSchemaObject The table the filter is being applied to. | ||
* @param select The select statement to apply the filter to, see docs for {@link | ||
* com.datastax.oss.driver.api.querybuilder.QueryBuilder} | ||
* @param positionalValues Mutatable array of values that are used when the {@link | ||
* com.datastax.oss.driver.api.querybuilder.QueryBuilder#bindMarker()} method is used, the | ||
* values are added to the select statement using {@link Select#build(Object...)} | ||
* @return The {@link Select} to use to continue building the query. NOTE: the query builder is a | ||
* fluent builder that returns immutable that are used in a chain, see the | ||
* https://docs.datastax.com/en/developer/java-driver/4.3/manual/query_builder/index.html | ||
*/ | ||
public abstract Select apply( | ||
TableSchemaObject tableSchemaObject, Select select, List<Object> positionalValues); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/TextTableFilter.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,9 @@ | ||
package io.stargate.sgv2.jsonapi.service.operation.filters.table; | ||
|
||
/** Filter to use any JSON string value against a table column. */ | ||
public class TextTableFilter extends NativeTypeTableFilter<String> { | ||
|
||
public TextTableFilter(String path, Operator operator, String value) { | ||
super(path, operator, value); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.../stargate/sgv2/jsonapi/service/operation/filters/table/codecs/FromJavaCodecException.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,24 @@ | ||
package io.stargate.sgv2.jsonapi.service.operation.filters.table.codecs; | ||
|
||
import com.datastax.oss.driver.api.core.type.DataType; | ||
|
||
public class FromJavaCodecException extends Exception { | ||
|
||
public final Object value; | ||
public final DataType targetCQLType; | ||
|
||
/** | ||
* TODO: confirm we want / need this, the idea is to encapsulate any exception when doing the | ||
* conversion to to the type CQL expects. This would be a checked exception, and not something we | ||
* expect to return to the user | ||
* | ||
* @param value | ||
* @param targetCQLType | ||
* @param cause | ||
*/ | ||
public FromJavaCodecException(Object value, DataType targetCQLType, Exception cause) { | ||
super("Error trying to convert value " + value + " to " + targetCQLType, cause); | ||
this.value = value; | ||
this.targetCQLType = targetCQLType; | ||
} | ||
} |
Oops, something went wrong.