Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -187,7 +188,7 @@ private static List<EntityTable> tables(Predicate<EntityRepository<?>> 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();
}
Expand Down Expand Up @@ -279,9 +280,22 @@ static List<String> outermost(List<String> 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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> BINDINGS = Map.of("hash0", HASH);
private static final List<String> TABLE_NAMES =
List.of("test_suite", "ai_application_entity", "query_entity");

private static GenericContainer<?> database;
private static ClassificationTagDAOs.TagUsageDAO tagUsage;

@BeforeAll
static void startMySql() {
Comment on lines +54 to +55

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Test setup is too long

startMySql() has about 23 nonblank code lines. The Java guide requires methods to stay around 15 lines and do one thing. Split container setup from the JDBI connection and table setup to satisfy this repository requirement before merging.

Context Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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 =
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
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());
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}

@Test
void theNamesakeProbeFindsTheHashInEveryTable() {
assertEquals(List.of(HASH, HASH, HASH), namesakeHashes(true));
}

@Test
void theNestedProbeFindsTheRowsUnderAPrefixInEveryTable() {
List<String> hashes =
tagUsage.listEntityFqnHashes(
FqnKeyedRowCleanup.allUnderProbes(tables(true), "under", 10), Map.of("under", "h0.%"));
assertEquals(List.of(HASH, HASH, HASH), hashes);
}

private static List<String> namesakeHashes(boolean onMySql) {
return tagUsage.listEntityFqnHashes(
FqnKeyedRowCleanup.inProbes(tables(onMySql), BINDINGS.keySet()), BINDINGS);
}

private static List<EntityTable> 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());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
class FqnKeyedRowCleanupTest {
private static final String FQN = "svc";
private static final String HASH = FullyQualifiedName.buildHash(FQN);
private static final List<EntityTable> TABLES =
List.of(
new EntityTable("table_entity", "fqnHash"),
new EntityTable("database_service_entity", "nameHash"));

private final List<String> preparedSql = new ArrayList<>();
private final List<Object> boundValues = new ArrayList<>();
Expand Down Expand Up @@ -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"),
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
preparedSql);
assertEquals(List.of("h0", "h1", "h0", "h1"), boundValues);
}
Expand All @@ -125,7 +123,7 @@ void theNestedProbesLookUnderEachPrefixAndCapEachTable(ConnectionType database)
Map<String, String> prefixes = new LinkedHashMap<>();
prefixes.put("under0", "a.%");
prefixes.put("under1", "b.%");
List<EntityTable> table = TABLES.subList(0, 1);
List<EntityTable> table = tables(database).subList(0, 1);

tagUsage(database)
.listEntityFqnHashes(FqnKeyedRowCleanup.anyUnderProbes(table, prefixes.keySet()), prefixes);
Expand All @@ -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);
}
Expand Down Expand Up @@ -177,6 +177,19 @@ void theFieldRelationshipDeleteKeepsTheRowsAtAndUnderEachKeptSubtree(ConnectionT
boundValues);
}

private static List<EntityTable> 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);
}
Expand Down
Loading