-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add support for JSON type in Pinot #13428
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
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
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
140 changes: 140 additions & 0 deletions
140
plugin/trino-pinot/src/main/java/io/trino/plugin/pinot/PinotTypeConverter.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,140 @@ | ||
| /* | ||
| * Licensed 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 io.trino.plugin.pinot; | ||
|
|
||
| import com.google.common.base.Suppliers; | ||
| import io.trino.spi.type.ArrayType; | ||
| import io.trino.spi.type.BigintType; | ||
| import io.trino.spi.type.BooleanType; | ||
| import io.trino.spi.type.DoubleType; | ||
| import io.trino.spi.type.IntegerType; | ||
| import io.trino.spi.type.RealType; | ||
| import io.trino.spi.type.Type; | ||
| import io.trino.spi.type.TypeManager; | ||
| import io.trino.spi.type.TypeSignature; | ||
| import io.trino.spi.type.VarbinaryType; | ||
| import io.trino.spi.type.VarcharType; | ||
| import org.apache.pinot.common.utils.DataSchema; | ||
| import org.apache.pinot.core.operator.transform.TransformResultMetadata; | ||
| import org.apache.pinot.spi.data.FieldSpec; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static io.trino.plugin.pinot.PinotErrorCode.PINOT_UNSUPPORTED_COLUMN_TYPE; | ||
| import static io.trino.spi.type.BigintType.BIGINT; | ||
| import static io.trino.spi.type.DoubleType.DOUBLE; | ||
| import static io.trino.spi.type.IntegerType.INTEGER; | ||
| import static io.trino.spi.type.RealType.REAL; | ||
| import static io.trino.spi.type.StandardTypes.JSON; | ||
| import static io.trino.spi.type.VarbinaryType.VARBINARY; | ||
| import static io.trino.spi.type.VarcharType.VARCHAR; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class PinotTypeConverter | ||
ebyhr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| // Supplier is used for compatibility unit tests using TestingTypeManager. | ||
| // TestingTypeManager does not support json type. | ||
| private final Supplier<Type> jsonTypeSupplier; | ||
|
|
||
| @Inject | ||
| public PinotTypeConverter(TypeManager typeManager) | ||
| { | ||
| requireNonNull(typeManager, "typeManager is null"); | ||
| this.jsonTypeSupplier = Suppliers.memoize(() -> typeManager.getType(new TypeSignature(JSON))); | ||
| } | ||
|
|
||
| public Type toTrinoType(FieldSpec field) | ||
| { | ||
| return toTrinoType(field.getDataType(), field.isSingleValueField()); | ||
| } | ||
|
|
||
| public Type toTrinoType(TransformResultMetadata transformResultMetadata) | ||
| { | ||
| return toTrinoType(transformResultMetadata.getDataType(), transformResultMetadata.isSingleValue()); | ||
| } | ||
|
|
||
| private Type toTrinoType(FieldSpec.DataType dataType, boolean isSingleValue) | ||
| { | ||
| Type type = toTrinoType(dataType); | ||
| if (isSingleValue) { | ||
| return type; | ||
| } | ||
| return new ArrayType(type); | ||
| } | ||
|
|
||
| private Type toTrinoType(FieldSpec.DataType dataType) | ||
| { | ||
| switch (dataType) { | ||
| case BOOLEAN: | ||
| return BooleanType.BOOLEAN; | ||
| case FLOAT: | ||
| return RealType.REAL; | ||
| case DOUBLE: | ||
| return DoubleType.DOUBLE; | ||
| case INT: | ||
| return IntegerType.INTEGER; | ||
| case LONG: | ||
| return BigintType.BIGINT; | ||
| case STRING: | ||
| return VarcharType.VARCHAR; | ||
| case JSON: | ||
| return jsonTypeSupplier.get(); | ||
| case BYTES: | ||
| return VarbinaryType.VARBINARY; | ||
| default: | ||
| break; | ||
| } | ||
| throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "Unsupported type conversion for pinot data type: " + dataType); | ||
| } | ||
|
|
||
| public Type toTrinoType(DataSchema.ColumnDataType columnDataType) | ||
| { | ||
| switch (columnDataType) { | ||
| case INT: | ||
| return INTEGER; | ||
| case LONG: | ||
| return BIGINT; | ||
| case FLOAT: | ||
| return REAL; | ||
| case DOUBLE: | ||
| return DOUBLE; | ||
| case STRING: | ||
| return VARCHAR; | ||
| case JSON: | ||
| return jsonTypeSupplier.get(); | ||
| case BYTES: | ||
| return VARBINARY; | ||
| case INT_ARRAY: | ||
| return new ArrayType(INTEGER); | ||
| case LONG_ARRAY: | ||
| return new ArrayType(BIGINT); | ||
| case DOUBLE_ARRAY: | ||
| return new ArrayType(DOUBLE); | ||
| case STRING_ARRAY: | ||
| return new ArrayType(VARCHAR); | ||
| default: | ||
| break; | ||
| } | ||
| throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "Unsupported column data type: " + columnDataType); | ||
| } | ||
|
|
||
| public boolean isJsonType(Type type) | ||
| { | ||
| requireNonNull(type, "type is null"); | ||
| return type.equals(jsonTypeSupplier.get()); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.