-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HIVE-27448: Hive Iceberg: Merge column stats
- Loading branch information
SimhadriG
committed
Jun 20, 2023
1 parent
7c83f6b
commit 7d07744
Showing
6 changed files
with
445 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
130 changes: 130 additions & 0 deletions
130
...erg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergColStats.java
This file contains 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,130 @@ | ||
/* | ||
* 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.iceberg.mr.hive; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.hadoop.hive.common.StatsSetupConst; | ||
import org.apache.hadoop.hive.conf.HiveConf; | ||
import org.apache.iceberg.PartitionSpec; | ||
import org.apache.iceberg.catalog.TableIdentifier; | ||
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TestHiveIcebergColStats extends HiveIcebergStorageHandlerWithEngineBase { | ||
|
||
@Test | ||
public void testStatsWithInsert() { | ||
TableIdentifier identifier = TableIdentifier.of("default", "customers"); | ||
|
||
shell.setHiveSessionValue(HiveConf.ConfVars.HIVESTATSAUTOGATHER.varname, true); | ||
testTables.createTable(shell, identifier.name(), HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, | ||
PartitionSpec.unpartitioned(), fileFormat, ImmutableList.of()); | ||
|
||
if (testTableType != TestTables.TestTableType.HIVE_CATALOG) { | ||
// If the location is set and we have to gather stats, then we have to update the table stats now | ||
shell.executeStatement("ANALYZE TABLE " + identifier + " COMPUTE STATISTICS FOR COLUMNS"); | ||
} | ||
|
||
String insert = testTables.getInsertQuery(HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS, identifier, false); | ||
shell.executeStatement(insert); | ||
|
||
checkColStat(identifier.name(), "customer_id", true); | ||
checkColStatMinMaxDistinctValue(identifier.name(), "customer_id", 0, 2, 3, 0); | ||
|
||
insert = testTables.getInsertQuery(HiveIcebergStorageHandlerTestUtils.OTHER_CUSTOMER_RECORDS_1, identifier, false); | ||
shell.executeStatement(insert); | ||
|
||
checkColStat(identifier.name(), "customer_id", true); | ||
checkColStatMinMaxDistinctValue(identifier.name(), "customer_id", 0, 5, 6, 0); | ||
|
||
insert = testTables.getInsertQuery(HiveIcebergStorageHandlerTestUtils.OTHER_CUSTOMER_RECORDS_2, identifier, false); | ||
shell.executeStatement(insert); | ||
checkColStat(identifier.name(), "customer_id", true); | ||
checkColStatMinMaxDistinctValue(identifier.name(), "customer_id", 0, 5, 6, 0); | ||
} | ||
|
||
private void checkColStat(String tableName, String colName, boolean accurate) { | ||
List<Object[]> rows = shell.executeStatement("DESCRIBE " + tableName + " " + colName); | ||
|
||
if (accurate) { | ||
Assert.assertEquals(2, rows.size()); | ||
Assert.assertEquals(StatsSetupConst.COLUMN_STATS_ACCURATE, rows.get(1)[0]); | ||
// Check if the value is not {} (empty) | ||
Assert.assertFalse(rows.get(1)[1].toString().matches("\\{\\}\\s*")); | ||
} else { | ||
// If we expect the stats to be not accurate | ||
if (rows.size() == 1) { | ||
// no stats now, we are ok | ||
return; | ||
} else { | ||
Assert.assertEquals(2, rows.size()); | ||
Assert.assertEquals(StatsSetupConst.COLUMN_STATS_ACCURATE, rows.get(1)[0]); | ||
// Check if the value is {} (empty) | ||
Assert.assertTrue(rows.get(1)[1].toString().matches("\\{\\}\\s*")); | ||
} | ||
} | ||
} | ||
|
||
private void checkColStatMinMaxDistinctValue(String tableName, String colName, int minValue, int maxValue, | ||
int distinct, int nulls) { | ||
|
||
shell.executeStatement("set hive.iceberg.stats.source=metastore"); | ||
List<Object[]> rows = shell.executeStatement("DESCRIBE FORMATTED " + tableName + " " + colName); | ||
|
||
// Check min | ||
Assert.assertEquals("min", rows.get(2)[0]); | ||
Assert.assertEquals(String.valueOf(minValue), rows.get(2)[1]); | ||
|
||
// Check max | ||
Assert.assertEquals("max", rows.get(3)[0]); | ||
Assert.assertEquals(String.valueOf(maxValue), rows.get(3)[1]); | ||
|
||
// Check num of nulls | ||
Assert.assertEquals("num_nulls", rows.get(4)[0]); | ||
Assert.assertEquals(String.valueOf(nulls), rows.get(4)[1]); | ||
|
||
// Check distinct | ||
Assert.assertEquals("distinct_count", rows.get(5)[0]); | ||
Assert.assertEquals(String.valueOf(distinct), rows.get(5)[1]); | ||
|
||
shell.executeStatement("set hive.iceberg.stats.source=iceberg"); | ||
rows = shell.executeStatement("DESCRIBE FORMATTED " + tableName + " " + colName); | ||
|
||
// Check min | ||
Assert.assertEquals("min", rows.get(2)[0]); | ||
Assert.assertEquals(String.valueOf(minValue), rows.get(2)[1]); | ||
|
||
// Check max | ||
Assert.assertEquals("max", rows.get(3)[0]); | ||
Assert.assertEquals(String.valueOf(maxValue), rows.get(3)[1]); | ||
|
||
// Check num of nulls | ||
Assert.assertEquals("num_nulls", rows.get(4)[0]); | ||
Assert.assertEquals(String.valueOf(nulls), rows.get(4)[1]); | ||
|
||
// Check distinct | ||
Assert.assertEquals("distinct_count", rows.get(5)[0]); | ||
Assert.assertEquals(String.valueOf(distinct), rows.get(5)[1]); | ||
|
||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
iceberg/iceberg-handler/src/test/queries/positive/puffin_col_stats.q
This file contains 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,21 @@ | ||
-- Mask random uuid | ||
--! qt:replace:/(\s+uuid\s+)\S+(\s*)/$1#Masked#$2/ | ||
set hive.stats.autogather=true; | ||
set hive.stats.column.autogather=true; | ||
|
||
set hive.iceberg.stats.source=iceberg; | ||
drop table if exists tbl_ice_puffin; | ||
create external table tbl_ice_puffin(a int, b string, c int) stored by iceberg tblproperties ('format-version'='2'); | ||
insert into tbl_ice_puffin values (1, 'one', 50), (2, 'two', 51),(2, 'two', 51),(2, 'two', 51), (3, 'three', 52), (4, 'four', 53), (5, 'five', 54), (111, 'one', 55), (333, 'two', 56); | ||
desc formatted tbl_ice_puffin a; | ||
desc formatted tbl_ice_puffin c; | ||
explain select * from tbl_ice_puffin order by a, b, c; | ||
insert into tbl_ice_puffin values (1000, 'one', 1000), (5000, 'two', 5000); | ||
desc formatted tbl_ice_puffin a; | ||
desc formatted tbl_ice_puffin c; | ||
explain select * from tbl_ice_puffin order by a, b, c; | ||
insert into tbl_ice_puffin values (10, 'one', 100000), (5000, 'two', 510000); | ||
explain select * from tbl_ice_puffin order by a, b, c; | ||
desc formatted tbl_ice_puffin a; | ||
desc formatted tbl_ice_puffin c; | ||
-- Result: a = (min: 1, max: 5000) , c =(min: 50, max: 51000) |
This file contains 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.