-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-2484: Add schema projection utilities #307
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 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
197 changes: 197 additions & 0 deletions
197
copycat/api/src/main/java/org/apache/kafka/copycat/data/SchemaProjector.java
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 |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
| * agreements. See the NOTICE file distributed with this work for additional information regarding | ||
| * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance with the License. You may obtain a | ||
| * copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License | ||
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| * or implied. See the License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| **/ | ||
|
|
||
| package org.apache.kafka.copycat.data; | ||
|
|
||
| import org.apache.kafka.copycat.data.Schema.Type; | ||
| import org.apache.kafka.copycat.errors.SchemaProjectorException; | ||
|
|
||
| import java.util.AbstractMap; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * <p> | ||
| * SchemaProjector is utility to project a value between compatible schemas and throw exceptions | ||
| * when non compatible schemas are provided. | ||
| * </p> | ||
| */ | ||
|
|
||
| public class SchemaProjector { | ||
|
|
||
| private static Set<AbstractMap.SimpleImmutableEntry<Type, Type>> promotable = new HashSet<>(); | ||
|
|
||
| static { | ||
| Type[] promotableTypes = {Type.INT8, Type.INT16, Type.INT32, Type.INT64, Type.FLOAT32, Type.FLOAT64}; | ||
| for (int i = 0; i < promotableTypes.length; ++i) { | ||
| for (int j = i; j < promotableTypes.length; ++j) { | ||
| promotable.add(new AbstractMap.SimpleImmutableEntry<>(promotableTypes[i], promotableTypes[j])); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This method project a value between compatible schemas and throw exceptions when non compatible schemas are provided | ||
| * @param source the schema used to construct the record | ||
| * @param record the value to project from source schema to target schema | ||
| * @param target the schema to project the record to | ||
| * @return the projected value with target schema | ||
| * @throws SchemaProjectorException | ||
| */ | ||
| public static Object project(Schema source, Object record, Schema target) throws SchemaProjectorException { | ||
| checkMaybeCompatible(source, target); | ||
| if (source.isOptional() && !target.isOptional()) { | ||
| if (target.defaultValue() != null) { | ||
| if (record != null) { | ||
| return projectRequiredSchema(source, record, target); | ||
| } else { | ||
| return target.defaultValue(); | ||
| } | ||
| } else { | ||
| throw new SchemaProjectorException("Writer schema is optional, however, target schema does not provide a default value."); | ||
| } | ||
| } else { | ||
| if (record != null) { | ||
| return projectRequiredSchema(source, record, target); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static Object projectRequiredSchema(Schema source, Object record, Schema target) throws SchemaProjectorException { | ||
| switch (target.type()) { | ||
| case INT8: | ||
| case INT16: | ||
| case INT32: | ||
| case INT64: | ||
| case FLOAT32: | ||
| case FLOAT64: | ||
| case BOOLEAN: | ||
| case BYTES: | ||
| case STRING: | ||
| return projectPrimitive(source, record, target); | ||
| case STRUCT: | ||
| return projectStruct(source, (Struct) record, target); | ||
| case ARRAY: | ||
| return projectArray(source, record, target); | ||
| case MAP: | ||
| return projectMap(source, record, target); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static Object projectStruct(Schema source, Struct sourceStruct, Schema target) throws SchemaProjectorException { | ||
| Struct targetStruct = new Struct(target); | ||
| for (Field targetField : target.fields()) { | ||
| String fieldName = targetField.name(); | ||
| Field sourceField = source.field(fieldName); | ||
| if (sourceField != null) { | ||
| Object sourceFieldValue = sourceStruct.get(fieldName); | ||
| try { | ||
| Object targetFieldValue = project(sourceField.schema(), sourceFieldValue, targetField.schema()); | ||
| targetStruct.put(fieldName, targetFieldValue); | ||
| } catch (SchemaProjectorException e) { | ||
| throw new SchemaProjectorException("Error projecting " + sourceField.name(), e); | ||
| } | ||
| } else { | ||
| Object targetDefault; | ||
| if (targetField.schema().defaultValue() != null) { | ||
| targetDefault = targetField.schema().defaultValue(); | ||
| } else { | ||
| throw new SchemaProjectorException("Cannot project " + source.schema() + " to " + target.schema()); | ||
| } | ||
| targetStruct.put(fieldName, targetDefault); | ||
| } | ||
| } | ||
| return targetStruct; | ||
| } | ||
|
|
||
|
|
||
| private static void checkMaybeCompatible(Schema source, Schema target) { | ||
| if (source.type() != target.type() && !isPromotable(source.type(), target.type())) { | ||
| throw new SchemaProjectorException("Schema type mismatch. source type: " + source.type() + " and target type: " + target.type()); | ||
| } else if (!Objects.equals(source.name(), target.name())) { | ||
| throw new SchemaProjectorException("Schema name mismatch. source name: " + source.name() + " and target name: " + target.name()); | ||
| } else if (!Objects.equals(source.parameters(), target.parameters())) { | ||
| throw new SchemaProjectorException("Schema parameters not equal. source parameters: " + source.parameters() + " and target parameters: " + target.parameters()); | ||
| } | ||
| } | ||
|
|
||
| private static Object projectArray(Schema source, Object record, Schema target) throws SchemaProjectorException { | ||
| List<?> array = (List<?>) record; | ||
| List<Object> retArray = new ArrayList<>(); | ||
| for (Object entry : array) { | ||
| retArray.add(project(source.valueSchema(), entry, target.valueSchema())); | ||
| } | ||
| return retArray; | ||
| } | ||
|
|
||
| private static Object projectMap(Schema source, Object record, Schema target) throws SchemaProjectorException { | ||
| Map<?, ?> map = (Map<?, ?>) record; | ||
| Map<Object, Object> retMap = new HashMap<>(); | ||
| for (Map.Entry<?, ?> entry : map.entrySet()) { | ||
| Object key = entry.getKey(); | ||
| Object value = entry.getValue(); | ||
| Object retKey = project(source.keySchema(), key, target.keySchema()); | ||
| Object retValue = project(source.valueSchema(), value, target.valueSchema()); | ||
| retMap.put(retKey, retValue); | ||
| } | ||
| return retMap; | ||
| } | ||
|
|
||
| private static Object projectPrimitive(Schema source, Object record, Schema target) throws SchemaProjectorException { | ||
| assert source.type().isPrimitive(); | ||
| assert target.type().isPrimitive(); | ||
| Object result; | ||
| if (isPromotable(source.type(), target.type())) { | ||
| Number numberRecord = (Number) record; | ||
| switch (target.type()) { | ||
| case INT8: | ||
| result = numberRecord.byteValue(); | ||
| break; | ||
| case INT16: | ||
| result = numberRecord.shortValue(); | ||
| break; | ||
| case INT32: | ||
| result = numberRecord.intValue(); | ||
| break; | ||
| case INT64: | ||
| result = numberRecord.longValue(); | ||
| break; | ||
| case FLOAT32: | ||
| result = numberRecord.floatValue(); | ||
| break; | ||
| case FLOAT64: | ||
| result = numberRecord.doubleValue(); | ||
| break; | ||
| default: | ||
| throw new SchemaProjectorException("Not promotable type."); | ||
| } | ||
| } else { | ||
| result = record; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static boolean isPromotable(Type sourceType, Type targetType) { | ||
| return promotable.contains(new AbstractMap.SimpleImmutableEntry<>(sourceType, targetType)); | ||
| } | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
copycat/api/src/main/java/org/apache/kafka/copycat/errors/SchemaProjectorException.java
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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
| * agreements. See the NOTICE file distributed with this work for additional information regarding | ||
| * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance with the License. You may obtain a | ||
| * copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License | ||
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| * or implied. See the License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| **/ | ||
| package org.apache.kafka.copycat.errors; | ||
|
|
||
| public class SchemaProjectorException extends DataException { | ||
| public SchemaProjectorException(String s) { | ||
| super(s); | ||
| } | ||
|
|
||
| public SchemaProjectorException(String s, Throwable throwable) { | ||
| super(s, throwable); | ||
| } | ||
|
|
||
| public SchemaProjectorException(Throwable throwable) { | ||
| super(throwable); | ||
| } | ||
| } |
Oops, something went wrong.
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.
If the record is null, this isn't doing any schema compatibility checks besides the check for optional/required check at the top of this method. This is causing the logical type test to fail.