Repository: cashapp/sqldelight
Issue: 1.5.2 regression: codegen inserts invalid & unused type conversion

### SQLDelight Version

1.5.2

### Operating System

Mac

### Gradle Version

7.2

### Kotlin Version

1.5.31

### AGP Version

7.0.3

### Describe the Bug

The codegen for this works fine in 1.5.1, and generates invalid code in 1.5.2 due to an unused, additional type conversion. This causes an irrecoverable build failure, no apparent workaround besides downgrade to 1.5.1. Build error reads: `Type mismatch: inferred type is Collection<ComboEnum> but ComboEnum was expected`. The enum can be anything, I've used an empty enum for this repro.

    CREATE TABLE ComboData (
        value TEXT AS ComboEnum NOT NULL
    );
    
    CREATE TABLE ComboData2 (
        value TEXT AS ComboEnum NOT NULL
    );
    
    countRecords:
    SELECT (SELECT count(*) FROM ComboData WHERE value IN :values) +
    (SELECT count(*) FROM ComboData2 WHERE value IN :values);

Generated code in 1.5.2:

    private inner class CountRecordsQuery<out T : Any>(
    public val values: Collection<ComboEnum>,
    mapper: (SqlCursor) -> T
    ) : Query<T>(countRecords, mapper) {
    public override fun execute(): SqlCursor {
      val valuesIndexes = createArguments(count = values.size)
      return driver.executeQuery(null, """
      |SELECT (SELECT count(*) FROM ComboData WHERE value IN $valuesIndexes) +
      |(SELECT count(*) FROM ComboData2 WHERE value IN $valuesIndexes)
      """.trimMargin(), values.size + values.size) {
        val values_ = database.ComboDataAdapter.valueAdapter.encode(values) // ERROR - UNUSED NONSENSE
        values.forEachIndexed { index, values__ ->
            bindString(index + 1, database.ComboDataAdapter.valueAdapter.encode(values__))
            }
        values.forEachIndexed { index, values___ ->
            bindString(index + values.size + 1,
                database.ComboDataAdapter.valueAdapter.encode(values___))
            }
      }
    }
    
    public override fun toString(): String = "ComboData.sq:countRecords"
    }

The code generated with 1.5.1 is the same except it's missing the ERROR line.

### Stacktrace

_No response_

### Gradle Build Script

_No response_

Changed files:
sqldelight-compiler/src/test/kotlin/com/squareup/sqldelight/core/queries/SelectQueryTypeTest.kt
sqldelight-compiler/src/main/kotlin/com/squareup/sqldelight/core/compiler/QueryGenerator.kt

diff --git a/sqldelight-compiler/src/main/kotlin/com/squareup/sqldelight/core/compiler/QueryGenerator.kt b/sqldelight-compiler/src/main/kotlin/com/squareup/sqldelight/core/compiler/QueryGenerator.kt
index 7e24f8ec7..dd0f47ebd 100644
--- a/sqldelight-compiler/src/main/kotlin/com/squareup/sqldelight/core/compiler/QueryGenerator.kt
+++ b/sqldelight-compiler/src/main/kotlin/com/squareup/sqldelight/core/compiler/QueryGenerator.kt
@@ -101,6 +101,7 @@ abstract class QueryGenerator(private val query: BindableQuery) {
     val extractedVariables = mutableMapOf<IntermediateType, String>()
     // extract the variable for duplicate types, so we don't encode twice
     for (type in duplicateTypes) {
+      if (type.bindArg?.isArrayParameter() == true) continue
       val encodedJavaType = type.encodedJavaType() ?: continue
       val variableName = argumentNameAllocator.newName(type.name)
       extractedVariables[type] = variableName
diff --git a/sqldelight-compiler/src/test/kotlin/com/squareup/sqldelight/core/queries/SelectQueryTypeTest.kt b/sqldelight-compiler/src/test/kotlin/com/squareup/sqldelight/core/queries/SelectQueryTypeTest.kt
index 4c8617f2e..a2be580a4 100644
--- a/sqldelight-compiler/src/test/kotlin/com/squareup/sqldelight/core/queries/SelectQueryTypeTest.kt
+++ b/sqldelight-compiler/src/test/kotlin/com/squareup/sqldelight/core/queries/SelectQueryTypeTest.kt
@@ -1047,4 +1047,52 @@ class SelectQueryTypeTest {
       |""".trimMargin()
     )
   }
+
+  @Test fun `don't extract variable for duplicate array parameters`() {
+    val file = FixtureCompiler.parseSql(
+      """
+      |CREATE TABLE ComboData (
+      |  value TEXT AS ComboEnum NOT NULL
+      |);
+      |
+      |CREATE TABLE ComboData2 (
+      |  value TEXT AS ComboEnum NOT NULL
+      |);
+      |
+      |countRecords:
+      |SELECT (SELECT count(*) FROM ComboData WHERE value IN :values) +
+      |(SELECT count(*) FROM ComboData2 WHERE value IN :values);
+      |""".trimMargin(),
+      tempFolder, fileName = "Data.sq"
+    )
+
+    val query = file.namedQueries.first()
+    val generator = SelectQueryGenerator(query)
+
+    assertThat(generator.querySubtype().toString()).isEqualTo(
+      """
+        |private inner class CountRecordsQuery<out T : kotlin.Any>(
+        |  public val values: kotlin.collections.Collection<ComboEnum>,
+        |  mapper: (com.squareup.sqldelight.db.SqlCursor) -> T
+        |) : com.squareup.sqldelight.Query<T>(countRecords, mapper) {
+        |  public override fun execute(): com.squareup.sqldelight.db.SqlCursor {
+        |    val valuesIndexes = createArguments(count = values.size)
+        |    return driver.executeQuery(null, ""${'"'}
+        |    |SELECT (SELECT count(*) FROM ComboData WHERE value IN ${"$"}valuesIndexes) +
+        |    |(SELECT count(*) FROM ComboData2 WHERE value IN ${"$"}valuesIndexes)
+        |    ""${'"'}.trimMargin(), values.size + values.size) {
+        |      values.forEachIndexed { index, values_ ->
+        |          bindString(index + 1, database.ComboDataAdapter.valueAdapter.encode(values_))
+        |          }
+        |      values.forEachIndexed { index, values__ ->
+        |          bindString(index + values.size + 1, database.ComboDataAdapter.valueAdapter.encode(values__))
+        |          }
+        |    }
+        |  }
+        |
+        |  public override fun toString(): kotlin.String = "Data.sq:countRecords"
+        |}
+        |""".trimMargin()
+    )
+  }
 }