Repository: slick/slick (scala)

Issue:
Distinct can confuse columns
```
  def testDistinctManyColumns = {
    case class AA(from: String, to: String, data: Int)
    case class T(table: String)(tag: Tag) extends Table[AA](tag, table) {
      def from = column[String]("from")
      def to = column[String]("to")
      def data = column[Int]("data")
      def * = (from, to, data).mapTo[AA]
    }
    val ts = TableQuery(T("t1"))
    val q2 = ts.distinctOn(t => (t.to, t.from)).sortBy(t => (t.to, t.from, t.data))
    val values = Seq(
      AA("from1", "to2", 1),
      AA("from1", "to1", 1),
      AA("from1", "to2", 11),
    )
    DBIO.seq(
      ts.schema.createIfNotExists,
      ts ++= values,
      q2.result.map(_ shouldBe Vector(
        AA("from1", "to1", 1),
        AA("from1", "to2", 1),
      )),
//      DBIO.failed(new Exception)
    )
  }
```
H2MemTest sometimes fails because columns in distinct are swapped
slick 3.6.0

<br/>
<hr/>

<details><summary>This repo is using Opire - what does it mean? 👇</summary><br/>💵 Everyone can add rewards for this issue commenting <code>/reward 100</code> (replace <code>100</code> with the amount).<br/>🕵️‍♂️ If someone starts working on this issue to earn the rewards, they can comment <code>/try</code> to let everyone know!<br/>🙌 And when they open the PR, they can comment <code>/claim #3146</code> either in the PR description or in a PR's comment.<br/><br/>🪙 Also, everyone can tip any user commenting <code>/tip 20 @OlegYch</code> (replace <code>20</code> with the amount, and <code>@OlegYch</code> with the user to tip).<br/><br/>📖 If you want to learn more, check out our <a href="https://docs.opire.dev">documentation</a>.</details>

Pull request:

Patch:
diff --git a/slick/src/main/scala/slick/compiler/RewriteDistinct.scala b/slick/src/main/scala/slick/compiler/RewriteDistinct.scala
index 449e1919bb..eff183e3e6 100644
--- a/slick/src/main/scala/slick/compiler/RewriteDistinct.scala
+++ b/slick/src/main/scala/slick/compiler/RewriteDistinct.scala
@@ -32,14 +32,14 @@ class RewriteDistinct extends Phase {
     }.toSet
     logger.debug("Referenced fields: " + refFields.mkString(", "))
     val onFlat = ProductNode(ConstArray(dist1.on)).flatten
-    val onNodes = onFlat.children.toSet
+    val onNodes = onFlat.children
     val onFieldPos = onNodes.iterator.zipWithIndex.collect[(TermSymbol, Int)] {
       case (Select(Ref(s), f), idx) if s == dist1.generator => (f, idx)
     }.toMap
     logger.debug("Fields used directly in 'on' clause: " + onFieldPos.keySet.mkString(", "))
     if((refFields -- onFieldPos.keys).isEmpty) {
       // Only distinct fields referenced -> Create subquery and remove 'on' clause
-      val onDefs = ConstArray.from(onNodes).map((new AnonSymbol, _))
+      val onDefs = onNodes.map((new AnonSymbol, _))
       val onLookup = onDefs.iterator.collect[(TermSymbol, AnonSymbol)] {
         case (a, Select(Ref(s), f)) if s == dist1.generator => (f, a)
       }.toMap


Test patch:
diff --git a/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/AggregateTest.scala b/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/AggregateTest.scala
index 4c4056ef0d..a2b13f6557 100644
--- a/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/AggregateTest.scala
+++ b/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/AggregateTest.scala
@@ -472,4 +472,31 @@ class AggregateTest extends AsyncTest[RelationalTestDB] {
       mark("q0", q.result).map(_ shouldBe Seq(1 -> Some(32), 3 -> Some(33))),
     )
   }
+
+  def testDistinctManyColumns = {
+    case class AA(from: String, to: String, data: Int)
+    case class T(table: String)(tag: Tag) extends Table[AA](tag, table) {
+      def from = column[String]("from")
+      def to = column[String]("to")
+      def data = column[Int]("data")
+      def * = (from, to, data).mapTo[AA]
+    }
+    val ts = TableQuery(T("t1"))
+    val q2 = ts.distinctOn(t => (t.to, t.from)).sortBy(t => (t.to, t.from, t.data))
+    val values = Seq(
+      AA("from1", "to2", 1),
+      AA("from1", "to1", 1),
+      AA("from1", "to2", 11),
+    )
+    DBIO.seq(
+      ts.schema.createIfNotExists,
+      ts ++= values,
+      q2.result.map(_ shouldBe Vector(
+        AA("from1", "to1", 1),
+        AA("from1", "to2", 1),
+      )),
+//      DBIO.failed(new Exception)
+    )
+  }
+
 }