From fe43d0b1d04b99f25a78ecd093d480e632e2207d Mon Sep 17 00:00:00 2001 From: shaharuk-yb Date: Tue, 23 Jul 2024 18:12:32 +0530 Subject: [PATCH] perf-data-loader: suggested order of data loading into tables --- .../dataloader/DataGeneratorLoader.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main/java/com/oltpbenchmark/benchmarks/dataloader/DataGeneratorLoader.java b/src/main/java/com/oltpbenchmark/benchmarks/dataloader/DataGeneratorLoader.java index ee964971d..9f476f1c0 100644 --- a/src/main/java/com/oltpbenchmark/benchmarks/dataloader/DataGeneratorLoader.java +++ b/src/main/java/com/oltpbenchmark/benchmarks/dataloader/DataGeneratorLoader.java @@ -560,10 +560,41 @@ public void getDistinctValuesFromParentTable(Connection conn, List f while (rs.next()) { distinctValues.add(rs.getObject(1)); } + if (distinctValues.isEmpty()) { + List hierarchy = getParentTableHierarchy(foreignKey.getForeignTableName(), conn); + + StringBuilder loadOrder = new StringBuilder(String.format("There are no entries in the parent " + + "table `%s` for column `%s` to be used as foreign key. Consider loading tables in " + + "following order: ", foreignKey.getForeignTableName(), + foreignKey.getForeignColumnName())); + for (String table: hierarchy) { + loadOrder.append(table).append(" --> "); + } + loadOrder.append(foreignKey.getTableName()); + throw new RuntimeException(loadOrder.toString()); + } foreignKey.setDistinctValues(distinctValues); } catch (SQLException e) { e.printStackTrace(); } } } + public static List getParentTableHierarchy(String tableName, Connection conn) { + List tableHierarchy = new ArrayList<>(); + findParentTables(tableName, conn, tableHierarchy); + return tableHierarchy; + } + + private static void findParentTables(String tableName, Connection conn, List tableHierarchy) { + List foreignKeys = getForeignKeys(tableName, conn); + + if (foreignKeys.isEmpty()) { + tableHierarchy.add(tableName); + } else { + for (ForeignKey foreignKey : foreignKeys) { + findParentTables(foreignKey.getForeignTableName(), conn, tableHierarchy); + } + tableHierarchy.add(tableName); + } + } }