hello-stephen commented on code in PR #71211:
URL: https://github.com/apache/doris/pull/71211#discussion_r4764376726


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java:
##########
@@ -1513,68 +1515,72 @@ private boolean shouldBindSlotBy(int namePartSize, Slot 
boundSlot) {
         return namePartSize <= boundSlot.getQualifier().size() + 1;
     }
 
-    private List<Slot> bindSingleSlotByName(String name, Scope scope) {
-        int namePartSize = 1;
+    /**
+     * Binds {@code name} to the slots of {@code scope} that {@code accepts} 
takes, renamed to the SQL's spelling.
+     * Names are told apart as {@link ColumnNames} does, so {@code Σ} binds a 
slot {@code σ} but not a slot
+     * {@code ς}; a name that is the same as no accepted slot binds the slots 
equal to it ignoring case, as every name
+     * did before. A name that bound one slot still binds it, and one that was 
ambiguous between slots such as
+     * {@code Σ} and {@code ς} binds the slot it names.
+     */
+    private static List<Slot> bindSlotsNamed(String name, Scope scope, boolean 
all, Predicate<Slot> accepts) {
+        List<Slot> equalIgnoringCase = scope.findSlotIgnoreCase(name, all);
+        List<Slot> sameName = acceptSlots(name, equalIgnoringCase,
+                slot -> ColumnNames.same(slot.getName(), name) && 
accepts.test(slot));
+        return sameName.isEmpty() ? acceptSlots(name, equalIgnoringCase, 
accepts) : sameName;

Review Comment:
   [P2] Preserve ORDER BY alias precedence before narrowing name matches
   
   This filtering runs before `bindExactSlotsByThisScope()` applies the 
existing priority for unqualified output aliases. It therefore changes a 
previously unambiguous query:
   
   ```sql
   SELECT -t.`Σ` AS `ς`, t.`Σ`
   FROM (SELECT 1 AS `Σ` UNION ALL SELECT 2 AS `Σ`) t
   ORDER BY `Σ` LIMIT 1;
   ```
   
   The relevant input is:
   
   ```text
   Limit(1)
     Sort(Σ)
       Project(-t.Σ AS ς#A [qualifier=[]], t.Σ#B [qualifier=[t]])
         SubqueryAlias(t)
           UnionAll(1, 2)
   ```
   
   On base, both slots match ignoring case, then `bindExactSlotsByThisScope()` 
selects the unqualified alias #A. Here `sameName` drops #A first, leaving only 
#B, so that method's single-candidate return bypasses alias precedence. The 
source-derived result changes from `(-2, 2)` to `(-1, 1)`, rather than merely 
resolving a previously ambiguous reference. Preserve the existing 
alias/qualification priority before applying the new name preference, and add 
coverage for this ordering case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to