From cd7f48e5a15ca91ebcca965f942a9bff72e6e966 Mon Sep 17 00:00:00 2001 From: tanjiro Kamado Date: Wed, 2 Dec 2026 12:15:12 +0530 Subject: [PATCH] fix: find DELETE target tables, UPSERT SET sources and CLONE sources TablesNamesFinder.visit(Delete) never visited Delete.getTables(), the table list in front of FROM in T-SQL and MySQL, so DELETE secrets FROM t reported only t. visit(Upsert) skipped Upsert.getUpdateSets(), and TableDefinitionTraversal.visit(CreateTable) did not accept getCloneTable(), so UPSERT INTO t SET a = (SELECT ... FROM secrets) and CREATE TABLE copy CLONE secrets hid secrets from the finder, the adapter and the validators. UpsertValidator now validates the SET assignments too. --- .../util/TableDefinitionTraversal.java | 1 + .../sf/jsqlparser/util/TablesNamesFinder.java | 43 +++++++++++++++++++ .../validation/validator/UpsertValidator.java | 11 +++-- .../util/TablesNamesFinderTest.java | 32 ++++++++++++++ .../validator/UpsertValidatorTest.java | 7 +++ 5 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/sf/jsqlparser/util/TableDefinitionTraversal.java b/src/main/java/net/sf/jsqlparser/util/TableDefinitionTraversal.java index 5ecac8def7..889d6f9d5a 100644 --- a/src/main/java/net/sf/jsqlparser/util/TableDefinitionTraversal.java +++ b/src/main/java/net/sf/jsqlparser/util/TableDefinitionTraversal.java @@ -167,6 +167,7 @@ public static void visit(CreateTable table, Consumer expressions, accept(table.getExecute().getExprList(), expressions); } accept(table.getTrailingLikeTable(), tables); + accept(table.getCloneTable(), tables); accept(table.getPartitionOf(), tables); visit(table.getPartitioning(), expressions); visit(table.getPartitionBound(), expressions); diff --git a/src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java b/src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java index b243851f4d..5683f44a9a 100644 --- a/src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java +++ b/src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java @@ -504,6 +504,47 @@ private void visitTargetTable(Table table, S context) { withItemsInScope = scope; } + /** + * The tables listed between DELETE and FROM (T-SQL {@code DELETE t FROM t JOIN u ...}, MySQL + * {@code DELETE t1, t2 FROM t1 JOIN t2 ...}) are the ones rows are deleted from. An entry + * naming the alias of a FROM item stands for that item, which is visited with the FROM clause. + */ + private void visitDeleteTargets(Delete delete, S context) { + if (delete.getTables() != null) { + for (Table target : delete.getTables()) { + if (!isAliasInDelete(delete, target.getFullyQualifiedName())) { + visitTargetTable(target, context); + } + } + } + } + + private static boolean isAliasInDelete(Delete delete, String name) { + boolean found = hasAlias(delete.getTable(), name) || hasAlias(delete.getJoins(), name) + || hasAlias(delete.getUsingJoins(), name); + if (!found && delete.getUsingFromItemList() != null) { + for (FromItem usingFromItem : delete.getUsingFromItemList()) { + found |= hasAlias(usingFromItem, name); + } + } + return found; + } + + private static boolean hasAlias(List joins, String name) { + boolean found = false; + if (joins != null) { + for (Join join : joins) { + found |= hasAlias(join.getRightItem(), name); + } + } + return found; + } + + private static boolean hasAlias(FromItem fromItem, String name) { + return fromItem != null && fromItem.getAlias() != null + && name.equalsIgnoreCase(fromItem.getAlias().getName()); + } + @Override public Void visit(WithItem withItem, S context) { if (withItem.getAlias() != null) { @@ -1601,6 +1642,7 @@ public Void visit(MySQLGroupConcat groupConcat, S context) { public Void visit(Delete delete, S context) { visitWithItems(delete.getWithItemsList(), context); visitTargetTable(delete.getTable(), context); + visitDeleteTargets(delete, context); if (delete.getUsingFromItemList() != null) { for (FromItem usingFromItem : delete.getUsingFromItemList()) { @@ -2401,6 +2443,7 @@ public void visit(Commit commit) { @Override public Void visit(Upsert upsert, S context) { visit(upsert.getTable(), context); + visitUpdateSets(upsert.getUpdateSets(), context); if (upsert.getExpressions() != null) { upsert.getExpressions().accept(this, context); } diff --git a/src/main/java/net/sf/jsqlparser/util/validation/validator/UpsertValidator.java b/src/main/java/net/sf/jsqlparser/util/validation/validator/UpsertValidator.java index 585cf71320..9801a5c9d6 100644 --- a/src/main/java/net/sf/jsqlparser/util/validation/validator/UpsertValidator.java +++ b/src/main/java/net/sf/jsqlparser/util/validation/validator/UpsertValidator.java @@ -16,6 +16,8 @@ import net.sf.jsqlparser.statement.upsert.Upsert; import net.sf.jsqlparser.util.validation.ValidationCapability; +import java.util.List; + /** * @author gitmotte */ @@ -28,9 +30,10 @@ public void validate(Upsert upsert) { } validateOptionalFromItem(upsert.getTable()); validateOptionalExpressions(upsert.getColumns()); + validateUpdateSets(upsert.getUpdateSets()); validateOptionalExpressions(upsert.getExpressions()); validateOptionalSelect(upsert.getSelect()); - validateDuplicate(upsert); + validateUpdateSets(upsert.getDuplicateUpdateSets()); } private void validateOptionalSelect(Select select) { @@ -40,9 +43,9 @@ private void validateOptionalSelect(Select select) { } } - private void validateDuplicate(Upsert upsert) { - if (upsert.getDuplicateUpdateSets() != null) { - for (UpdateSet updateSet : upsert.getDuplicateUpdateSets()) { + private void validateUpdateSets(List updateSets) { + if (updateSets != null) { + for (UpdateSet updateSet : updateSets) { validateOptionalExpressions(updateSet.getColumns()); validateOptionalExpressions(updateSet.getValues()); } diff --git a/src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java b/src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java index 3e10d1c08a..138d1a66da 100644 --- a/src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java +++ b/src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java @@ -1227,6 +1227,38 @@ void testSubqueryInDeclareIsFound(String sqlStr) throws JSQLParserException { assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactly("secrets"); } + @ParameterizedTest + @ValueSource(strings = { + // T-SQL and MySQL list the tables to delete from in front of FROM + "DELETE secrets FROM t", + "DELETE secrets FROM t WHERE t.id = 1", + "DELETE secrets OUTPUT deleted.* FROM t", + "DELETE secrets, t FROM secrets JOIN t ON secrets.id = t.id", + // an entry naming the alias of a FROM item is that item, not a table + "DELETE s FROM secrets s JOIN t ON s.id = t.id", + "DELETE s FROM t JOIN secrets AS s ON s.id = t.id", + "DELETE S, t FROM t JOIN secrets s ON s.id = t.id"}) + void testDeleteTargetListIsFound(String sqlStr) throws JSQLParserException { + assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactlyInAnyOrder("secrets", "t"); + } + + @ParameterizedTest + @ValueSource(strings = { + "UPSERT INTO t SET a = (SELECT max(pw) FROM secrets)", + "UPSERT INTO t SET a = 1, (b, c) = (SELECT x, y FROM secrets), d = 4"}) + void testSubqueryInUpsertSetIsFound(String sqlStr) throws JSQLParserException { + assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactlyInAnyOrder("t", "secrets"); + } + + @ParameterizedTest + @ValueSource(strings = { + "CREATE TABLE copy CLONE secrets", + "CREATE SNAPSHOT TABLE copy CLONE secrets FOR SYSTEM_TIME AS OF TIMESTAMP '2020-01-01'"}) + void testCloneSourceIsFound(String sqlStr) throws JSQLParserException { + assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactlyInAnyOrder("copy", + "secrets"); + } + @Test void testManyDerivedNamesDoNotScaleQuadratically() { // Every plain table reference used to be matched against the CTE aliases with diff --git a/src/test/java/net/sf/jsqlparser/util/validation/validator/UpsertValidatorTest.java b/src/test/java/net/sf/jsqlparser/util/validation/validator/UpsertValidatorTest.java index ff3b9e4660..fddb2a1e16 100644 --- a/src/test/java/net/sf/jsqlparser/util/validation/validator/UpsertValidatorTest.java +++ b/src/test/java/net/sf/jsqlparser/util/validation/validator/UpsertValidatorTest.java @@ -41,4 +41,11 @@ public void testValidationExecuteNotAllowed() throws Exception { } } + @Test + public void testValidateSetAssignmentsOfUpsert() { + String sql = "UPSERT INTO t SET a = f(1), b = 2"; + validateNotAllowed(sql, 1, 1, + new FeaturesAllowed(Feature.values()).remove(Feature.function), Feature.function); + } + }