-
Notifications
You must be signed in to change notification settings - Fork 20
[DRAFT] Support for frozen collections and UDTs #12
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
Draft
avelanarius
wants to merge
5
commits into
scylladb:master
Choose a base branch
from
avelanarius:advanced-frozen-types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,7 +14,9 @@ | |||||
| import org.slf4j.LoggerFactory; | ||||||
|
|
||||||
| import java.util.HashMap; | ||||||
| import java.util.List; | ||||||
| import java.util.Map; | ||||||
| import java.util.stream.Collectors; | ||||||
|
|
||||||
| public class ScyllaSchema implements DatabaseSchema<CollectionId> { | ||||||
| private static final Logger LOGGER = LoggerFactory.getLogger(ScyllaSchema.class); | ||||||
|
|
@@ -72,7 +74,7 @@ private Map<String, Schema> computeCellSchemas(ChangeSchema changeSchema, Collec | |||||
| for (ChangeSchema.ColumnDefinition cdef : changeSchema.getNonCdcColumnDefinitions()) { | ||||||
| if (cdef.getBaseTableColumnType() == ChangeSchema.ColumnType.PARTITION_KEY | ||||||
| || cdef.getBaseTableColumnType() == ChangeSchema.ColumnType.CLUSTERING_KEY) continue; | ||||||
| if (!isSupportedColumnSchema(cdef)) continue; | ||||||
| if (!isSupportedColumnSchema(changeSchema, cdef)) continue; | ||||||
|
|
||||||
| Schema columnSchema = computeColumnSchema(cdef); | ||||||
| Schema cellSchema = SchemaBuilder.struct() | ||||||
|
|
@@ -89,7 +91,7 @@ private Schema computeKeySchema(ChangeSchema changeSchema, CollectionId collecti | |||||
| for (ChangeSchema.ColumnDefinition cdef : changeSchema.getNonCdcColumnDefinitions()) { | ||||||
| if (cdef.getBaseTableColumnType() != ChangeSchema.ColumnType.PARTITION_KEY | ||||||
| && cdef.getBaseTableColumnType() != ChangeSchema.ColumnType.CLUSTERING_KEY) continue; | ||||||
| if (!isSupportedColumnSchema(cdef)) continue; | ||||||
| if (!isSupportedColumnSchema(changeSchema, cdef)) continue; | ||||||
|
|
||||||
| Schema columnSchema = computeColumnSchema(cdef); | ||||||
| keySchemaBuilder = keySchemaBuilder.field(cdef.getColumnName(), columnSchema); | ||||||
|
|
@@ -102,7 +104,7 @@ private Schema computeAfterSchema(ChangeSchema changeSchema, Map<String, Schema> | |||||
| SchemaBuilder afterSchemaBuilder = SchemaBuilder.struct() | ||||||
| .name(adjuster.adjust(configuration.getLogicalName() + "." + collectionId.getTableName().keyspace + "." + collectionId.getTableName().name + ".After")); | ||||||
| for (ChangeSchema.ColumnDefinition cdef : changeSchema.getNonCdcColumnDefinitions()) { | ||||||
| if (!isSupportedColumnSchema(cdef)) continue; | ||||||
| if (!isSupportedColumnSchema(changeSchema, cdef)) continue; | ||||||
|
|
||||||
| if (cdef.getBaseTableColumnType() != ChangeSchema.ColumnType.PARTITION_KEY && cdef.getBaseTableColumnType() != ChangeSchema.ColumnType.CLUSTERING_KEY) { | ||||||
| afterSchemaBuilder = afterSchemaBuilder.field(cdef.getColumnName(), cellSchemas.get(cdef.getColumnName())); | ||||||
|
|
@@ -118,7 +120,7 @@ private Schema computeBeforeSchema(ChangeSchema changeSchema, Map<String, Schema | |||||
| SchemaBuilder beforeSchemaBuilder = SchemaBuilder.struct() | ||||||
| .name(adjuster.adjust(configuration.getLogicalName() + "." + collectionId.getTableName().keyspace + "." + collectionId.getTableName().name + ".Before")); | ||||||
| for (ChangeSchema.ColumnDefinition cdef : changeSchema.getNonCdcColumnDefinitions()) { | ||||||
| if (!isSupportedColumnSchema(cdef)) continue; | ||||||
| if (!isSupportedColumnSchema(changeSchema, cdef)) continue; | ||||||
|
|
||||||
| if (cdef.getBaseTableColumnType() != ChangeSchema.ColumnType.PARTITION_KEY && cdef.getBaseTableColumnType() != ChangeSchema.ColumnType.CLUSTERING_KEY) { | ||||||
| beforeSchemaBuilder = beforeSchemaBuilder.field(cdef.getColumnName(), cellSchemas.get(cdef.getColumnName())); | ||||||
|
|
@@ -130,8 +132,12 @@ private Schema computeBeforeSchema(ChangeSchema changeSchema, Map<String, Schema | |||||
| return beforeSchemaBuilder.optional().build(); | ||||||
| } | ||||||
|
|
||||||
| private Schema computeColumnSchema(ChangeSchema.ColumnDefinition cdef) { | ||||||
| switch (cdef.getCdcLogDataType().getCqlType()) { | ||||||
| protected static Schema computeColumnSchema(ChangeSchema.ColumnDefinition cdef) { | ||||||
| return computeColumnSchema(cdef.getCdcLogDataType()); | ||||||
| } | ||||||
|
|
||||||
| protected static Schema computeColumnSchema(ChangeSchema.DataType type) { | ||||||
| switch (type.getCqlType()) { | ||||||
| case ASCII: | ||||||
| return Schema.OPTIONAL_STRING_SCHEMA; | ||||||
| case BIGINT: | ||||||
|
|
@@ -179,21 +185,51 @@ private Schema computeColumnSchema(ChangeSchema.ColumnDefinition cdef) { | |||||
| return Schema.OPTIONAL_INT8_SCHEMA; | ||||||
| case DURATION: | ||||||
| return Schema.OPTIONAL_STRING_SCHEMA; | ||||||
| case LIST: | ||||||
| case MAP: | ||||||
| case SET: | ||||||
| case UDT: | ||||||
| case TUPLE: | ||||||
| case LIST: { | ||||||
| Schema innerSchema = computeColumnSchema(type.getTypeArguments().get(0)); | ||||||
| return SchemaBuilder.array(innerSchema); | ||||||
| } | ||||||
| case MAP: { | ||||||
| Schema keySchema = computeColumnSchema(type.getTypeArguments().get(0)); | ||||||
| Schema valueSchema = computeColumnSchema(type.getTypeArguments().get(1)); | ||||||
| return SchemaBuilder.map(keySchema, valueSchema); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| case TUPLE: { | ||||||
| List<Schema> innerSchemas = type.getTypeArguments().stream() | ||||||
| .map(ScyllaSchema::computeColumnSchema).collect(Collectors.toList()); | ||||||
| SchemaBuilder tupleSchema = SchemaBuilder.struct(); | ||||||
| for (int i = 0; i < innerSchemas.size(); i++) { | ||||||
| tupleSchema = tupleSchema.field("tuple_member_" + i, innerSchemas.get(i)); | ||||||
| } | ||||||
| return tupleSchema.optional().build(); | ||||||
| } | ||||||
| case UDT: { | ||||||
| SchemaBuilder udtSchema = SchemaBuilder.struct(); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| for (Map.Entry<String, ChangeSchema.DataType> field : type.getUdtType().getFields().entrySet()) { | ||||||
| udtSchema = udtSchema.field(field.getKey(), computeColumnSchema(field.getValue())); | ||||||
| } | ||||||
| return udtSchema.optional().build(); | ||||||
| } | ||||||
| default: | ||||||
| throw new UnsupportedOperationException(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| protected static boolean isSupportedColumnSchema(ChangeSchema.ColumnDefinition cdef) { | ||||||
| protected static boolean isSupportedColumnSchema(ChangeSchema changeSchema, ChangeSchema.ColumnDefinition cdef) { | ||||||
| ChangeSchema.CqlType type = cdef.getCdcLogDataType().getCqlType(); | ||||||
| return type != ChangeSchema.CqlType.LIST && type != ChangeSchema.CqlType.MAP && | ||||||
| type != ChangeSchema.CqlType.SET && type != ChangeSchema.CqlType.UDT && | ||||||
| type != ChangeSchema.CqlType.TUPLE; | ||||||
| if (type == ChangeSchema.CqlType.LIST || type == ChangeSchema.CqlType.SET | ||||||
| || type == ChangeSchema.CqlType.MAP || type == ChangeSchema.CqlType.UDT) { | ||||||
| // We only support frozen lists, sets, maps and UDTs, | ||||||
| // (which can be identified by cdc$deleted_elements_ column). | ||||||
|
|
||||||
| // FIXME: When isFrozen is fixed in scylla-cdc-java (PR #60), | ||||||
| // replace with just a call to isFrozen. | ||||||
| String deletedElementsColumnName = "cdc$deleted_elements_" + cdef.getColumnName(); | ||||||
| return changeSchema.getAllColumnDefinitions().stream() | ||||||
| .noneMatch(c -> c.getColumnName().equals(deletedElementsColumnName)); | ||||||
| } | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| public ScyllaCollectionSchema updateChangeSchema(CollectionId collectionId, ChangeSchema changeSchema) { | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.