-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POC for table projections and using JSONCodec #1314
Conversation
Encapsulates behavior for clauses that can be validated and better supports schema object types.
This PR refactors how we process filters by moving the base class FilterableResolver that was used by command resolvers to be standalone class FilterResolver that has CollectionFilterResolver and TableFilterResolver subclasses. The changes to the resolvers are mostly to handle this change, other than findOne. The next part is findOne updated to process the filter for a table. Using the TableFilterResolver to make filters, which then use the NativeTypeTableFilter and JSONCodec to map from the types used for JSOn into what CQL expects. This is POC work, we then need to expand this to handle all the situations we expect.
POC to show pushing the projection down how we build the select and how we build the document from the row. see OperationProjection
Tis commit changes the InsertTableOperation to use the same JSONCodec created to process data for a filter to transform the data from the incoming document into what the driver wants to write to the CQL column
Expands the JSONCode to support mapping toJSON from the objects the driver sent, to be used in the projection
* @throws ToJSONCodecException Checked exception raised for any error, users of the function | ||
* must catch and convert to the appropriate error for the use case. | ||
*/ | ||
JsonNode apply(ObjectMapper objectMapper, DataType fromCQLType, CqlT value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we don't have to use very vague name like "apply()", could use a more meaningful name;
toJson()
or whatever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed as toJson
DataTypes.BOOLEAN, | ||
JSONCodec.ToCQL.unsafeIdentity(), | ||
JSONCodec.ToJSON.unsafeNodeFactory(JsonNodeFactory.instance::booleanNode)); | ||
|
||
// Numeric Codecs | ||
public static final JSONCodec<BigDecimal, Long> BIGINT = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For performance reasons we could also consider only converting FP values into BigDecimal
JsonNode -- but converting CQL integer values into long
-valued JsonNode
.
I think our internal handling can deal with Integer
and Long
valued JsonNode
s and this avoids some of BigDecimal
overhead (avoids conversion overhead, serialization is faster).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added TODO
*/ | ||
public class MissingJSONCodecException extends Exception { | ||
|
||
// TODO: both javTupe and value may be null when going toJSON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: javTupe -> javaType
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
solved
Map<CqlIdentifier, Term> colValues = | ||
row.allColumnValues().entrySet().stream() | ||
.collect(Collectors.toMap(Map.Entry::getKey, e -> literal(e.getValue()))); | ||
Preconditions.checkArgument( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should throw proper exception if and when this can be triggered by an insert operation (if not, it's an assertion and this is fine).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will leave these checks and not blocking the merge,
since this is only on the table path, we will refactor these when putting details into these poc work
: ongoingInsert.value(entry.getKey(), bindMarker()); | ||
} | ||
|
||
assert ongoingInsert != null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we use (or should use Java assert
keyword) -- Objects.requireNonNull()
better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will leave these checks and not blocking the merge,
since this is only on the table path, we will refactor these when putting details into these poc work
* @param <JavaT> The type of the Java object that needs to be transformed into the type CQL expects | ||
* @param <CqlT> The type Java object the CQL driver expects | ||
*/ | ||
public record JSONCodec<JavaT, CqlT>( | ||
GenericType<JavaT> javaType, DataType targetCQLType, FromJava<JavaT, CqlT> fromJava) | ||
implements BiPredicate<DataType, Object> { | ||
GenericType<JavaT> javaType, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The codec looks fine for primitive type. Needs a revisit when we doing complex types where only few fields will need to be returned. Will we be creating custom Codec based on user requests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a TODO
TableMetadata table, CqlIdentifier column, Object value) | ||
throws UnknownColumnException, MissingJSONCodecException { | ||
|
||
Preconditions.checkNotNull(table, "table must not be null"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These checks will create runtime exceptions. Need to be handled with error codes.
try { | ||
return objectMapper.readTree(row.getString("[json]")); | ||
} catch (Exception e) { | ||
throw new NotImplementedException("BANG " + e.getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a better error message. IMO shouldn't go into main branch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, many checks and error msgs need to be changed
refactor the message
@@ -28,42 +29,71 @@ | |||
* CQL expects. | |||
* @param targetCQLType {@link DataType} of the CQL column type the Java object needs to be | |||
* transformed into. | |||
* @param fromJava Function that transforms the Java object into the CQL object | |||
* @param toCQL Function that transforms the Java object into the CQL object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing another param explanation toJSON
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
# Conflicts: # src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/NativeTypeTableFilter.java # src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/TableFilter.java # src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodec.java # src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/JSONCodecRegistry.java # src/main/java/io/stargate/sgv2/jsonapi/service/operation/tables/FindTableOperation.java # src/main/java/io/stargate/sgv2/jsonapi/service/resolver/FindOneAndUpdateCommandResolver.java # src/main/java/io/stargate/sgv2/jsonapi/service/resolver/FindOneCommandResolver.java # src/main/java/io/stargate/sgv2/jsonapi/service/resolver/matcher/FilterResolver.java
NOTE: this PR builds on the
ajm/filter-codec-poc
#1313 work, it should be able to be applied to main after that. I can rebase if neededCI https://github.com/stargate/data-api/actions/runs/10102613949
What this PR does:
Adds a framework for projections with tables, and a JSONCodec for mapping the values between java/json and CQL.
See comments on the 3 commits .
Which issue(s) this PR fixes:
Fixes #
Checklist