apache / apache/datafusion

Incorrect result when use volatile function alias in having clause

Offen
#7,976 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
bug
Vorherrschende Sprache
Rust
Sterne
9.3k
Forks
2.4k
Ø Merge
3 T. 11 Std.
Gemergte PRs (30 T.)
360

Beschreibung

### Is your feature request related to a problem or challenge?

related to #7876
when we do the query like
```sql
select a, random() as r from test group by a having r > 0.5;
```
we will get the incorrect result like
```
+----+---------------------+
| a | r |
+----+---------------------+
| c | 0.1664909010224056 |
| e | 0.35292520754684475 |
| b | 0.26159048688135855 |
+----+---------------------+
```
this is because when we convert the Statement into a LogicalPlan, we will "dereferences" any aliases in the HAVING clause in the below section
https://github.com/apache/arrow-datafusion/blob/1dd887cdff518ede1d1de457f4b20c22a9c7228f/datafusion/sql/src/select.rs#L110-L122

### Describe the solution you'd like

Based on the way we implemented alias in having clause, I think we should disable the use of aliases for volatile functions in the having clause and report a error.
and we can use subqueries to implement the above SQL
```sql
select t.a, t.r from (select a, random() as r from test group by a) as t where t.r > 0.5;
```

### Describe alternatives you've considered

support the sql like
```sql
select a, random() as r from test group by a having r > 0.5;
```
but this method is more complicated

### Additional context

duchdb also do this query incorrect
```
D select t.c1, random() as r from agg.csv as t group by t.c1 having r > 0.5;
┌─────────┬─────────────────────┐
│ c1 │ r │
│ varchar │ double │
├─────────┼─────────────────────┤
│ b │ 0.5120539779309183 │
│ d │ 0.20845546829514205 │
└─────────┴─────────────────────┘
```
mysql can get correct result
postgres disallow use alias in having clause

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Reproduziere die Abfrage im Issue und untersuche anschließend datafusion/sql/src/select.rs ungefähr in den Zeilen 110-122, wo HAVING-Aliase während der Konvertierung in LogicalPlan dereferenziert werden. Die Änderung sollte verhindern, dass Aliase für volatile Funktionen in HAVING akzeptiert werden, und einen Fehler melden, wobei die Beispielabfrage des Issues als Verhaltensprüfung dient.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
rust, sql
Bereich
databases
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.