[Bug] array_sort's second comparator argument takes the first argument's name: a nested lambda reads the wrong argument, and alias functions sort nothing

Aperta Adatta ai principianti
#69,919 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@adarshomrey ci sta già lavorando.

Dal 1/11/2026.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
databases

Direzione di ricerca

Individua la classe ExpressionTranslator dove gli oggetti ColumnRefExpr del comparatore di array_sort sono costruiti. Aggiungi column2.setName(arrayItemReference.getName()); come mostrato nel diff. Verifica eseguendo gli esempi SQL: la funzione alias deve restituire un array ordinato e il lambda annidato deve essere rifiutato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Search before asking
  • I had searched in the issues and found no similar issues.
Version

master and branch-4.1 (the translator code is the same on both). Reproduced on a 4.1 build that includes the nested-lambda capture changes (#68062, #68471).

What's wrong?

ExpressionTranslator builds the ColumnRef of array_sort's second comparator argument as a copy of the first one, and keeps the first argument's name:

ColumnRefExpr column2 = new ColumnRefExpr(column);
column2.setColumnId(1);

That causes two wrong results.

1. A nested lambda in the comparator reads the wrong argument. The BE identifies comparator arguments by name when it rejects a nested lambda that captures one, and both arguments are named after the first. So a nested lambda that re-declares the first argument's name and reads the second passes that check. At run time it reads its own argument instead, the comparator returns a constant, and the array comes back unsorted with no error:

select array_sort((x, y) -> if(array_contains(array_map(x -> x < y, array(x)), true), -1, 1), [3, 1, 2]);
-- returns [3, 1, 2]

2. Alias functions with an array_sort comparator sort nothing, even with a flat comparator. CREATE ALIAS FUNCTION stores the translated expression and rebuilds the function from it, so both comparator arguments come back under the first name, as (x, y) -> ... (x < x) ...:

CREATE ALIAS FUNCTION db.asc_sort(ARRAY<INT>) WITH PARAMETER(arr)
    AS array_sort((x, y) -> IF(x < y, -1, IF(x = y, 0, 1)), arr);
select db.asc_sort([3, 1, 2]);
-- returns [3, 1, 2]

The same comparator in a plain query returns [1, 2, 3].

What you expected to happen
  1. The first query is rejected, like any nested lambda that captures a comparator argument (array_sort comparator does not support nested lambda capturing comparator argument 'y').
  2. The alias returns [1, 2, 3].
How to reproduce

Run the statements above.

Anything else?

Fix: give the second ColumnRef its own name. The comparator frame is position-based (column ids 0 and 1), so flat comparators in queries are unaffected:

         arrayItemReference = lambda.getLambdaArgument(1);
         ColumnRefExpr column2 = new ColumnRefExpr(column);
+        column2.setName(arrayItemReference.getName());
         column2.setColumnId(1);

With that change, query 1 is rejected with the error above, and a flat comparator still sorts [3, 1, 2] to [1, 2, 3]. Aliases created before the fix keep the stored broken body until they are dropped and recreated.

Related: #68977

Are you willing to submit PR?
  • Yes I am willing to submit a PR!
Code of Conduct
Lingua principale
Java
Stelle
16.2k
Fork
4k
Merge medio
1g 19h
PR unite (30g)
671

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/doris

Tutte le issue di apache/doris

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.