Skip to content

Commit

Permalink
[#1557] feat(hive): Handle column default value in Hive catalog (#1592)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

throw an exception, when the Hive catalog detects default values
specified during table creation

### Why are the changes needed?

Fix: #1557 

### Does this PR introduce _any_ user-facing change?

no

### How was this patch tested?

UT added
  • Loading branch information
mchades authored Jan 19, 2024
1 parent d40f072 commit 5bcd9c0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.datastrato.gravitino.rel.Table;
import com.datastrato.gravitino.rel.TableCatalog;
import com.datastrato.gravitino.rel.TableChange;
import com.datastrato.gravitino.rel.expressions.Expression;
import com.datastrato.gravitino.rel.expressions.NamedReference;
import com.datastrato.gravitino.rel.expressions.distributions.Distribution;
import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
Expand Down Expand Up @@ -568,7 +569,12 @@ public Table createTable(
validatePartitionForCreate(columns, partitioning);
validateDistributionAndSort(distribution, sortOrders);

Arrays.stream(columns).forEach(c -> validateNullable(c.name(), c.nullable()));
Arrays.stream(columns)
.forEach(
c -> {
validateNullable(c.name(), c.nullable());
validateColumnDefaultValue(c.name(), c.defaultValue());
});

TableType tableType = (TableType) tablePropertiesMetadata.getOrDefault(properties, TABLE_TYPE);
Preconditions.checkArgument(
Expand Down Expand Up @@ -723,6 +729,17 @@ public Table alterTable(NameIdentifier tableIdent, TableChange... changes)
}
}

private void validateColumnDefaultValue(String fieldName, Expression defaultValue) {
// The DEFAULT constraint for column is supported since Hive3.0, see
// https://issues.apache.org/jira/browse/HIVE-18726
if (!defaultValue.equals(Column.DEFAULT_VALUE_NOT_SET)) {
throw new IllegalArgumentException(
"The DEFAULT constraint for column is only supported since Hive 3.0, "
+ "but the current Gravitino Hive catalog only supports Hive 2.x. Illegal column: "
+ fieldName);
}
}

private void validateNullable(String fieldName, boolean nullable) {
// The NOT NULL constraint for column is supported since Hive3.0, see
// https://issues.apache.org/jira/browse/HIVE-16575
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected HiveColumn internalBuild() {
hiveColumn.comment = comment;
hiveColumn.dataType = dataType;
hiveColumn.nullable = nullable;
hiveColumn.defaultValue = defaultValue == null ? DEFAULT_VALUE_NOT_SET : defaultValue;
return hiveColumn;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
import com.datastrato.gravitino.rel.expressions.NamedReference;
import com.datastrato.gravitino.rel.expressions.distributions.Distribution;
import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
import com.datastrato.gravitino.rel.expressions.literals.Literals;
import com.datastrato.gravitino.rel.expressions.sorts.NullOrdering;
import com.datastrato.gravitino.rel.expressions.sorts.SortDirection;
import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
import com.datastrato.gravitino.rel.expressions.sorts.SortOrders;
import com.datastrato.gravitino.rel.expressions.transforms.Transform;
import com.datastrato.gravitino.rel.expressions.transforms.Transforms;
import com.datastrato.gravitino.rel.types.Types;
import com.google.common.collect.Maps;
import java.time.Instant;
Expand Down Expand Up @@ -229,6 +231,36 @@ public void testCreateHiveTable() {
.contains(
"The NOT NULL constraint for column is only supported since Hive 3.0, "
+ "but the current Gravitino Hive catalog only supports Hive 2.x"));

HiveColumn withDefault =
new HiveColumn.Builder()
.withName("col_3")
.withType(Types.ByteType.get())
.withComment(HIVE_COMMENT)
.withNullable(true)
.withDefaultValue(Literals.NULL)
.build();
exception =
Assertions.assertThrows(
IllegalArgumentException.class,
() ->
hiveCatalog
.asTableCatalog()
.createTable(
tableIdentifier,
new Column[] {withDefault},
HIVE_COMMENT,
properties,
Transforms.EMPTY_TRANSFORM,
distribution,
sortOrders));
Assertions.assertTrue(
exception
.getMessage()
.contains(
"The DEFAULT constraint for column is only supported since Hive 3.0, "
+ "but the current Gravitino Hive catalog only supports Hive 2.x"),
"The exception message is: " + exception.getMessage());
}

@Test
Expand Down

0 comments on commit 5bcd9c0

Please sign in to comment.