morningman commented on code in PR #71211:
URL: https://github.com/apache/doris/pull/71211#discussion_r4768511829


##########
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:
   Confirmed and fixed in 976fc27dfb0.
   
   The preload now resolves the name against the table's full schema with 
`ColumnNames.find`, the analyzer's rule including its case-insensitive 
fallback, and binds it only when that column is a partition column. A partition 
column spelled another way is still prefiltered: `KIRMIZI` or `Σ` for `kirmizi` 
or `σ`, and `kırmızı` for `kirmizi` when the table has no column of that name 
(the fallback).
   
   Two notes on the trigger. It is wider than a join run as root: any statement 
that also reads or writes an internal table reaches the preload, `INSERT INTO` 
an internal table included, and so does a user without a data mask on the Hive 
table. And for a non-ASCII key such as `σ` the Hive connector sends no 
metastore filter; it lists the partition names and prunes them itself, with the 
same empty result. An ASCII key next to a non-ASCII data column (`kirmizi` and 
`kırmızı`) goes through the metastore filter.
   
   On the regression: HiveQL `CREATE TABLE` refuses a data column and a 
partition column that are equal ignoring case (`CreateTableDesc.validate` in 
Hive), and the docker Hive fixtures are built with HiveQL. So the case is in 
`StatementContextPartitionPredicateSafetyTest`, next to the preload's other 
safety cases: it runs a mixed internal and Hive query from collection through 
`PruneFileScanPartition`, with the real `HiveConnectorMetadata` over a mocked 
metastore, for both connector paths. Without the fix it fails with `expected: 
<[kirmizi=y, kirmizi=x]> but was: <[]>`, and the same for `σ`.
   



##########
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:
   Confirmed, thanks. Fixed in fc29691e915.
   
   `bindSlotsNamed` now tells names apart only within one precedence. It keeps 
only the slots of the same name when the slots `bindExactSlotsByThisScope` 
prefers (qualified with as many parts as the name puts before the column, or 
all of them when none is) include one; otherwise it keeps every slot equal to 
the name ignoring case, as before this PR, and the existing precedence picks. 
Your query binds the alias `ς` again and returns `(-2, 2)`.
   
   The same ordering had a second symptom: with `t.Σ` not selected, `Σ` still 
bound the alias through the case-insensitive fallback, so selecting a column 
changed what ORDER BY sorted by. Both forms now bind the alias. Turkish names 
were affected too: `select upper(t.isim) as İSİM, t.isim from (select 'a' as 
isim union all select 'B') t order by isim limit 1` returned `(B, B)` with the 
first commit and returns `(A, a)` again, as before this PR.
   
   What still changes from before this PR, on purpose, is a choice among 
candidates of one precedence: `select * from (select 1 as Σ, 20 as ς union all 
select 2, 10) t order by ς limit 1` took the first column, `(1, 20)`, and now 
returns `(2, 10)`; `GROUP BY Σ` over columns `Σ` and `ς` fell back to an output 
alias `Σ` and now groups by the column. The description no longer says that 
unambiguous bindings never change; it states this rule instead.
   
   Coverage: 
`ExpressionAnalyzerTest.testSlotNamesAreToldApartAmongTheSlotsOfOnePrecedence`, 
which fails on the previous head with `expected: <[ς#1]> but was: <[Σ#2]>`, and 
three cases in `nereids_syntax_p0/order_by_bind_priority`: your query, the 
alias alone, and the `select *` case. On a local cluster they pass on this 
head; the FE of the first commit fails your query (`-1` for `-2`), and the FE 
before this PR fails the `select *` case (it takes the first column).
   



-- 
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