-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(client): Add serialized response format option to Presto client #26654
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
Open
hdikeman
wants to merge
1
commit into
prestodb:master
Choose a base branch
from
hdikeman:export-D87268210
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.
Open
Changes from all commits
Commits
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
166 changes: 166 additions & 0 deletions
166
presto-client/src/main/java/com/facebook/presto/client/BinaryDataDeserializer.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,166 @@ | ||
| /* | ||
| * 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 com.facebook.presto.client; | ||
|
|
||
| import com.facebook.presto.common.Page; | ||
| import com.facebook.presto.common.block.Block; | ||
| import com.facebook.presto.common.block.BlockEncodingSerde; | ||
| import com.facebook.presto.common.function.SqlFunctionProperties; | ||
| import com.facebook.presto.common.type.Type; | ||
| import com.facebook.presto.common.type.TypeManager; | ||
| import com.facebook.presto.common.type.TypeSignature; | ||
| import com.facebook.presto.common.type.TypeSignatureParameter; | ||
| import com.facebook.presto.spi.page.PagesSerde; | ||
| import com.facebook.presto.spi.page.SerializedPage; | ||
| import com.google.common.collect.ImmutableList; | ||
| import io.airlift.slice.BasicSliceInput; | ||
| import io.airlift.slice.Slice; | ||
| import io.airlift.slice.Slices; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Base64; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import static com.facebook.presto.spi.page.PagesSerdeUtil.readSerializedPage; | ||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class BinaryDataDeserializer | ||
| { | ||
| private final PagesSerde pagesSerde; | ||
| private final TypeManager typeManager; | ||
| private final SqlFunctionProperties sqlFunctionProperties; | ||
|
|
||
| public BinaryDataDeserializer( | ||
| BlockEncodingSerde blockEncodingSerde, | ||
| TypeManager typeManager, | ||
| ClientSession session) | ||
| { | ||
| requireNonNull(blockEncodingSerde, "blockEncodingSerde is null"); | ||
| requireNonNull(typeManager, "typeManager is null"); | ||
| requireNonNull(session, "session is null"); | ||
|
|
||
| this.pagesSerde = new PagesSerde(blockEncodingSerde, Optional.empty(), Optional.empty(), Optional.empty(), false); | ||
| this.typeManager = typeManager; | ||
| this.sqlFunctionProperties = createSqlFunctionPropertiesFromSession(session); | ||
| } | ||
|
|
||
| public Iterable<List<Object>> deserialize(List<Column> columns, Iterable<String> binaryData) | ||
| { | ||
| requireNonNull(columns, "columns is null"); | ||
| requireNonNull(binaryData, "binaryData is null"); | ||
|
|
||
| List<Type> columnTypes = extractTypesFromColumns(columns); | ||
| ImmutableList.Builder<List<Object>> allRows = ImmutableList.builder(); | ||
|
|
||
| for (String encodedPage : binaryData) { | ||
| byte[] pageBytes = Base64.getDecoder().decode(encodedPage); | ||
| Slice slice = Slices.wrappedBuffer(pageBytes); | ||
|
|
||
| BasicSliceInput sliceInput = slice.getInput(); | ||
| SerializedPage serializedPage = readSerializedPage(sliceInput); | ||
|
|
||
| Page page = pagesSerde.deserialize(serializedPage); | ||
|
|
||
| allRows.addAll(convertPageToRows(page, columnTypes)); | ||
| } | ||
|
|
||
| return allRows.build(); | ||
| } | ||
|
|
||
| private List<List<Object>> convertPageToRows(Page page, List<Type> columnTypes) | ||
| { | ||
| checkArgument( | ||
| page.getChannelCount() == columnTypes.size(), | ||
| "Expected %s columns in serialized page, found %s", | ||
| columnTypes.size(), | ||
| page.getChannelCount()); | ||
|
|
||
| ImmutableList.Builder<List<Object>> rows = ImmutableList.builder(); | ||
|
|
||
| for (int position = 0; position < page.getPositionCount(); position++) { | ||
| List<Object> row = new ArrayList<>(page.getChannelCount()); | ||
|
|
||
| for (int channel = 0; channel < page.getChannelCount(); channel++) { | ||
| Type type = columnTypes.get(channel); | ||
| Block block = page.getBlock(channel); | ||
|
|
||
| Object value = type.getObjectValue(sqlFunctionProperties, block, position); | ||
| row.add(value); | ||
| } | ||
|
|
||
| rows.add(Collections.unmodifiableList(row)); | ||
| } | ||
|
|
||
| return rows.build(); | ||
| } | ||
|
|
||
| private List<Type> extractTypesFromColumns(List<Column> columns) | ||
| { | ||
| ImmutableList.Builder<Type> types = ImmutableList.builder(); | ||
|
|
||
| for (Column column : columns) { | ||
| ClientTypeSignature clientTypeSignature = column.getTypeSignature(); | ||
| TypeSignature typeSignature = convertClientTypeSignatureToTypeSignature(clientTypeSignature); | ||
| Type type = typeManager.getType(typeSignature); | ||
|
|
||
| if (type == null) { | ||
| throw new IllegalArgumentException("Unknown type: " + typeSignature); | ||
| } | ||
|
|
||
| types.add(type); | ||
| } | ||
|
|
||
| return types.build(); | ||
| } | ||
|
|
||
| private TypeSignature convertClientTypeSignatureToTypeSignature(ClientTypeSignature clientTypeSignature) | ||
| { | ||
| List<TypeSignatureParameter> parameters = new ArrayList<>(); | ||
|
|
||
| for (ClientTypeSignatureParameter argument : clientTypeSignature.getArguments()) { | ||
| parameters.add(convertClientTypeSignatureParameterToTypeSignatureParameter(argument)); | ||
| } | ||
|
|
||
| return new TypeSignature(clientTypeSignature.getRawType(), parameters); | ||
| } | ||
|
|
||
| private TypeSignatureParameter convertClientTypeSignatureParameterToTypeSignatureParameter( | ||
| ClientTypeSignatureParameter parameter) | ||
| { | ||
| switch (parameter.getKind()) { | ||
| case TYPE: | ||
| return TypeSignatureParameter.of( | ||
| convertClientTypeSignatureToTypeSignature(parameter.getTypeSignature())); | ||
| case LONG: | ||
| return TypeSignatureParameter.of(parameter.getLongLiteral()); | ||
| case NAMED_TYPE: | ||
| return TypeSignatureParameter.of(parameter.getNamedTypeSignature()); | ||
| default: | ||
| throw new UnsupportedOperationException("Unknown parameter kind: " + parameter.getKind()); | ||
| } | ||
| } | ||
|
|
||
| private SqlFunctionProperties createSqlFunctionPropertiesFromSession(ClientSession session) | ||
| { | ||
| return SqlFunctionProperties.builder() | ||
| .setTimeZoneKey(session.getTimeZone()) | ||
| .setSessionLocale(session.getLocale()) | ||
| .setSessionUser(session.getUser()) | ||
| .setSessionStartTime(System.currentTimeMillis()) | ||
| .build(); | ||
| } | ||
| } | ||
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.
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.
suggestion: Consider logging or reporting unknown type errors with column context.
Including the column name or index in the exception message will help identify which column caused the error.