Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#484][#548] fix(hive): Fix bad paritioned table definition (#549)

mchadesad6550698c3e
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
index c5bf149d63..325585e97a 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
@@ -44,6 +44,7 @@ import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.hadoop.conf.Configuration;
@@ -430,6 +431,55 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
}
}
+ private void validatePartitionForCreate(Column[] columns, Transform[] partitions) {
+ int partitionStartIndex = columns.length - partitions.length;
+
+ for (int i = 0; i < partitions.length; i++) {
+ Preconditions.checkArgument(
+ partitions[i] instanceof Transforms.NamedReference,
+ "Hive partition only supports identity transform");
+
+ Transforms.NamedReference namedReference = (Transforms.NamedReference) partitions[i];
+ Preconditions.checkArgument(
+ namedReference.value().length == 1, "Hive partition does not support nested field");
+
+ // The partition field must be placed at the end of the columns in order.
+ // For example, if the table has columns [a, b, c, d], then the partition field must be
+ // [b, c, d] or [c, d] or [d].
+ Preconditions.checkArgument(
+ columns[partitionStartIndex + i].name().equals(namedReference.value()[0]),
+ "The partition field must be placed at the end of the columns in order");
+ }
+ }
+
+ private void validatePartitionForAlter(
+ TableChange[] changes, org.apache.hadoop.hive.metastore.api.Table hiveTable) {
+ Set<String> partitionFields =
+ hiveTable.getPartitionKeys().stream().map(FieldSchema::getName).collect(Collectors.toSet());
+ Arrays.stream(changes)
+ .filter(c -> c instanceof TableChange.ColumnChange)
+ .forEach(
+ c -> {
+ String changeColumn = ((TableChange.ColumnChange) c).fieldNames()[0];
+ Preconditions.checkArgument(
+ c instanceof TableChange.UpdateColumnComment
+ || !partitionFields.contains(changeColumn),
+ "Cannot alter partition column: " + changeColumn);
+
+ if (c instanceof TableChange.UpdateColumnPosition
+ && ((TableChange.UpdateColumnPosition) c).getPosition()
+ instanceof TableChange.After) {
+ String afterColumn =
+ ((TableChange.After) ((TableChange.UpdateColumnPosition) c).getPosition())
+ .getColumn();
+ if (partitionFields.contains(afterColumn)) {
+ throw new IllegalArgumentException(
+ "Cannot alter column position to after partition column: " + afterColumn);
+ }
+ }
+ });
+ }
+
private void validateDistributionAndSort(Distribution distribution, SortOrder[] sortOrder) {
if (distribution != Distribution.NONE) {
boolean allNameReference =
@@ -471,9 +521,7 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
throws NoSuchSchemaException, TableAlreadyExistsException {
NameIdentifier schemaIdent = NameIdentifier.of(tableIdent.namespace().levels());
- Preconditions.checkArgument(
- Arrays.stream(partitions).allMatch(p -> p instanceof Transforms.NamedReference),
- "Hive partition only supports identity transform");
+ validatePartitionForCreate(columns, partitions);
validateDistributionAndSort(distribution, sortOrders);
Arrays.stream(columns).forEach(c -> validateColumnType(c.name(), c.dataType()));
@@ -549,6 +597,8 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
org.apache.hadoop.hive.metastore.api.Table alteredHiveTable =
table.toHiveTable(tablePropertiesMetadata);
+ validatePartitionForAlter(changes, alteredHiveTable);
+
for (TableChange change : changes) {
// Table change
if (change instanceof TableChange.RenameTable) {
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveTable.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveTable.java
index 5e11858ebf..85deb91a09 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveTable.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveTable.java
@@ -29,7 +29,6 @@ import com.datastrato.gravitino.rel.SortOrder.Direction;
import com.datastrato.gravitino.rel.transforms.Transform;
import com.datastrato.gravitino.rel.transforms.Transforms;
import com.datastrato.gravitino.rel.transforms.Transforms.NamedReference;
-import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.time.Instant;
@@ -39,6 +38,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import lombok.ToString;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
@@ -102,20 +102,31 @@ public class HiveTable extends BaseTable {
.toArray(SortOrder[]::new);
}
+ Column[] columns =
+ Stream.concat(
+ table.getSd().getCols().stream()
+ .map(
+ f ->
+ new HiveColumn.Builder()
+ .withName(f.getName())
+ .withType(FromHiveType.convert(f.getType()))
+ .withComment(f.getComment())
+ .build()),
+ table.getPartitionKeys().stream()
+ .map(
+ p ->
+ new HiveColumn.Builder()
+ .withName(p.getName())
+ .withType(FromHiveType.convert(p.getType()))
+ .withComment(p.getComment())
+ .build()))
+ .toArray(Column[]::new);
+
return new HiveTable.Builder()
.withName(table.getTableName())
.withComment(table.getParameters().get(COMMENT))
.withProperties(buildTableProperties(table))
- .withColumns(
- table.getSd().getCols().stream()
- .map(
- f ->
- new HiveColumn.Builder()
- .withName(f.getName())
- .withType(FromHiveType.convert(f.getType()))
- .withComment(f.getComment())
- .build())
- .toArray(HiveColumn[]::new))
+ .withColumns(columns)
.withDistribution(distribution)
.withSortOrders(sortOrders)
.withAuditInfo(auditInfoBuilder.build())
@@ -159,9 +170,10 @@ public class HiveTable extends BaseTable {
hiveTable.setDbName(schemaName);
hiveTable.setTableType(
((TableType) tablePropertiesMetadata.getOrDefault(properties(), TABLE_TYPE)).name());
- hiveTable.setSd(buildStorageDescriptor(tablePropertiesMetadata));
+ List<FieldSchema> partitionFields = buildPartitionKeys();
+ hiveTable.setSd(buildStorageDescriptor(tablePropertiesMetadata, partitionFields));
hiveTable.setParameters(buildTableParameters());
- hiveTable.setPartitionKeys(buildPartitionKeys());
+ hiveTable.setPartitionKeys(partitionFields);
// Set AuditInfo to Hive's Table object. Hive's Table doesn't support setting last modifier
// and last modified time, so we only set creator and create time.
@@ -197,14 +209,10 @@ public class HiveTable extends BaseTable {
}
private FieldSchema getPartitionKey(String[] fieldName) {
- Preconditions.checkArgument(
- fieldName.length == 1, "Hive partition does not support nested field");
List<Column> partitionColumns =
Arrays.stream(columns)
.filter(c -> c.name().equals(fieldName[0]))
.collect(Collectors.toList());
- Preconditions.checkArgument(
- partitionColumns.size() == 1, "Hive partition must match one column");
return new FieldSchema(
partitionColumns.get(0).name(),
partitionColumns.get(0).dataType().accept(ToHiveType.INSTANCE).getQualifiedName(),
@@ -212,10 +220,13 @@ public class HiveTable extends BaseTable {
}
private StorageDescriptor buildStorageDescriptor(
- HiveTablePropertiesMetadata tablePropertiesMetadata) {
+ HiveTablePropertiesMetadata tablePropertiesMetadata, List<FieldSchema> partitionFields) {
StorageDescriptor sd = new StorageDescriptor();
+ List<String> partitionKeys =
+ partitionFields.stream().map(FieldSchema::getName).collect(Collectors.toList());
sd.setCols(
Arrays.stream(columns)
+ .filter(c -> !partitionKeys.contains(c.name()))
.map(
c ->
new FieldSchema(
diff --git a/catalogs/catalog-hive/src/test/java/com/datastrato/gravitino/catalog/hive/TestHiveTable.java b/catalogs/catalog-hive/src/test/java/com/datastrato/gravitino/catalog/hive/TestHiveTable.java
index b7cd70f193..87b3cd4c10 100644
--- a/catalogs/catalog-hive/src/test/java/com/datastrato/gravitino/catalog/hive/TestHiveTable.java
+++ b/catalogs/catalog-hive/src/test/java/com/datastrato/gravitino/catalog/hive/TestHiveTable.java
@@ -28,7 +28,6 @@ import com.datastrato.gravitino.rel.transforms.Transform;
import com.datastrato.gravitino.rel.transforms.Transforms;
import com.google.common.collect.Maps;
import io.substrait.type.TypeCreator;
-import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Map;
@@ -258,7 +257,7 @@ public class TestHiveTable extends MiniHiveMetastoreService {
.build();
Column[] columns = new Column[] {col1, col2};
- Transform[] partitions = new Transform[] {identity(new String[] {col1.name()})};
+ Transform[] partitions = new Transform[] {identity(new String[] {col2.name()})};
Table table =
hiveCatalog
@@ -319,18 +318,19 @@ public class TestHiveTable extends MiniHiveMetastoreService {
exception =
Assertions.assertThrows(
- RuntimeException.class,
+ IllegalArgumentException.class,
() ->
hiveCatalog
.asTableCatalog()
.createTable(
- NameIdentifier.of(
- META_LAKE_NAME, hiveCatalog.name(), hiveSchema.name(), genRandomName()),
+ tableIdentifier,
columns,
HIVE_COMMENT,
properties,
- new Transform[] {identity(new String[] {"not_exist_field"})}));
- Assertions.assertTrue(exception.getMessage().contains("Hive partition must match one column"));
+ new Transform[] {identity(new String[] {col1.name()})}));
+ Assertions.assertEquals(
+ "The partition field must be placed at the end of the columns in order",
+ exception.getMessage());
}
@Test
@@ -381,7 +381,7 @@ public class TestHiveTable extends MiniHiveMetastoreService {
}
@Test
- public void testAlterHiveTable() throws IOException {
+ public void testAlterHiveTable() {
// create a table with random name
NameIdentifier tableIdentifier =
NameIdentifier.of(META_LAKE_NAME, hiveCatalog.name(), hiveSchema.name(), genRandomName());
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml
index 3c4f526048..c8d8b6dbe0 100644
--- a/gradle/libs.versions.toml
+++ b/gradle/libs.versions.toml
@@ -95,6 +95,7 @@ iceberg-spark-runtime = { group = "org.apache.iceberg", name = "iceberg-spark-ru
spark-sql = { group = "org.apache.spark", name = "spark-sql_2.13", version.ref = "spark" }
scala-collection-compat = { group = "org.scala-lang.modules", name = "scala-collection-compat_2.13", version.ref = "scala-collection-compat" }
sqlite-jdbc = { group = "org.xerial", name = "sqlite-jdbc", version.ref = "sqlite-jdbc" }
+spark-hive = { group = "org.apache.spark", name = "spark-hive_2.13", version.ref = "spark" }
[bundles]
diff --git a/integration-test/build.gradle.kts b/integration-test/build.gradle.kts
index f5b0c85b39..cef2a58069 100644
--- a/integration-test/build.gradle.kts
+++ b/integration-test/build.gradle.kts
@@ -110,6 +110,7 @@ dependencies {
testImplementation(libs.slf4j.jdk14)
testImplementation(libs.scala.collection.compat)
testImplementation(libs.sqlite.jdbc)
+ testImplementation(libs.spark.hive)
}
/* Optimizing integration test execution conditions */
@@ -199,7 +200,8 @@ tasks.test {
doFirst {
// Default use MiniGravitino to run integration tests
environment("GRAVITINO_ROOT_DIR", rootDir.path)
- environment("HADOOP_USER_NAME", "hive")
+ // TODO: use hive user instead after we fix the permission issue #554
+ environment("HADOOP_USER_NAME", "root")
environment("HADOOP_HOME", "/tmp")
environment("PROJECT_VERSION", version)
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
index 6f0aa21c95..4cfd3502c1 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
@@ -22,6 +22,10 @@ import static com.datastrato.gravitino.catalog.hive.HiveTablePropertiesMetadata.
import static com.datastrato.gravitino.catalog.hive.HiveTablePropertiesMetadata.TRANSIENT_LAST_DDL_TIME;
import static com.datastrato.gravitino.rel.transforms.Transforms.identity;
import static org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE;
+import static org.apache.hadoop.hive.serde.serdeConstants.DATE_TYPE_NAME;
+import static org.apache.hadoop.hive.serde.serdeConstants.INT_TYPE_NAME;
+import static org.apache.hadoop.hive.serde.serdeConstants.STRING_TYPE_NAME;
+import static org.apache.hadoop.hive.serde.serdeConstants.TINYINT_TYPE_NAME;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.datastrato.gravitino.Catalog;
@@ -47,6 +51,7 @@ import com.datastrato.gravitino.rel.transforms.Transforms.NamedReference;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import io.substrait.type.TypeCreator;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -57,6 +62,7 @@ import org.apache.hadoop.hive.metastore.TableType;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
+import org.apache.spark.sql.SparkSession;
import org.apache.thrift.TException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
@@ -79,20 +85,42 @@ public class CatalogHiveIT extends AbstractIT {
public static String HIVE_COL_NAME1 = "hive_col_name1";
public static String HIVE_COL_NAME2 = "hive_col_name2";
public static String HIVE_COL_NAME3 = "hive_col_name3";
- static String HIVE_METASTORE_URIS = "thrift://localhost:9083";
+ private static String HIVE_METASTORE_URIS = "thrift://localhost:9083";
private static final String provider = "hive";
-
private static HiveClientPool hiveClientPool;
-
private static GravitinoMetaLake metalake;
-
private static Catalog catalog;
+ private static SparkSession sparkSession;
+ private static String SELECT_ALL_TEMPLATE = "SELECT * FROM %s.%s";
+ private static String INSERT_WITHOUT_PARTITION_TEMPLATE = "INSERT INTO %s.%s VALUES (%s)";
+ private static String INSERT_WITH_PARTITION_TEMPLATE =
+ "INSERT INTO %s.%s PARTITION (%s) VALUES (%s)";
+
+ private static Map<String, String> typeConstant =
+ ImmutableMap.of(
+ TINYINT_TYPE_NAME,
+ "1",
+ INT_TYPE_NAME,
+ "2",
+ DATE_TYPE_NAME,
+ "'2023-01-01'",
+ STRING_TYPE_NAME,
+ "'gravitino_it_test'");
@BeforeAll
public static void startup() throws Exception {
HiveConf hiveConf = GravitinoITUtils.hiveConfig();
hiveClientPool = new HiveClientPool(1, hiveConf);
+ sparkSession =
+ SparkSession.builder()
+ .master("local[1]")
+ .appName("Hive Catalog integration test")
+ .config("hive.metastore.uris", HIVE_METASTORE_URIS)
+ .config("spark.sql.storeAssignmentPolicy", "LEGACY")
+ .config("mapreduce.input.fileinputformat.input.dir.recursive", "true")
+ .enableHiveSupport()
+ .getOrCreate();
createMetalake();
createCatalog();
createSchema();
@@ -104,6 +132,10 @@ public class CatalogHiveIT extends AbstractIT {
if (hiveClientPool != null) {
hiveClientPool.close();
}
+
+ if (sparkSession != null) {
+ sparkSession.close();
+ }
}
@AfterEach
@@ -138,16 +170,14 @@ public class CatalogHiveIT extends AbstractIT {
CATALOG_BYPASS_PREFIX + HiveConf.ConfVars.METASTORE_CLIENT_CONNECT_RETRY_DELAY.varname,
"5");
- Catalog createdCatalog =
- metalake.createCatalog(
- NameIdentifier.of(metalakeName, catalogName),
- Catalog.Type.RELATIONAL,
- provider,
- "comment",
- properties);
- Catalog loadCatalog = metalake.loadCatalog(NameIdentifier.of(metalakeName, catalogName));
+ metalake.createCatalog(
+ NameIdentifier.of(metalakeName, catalogName),
+ Catalog.Type.RELATIONAL,
+ provider,
+ "comment",
+ properties);
- catalog = loadCatalog;
+ catalog = metalake.loadCatalog(NameIdentifier.of(metalakeName, catalogName));
}
private static void createSchema() throws TException, InterruptedException {
@@ -191,6 +221,30 @@ public class CatalogHiveIT extends AbstractIT {
return new ColumnDTO[] {col1, col2, col3};
}
+ private void checkTableReadWrite(org.apache.hadoop.hive.metastore.api.Table table) {
+ String dbName = table.getDbName();
+ String tableName = table.getTableName();
+ long count = sparkSession.sql(String.format(SELECT_ALL_TEMPLATE, dbName, tableName)).count();
+ String values =
+ table.getSd().getCols().stream()
+ .map(f -> typeConstant.get(f.getType()))
+ .map(Object::toString)
+ .collect(Collectors.joining(","));
+ if (table.getPartitionKeys().isEmpty()) {
+ sparkSession.sql(String.format(INSERT_WITHOUT_PARTITION_TEMPLATE, dbName, tableName, values));
+ } else {
+ String partitionExpressions =
+ table.getPartitionKeys().stream()
+ .map(f -> f.getName() + "=" + typeConstant.get(f.getType()))
+ .collect(Collectors.joining(","));
+ sparkSession.sql(
+ String.format(
+ INSERT_WITH_PARTITION_TEMPLATE, dbName, tableName, partitionExpressions, values));
+ }
+ Assertions.assertEquals(
+ count + 1, sparkSession.sql(String.format(SELECT_ALL_TEMPLATE, dbName, tableName)).count());
+ }
+
private Map<String, String> createProperties() {
Map<String, String> properties = Maps.newHashMap();
properties.put("key1", "val1");
@@ -199,7 +253,8 @@ public class CatalogHiveIT extends AbstractIT {
}
@Test
- void testCreateHiveTableWithDistributionAndSortOrder() throws TException, InterruptedException {
+ public void testCreateHiveTableWithDistributionAndSortOrder()
+ throws TException, InterruptedException {
// Create table from Gravitino API
ColumnDTO[] columns = createColumns();
@@ -242,6 +297,7 @@ public class CatalogHiveIT extends AbstractIT {
.forEach(
key -> Assertions.assertEquals(properties.get(key), hiveTab.getParameters().get(key)));
assertTableEquals(createdTable, hiveTab);
+ checkTableReadWrite(hiveTab);
// test null partition
resetSchema();
@@ -259,6 +315,7 @@ public class CatalogHiveIT extends AbstractIT {
key ->
Assertions.assertEquals(properties.get(key), hiveTable1.getParameters().get(key)));
assertTableEquals(createdTable1, hiveTable1);
+ checkTableReadWrite(hiveTable1);
// Test bad request
// Bad name in distribution
@@ -330,6 +387,7 @@ public class CatalogHiveIT extends AbstractIT {
.forEach(
key -> Assertions.assertEquals(properties.get(key), hiveTab.getParameters().get(key)));
assertTableEquals(createdTable, hiveTab);
+ checkTableReadWrite(hiveTab);
// test null partition
resetSchema();
@@ -347,6 +405,7 @@ public class CatalogHiveIT extends AbstractIT {
key ->
Assertions.assertEquals(properties.get(key), hiveTable1.getParameters().get(key)));
assertTableEquals(createdTable1, hiveTable1);
+ checkTableReadWrite(hiveTable1);
}
@Test
@@ -364,6 +423,7 @@ public class CatalogHiveIT extends AbstractIT {
org.apache.hadoop.hive.metastore.api.Table actualTable =
hiveClientPool.run(client -> client.getTable(schemaName, tableName));
assertDefaultTableProperties(createdTable, actualTable);
+ checkTableReadWrite(actualTable);
// test set properties
String table2 = GravitinoITUtils.genRandomName("CatalogHiveIT_table");
@@ -401,6 +461,7 @@ public class CatalogHiveIT extends AbstractIT {
Assertions.assertNotNull(createdTable2.properties().get(TRANSIENT_LAST_DDL_TIME));
Assertions.assertNotNull(createdTable2.properties().get(NUM_FILES));
Assertions.assertNotNull(createdTable2.properties().get(TOTAL_SIZE));
+ checkTableReadWrite(actualTable2);
// test alter properties exception
IllegalArgumentException exception =
@@ -432,7 +493,7 @@ public class CatalogHiveIT extends AbstractIT {
columns,
table_comment,
properties,
- new Transform[] {identity(columns[0]), identity(columns[1])});
+ new Transform[] {identity(columns[1]), identity(columns[2])});
// Directly get table from hive metastore to check if the table is created successfully.
org.apache.hadoop.hive.metastore.api.Table hiveTab =
@@ -442,28 +503,52 @@ public class CatalogHiveIT extends AbstractIT {
.forEach(
key -> Assertions.assertEquals(properties.get(key), hiveTab.getParameters().get(key)));
assertTableEquals(createdTable, hiveTab);
+ checkTableReadWrite(hiveTab);
+
+ // test exception
+ RuntimeException exception =
+ assertThrows(
+ RuntimeException.class,
+ () -> {
+ catalog
+ .asTableCatalog()
+ .createTable(
+ nameIdentifier,
+ columns,
+ table_comment,
+ properties,
+ new Transform[] {identity(columns[0]), identity(columns[1])});
+ });
+ Assertions.assertTrue(
+ exception
+ .getMessage()
+ .contains("The partition field must be placed at the end of the columns in order"));
}
private void assertTableEquals(
Table createdTable, org.apache.hadoop.hive.metastore.api.Table hiveTab) {
Distribution distribution = createdTable.distribution();
SortOrder[] sortOrders = createdTable.sortOrder();
+
+ List<FieldSchema> actualColumns = new ArrayList<>();
+ actualColumns.addAll(hiveTab.getSd().getCols());
+ actualColumns.addAll(hiveTab.getPartitionKeys());
Assertions.assertEquals(schemaName.toLowerCase(), hiveTab.getDbName());
Assertions.assertEquals(tableName.toLowerCase(), hiveTab.getTableName());
Assertions.assertEquals("MANAGED_TABLE", hiveTab.getTableType());
Assertions.assertEquals(table_comment, hiveTab.getParameters().get("comment"));
- Assertions.assertEquals(HIVE_COL_NAME1, hiveTab.getSd().getCols().get(0).getName());
- Assertions.assertEquals("tinyint", hiveTab.getSd().getCols().get(0).getType());
- Assertions.assertEquals("col_1_comment", hiveTab.getSd().getCols().get(0).getComment());
+ Assertions.assertEquals(HIVE_COL_NAME1, actualColumns.get(0).getName());
+ Assertions.assertEquals("tinyint", actualColumns.get(0).getType());
+ Assertions.assertEquals("col_1_comment", actualColumns.get(0).getComment());
- Assertions.assertEquals(HIVE_COL_NAME2, hiveTab.getSd().getCols().get(1).getName());
- Assertions.assertEquals("date", hiveTab.getSd().getCols().get(1).getType());
- Assertions.assertEquals("col_2_comment", hiveTab.getSd().getCols().get(1).getComment());
+ Assertions.assertEquals(HIVE_COL_NAME2, actualColumns.get(1).getName());
+ Assertions.assertEquals("date", actualColumns.get(1).getType());
+ Assertions.assertEquals("col_2_comment", actualColumns.get(1).getComment());
- Assertions.assertEquals(HIVE_COL_NAME3, hiveTab.getSd().getCols().get(2).getName());
- Assertions.assertEquals("string", hiveTab.getSd().getCols().get(2).getType());
- Assertions.assertEquals("col_3_comment", hiveTab.getSd().getCols().get(2).getComment());
+ Assertions.assertEquals(HIVE_COL_NAME3, actualColumns.get(2).getName());
+ Assertions.assertEquals("string", actualColumns.get(2).getType());
+ Assertions.assertEquals("col_3_comment", actualColumns.get(2).getComment());
Assertions.assertEquals(
distribution == null ? 0 : distribution.number(), hiveTab.getSd().getNumBuckets());
@@ -505,7 +590,7 @@ public class CatalogHiveIT extends AbstractIT {
columns,
table_comment,
createProperties(),
- new Transform[] {identity(columns[0])});
+ new Transform[] {identity(columns[2])});
Table alteredTable =
catalog
.asTableCatalog()
@@ -536,17 +621,28 @@ public class CatalogHiveIT extends AbstractIT {
Assertions.assertEquals("date", hiveTab.getSd().getCols().get(1).getType());
Assertions.assertEquals("col_2_comment", hiveTab.getSd().getCols().get(1).getComment());
- Assertions.assertEquals(HIVE_COL_NAME3, hiveTab.getSd().getCols().get(2).getName());
+ Assertions.assertEquals("col_4", hiveTab.getSd().getCols().get(2).getName());
Assertions.assertEquals("string", hiveTab.getSd().getCols().get(2).getType());
- Assertions.assertEquals("col_3_comment", hiveTab.getSd().getCols().get(2).getComment());
-
- Assertions.assertEquals("col_4", hiveTab.getSd().getCols().get(3).getName());
- Assertions.assertEquals("string", hiveTab.getSd().getCols().get(3).getType());
- Assertions.assertNull(hiveTab.getSd().getCols().get(3).getComment());
+ Assertions.assertNull(hiveTab.getSd().getCols().get(2).getComment());
Assertions.assertEquals(1, hiveTab.getPartitionKeys().size());
- Assertions.assertEquals(columns[0].name(), hiveTab.getPartitionKeys().get(0).getName());
+ Assertions.assertEquals(columns[2].name(), hiveTab.getPartitionKeys().get(0).getName());
assertDefaultTableProperties(alteredTable, hiveTab);
+ checkTableReadWrite(hiveTab);
+
+ // test alter partition column exception
+ RuntimeException exception =
+ assertThrows(
+ RuntimeException.class,
+ () -> {
+ catalog
+ .asTableCatalog()
+ .alterTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, alertTableName),
+ TableChange.updateColumnType(
+ new String[] {HIVE_COL_NAME3}, TypeCreator.NULLABLE.I32));
+ });
+ Assertions.assertTrue(exception.getMessage().contains("Cannot alter partition column"));
// test updateColumnPosition exception
ColumnDTO col1 =
@@ -585,7 +681,7 @@ public class CatalogHiveIT extends AbstractIT {
Distribution.NONE,
new SortOrder[0]);
- IllegalArgumentException exception =
+ exception =
assertThrows(
IllegalArgumentException.class,
() ->
@@ -646,15 +742,9 @@ public class CatalogHiveIT extends AbstractIT {
() -> hiveClientPool.run(client -> client.getTable(schemaName, alertTableName)));
}
- // TODO (xun) enable this test waiting for fixed [#316] [Bug report] alterSchema throw
- // NoSuchSchemaException
- // @Test
+ @Test
public void testAlterSchema() throws TException, InterruptedException {
NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName, schemaName);
- Map<String, String> properties = Maps.newHashMap();
- properties.put("key1", "val1");
- properties.put("key2", "val2");
- String comment = "comment";
GravitinoMetaLake metalake = client.loadMetalake(NameIdentifier.of(metalakeName));
Catalog catalog = metalake.loadCatalog(NameIdentifier.of(metalakeName, catalogName));
@@ -665,8 +755,6 @@ public class CatalogHiveIT extends AbstractIT {
SchemaChange.removeProperty("key1"),
SchemaChange.setProperty("key2", "val2-alter"));
- NameIdentifier[] nameIdentifiers = catalog.asSchemas().listSchemas(ident.namespace());
-
Map<String, String> properties2 = catalog.asSchemas().loadSchema(ident).properties();
Assertions.assertFalse(properties2.containsKey("key1"));
Assertions.assertEquals("val2-alter", properties2.get("key2"));