FANNG1 commented on code in PR #9689:
URL: https://github.com/apache/gravitino/pull/9689#discussion_r2752432884
##########
flink-connector/flink/src/main/java/org/apache/gravitino/flink/connector/hive/GravitinoHiveCatalog.java:
##########
@@ -67,4 +92,155 @@ public Optional<Factory> getFactory() {
protected AbstractCatalog realCatalog() {
return hiveCatalog;
}
+
+ @Override
+ public void createTable(ObjectPath tablePath, CatalogBaseTable table,
boolean ignoreIfExists)
+ throws TableAlreadyExistException, DatabaseNotExistException,
CatalogException {
+ Preconditions.checkArgument(
+ table instanceof ResolvedCatalogBaseTable, "table should be resolved");
+
+ if (!FlinkGenericTableUtil.isGenericTableWhenCreate(table.getOptions())) {
+ super.createTable(tablePath, table, ignoreIfExists);
+ return;
+ }
+
+ if (!(table instanceof ResolvedCatalogTable)) {
+ throw new CatalogException("Generic table must be a resolved catalog
table");
+ }
+ ResolvedCatalogTable resolvedTable = (ResolvedCatalogTable) table;
+
+ NameIdentifier identifier =
+ NameIdentifier.of(tablePath.getDatabaseName(),
tablePath.getObjectName());
+ Map<String, String> properties =
+ FlinkGenericTableUtil.toGravitinoGenericTableProperties(resolvedTable);
+
+ try {
+ catalog()
+ .asTableCatalog()
+ .createTable(
+ identifier,
+ new Column[0],
+ table.getComment(),
+ properties,
+ new Transform[0],
+ Distributions.NONE,
+ new SortOrder[0],
+ new Index[0]);
+ } catch (NoSuchSchemaException e) {
+ throw new DatabaseNotExistException(catalogName(),
tablePath.getDatabaseName(), e);
+ } catch (TableAlreadyExistsException e) {
+ if (!ignoreIfExists) {
+ throw new TableAlreadyExistException(catalogName(), tablePath, e);
+ }
+ } catch (Exception e) {
+ throw new CatalogException(e);
+ }
+ }
+
+ @Override
+ public CatalogBaseTable getTable(ObjectPath tablePath)
+ throws TableNotExistException, CatalogException {
+ try {
+ Table table =
+ catalog()
+ .asTableCatalog()
+ .loadTable(NameIdentifier.of(tablePath.getDatabaseName(),
tablePath.getObjectName()));
+ if (FlinkGenericTableUtil.isGenericTableWhenLoad(table.properties())) {
+ return FlinkGenericTableUtil.toFlinkGenericTable(table);
+ }
+ return super.toFlinkTable(table, tablePath);
+ } catch (NoSuchTableException e) {
+ throw new TableNotExistException(catalogName(), tablePath, e);
+ } catch (Exception e) {
+ throw new CatalogException(e);
+ }
+ }
+
+ @Override
+ public void alterTable(ObjectPath tablePath, CatalogBaseTable newTable,
boolean ignoreIfNotExists)
+ throws TableNotExistException, CatalogException {
+ Table table = loadGravitinoTable(tablePath, ignoreIfNotExists);
+ if (table == null) {
+ return;
+ }
+ if (!FlinkGenericTableUtil.isGenericTableWhenLoad(table.properties())) {
+ super.alterTable(tablePath, newTable, ignoreIfNotExists);
+ return;
+ }
+ if (!(newTable instanceof ResolvedCatalogTable)) {
+ throw new CatalogException("Generic table must be a resolved catalog
table");
+ }
+ applyGenericTableAlter(tablePath, table, (ResolvedCatalogTable) newTable);
+ }
+
+ @Override
+ public void alterTable(
+ ObjectPath tablePath,
+ CatalogBaseTable newTable,
+ java.util.List<org.apache.flink.table.catalog.TableChange> tableChanges,
+ boolean ignoreIfNotExists)
+ throws TableNotExistException, CatalogException {
+ Table table = loadGravitinoTable(tablePath, ignoreIfNotExists);
+ if (table == null) {
+ return;
+ }
+ if (!FlinkGenericTableUtil.isGenericTableWhenLoad(table.properties())) {
+ super.alterTable(tablePath, newTable, tableChanges, ignoreIfNotExists);
+ return;
+ }
+ if (!(newTable instanceof ResolvedCatalogTable)) {
+ throw new CatalogException("Generic table must be a resolved catalog
table");
+ }
Review Comment:
updated
##########
flink-connector/flink/src/main/java/org/apache/gravitino/flink/connector/hive/GravitinoHiveCatalog.java:
##########
@@ -67,4 +92,155 @@ public Optional<Factory> getFactory() {
protected AbstractCatalog realCatalog() {
return hiveCatalog;
}
+
+ @Override
+ public void createTable(ObjectPath tablePath, CatalogBaseTable table,
boolean ignoreIfExists)
+ throws TableAlreadyExistException, DatabaseNotExistException,
CatalogException {
+ Preconditions.checkArgument(
+ table instanceof ResolvedCatalogBaseTable, "table should be resolved");
+
+ if (!FlinkGenericTableUtil.isGenericTableWhenCreate(table.getOptions())) {
+ super.createTable(tablePath, table, ignoreIfExists);
+ return;
+ }
+
+ if (!(table instanceof ResolvedCatalogTable)) {
+ throw new CatalogException("Generic table must be a resolved catalog
table");
+ }
+ ResolvedCatalogTable resolvedTable = (ResolvedCatalogTable) table;
+
+ NameIdentifier identifier =
+ NameIdentifier.of(tablePath.getDatabaseName(),
tablePath.getObjectName());
+ Map<String, String> properties =
+ FlinkGenericTableUtil.toGravitinoGenericTableProperties(resolvedTable);
+
+ try {
+ catalog()
+ .asTableCatalog()
+ .createTable(
+ identifier,
+ new Column[0],
+ table.getComment(),
+ properties,
+ new Transform[0],
+ Distributions.NONE,
+ new SortOrder[0],
+ new Index[0]);
+ } catch (NoSuchSchemaException e) {
+ throw new DatabaseNotExistException(catalogName(),
tablePath.getDatabaseName(), e);
+ } catch (TableAlreadyExistsException e) {
+ if (!ignoreIfExists) {
+ throw new TableAlreadyExistException(catalogName(), tablePath, e);
+ }
+ } catch (Exception e) {
+ throw new CatalogException(e);
+ }
+ }
+
+ @Override
+ public CatalogBaseTable getTable(ObjectPath tablePath)
+ throws TableNotExistException, CatalogException {
+ try {
+ Table table =
+ catalog()
+ .asTableCatalog()
+ .loadTable(NameIdentifier.of(tablePath.getDatabaseName(),
tablePath.getObjectName()));
+ if (FlinkGenericTableUtil.isGenericTableWhenLoad(table.properties())) {
+ return FlinkGenericTableUtil.toFlinkGenericTable(table);
+ }
+ return super.toFlinkTable(table, tablePath);
+ } catch (NoSuchTableException e) {
+ throw new TableNotExistException(catalogName(), tablePath, e);
+ } catch (Exception e) {
+ throw new CatalogException(e);
+ }
+ }
+
+ @Override
+ public void alterTable(ObjectPath tablePath, CatalogBaseTable newTable,
boolean ignoreIfNotExists)
+ throws TableNotExistException, CatalogException {
+ Table table = loadGravitinoTable(tablePath, ignoreIfNotExists);
+ if (table == null) {
+ return;
+ }
+ if (!FlinkGenericTableUtil.isGenericTableWhenLoad(table.properties())) {
+ super.alterTable(tablePath, newTable, ignoreIfNotExists);
+ return;
+ }
+ if (!(newTable instanceof ResolvedCatalogTable)) {
+ throw new CatalogException("Generic table must be a resolved catalog
table");
+ }
+ applyGenericTableAlter(tablePath, table, (ResolvedCatalogTable) newTable);
+ }
+
+ @Override
+ public void alterTable(
+ ObjectPath tablePath,
+ CatalogBaseTable newTable,
+ java.util.List<org.apache.flink.table.catalog.TableChange> tableChanges,
+ boolean ignoreIfNotExists)
+ throws TableNotExistException, CatalogException {
+ Table table = loadGravitinoTable(tablePath, ignoreIfNotExists);
+ if (table == null) {
+ return;
+ }
+ if (!FlinkGenericTableUtil.isGenericTableWhenLoad(table.properties())) {
+ super.alterTable(tablePath, newTable, tableChanges, ignoreIfNotExists);
+ return;
+ }
+ if (!(newTable instanceof ResolvedCatalogTable)) {
+ throw new CatalogException("Generic table must be a resolved catalog
table");
+ }
+ applyGenericTableAlter(tablePath, table, (ResolvedCatalogTable) newTable);
+ }
+
+ private Table loadGravitinoTable(ObjectPath tablePath, boolean
ignoreIfNotExists)
+ throws TableNotExistException, CatalogException {
+ try {
+ return catalog()
+ .asTableCatalog()
+ .loadTable(NameIdentifier.of(tablePath.getDatabaseName(),
tablePath.getObjectName()));
+ } catch (NoSuchTableException e) {
+ if (!ignoreIfNotExists) {
+ throw new TableNotExistException(catalogName(), tablePath, e);
+ }
+ return null;
+ } catch (Exception e) {
+ throw new CatalogException(e);
+ }
+ }
+
+ private void applyGenericTableAlter(
+ ObjectPath tablePath, Table existingTable, ResolvedCatalogTable newTable)
+ throws CatalogException {
+ NameIdentifier identifier =
+ NameIdentifier.of(tablePath.getDatabaseName(),
tablePath.getObjectName());
+ Map<String, String> updatedProperties =
+ FlinkGenericTableUtil.toGravitinoGenericTableProperties(newTable);
+ Map<String, String> currentProperties =
+ existingTable.properties() == null ? Collections.emptyMap() :
existingTable.properties();
+
+ List<TableChange> changes = new ArrayList<>();
+ if (!Objects.equals(existingTable.comment(), newTable.getComment())) {
+ changes.add(TableChange.updateComment(newTable.getComment()));
+ }
+
+ currentProperties.keySet().stream()
+ .filter(
+ key ->
+ (key.startsWith(CatalogPropertiesUtil.FLINK_PROPERTY_PREFIX)
+ || CatalogPropertiesUtil.IS_GENERIC.equals(key))
+ && !updatedProperties.containsKey(key))
+ .forEach(key -> changes.add(TableChange.removeProperty(key)));
+
+ updatedProperties.forEach(
+ (key, value) -> {
+ String currentValue = currentProperties.get(key);
+ if (!value.equals(currentValue)) {
+ changes.add(TableChange.setProperty(key, value));
+ }
+ });
+
+ catalog().asTableCatalog().alterTable(identifier, changes.toArray(new
TableChange[0]));
Review Comment:
updated
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]