From 5ec25d7664b4f80c01c6ef166ac4ff75ca332810 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 18 Nov 2020 15:29:12 +0100 Subject: [PATCH] [jOOQ/jOOQ#10970] Unknown field identifier when parsing ORDER BY clauses referencing field aliases with parseWithMetaLookups --- .../main/java/org/jooq/impl/ParserImpl.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index f5b2b933af..d924c50735 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -342,6 +342,7 @@ import static org.jooq.impl.Tools.EMPTY_NAME; import static org.jooq.impl.Tools.EMPTY_QUERYPART; import static org.jooq.impl.Tools.EMPTY_ROW; import static org.jooq.impl.Tools.EMPTY_SORTFIELD; +import static org.jooq.impl.Tools.aliased; import static org.jooq.impl.Tools.normaliseNameCase; import static org.jooq.impl.XMLPassingMechanism.BY_REF; import static org.jooq.impl.XMLPassingMechanism.BY_VALUE; @@ -1134,6 +1135,10 @@ final class ParserImpl implements Parser { SelectQueryImpl result = parseQueryExpressionBody(ctx, degree, with, null); List> orderBy = null; + for (Field field : result.getSelect()) + if (aliased(field) != null) + ctx.scope(field); + if (parseKeywordIf(ctx, "ORDER")) { if (parseKeywordIf(ctx, "SIBLINGS BY") && ctx.requireProEdition()) { @@ -12141,6 +12146,7 @@ final class ParserContext { private int bindIndex = 0; private String delimiter = ";"; private final ScopeStack> tableScope = new ScopeStack<>(null); + private final ScopeStack> fieldScope = new ScopeStack<>(null); private final ScopeStack> lookupFields = new ScopeStack<>(null); private boolean scopeClear = false; @@ -12433,8 +12439,13 @@ final class ParserContext { tableScope.set(table.getName(), table); } + void scope(Field field) { + fieldScope.set(field.getName(), field); + } + void scopeStart() { tableScope.scopeStart(); + fieldScope.scopeStart(); lookupFields.scopeStart(); lookupFields.setAll(null); } @@ -12446,8 +12457,19 @@ final class ParserContext { for (FieldProxy f : lookupFields) { Field f1 = null; + for (Field a : fieldScope) { + if (a.getName().equals(f.getName())) { + if (f1 != null) { + position(f.position()); + throw exception("Ambiguous field identifier"); + } + + f1 = a; + } + } for (Table t : tableScope) { Field f2; + if ((f2 = t.field(f.getName())) != null) { if (f1 != null) { position(f.position()); @@ -12466,6 +12488,7 @@ final class ParserContext { lookupFields.scopeEnd(); tableScope.scopeEnd(); + fieldScope.scopeEnd(); for (FieldProxy r : retain) if (lookupFields.get(r.getName()) == null)