-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove dependency on org.apache.hadoop.hive.serde2.avro.AvroSerdeUtils (
#84) Co-authored-by: Kai Xu <[email protected]>
- Loading branch information
Showing
3 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
coral-schema/src/main/java/com/linkedin/coral/schema/avro/AvroSerdeUtils.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,47 @@ | ||
/** | ||
* Copyright 2021 LinkedIn Corporation. All rights reserved. | ||
* Licensed under the BSD-2 Clause license. | ||
* See LICENSE in the project root for license information. | ||
*/ | ||
package com.linkedin.coral.schema.avro; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.avro.Schema; | ||
|
||
|
||
/** | ||
* Utilities useful only to the Hive AvroSerde itself. | ||
* Please refer {@link org.apache.hadoop.hive.serde2.avro.AvroSerdeUtils} for original implementation | ||
*/ | ||
final class AvroSerdeUtils { | ||
|
||
public static final String AVRO_SCHEMA_LITERAL = "avro.schema.literal"; | ||
|
||
private AvroSerdeUtils() { | ||
|
||
} | ||
|
||
/** | ||
* Determine if an Avro schema is of type Union[T, NULL]. Avro supports nullable | ||
* types via a union of type T and null. This is a very common use case. | ||
* As such, we want to silently convert it to just T and allow the value to be null. | ||
* | ||
* @return true if type represents Union[T, Null], false otherwise | ||
*/ | ||
public static boolean isNullableType(Schema schema) { | ||
return schema.getType().equals(Schema.Type.UNION) && schema.getTypes().size() == 2 | ||
&& (schema.getTypes().get(0).getType().equals(Schema.Type.NULL) | ||
|| schema.getTypes().get(1).getType().equals(Schema.Type.NULL)); | ||
// [null, null] not allowed, so this check is ok. | ||
} | ||
|
||
/** | ||
* In a nullable type, get the schema for the non-nullable type. This method | ||
* does no checking that the provides Schema is nullable. | ||
*/ | ||
public static Schema getOtherTypeFromNullableType(Schema schema) { | ||
List<Schema> types = schema.getTypes(); | ||
return types.get(0).getType().equals(Schema.Type.NULL) ? types.get(1) : types.get(0); | ||
} | ||
} |
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