Repository navigation
GH-3289: Test schema and metadata through CLI arguments #3843
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
wgtmac
merged 2 commits into
apache:master
from
zhangxinyao88:codex/parquet-3289-cli-entry-tests
Oct 10, 2026
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
60 changes: 60 additions & 0 deletions
60
parquet-cli/src/test/java/org/apache/parquet/cli/CliTestBase.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,60 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.parquet.cli; | ||
|
|
||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.util.ToolRunner; | ||
| import org.apache.parquet.cli.commands.ParquetFileTest; | ||
| import org.slf4j.event.Level; | ||
| import org.slf4j.event.LoggingEvent; | ||
| import org.slf4j.helpers.MessageFormatter; | ||
| import org.slf4j.helpers.SubstituteLoggerFactory; | ||
|
|
||
| abstract class CliTestBase extends ParquetFileTest { | ||
|
|
||
| protected static CliResult cli(String... args) throws Exception { | ||
| SubstituteLoggerFactory loggerFactory = new SubstituteLoggerFactory(); | ||
| try { | ||
| int exitCode = ToolRunner.run(new Configuration(), new Main(loggerFactory.getLogger("cli-test")), args); | ||
| String output = loggerFactory.getEventQueue().stream() | ||
| .filter(event -> event.getLevel().toInt() >= Level.INFO.toInt()) | ||
| .map(CliTestBase::formatEvent) | ||
| .collect(Collectors.joining("\n")); | ||
| return new CliResult(exitCode, output); | ||
| } finally { | ||
| loggerFactory.clear(); | ||
| } | ||
| } | ||
|
|
||
| private static String formatEvent(LoggingEvent event) { | ||
| String message = MessageFormatter.arrayFormat(event.getMessage(), event.getArgumentArray()) | ||
| .getMessage(); | ||
| if (event.getThrowable() == null) { | ||
| return message; | ||
| } | ||
| StringWriter stackTrace = new StringWriter(); | ||
| event.getThrowable().printStackTrace(new PrintWriter(stackTrace)); | ||
| return message + "\n" + stackTrace; | ||
| } | ||
|
|
||
| protected record CliResult(int exitCode, String output) {} | ||
| } | ||
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
46 changes: 46 additions & 0 deletions
46
parquet-cli/src/test/java/org/apache/parquet/cli/MetadataCliTest.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,46 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.parquet.cli; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class MetadataCliTest extends CliTestBase { | ||
|
|
||
| @Test | ||
| void printsMetadataForPathContainingSpaces() throws Exception { | ||
| Path input = getTempFolder().toPath().resolve("metadata input.parquet"); | ||
| Files.copy(parquetFile().toPath(), input); | ||
|
|
||
| CliResult result = cli("meta", input.toString()); | ||
|
|
||
| assertThat(result.exitCode()).isZero(); | ||
| assertThat(result.output()) | ||
| .contains( | ||
| "File path: " + input, | ||
| "Created by: parquet-mr", | ||
| "Schema:\nmessage schema {", | ||
| "required int32 int32_field;", | ||
| "required fixed_len_byte_array(12) flba_field;", | ||
| "Row group 0: count: 10"); | ||
| } | ||
| } |
123 changes: 123 additions & 0 deletions
123
parquet-cli/src/test/java/org/apache/parquet/cli/SchemaCliTest.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,123 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.parquet.cli; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import org.apache.avro.Schema; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class SchemaCliTest extends CliTestBase { | ||
|
|
||
| private static final String PARQUET_SCHEMA = String.join( | ||
| "\n", | ||
| "message schema {", | ||
| " required int32 int32_field;", | ||
| " required int64 int64_field;", | ||
| " required float float_field;", | ||
| " required double double_field;", | ||
| " required binary binary_field;", | ||
| " required fixed_len_byte_array(12) flba_field;", | ||
| " required int32 date_field (DATE);", | ||
| "}", | ||
| ""); | ||
|
|
||
| @Test | ||
| void printsAvroSchemaByDefault() throws Exception { | ||
| CliResult result = cli("schema", parquetFile().getAbsolutePath()); | ||
|
|
||
| assertThat(result.exitCode()).isZero(); | ||
| Schema schema = new Schema.Parser().parse(result.output()); | ||
| assertThat(schema.getName()).isEqualTo("schema"); | ||
| assertThat(schema.getFields()) | ||
| .extracting(Schema.Field::name) | ||
| .containsExactly( | ||
| "int32_field", | ||
| "int64_field", | ||
| "float_field", | ||
| "double_field", | ||
| "binary_field", | ||
| "flba_field", | ||
| "date_field"); | ||
| assertThat(schema.getField("int32_field").schema().getType()).isEqualTo(Schema.Type.INT); | ||
| assertThat(schema.getField("flba_field").schema().getFixedSize()).isEqualTo(12); | ||
| assertThat(schema.getField("date_field").schema().getLogicalType().getName()) | ||
| .isEqualTo("date"); | ||
| } | ||
|
|
||
| @Test | ||
| void printsParquetSchemaForPathContainingSpaces() throws Exception { | ||
| Path input = getTempFolder().toPath().resolve("input with spaces.parquet"); | ||
| Files.copy(parquetFile().toPath(), input); | ||
|
|
||
| CliResult result = cli("schema", "--parquet", input.toString()); | ||
|
|
||
| assertThat(result.exitCode()).isZero(); | ||
| assertThat(result.output()).isEqualTo(PARQUET_SCHEMA); | ||
| } | ||
|
|
||
| @Test | ||
| void writesSchemaToOutputFile() throws Exception { | ||
| Path output = getTempFolder().toPath().resolve("schema output.txt"); | ||
|
|
||
| CliResult result = cli( | ||
| "schema", | ||
| "--parquet", | ||
| "--output", | ||
| output.toString(), | ||
| parquetFile().getAbsolutePath()); | ||
|
|
||
| assertThat(result.exitCode()).isZero(); | ||
| assertThat(result.output()).isEmpty(); | ||
| assertThat(Files.readString(output, StandardCharsets.UTF_8)).isEqualTo(PARQUET_SCHEMA); | ||
| } | ||
|
|
||
| @Test | ||
| void refusesToOverwriteOutputWithoutFlag() throws Exception { | ||
| Path output = getTempFolder().toPath().resolve("schema.txt"); | ||
| Files.writeString(output, "existing contents", StandardCharsets.UTF_8); | ||
|
|
||
| CliResult result = | ||
| cli("schema", "--output", output.toString(), parquetFile().getAbsolutePath()); | ||
|
|
||
| assertThat(result.exitCode()).isEqualTo(1); | ||
| assertThat(result.output()).contains("File already exists", output.toString()); | ||
| assertThat(Files.readString(output, StandardCharsets.UTF_8)).isEqualTo("existing contents"); | ||
| } | ||
|
|
||
| @Test | ||
| void overwritesOutputWithFlag() throws Exception { | ||
| Path output = getTempFolder().toPath().resolve("schema.txt"); | ||
| Files.writeString(output, "existing contents", StandardCharsets.UTF_8); | ||
|
|
||
| CliResult result = cli( | ||
| "schema", | ||
| "--parquet", | ||
| "-o", | ||
| output.toString(), | ||
| "--overwrite", | ||
| parquetFile().getAbsolutePath()); | ||
|
|
||
| assertThat(result.exitCode()).isZero(); | ||
| assertThat(Files.readString(output, StandardCharsets.UTF_8)).isEqualTo(PARQUET_SCHEMA); | ||
| } | ||
| } |
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.