From 4f9ccf8a18da3d4c843406eee69118fd629f2096 Mon Sep 17 00:00:00 2001 From: Ram Narayan Balaji Date: Mon, 23 Nov 2026 13:55:43 +0530 Subject: [PATCH 1/2] Select FQN-cleanup probe hashes in one collation on MySQL The hard-delete cleanup from #38126 probes every taggable entity table in one UNION ALL. MySQL rejects a UNION whose columns have different collations, and an install's hash columns do not share one: most are ascii_bin, the newer tables declare utf8mb4_0900_ai_ci, and query_entity.fqnHash (renamed in 1.2.0 without a collation) takes the server default. On a server whose default is not utf8mb4_0900_ai_ci, every hard delete of a taggable entity fails with "Illegal mix of collations for operation 'UNION'". On MySQL each probe now selects CONVERT(hash USING utf8mb4) COLLATE utf8mb4_bin. The WHERE clause still reads the bare column, so the lookup keeps its index. No schema change. --- .../service/jdbi3/FqnKeyedRowCleanup.java | 20 +++++++-- .../service/jdbi3/FqnKeyedRowCleanupTest.java | 41 ++++++++++++------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanup.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanup.java index 27b951fe67a2..f256e8de4310 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanup.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanup.java @@ -15,6 +15,7 @@ import java.util.function.Function; import java.util.function.Predicate; import org.openmetadata.service.Entity; +import org.openmetadata.service.resources.databases.DatasourceConfig; import org.openmetadata.service.util.FullyQualifiedName; /** @@ -187,7 +188,7 @@ private static List tables(Predicate> include) .filter(FqnKeyedRowCleanup::hasTable) .filter(include) .map(repository -> repository.getDao()) - .map(dao -> new EntityTable(dao.getTableName(), dao.getNameHashColumn())) + .map(dao -> new EntityTable(dao.getTableName(), dao.getNameHashColumn(), onMySql())) .distinct() .toList(); } @@ -279,9 +280,22 @@ static List outermost(List hashes) { return List.copyOf(outermost); } - record EntityTable(String name, String hashColumn) { + private static boolean onMySql() { + return Boolean.TRUE.equals(DatasourceConfig.getInstance().isMySQL()); + } + + /** + * One entity table's hash column, selected as {@code hash}. MySQL rejects a UNION over columns of + * different collations, and an install's hash columns do not share one: most are ascii_bin, some + * tables declare utf8mb4_0900_ai_ci, and query_entity.fqnHash takes the server's default. So on + * MySQL each probe selects its hash as binary utf8mb4; the WHERE clause still reads the bare + * column, so the lookup keeps its index. + */ + record EntityTable(String name, String hashColumn, boolean onMySql) { String select() { - return "SELECT " + hashColumn + " AS hash FROM " + name; + String hash = + onMySql ? "CONVERT(" + hashColumn + " USING utf8mb4) COLLATE utf8mb4_bin" : hashColumn; + return "SELECT " + hash + " AS hash FROM " + name; } } diff --git a/openmetadata-service/src/test/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanupTest.java b/openmetadata-service/src/test/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanupTest.java index 870fee3068c1..ab12bf80bc7f 100644 --- a/openmetadata-service/src/test/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanupTest.java +++ b/openmetadata-service/src/test/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanupTest.java @@ -35,10 +35,6 @@ class FqnKeyedRowCleanupTest { private static final String FQN = "svc"; private static final String HASH = FullyQualifiedName.buildHash(FQN); - private static final List TABLES = - List.of( - new EntityTable("table_entity", "fqnHash"), - new EntityTable("database_service_entity", "nameHash")); private final List preparedSql = new ArrayList<>(); private final List boundValues = new ArrayList<>(); @@ -107,14 +103,16 @@ void theNamesakeProbeLooksUpEveryHashInEachTable(ConnectionType database) { bindings.put("hash1", "h1"); tagUsage(database) - .listEntityFqnHashes(FqnKeyedRowCleanup.inProbes(TABLES, bindings.keySet()), bindings); + .listEntityFqnHashes( + FqnKeyedRowCleanup.inProbes(tables(database), bindings.keySet()), bindings); assertEquals( List.of( - "SELECT probe.hash FROM (" - + "(SELECT fqnHash AS hash FROM table_entity WHERE fqnHash IN (?, ?)) UNION ALL " - + "(SELECT nameHash AS hash FROM database_service_entity WHERE nameHash IN (?, ?))" - + ") probe"), + "SELECT probe.hash FROM ((SELECT " + + selected(database, "fqnHash") + + " AS hash FROM table_entity WHERE fqnHash IN (?, ?)) UNION ALL (SELECT " + + selected(database, "nameHash") + + " AS hash FROM database_service_entity WHERE nameHash IN (?, ?))) probe"), preparedSql); assertEquals(List.of("h0", "h1", "h0", "h1"), boundValues); } @@ -125,7 +123,7 @@ void theNestedProbesLookUnderEachPrefixAndCapEachTable(ConnectionType database) Map prefixes = new LinkedHashMap<>(); prefixes.put("under0", "a.%"); prefixes.put("under1", "b.%"); - List table = TABLES.subList(0, 1); + List table = tables(database).subList(0, 1); tagUsage(database) .listEntityFqnHashes(FqnKeyedRowCleanup.anyUnderProbes(table, prefixes.keySet()), prefixes); @@ -136,10 +134,12 @@ void theNestedProbesLookUnderEachPrefixAndCapEachTable(ConnectionType database) assertEquals( List.of( - "SELECT probe.hash FROM ((SELECT fqnHash AS hash FROM table_entity" - + " WHERE (fqnHash LIKE ? OR fqnHash LIKE ?) LIMIT 1)) probe", - "SELECT probe.hash FROM ((SELECT fqnHash AS hash FROM table_entity" - + " WHERE fqnHash LIKE ? LIMIT 101)) probe"), + "SELECT probe.hash FROM ((SELECT " + + selected(database, "fqnHash") + + " AS hash FROM table_entity WHERE (fqnHash LIKE ? OR fqnHash LIKE ?) LIMIT 1)) probe", + "SELECT probe.hash FROM ((SELECT " + + selected(database, "fqnHash") + + " AS hash FROM table_entity WHERE fqnHash LIKE ? LIMIT 101)) probe"), preparedSql); assertEquals(List.of("a.%", "b.%", "a.%"), boundValues); } @@ -177,6 +177,19 @@ void theFieldRelationshipDeleteKeepsTheRowsAtAndUnderEachKeptSubtree(ConnectionT boundValues); } + private static List tables(ConnectionType database) { + boolean onMySql = database == ConnectionType.MYSQL; + return List.of( + new EntityTable("table_entity", "fqnHash", onMySql), + new EntityTable("database_service_entity", "nameHash", onMySql)); + } + + private static String selected(ConnectionType database, String hashColumn) { + return database == ConnectionType.MYSQL + ? "CONVERT(" + hashColumn + " USING utf8mb4) COLLATE utf8mb4_bin" + : hashColumn; + } + private ClassificationTagDAOs.TagUsageDAO tagUsage(ConnectionType database) { return dao(database, ClassificationTagDAOs.TagUsageDAO.class); } From 5316487bd2a12d87547b932da96506adba9915d6 Mon Sep 17 00:00:00 2001 From: Ram Narayan Balaji Date: Mon, 23 Nov 2026 14:05:31 +0530 Subject: [PATCH 2/2] Run the FQN-cleanup probes on a real MySQL with mixed hash collations The SQL-string test cannot catch the collation error. This one starts MySQL with utf8mb4_unicode_ci as the server default and three tables on the collations an install mixes (ascii_bin, utf8mb4_0900_ai_ci, server default). The bare-column union fails with 'Illegal mix of collations'; the namesake and nested probes return the hash from every table. --- .../jdbi3/FqnKeyedRowCleanupMySqlTest.java | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 openmetadata-service/src/test/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanupMySqlTest.java diff --git a/openmetadata-service/src/test/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanupMySqlTest.java b/openmetadata-service/src/test/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanupMySqlTest.java new file mode 100644 index 000000000000..4941b2ad3582 --- /dev/null +++ b/openmetadata-service/src/test/java/org/openmetadata/service/jdbi3/FqnKeyedRowCleanupMySqlTest.java @@ -0,0 +1,135 @@ +/* + * Copyright 2026 Collate + * Licensed 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.openmetadata.service.jdbi3; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.Map; +import org.jdbi.v3.core.Jdbi; +import org.jdbi.v3.core.statement.UnableToExecuteStatementException; +import org.jdbi.v3.sqlobject.SqlObjectPlugin; +import org.jdbi.v3.sqlobject.SqlObjects; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; +import org.openmetadata.service.jdbi3.FqnKeyedRowCleanup.EntityTable; +import org.openmetadata.service.jdbi3.locator.ConnectionAwareAnnotationSqlLocator; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.junit.jupiter.Testcontainers; + +/** + * Runs the cleanup's UNION probes on a MySQL server whose default collation is not the one newer + * tables declare. The three tables carry the hash collations a real install mixes: ascii_bin, a + * table declared utf8mb4_0900_ai_ci, and query_entity's column, which takes the server default. + */ +@Testcontainers(disabledWithoutDocker = true) +@Execution(ExecutionMode.SAME_THREAD) +class FqnKeyedRowCleanupMySqlTest { + private static final String DATABASE = "fqn_cleanup"; + private static final String PASSWORD = "fqn-cleanup-test"; + private static final String HASH = "h0.h1"; + private static final Map BINDINGS = Map.of("hash0", HASH); + private static final List TABLE_NAMES = + List.of("test_suite", "ai_application_entity", "query_entity"); + + private static GenericContainer database; + private static ClassificationTagDAOs.TagUsageDAO tagUsage; + + @BeforeAll + static void startMySql() { + database = + new GenericContainer<>("mysql:8.4.13") + .withEnv("MYSQL_DATABASE", DATABASE) + .withEnv("MYSQL_ROOT_PASSWORD", PASSWORD) + .withEnv("MYSQL_ROOT_HOST", "%") + .withCommand("--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci") + .withExposedPorts(3306) + .waitingFor( + Wait.forSuccessfulCommand( + "mysql --protocol=TCP --host=127.0.0.1 --user=root --password=%s --execute='SELECT 1'" + .formatted(PASSWORD))); + database.start(); + Jdbi jdbi = + Jdbi.create( + "jdbc:mysql://%s:%d/%s?allowPublicKeyRetrieval=true&useSSL=false" + .formatted(database.getHost(), database.getMappedPort(3306), DATABASE), + "root", + PASSWORD); + jdbi.installPlugin(new SqlObjectPlugin()); + jdbi.getConfig(SqlObjects.class) + .setSqlLocator(new ConnectionAwareAnnotationSqlLocator("com.mysql.cj.jdbc.Driver")); + seedTables(jdbi); + tagUsage = jdbi.onDemand(ClassificationTagDAOs.TagUsageDAO.class); + } + + @AfterAll + static void stopMySql() { + database.stop(); + } + + @Test + void bareHashColumnsOfMixedCollationsCannotBeUnioned() { + UnableToExecuteStatementException error = + assertThrows(UnableToExecuteStatementException.class, () -> namesakeHashes(false)); + assertTrue(error.getMessage().contains("Illegal mix of collations"), error.getMessage()); + } + + @Test + void theNamesakeProbeFindsTheHashInEveryTable() { + assertEquals(List.of(HASH, HASH, HASH), namesakeHashes(true)); + } + + @Test + void theNestedProbeFindsTheRowsUnderAPrefixInEveryTable() { + List hashes = + tagUsage.listEntityFqnHashes( + FqnKeyedRowCleanup.allUnderProbes(tables(true), "under", 10), Map.of("under", "h0.%")); + assertEquals(List.of(HASH, HASH, HASH), hashes); + } + + private static List namesakeHashes(boolean onMySql) { + return tagUsage.listEntityFqnHashes( + FqnKeyedRowCleanup.inProbes(tables(onMySql), BINDINGS.keySet()), BINDINGS); + } + + private static List tables(boolean onMySql) { + return TABLE_NAMES.stream().map(name -> new EntityTable(name, "fqnHash", onMySql)).toList(); + } + + private static void seedTables(Jdbi jdbi) { + jdbi.useHandle( + handle -> { + handle.execute( + "CREATE TABLE test_suite (fqnHash VARCHAR(768) CHARACTER SET ascii COLLATE ascii_bin," + + " KEY fqn_hash (fqnHash))"); + handle.execute( + "CREATE TABLE ai_application_entity (fqnHash VARCHAR(768), KEY fqn_hash (fqnHash))" + + " DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci"); + handle.execute( + "CREATE TABLE query_entity (fqnHash VARCHAR(256), KEY fqn_hash (fqnHash))"); + TABLE_NAMES.forEach( + table -> + handle + .createUpdate( + "INSERT INTO %s (fqnHash) VALUES (:hash), ('other')".formatted(table)) + .bind("hash", HASH) + .execute()); + }); + } +}