Repository navigation
[format] Add csv.skip-empty-lines to skip empty lines when reading CSV #10824
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
COLDMANYU
wants to merge
2
commits into
apache:master
Choose a base branch
from
COLDMANYU:csv-skip-empty-lines
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.
+338
−0
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
155 changes: 155 additions & 0 deletions
155
paimon-core/src/test/java/org/apache/paimon/table/CsvSkipEmptyLinesTableTest.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,155 @@ | ||
| /* | ||
| * 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.paimon.table; | ||
|
|
||
| import org.apache.paimon.CoreOptions; | ||
| import org.apache.paimon.catalog.Identifier; | ||
| import org.apache.paimon.data.GenericRow; | ||
| import org.apache.paimon.data.InternalRow; | ||
| import org.apache.paimon.format.csv.CsvOptions; | ||
| import org.apache.paimon.fs.local.LocalFileIO; | ||
| import org.apache.paimon.reader.RecordReader; | ||
| import org.apache.paimon.schema.Schema; | ||
| import org.apache.paimon.schema.SchemaChange; | ||
| import org.apache.paimon.table.source.Split; | ||
| import org.apache.paimon.table.source.TableScan; | ||
| import org.apache.paimon.types.DataTypes; | ||
| import org.apache.paimon.types.RowType; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.apache.paimon.testutils.assertj.PaimonAssertions.anyCauseMatches; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| /** | ||
| * Tests that {@link CsvOptions#SKIP_EMPTY_LINES} is only accepted for format tables. A file-store | ||
| * table records the number of written rows in its file metadata, which COUNT(*) push-down reads, so | ||
| * skipping rows while scanning would make the two disagree. | ||
| */ | ||
| class CsvSkipEmptyLinesTableTest extends TableTestBase { | ||
|
|
||
| private static final String ERROR = "csv.skip-empty-lines is only supported for format tables"; | ||
|
|
||
| @Test | ||
| void testRejectedWhenCreatingFileStoreTable() { | ||
| assertThatThrownBy( | ||
| () -> | ||
| catalog.createTable( | ||
| identifier("t_create"), | ||
| csvTableSchema( | ||
| Collections.singletonMap( | ||
| CsvOptions.SKIP_EMPTY_LINES.key(), "true")), | ||
| false)) | ||
| .satisfies(anyCauseMatches(IllegalArgumentException.class, ERROR)); | ||
| } | ||
|
|
||
| @Test | ||
| void testRejectedWhenAlteringFileStoreTable() throws Exception { | ||
| Identifier identifier = identifier("t_alter"); | ||
| catalog.createTable(identifier, csvTableSchema(Collections.emptyMap()), false); | ||
|
|
||
| assertThatThrownBy( | ||
| () -> | ||
| catalog.alterTable( | ||
| identifier, | ||
| SchemaChange.setOption( | ||
| CsvOptions.SKIP_EMPTY_LINES.key(), "true"), | ||
| false)) | ||
| .satisfies(anyCauseMatches(IllegalArgumentException.class, ERROR)); | ||
| } | ||
|
|
||
| @Test | ||
| void testRejectedAsDynamicOptionOfFileStoreTable() throws Exception { | ||
| Identifier identifier = identifier("t_dynamic"); | ||
| catalog.createTable(identifier, csvTableSchema(Collections.emptyMap()), false); | ||
| Table table = catalog.getTable(identifier); | ||
| // A single NULL column is written as an empty line, so this is the row that would be | ||
| // skipped while the file metadata still counts it. | ||
| write(table, GenericRow.of((Object) null), GenericRow.of(1)); | ||
|
|
||
| assertThatThrownBy( | ||
| () -> | ||
| table.copy( | ||
| Collections.singletonMap( | ||
| CsvOptions.SKIP_EMPTY_LINES.key(), "true"))) | ||
| .satisfies(anyCauseMatches(IllegalArgumentException.class, ERROR)); | ||
|
|
||
| // Without the option, the scan and the row count in the metadata agree. | ||
| assertThat(read(table)).hasSize(2); | ||
| long metadataRowCount = 0; | ||
| for (Split split : table.newReadBuilder().newScan().plan().splits()) { | ||
| metadataRowCount += split.mergedRowCount().getAsLong(); | ||
| } | ||
| assertThat(metadataRowCount).isEqualTo(2); | ||
| } | ||
|
|
||
| @Test | ||
| void testFormatTableSkipsEmptyLinesWithoutRowCount() throws Exception { | ||
| java.nio.file.Path location = tempPath.resolve("format_table"); | ||
| Files.createDirectories(location); | ||
| Files.write(location.resolve("data.csv"), "1\n\n \n2\n".getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| Map<String, String> options = new HashMap<>(); | ||
| options.put(CoreOptions.FILE_FORMAT.key(), "csv"); | ||
| options.put(CsvOptions.SKIP_EMPTY_LINES.key(), "true"); | ||
| FormatTable table = | ||
| FormatTable.builder() | ||
| .fileIO(LocalFileIO.create()) | ||
| .identifier(identifier("t_format")) | ||
| .rowType(RowType.of(DataTypes.INT())) | ||
| .partitionKeys(Collections.emptyList()) | ||
| .location(location.toString()) | ||
| .format(FormatTable.Format.CSV) | ||
| .options(options) | ||
| .build(); | ||
|
|
||
| TableScan.Plan plan = table.newReadBuilder().newScan().plan(); | ||
| // Format table splits carry no row count, so COUNT(*) can never be answered from | ||
| // metadata and always scans the rows, which skips the empty lines. | ||
| for (Split split : plan.splits()) { | ||
| assertThat(split.mergedRowCount()).isEmpty(); | ||
| } | ||
| List<Integer> ids = new ArrayList<>(); | ||
| try (RecordReader<InternalRow> reader = | ||
| table.newReadBuilder().newRead().createReader(plan)) { | ||
| reader.forEachRemaining(row -> ids.add(row.getInt(0))); | ||
| } | ||
| assertThat(ids).containsExactly(1, 2); | ||
| } | ||
|
|
||
| private static Schema csvTableSchema(Map<String, String> extraOptions) { | ||
| Schema.Builder builder = | ||
| Schema.newBuilder() | ||
| .column("v", DataTypes.INT()) | ||
| .option(CoreOptions.FILE_FORMAT.key(), "csv") | ||
| .option(CoreOptions.FILE_COMPRESSION.key(), "gzip") | ||
| .option(CoreOptions.BUCKET.key(), "-1"); | ||
| extraOptions.forEach(builder::option); | ||
| return builder.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
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.
[P2] Keep persisted-table metadata counts consistent when skipping rows
This option is also accepted by a normal append-only FileStoreTable. I reproduced this with a nullable single INT column,
file.format=csv,file.compression=gzip,bucket=-1, andcsv.skip-empty-lines=true: write(NULL), (1), commit, and scan. The reader returns 1 row, but the planned DataSplit still reportsmergedRowCount() = 2. With the option disabled, both counts are 2. Spark'sAggregatePushDownUtils.tryPushdownAggregationaccepts this split andCountStarEvaluatorsums that metadata, so a pushedCOUNT(*)reports 2 although a normal scan returns only 1. Please restrict this behavior to external FormatTable reads, or reject/handle it for persisted tables and disable metadata aggregation wherever rows can be skipped. Add a table-level regression covering the count versus scanned rows.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.
Thanks for the careful review and the reproduction! You're right, I only had external format tables in mind and missed that a normal file-store table accepts the option too.
I chose to reject it for persisted tables. Our use case only needs it for external format tables that read CSV files written by Spark, so restricting the option keeps the change small and avoids touching metadata aggregation. If there is a use case for file-store tables later, it could be supported then by disabling metadata aggregation wherever rows can be skipped.
SchemaValidation.validateTableSchemanow rejectscsv.skip-empty-lines=trueunless the table type isformat-table. This check also runs when anALTER TABLEsets the option and when the option is passed as a dynamic option throughTable#copy, so none of these paths can turn it on for a file-store table.FormatDataSplit#mergedRowCount()is always empty, soAggregatePushDownUtils.tryPushdownAggregationnever pushesCOUNT(*)down for them and the rows are always scanned.New table-level tests in
CsvSkipEmptyLinesTableTest:ALTER TABLE, and passing it as a dynamic option all fail.csv,gzip,bucket=-1, rows(NULL), (1)), a scan without the option returns 2 rows and the metadata row count is also 2.Without the new check, the three rejection tests fail. A scan with the option then returned 1 row while the metadata still reported 2, the same mismatch you found.