Last active
February 21, 2025 10:50
-
-
Save wendigo/5fbc1a79b889c55b4495549ff8437824 to your computer and use it in GitHub Desktop.
Out-of-bound data retrieval using spooling protocol
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
| /* | |
| * 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.testing; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.google.common.collect.ImmutableList; | |
| import io.trino.client.ClientSession; | |
| import io.trino.client.Column; | |
| import io.trino.client.OkHttpSegmentLoader; | |
| import io.trino.client.ResultRowsDecoder; | |
| import io.trino.client.StatementClient; | |
| import io.trino.client.spooling.DataAttribute; | |
| import io.trino.client.spooling.EncodedQueryData; | |
| import okhttp3.OkHttpClient; | |
| import java.io.IOException; | |
| import java.net.URI; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Optional; | |
| import java.util.concurrent.atomic.AtomicInteger; | |
| import java.util.stream.Collectors; | |
| import static com.google.common.collect.ImmutableList.toImmutableList; | |
| import static io.trino.client.StatementClientFactory.newStatementClient; | |
| import static java.time.ZoneId.systemDefault; | |
| public class TestSegmentClient | |
| { | |
| private static final ObjectMapper mapper = new ObjectMapper(); | |
| private TestSegmentClient() {} | |
| public static void main(String[] args) | |
| throws InterruptedException, IOException | |
| { | |
| OkHttpClient okHttpClient = new OkHttpClient(); | |
| ClientSession session = ClientSession.builder() | |
| .server(URI.create("http://localhost:8080")) | |
| .user(Optional.of("admin")) | |
| .source("wendigo/v1.1") | |
| .timeZone(systemDefault()) | |
| .encoding(Optional.of("json+zstd")) | |
| .properties(Map.of("spooling_inlining_enabled", "false")) | |
| .build(); | |
| List<String> serializedSegments = new ArrayList<>(); | |
| List<Column> columns = new ArrayList<>(); | |
| long start = System.nanoTime(); | |
| try (StatementClient clientV1 = newStatementClient(okHttpClient, okHttpClient, session, "select * from tpch.sf10.lineitem LIMIT 5_000_000", Optional.empty())) { | |
| while (clientV1.advance()) { | |
| if (clientV1.currentStatusInfo().getError() != null) { | |
| System.out.println("Query failed: " + clientV1.currentStatusInfo().getError()); | |
| System.exit(3); | |
| } | |
| if (clientV1.currentData() == null || clientV1.currentData().isNull()) { | |
| continue; | |
| } | |
| columns = clientV1.currentStatusInfo().getColumns(); | |
| if (clientV1.currentData() instanceof EncodedQueryData encodedQueryData) { | |
| List<EncodedQueryData> perSegmentData = encodedQueryData.getSegments().stream().map(segment -> | |
| new EncodedQueryData(encodedQueryData.getEncoding(), encodedQueryData.getMetadata(), ImmutableList.of(segment))) | |
| .collect(toImmutableList()); | |
| for (EncodedQueryData perSegment : perSegmentData) { | |
| serializedSegments.add(mapper.writeValueAsString(perSegment)); | |
| } | |
| } | |
| else { | |
| throw new IllegalStateException("Current data is not EncodedQueryData"); | |
| } | |
| } | |
| } | |
| System.out.println(serializedSegments); | |
| System.out.println("Query finished in " + ((System.nanoTime() - start) / 1_000_000_000d) + " s"); | |
| System.out.println("Now reading data out of band"); | |
| AtomicInteger rowsCount = new AtomicInteger(); | |
| AtomicInteger dataSize = new AtomicInteger(); | |
| try (ResultRowsDecoder decoder = new ResultRowsDecoder(new OkHttpSegmentLoader(okHttpClient))) { | |
| for (String segment : serializedSegments) { | |
| EncodedQueryData data = (EncodedQueryData) mapper.readValue(segment, EncodedQueryData.class); | |
| dataSize.addAndGet(data.getSegments().getFirst().getMetadata().get(DataAttribute.SEGMENT_SIZE, Integer.class)); | |
| decoder.toRows(columns, data).forEach(row -> { | |
| if (rowsCount.incrementAndGet() % 1_000_000 == 0) { | |
| System.out.println(row.stream() | |
| .map(value -> value.getClass().getSimpleName() + "('" + value + "')") | |
| .collect(Collectors.joining(", ", "[", "]"))); | |
| } | |
| }); | |
| } | |
| System.out.println("Fully read data in " + ((System.nanoTime() - start) / 1_000_000_000d) + " s"); | |
| System.out.println("Total row count is " + rowsCount.get()); | |
| System.out.println("Total data size is " + dataSize.get()); | |
| } | |
| catch (Exception e) { | |
| throw new RuntimeException(e); | |
| } | |
| okHttpClient.dispatcher().executorService().shutdown(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment