-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Support converting column stats on row type to json in Delta Lake #14314
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import io.trino.Session; | ||
| import io.trino.execution.QueryManager; | ||
| import io.trino.operator.OperatorStats; | ||
| import io.trino.plugin.deltalake.transactionlog.AddFileEntry; | ||
| import io.trino.plugin.hive.TestingHivePlugin; | ||
| import io.trino.plugin.hive.containers.HiveHadoop; | ||
| import io.trino.plugin.hive.containers.HiveMinioDataLake; | ||
|
|
@@ -40,11 +41,13 @@ | |
| import org.testng.annotations.DataProvider; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.BiConsumer; | ||
|
|
||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static com.google.common.collect.ImmutableSet.toImmutableSet; | ||
| import static com.google.common.collect.MoreCollectors.onlyElement; | ||
| import static com.google.common.collect.Sets.union; | ||
|
|
@@ -65,6 +68,7 @@ | |
| import static io.trino.tpch.TpchTable.LINE_ITEM; | ||
| import static io.trino.tpch.TpchTable.ORDERS; | ||
| import static java.lang.String.format; | ||
| import static java.util.Comparator.comparing; | ||
| import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
@@ -93,6 +97,7 @@ public abstract class BaseDeltaLakeConnectorSmokeTest | |
| "old_timestamps", | ||
| "nested_timestamps", | ||
| "nested_timestamps_parquet_stats", | ||
| "json_stats_on_row_type", | ||
| "parquet_stats_missing", | ||
| "uppercase_columns", | ||
| "default_partitions", | ||
|
|
@@ -831,6 +836,59 @@ public void testSelectNestedTimestamps() | |
| assertQuery("SELECT CAST(col1[1].ts AS VARCHAR) FROM nested_timestamps_parquet_stats LIMIT 1", "VALUES '2010-02-03 12:11:10.000 UTC'"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testConvertJsonStatisticsToParquetOnRowType() | ||
| throws Exception | ||
| { | ||
| verifySupportsInsert(); | ||
|
Member
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. a "verify ..." should verify, i.e. ensure something is true as a follow-up we could rename this to eg
Member
Author
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. I will send a follow-up PR. |
||
|
|
||
| assertQuery("SELECT count(*) FROM json_stats_on_row_type", "VALUES 2"); | ||
| String transactionLogDirectory = "json_stats_on_row_type/_delta_log"; | ||
| String newTransactionFile = getLocationForTable(bucketName, "json_stats_on_row_type") + "/_delta_log/00000000000000000004.json"; | ||
| String newCheckpointFile = getLocationForTable(bucketName, "json_stats_on_row_type") + "/_delta_log/00000000000000000004.checkpoint.parquet"; | ||
| assertThat(getTableFiles(transactionLogDirectory)) | ||
| .doesNotContain(newTransactionFile, newCheckpointFile); | ||
|
|
||
| assertUpdate("INSERT INTO json_stats_on_row_type SELECT CAST(row(3) AS row(x bigint)), CAST(row(row('test insert')) AS row(y row(nested varchar)))", 1); | ||
| assertThat(getTableFiles(transactionLogDirectory)) | ||
| .contains(newTransactionFile, newCheckpointFile); | ||
| assertThat(getAddFileEntries("json_stats_on_row_type")).hasSize(3); | ||
|
|
||
| // The first two entries created by Databricks have column stats. The last one doesn't have column stats because the connector doesn't support collecting it on row columns. | ||
| List<AddFileEntry> addFileEntries = getAddFileEntries("json_stats_on_row_type").stream().sorted(comparing(AddFileEntry::getModificationTime)).collect(toImmutableList()); | ||
|
Member
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. do the i think the intention is that we create transaction 4 and a checkpoint, so let's verify that happened |
||
| assertThat(addFileEntries).hasSize(3); | ||
| assertJsonStatistics( | ||
|
Contributor
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. The assertions for
Member
Author
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. No, they're relevant. Those two assertions fail if we don't copy the statistics. |
||
| addFileEntries.get(0), | ||
| "{" + | ||
| "\"numRecords\":1," + | ||
| "\"minValues\":{\"nested_struct_col\":{\"y\":{\"nested\":\"test\"}},\"struct_col\":{\"x\":1}}," + | ||
| "\"maxValues\":{\"nested_struct_col\":{\"y\":{\"nested\":\"test\"}},\"struct_col\":{\"x\":1}}," + | ||
| "\"nullCount\":{\"struct_col\":{\"x\":0},\"nested_struct_col\":{\"y\":{\"nested\":0}}}" + | ||
| "}"); | ||
| assertJsonStatistics( | ||
| addFileEntries.get(1), | ||
| "{" + | ||
| "\"numRecords\":1," + | ||
| "\"minValues\":{\"nested_struct_col\":{\"y\":{}},\"struct_col\":{}}," + | ||
| "\"maxValues\":{\"nested_struct_col\":{\"y\":{}},\"struct_col\":{}}," + | ||
| "\"nullCount\":{\"struct_col\":{\"x\":1},\"nested_struct_col\":{\"y\":{\"nested\":1}}}" + | ||
| "}"); | ||
| assertJsonStatistics( | ||
| addFileEntries.get(2), | ||
| "{\"numRecords\":1,\"minValues\":{},\"maxValues\":{},\"nullCount\":{}}"); | ||
| } | ||
|
|
||
| private List<AddFileEntry> getAddFileEntries(String tableName) | ||
| throws IOException | ||
| { | ||
| return TestingDeltaLakeUtils.getAddFileEntries(getLocationForTable(bucketName, tableName)); | ||
| } | ||
|
|
||
| private void assertJsonStatistics(AddFileEntry addFileEntry, @Language("JSON") String jsonStatistics) | ||
| { | ||
| assertEquals(addFileEntry.getStatsString().orElseThrow(), jsonStatistics); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMissingParquetStats() | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * 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.plugin.deltalake; | ||
|
|
||
| import io.trino.filesystem.hdfs.HdfsFileSystemFactory; | ||
| import io.trino.plugin.deltalake.transactionlog.AddFileEntry; | ||
| import io.trino.plugin.deltalake.transactionlog.TransactionLogAccess; | ||
| import io.trino.plugin.deltalake.transactionlog.checkpoint.CheckpointSchemaManager; | ||
| import io.trino.plugin.hive.FileFormatDataSourceStats; | ||
| import io.trino.plugin.hive.parquet.ParquetReaderConfig; | ||
| import io.trino.spi.connector.SchemaTableName; | ||
| import io.trino.testing.TestingConnectorContext; | ||
| import org.apache.hadoop.fs.Path; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import static io.trino.plugin.hive.HiveTestUtils.HDFS_ENVIRONMENT; | ||
| import static io.trino.testing.TestingConnectorSession.SESSION; | ||
|
|
||
| public final class TestingDeltaLakeUtils | ||
| { | ||
| private TestingDeltaLakeUtils() {} | ||
|
|
||
| public static List<AddFileEntry> getAddFileEntries(String tableLocation) | ||
| throws IOException | ||
| { | ||
| SchemaTableName dummyTable = new SchemaTableName("dummy_schema_placeholder", "dummy_table_placeholder"); | ||
| TestingConnectorContext context = new TestingConnectorContext(); | ||
|
|
||
| TransactionLogAccess transactionLogAccess = new TransactionLogAccess( | ||
| context.getTypeManager(), | ||
| new CheckpointSchemaManager(context.getTypeManager()), | ||
| new DeltaLakeConfig(), | ||
| new FileFormatDataSourceStats(), | ||
| new HdfsFileSystemFactory(HDFS_ENVIRONMENT), | ||
| new ParquetReaderConfig()); | ||
|
|
||
| return transactionLogAccess.getActiveFiles(transactionLogAccess.loadSnapshot(dummyTable, new Path(tableLocation), SESSION), SESSION); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| Data generated using Databricks 10.4: | ||
|
|
||
| ```sql | ||
| CREATE TABLE default.json_stats_on_row_type | ||
| (struct_col struct<x bigint>, nested_struct_col struct<y struct<nested string>>) | ||
| USING DELTA | ||
| LOCATION 's3://bucket/table' | ||
| TBLPROPERTIES ( | ||
| delta.checkpointInterval = 2, | ||
| delta.checkpoint.writeStatsAsJson = false, | ||
| delta.checkpoint.writeStatsAsStruct = true | ||
| ); | ||
|
|
||
| INSERT INTO default.json_stats_on_row_type SELECT named_struct('x', 1), named_struct('y', named_struct('nested', 'test')); | ||
| INSERT INTO default.json_stats_on_row_type SELECT named_struct('x', NULL), named_struct('y', named_struct('nested', NULL)); | ||
|
|
||
| ALTER TABLE default.json_stats_on_row_type SET TBLPROPERTIES ( | ||
| 'delta.checkpoint.writeStatsAsJson' = true, | ||
| 'delta.checkpoint.writeStatsAsStruct' = false | ||
| ); | ||
| ``` | ||
|
Comment on lines
1
to
21
Member
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. ❤️ |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| {"add":{"path":"part-00000-df481541-fe59-4af2-a37f-68a39a1e2a5d-c000.snappy.parquet","partitionValues":{},"size":1074,"modificationTime":1664924826000,"dataChange":true,"stats":"{\"numRecords\":1,\"minValues\":{\"struct_col\":{},\"nested_struct_col\":{\"y\":{}}},\"maxValues\":{\"struct_col\":{},\"nested_struct_col\":{\"y\":{}}},\"nullCount\":{\"struct_col\":{\"x\":1},\"nested_struct_col\":{\"y\":{\"nested\":1}}}}","tags":{"INSERTION_TIME":"1664924826000000","OPTIMIZE_TARGET_SIZE":"268435456"}}} | ||
| {"commitInfo":{"timestamp":1664924826032,"userId":"7853186923043731","userName":"yuya.ebihara@starburstdata.com","operation":"WRITE","operationParameters":{"mode":"Append","partitionBy":"[]"},"notebook":{"notebookId":"2299734316069194"},"clusterId":"0620-043712-o6vqr39c","readVersion":1,"isolationLevel":"WriteSerializable","isBlindAppend":true,"operationMetrics":{"numFiles":"1","numOutputRows":"1","numOutputBytes":"1074"},"engineInfo":"Databricks-Runtime/10.4.x-scala2.12","txnId":"974a8474-26b8-42fc-a2c6-4d6af29109a2"}} |
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.
Rather than
getChildrenI think you want to convert the rowBlock to aColumnarRowThere 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.
The argument is
SingleRowBlockwhich is unsupported inColumnarRow#toColumnarRow.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.
That's surprising,
toColumnarRowchecks that the input is an instance of AbstractRowBlock, which SingleRowBlock extends. Seems like it should work.Where does the error come from?
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.
SingleRowBlock extends AbstractSingleRowBlock, not AbstractRowBlock.
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.
Ah, sorry I can't read