World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#10034] fix(core): Fix loading table problem due to incorrect SQL sentence in fetch column infos (#10062)
Qi Yu13b3409c700e
diff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex 0c6a97e27e..cd6ca287dc 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -111,7 +111,7 @@ jobs:strategy:matrix:java-version: [ 17 ]- timeout-minutes: 60+ timeout-minutes: 90needs: changesif: needs.changes.outputs.source_changes == 'true'# Steps represent a sequence of tasks that will be executed as part of the jobdiff --git a/core/src/main/java/org/apache/gravitino/catalog/TableOperationDispatcher.java b/core/src/main/java/org/apache/gravitino/catalog/TableOperationDispatcher.javaindex 7446135834..57499b6f6d 100644--- a/core/src/main/java/org/apache/gravitino/catalog/TableOperationDispatcher.java+++ b/core/src/main/java/org/apache/gravitino/catalog/TableOperationDispatcher.java@@ -671,6 +671,48 @@ public class TableOperationDispatcher extends OperationDispatcher implements Tab&& Objects.equal(left.defaultValue(), right.defaultValue());}+ private String columnDifferenceContent(+ Column catalogColumn, int catalogPosition, ColumnEntity entityColumn) {+ List<String> differences = Lists.newArrayList();+ if (!Objects.equal(catalogColumn.name(), entityColumn.name())) {+ differences.add(+ String.format("name[catalog=%s, store=%s]", catalogColumn.name(), entityColumn.name()));+ }+ if (catalogPosition != entityColumn.position()) {+ differences.add(+ String.format(+ "position[catalog=%s, store=%s]", catalogPosition, entityColumn.position()));+ }+ if (!Objects.equal(catalogColumn.dataType(), entityColumn.dataType())) {+ differences.add(+ String.format(+ "type[catalog=%s, store=%s]", catalogColumn.dataType(), entityColumn.dataType()));+ }+ if (!Objects.equal(catalogColumn.comment(), entityColumn.comment())) {+ differences.add(+ String.format(+ "comment[catalog=%s, store=%s]", catalogColumn.comment(), entityColumn.comment()));+ }+ if (catalogColumn.nullable() != entityColumn.nullable()) {+ differences.add(+ String.format(+ "nullable[catalog=%s, store=%s]", catalogColumn.nullable(), entityColumn.nullable()));+ }+ if (catalogColumn.autoIncrement() != entityColumn.autoIncrement()) {+ differences.add(+ String.format(+ "autoIncrement[catalog=%s, store=%s]",+ catalogColumn.autoIncrement(), entityColumn.autoIncrement()));+ }+ if (!Objects.equal(catalogColumn.defaultValue(), entityColumn.defaultValue())) {+ differences.add(+ String.format(+ "defaultValue[catalog=%s, store=%s]",+ catalogColumn.defaultValue(), entityColumn.defaultValue()));+ }+ return String.join(", ", differences);+ }+private Pair<Boolean, List<ColumnEntity>> updateColumnsIfNecessary(Table tableFromCatalog, TableEntity tableFromGravitino) {if (tableFromCatalog == null || tableFromGravitino == null) {@@ -705,11 +747,12 @@ public class TableOperationDispatcher extends OperationDispatcher implements TabcolumnsNeedsUpdate = true;} else if (!isSameColumn(columnPair.getRight(), columnPair.getLeft(), entry.getValue())) {- // If the column need to be updated, we create a new ColumnEntity with the same id+ // Add debug log to print the difference between the two columnsLOG.debug("Column {} is found in the table from underlying source, but it is different "- + "from the one in the table entity, it will be updated",- entry.getKey());+ + "from the one in the table entity, it will be updated, differences: {}",+ entry.getKey(),+ columnDifferenceContent(columnPair.getRight(), columnPair.getLeft(), entry.getValue()));Column column = columnPair.getRight();ColumnEntity updatedColumnEntity =@@ -744,9 +787,10 @@ public class TableOperationDispatcher extends OperationDispatcher implements Tabfor (Map.Entry<String, Pair<Integer, Column>> entry : columnsFromCatalogTable.entrySet()) {if (!columnsFromTableEntity.containsKey(entry.getKey())) {LOG.debug(- "Column {} is found in the table from underlying source but not in the table "+ "Column {} of table: {} is found in the table from underlying source but not in the table "+ "entity, it will be added to the table entity",- entry.getKey());+ entry.getKey(),+ tableFromGravitino.id());ColumnEntity newColumnEntity =ColumnEntity.toColumnEntity(entry.getValue().getRight(),diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/TableColumnBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/TableColumnBaseSQLProvider.javaindex f42cee072f..98ac81ecbf 100644--- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/TableColumnBaseSQLProvider.java+++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/TableColumnBaseSQLProvider.java@@ -123,7 +123,10 @@ public class TableColumnBaseSQLProvider {+ " FROM "+ TableColumnMapper.COLUMN_TABLE_NAME+ " WHERE table_id = #{tableId} AND column_name = #{columnName} AND deleted_at = 0"- + " ORDER BY table_version DESC LIMIT 1";+ // Update a column will generate two records with the same version, one with op_type = 2+ // (update) and another with op_type = 3 (delete). We should not return NULL if both records+ // exist with the same version, otherwise the caller will think the column does not exist.+ + " ORDER BY table_version DESC, column_op_type ASC, id DESC LIMIT 1";}public String selectColumnPOById(@Param("columnId") Long columnId) {diff --git a/core/src/test/java/org/apache/gravitino/storage/relational/service/TestTableColumnMetaService.java b/core/src/test/java/org/apache/gravitino/storage/relational/service/TestTableColumnMetaService.javaindex 528db047e5..057a12ac16 100644--- a/core/src/test/java/org/apache/gravitino/storage/relational/service/TestTableColumnMetaService.java+++ b/core/src/test/java/org/apache/gravitino/storage/relational/service/TestTableColumnMetaService.java@@ -529,6 +529,70 @@ public class TestTableColumnMetaService extends TestJDBCBackend {() -> TableColumnMetaService.getInstance().getColumnPOById(updatedColumn.id()));}+ @TestTemplate+ public void testGetColumnIdByNamePrefersNonDeletedRecordInSameVersion() throws IOException {+ String catalogName = "catalog1";+ String schemaName = "schema1";+ createParentEntities(METALAKE_NAME, catalogName, schemaName, AUDIT_INFO);++ ColumnEntity oldColumn =+ ColumnEntity.builder()+ .withId(RandomIdGenerator.INSTANCE.nextId())+ .withName("column1")+ .withPosition(0)+ .withComment("comment1")+ .withDataType(Types.IntegerType.get())+ .withNullable(true)+ .withAutoIncrement(false)+ .withDefaultValue(Literals.integerLiteral(1))+ .withAuditInfo(AUDIT_INFO)+ .build();++ TableEntity createdTable =+ TableEntity.builder()+ .withId(RandomIdGenerator.INSTANCE.nextId())+ .withName("table_same_name")+ .withNamespace(Namespace.of(METALAKE_NAME, catalogName, schemaName))+ .withColumns(Lists.newArrayList(oldColumn))+ .withAuditInfo(AUDIT_INFO)+ .build();+ TableMetaService.getInstance().insertTable(createdTable, false);++ TableEntity retrievedTable =+ TableMetaService.getInstance().getTableByIdentifier(createdTable.nameIdentifier());++ // Replace the column with a new column id but the same name in one table update.+ ColumnEntity newColumn =+ ColumnEntity.builder()+ .withId(RandomIdGenerator.INSTANCE.nextId())+ .withName("column1")+ .withPosition(0)+ .withComment("comment1_new")+ .withDataType(Types.LongType.get())+ .withNullable(true)+ .withAutoIncrement(false)+ .withDefaultValue(Literals.longLiteral(2L))+ .withAuditInfo(AUDIT_INFO)+ .build();++ TableEntity updatedTable =+ TableEntity.builder()+ .withId(retrievedTable.id())+ .withName(retrievedTable.name())+ .withNamespace(retrievedTable.namespace())+ .withColumns(Lists.newArrayList(newColumn))+ .withAuditInfo(retrievedTable.auditInfo())+ .build();++ TableMetaService.getInstance()+ .updateTable(retrievedTable.nameIdentifier(), old -> updatedTable);++ Long selectedColumnId =+ TableColumnMetaService.getInstance()+ .getColumnIdByTableIdAndName(retrievedTable.id(), newColumn.name());+ Assertions.assertEquals(newColumn.id(), selectedColumnId);+ }+private void compareTwoColumns(List<ColumnEntity> expectedColumns, List<ColumnEntity> actualColumns) {Assertions.assertEquals(expectedColumns.size(), actualColumns.size());