enso-org / enso

Hybrid visual and textual functional programming.
https://enso.org
Apache License 2.0
7.31k stars 317 forks source link

`Concatenate` aggregation is not compatible with `order_by` on the Postgres backend #10321

Open radeusgd opened 1 week ago

radeusgd commented 1 week ago

While working on #10319 I tried introducing deterministic ordering into my input tables, by adding order_by with an 'index' column.

I have noticed that concat aggregate breaks if the table was ordered before.

Repro (e.g. in REPL):

c = Database.connect (Postgres ...) # put your Postgres connection details here
t = (Table.new [["X", [1,2,3]], ["Y", ['a', 'b', 'c']]]).select_into_database_table c "foobar" temporary=True
t.order_by "X" . print
r_unsorted = t.aggregate [Aggregate_Column.Concatenate "Y"]
r_unsorted.print
r_sorted = t.order_by "X" . aggregate [Aggregate_Column.Concatenate "Y"]
IO.println r_sorted
IO.println r_sorted.print

Expected behaviour

Both sorted and unsorted version should work and return a new table. The unsorted version may have an unspecified ordering in the concatenated string.

Actual behaviour

The line IO.println r_sorted.print currently prints:

(Error: There was an SQL error: ERROR: column "foobar.X" must appear in the GROUP BY clause or be used in an aggregate function
  Pozycja: 214. [Query was: SELECT "foobar"."Concatenate Y" AS "Concatenate Y" FROM (SELECT (?) || string_agg(CASE WHEN "foobar"."Y" IS NULL THEN '' ELSE ("foobar"."Y") END, ?) || (?) AS "Concatenate Y" FROM "foobar" AS "foobar" ORDER BY "foobar"."X" ASC NULLS FIRST) AS "foobar" LIMIT 11])
radeusgd commented 1 week ago

We probably need to find the Context.orders and add an OVER clause to the aggregate to make Postgres happy.

Not sure how this will work for more complex nested queries.