-
Notifications
You must be signed in to change notification settings - Fork 5
Added functionality similar to parquet-tools to print info of parquet… #65
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
subkanthi
wants to merge
6
commits into
master
Choose a base branch
from
59-provide-convenient-introspection-of-parquet-files
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.
+387
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
01515c8
Added functionality similar to parquet-tools to print info of parquet…
subkanthi 967bad6
Fixed unit tests.
subkanthi 6dd8278
Formatting changes.
subkanthi b422f8d
Merge branch 'master' of github.com:Altinity/ice into 59-provide-conv…
subkanthi ce3c2f0
Pass s3 region and no-sign-request options to show parquet metadata o…
subkanthi be1d9f0
use S3CrossRegionSyncClient
subkanthi 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
228 changes: 228 additions & 0 deletions
228
ice/src/main/java/com/altinity/ice/cli/internal/cmd/DescribeParquet.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,228 @@ | ||
| /* | ||
| * Copyright (c) 2025 Altinity Inc and/or its affiliates. All rights reserved. | ||
| * | ||
| * 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 | ||
| */ | ||
| package com.altinity.ice.cli.internal.cmd; | ||
|
|
||
| import com.altinity.ice.cli.internal.iceberg.io.Input; | ||
| import com.altinity.ice.cli.internal.iceberg.parquet.Metadata; | ||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.apache.iceberg.io.FileIO; | ||
| import org.apache.iceberg.io.InputFile; | ||
| import org.apache.iceberg.rest.RESTCatalog; | ||
| import org.apache.parquet.column.statistics.Statistics; | ||
| import org.apache.parquet.hadoop.metadata.BlockMetaData; | ||
| import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData; | ||
| import org.apache.parquet.hadoop.metadata.FileMetaData; | ||
| import org.apache.parquet.hadoop.metadata.ParquetMetadata; | ||
| import org.apache.parquet.schema.MessageType; | ||
| import org.apache.parquet.schema.Type; | ||
| import software.amazon.awssdk.services.s3.internal.crossregion.S3CrossRegionSyncClient; | ||
| import software.amazon.awssdk.utils.Lazy; | ||
|
|
||
| public final class DescribeParquet { | ||
|
|
||
| private DescribeParquet() {} | ||
|
|
||
| public enum Option { | ||
| ALL, | ||
| SUMMARY, | ||
| COLUMNS, | ||
| ROW_GROUPS, | ||
| ROW_GROUP_DETAILS | ||
| } | ||
|
|
||
| public static void run( | ||
| RESTCatalog catalog, | ||
| String filePath, | ||
| boolean json, | ||
| boolean s3NoSignRequest, | ||
| Option... options) | ||
| throws IOException { | ||
|
|
||
| Lazy<software.amazon.awssdk.services.s3.S3Client> s3ClientLazy = | ||
| new Lazy<>( | ||
| () -> | ||
| new S3CrossRegionSyncClient( | ||
| com.altinity.ice.cli.internal.s3.S3.newClient(s3NoSignRequest))); | ||
| FileIO io = Input.newIO(filePath, null, s3ClientLazy); | ||
| InputFile inputFile = Input.newFile(filePath, catalog, io); | ||
| run(inputFile, json, options); | ||
| } | ||
|
|
||
| public static void run(InputFile inputFile, boolean json, Option... options) throws IOException { | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line here could be removed |
||
| ParquetMetadata metadata = Metadata.read(inputFile); | ||
|
|
||
| ParquetInfo info = extractParquetInfo(metadata, options); | ||
|
|
||
| ObjectMapper mapper = json ? new ObjectMapper() : new ObjectMapper(new YAMLFactory()); | ||
| mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
| String output = mapper.writeValueAsString(info); | ||
| System.out.println(output); | ||
| } | ||
|
|
||
| private static ParquetInfo extractParquetInfo(ParquetMetadata metadata, Option... options) { | ||
| var optionsSet = java.util.Set.of(options); | ||
| boolean includeAll = optionsSet.contains(Option.ALL); | ||
|
|
||
| FileMetaData fileMetadata = metadata.getFileMetaData(); | ||
|
|
||
| // Summary info | ||
| Summary summary = null; | ||
| if (includeAll || optionsSet.contains(Option.SUMMARY)) { | ||
| long totalRows = metadata.getBlocks().stream().mapToLong(BlockMetaData::getRowCount).sum(); | ||
|
|
||
| long compressedSize = | ||
| metadata.getBlocks().stream().mapToLong(BlockMetaData::getCompressedSize).sum(); | ||
|
|
||
| long uncompressedSize = | ||
| metadata.getBlocks().stream().mapToLong(BlockMetaData::getTotalByteSize).sum(); | ||
|
|
||
| summary = | ||
| new Summary( | ||
| totalRows, | ||
| metadata.getBlocks().size(), | ||
| compressedSize, | ||
| uncompressedSize, | ||
| fileMetadata.getCreatedBy(), | ||
| fileMetadata.getSchema().getFieldCount()); | ||
| } | ||
|
|
||
| // Column info | ||
| List<Column> columns = null; | ||
| if (includeAll || optionsSet.contains(Option.COLUMNS)) { | ||
| columns = extractColumns(fileMetadata.getSchema()); | ||
| } | ||
|
|
||
| // Row group info | ||
| List<RowGroup> rowGroups = null; | ||
| if (includeAll | ||
| || optionsSet.contains(Option.ROW_GROUPS) | ||
| || optionsSet.contains(Option.ROW_GROUP_DETAILS)) { | ||
| boolean includeDetails = includeAll || optionsSet.contains(Option.ROW_GROUP_DETAILS); | ||
| rowGroups = extractRowGroups(metadata.getBlocks(), includeDetails); | ||
| } | ||
|
|
||
| return new ParquetInfo(summary, columns, rowGroups); | ||
| } | ||
|
|
||
| private static List<Column> extractColumns(MessageType schema) { | ||
| List<Column> columns = new ArrayList<>(); | ||
| for (Type field : schema.getFields()) { | ||
| String logicalType = null; | ||
| if (field.isPrimitive()) { | ||
| var annotation = field.asPrimitiveType().getLogicalTypeAnnotation(); | ||
| logicalType = annotation != null ? annotation.toString() : null; | ||
| } | ||
| columns.add( | ||
| new Column( | ||
| field.getName(), | ||
| field.isPrimitive() ? field.asPrimitiveType().getPrimitiveTypeName().name() : "GROUP", | ||
| field.getRepetition().name(), | ||
| logicalType)); | ||
| } | ||
| return columns; | ||
| } | ||
|
|
||
| private static List<RowGroup> extractRowGroups( | ||
| List<BlockMetaData> blocks, boolean includeDetails) { | ||
| List<RowGroup> rowGroups = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < blocks.size(); i++) { | ||
| BlockMetaData block = blocks.get(i); | ||
|
|
||
| List<ColumnChunk> columnChunks = null; | ||
| if (includeDetails) { | ||
| columnChunks = new ArrayList<>(); | ||
| for (ColumnChunkMetaData column : block.getColumns()) { | ||
| Statistics<?> stats = column.getStatistics(); | ||
|
|
||
| ColumnStats columnStats = null; | ||
| if (stats != null && !stats.isEmpty()) { | ||
| long nulls = stats.isNumNullsSet() ? stats.getNumNulls() : 0; | ||
| String min = null; | ||
| String max = null; | ||
| if (stats.hasNonNullValue()) { | ||
| Object minVal = stats.genericGetMin(); | ||
| Object maxVal = stats.genericGetMax(); | ||
| min = minVal != null ? minVal.toString() : null; | ||
| max = maxVal != null ? maxVal.toString() : null; | ||
| } | ||
| columnStats = new ColumnStats(nulls, min, max); | ||
| } | ||
|
|
||
| columnChunks.add( | ||
| new ColumnChunk( | ||
| column.getPath().toDotString(), | ||
| column.getPrimitiveType().getName(), | ||
| column.getEncodings().toString(), | ||
| column.getCodec().name(), | ||
| column.getTotalSize(), | ||
| column.getTotalUncompressedSize(), | ||
| column.getValueCount(), | ||
| columnStats)); | ||
| } | ||
| } | ||
|
|
||
| rowGroups.add( | ||
| new RowGroup( | ||
| i, | ||
| block.getRowCount(), | ||
| block.getTotalByteSize(), | ||
| block.getCompressedSize(), | ||
| block.getStartingPos(), | ||
| columnChunks)); | ||
| } | ||
|
|
||
| return rowGroups; | ||
| } | ||
|
|
||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public record ParquetInfo(Summary summary, List<Column> columns, List<RowGroup> rowGroups) {} | ||
|
|
||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public record Summary( | ||
| long rows, | ||
| int rowGroups, | ||
| long compressedSize, | ||
| long uncompressedSize, | ||
| String createdBy, | ||
| int columnCount) {} | ||
|
|
||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public record Column(String name, String type, String repetition, String logicalType) {} | ||
|
|
||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public record RowGroup( | ||
| int index, | ||
| long rowCount, | ||
| long totalSize, | ||
| long compressedSize, | ||
| long startingPos, | ||
| List<ColumnChunk> columns) {} | ||
|
|
||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public record ColumnChunk( | ||
| String path, | ||
| String type, | ||
| String encodings, | ||
| String codec, | ||
| long totalSize, | ||
| long uncompressedSize, | ||
| long valueCount, | ||
| ColumnStats stats) {} | ||
|
|
||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public record ColumnStats(long nulls, String min, String max) {} | ||
| } | ||
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.
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.
This could be imported