Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.catalyst.expressions

import java.util.Comparator
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent.atomic.{AtomicInteger, AtomicReference}

import scala.collection.mutable

Expand Down Expand Up @@ -54,6 +54,16 @@ case class UnresolvedNamedLambdaVariable(nameParts: Seq[String])
override def sql: String = name
}

object UnresolvedNamedLambdaVariable {

// Counter to ensure lambda variable names are unique
private val nextVarNameId = new AtomicInteger(0)

def freshVarName(name: String): String = {
s"${name}_${nextVarNameId.getAndIncrement()}"
}
}

/**
* A named lambda variable.
*/
Expand Down
12 changes: 6 additions & 6 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3800,22 +3800,22 @@ object functions {
}

private def createLambda(f: Column => Column) = {
val x = UnresolvedNamedLambdaVariable(Seq("x"))
val x = UnresolvedNamedLambdaVariable(Seq(UnresolvedNamedLambdaVariable.freshVarName("x")))
val function = f(Column(x)).expr
LambdaFunction(function, Seq(x))
}

private def createLambda(f: (Column, Column) => Column) = {
val x = UnresolvedNamedLambdaVariable(Seq("x"))
val y = UnresolvedNamedLambdaVariable(Seq("y"))
val x = UnresolvedNamedLambdaVariable(Seq(UnresolvedNamedLambdaVariable.freshVarName("x")))
val y = UnresolvedNamedLambdaVariable(Seq(UnresolvedNamedLambdaVariable.freshVarName("y")))
val function = f(Column(x), Column(y)).expr
LambdaFunction(function, Seq(x, y))
}

private def createLambda(f: (Column, Column, Column) => Column) = {
val x = UnresolvedNamedLambdaVariable(Seq("x"))
val y = UnresolvedNamedLambdaVariable(Seq("y"))
val z = UnresolvedNamedLambdaVariable(Seq("z"))
val x = UnresolvedNamedLambdaVariable(Seq(UnresolvedNamedLambdaVariable.freshVarName("x")))
val y = UnresolvedNamedLambdaVariable(Seq(UnresolvedNamedLambdaVariable.freshVarName("y")))
val z = UnresolvedNamedLambdaVariable(Seq(UnresolvedNamedLambdaVariable.freshVarName("z")))
val function = f(Column(x), Column(y), Column(z)).expr
LambdaFunction(function, Seq(x, y, z))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3629,6 +3629,29 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSparkSession {
df.select(map(map_entries($"m"), lit(1))),
Row(Map(Seq(Row(1, "a")) -> 1)))
}

test("SPARK-34794: lambda variable name issues in nested functions") {
val df1 = Seq((Seq(1, 2), Seq("a", "b"))).toDF("numbers", "letters")

checkAnswer(df1.select(flatten(transform($"numbers", (number: Column) =>
transform($"letters", (letter: Column) =>
struct(number, letter))))),
Seq(Row(Seq(Row(1, "a"), Row(1, "b"), Row(2, "a"), Row(2, "b"))))
)
checkAnswer(df1.select(flatten(transform($"numbers", (number: Column, i: Column) =>
transform($"letters", (letter: Column, j: Column) =>
struct(number + j, concat(letter, i)))))),
Seq(Row(Seq(Row(1, "a0"), Row(2, "b0"), Row(2, "a1"), Row(3, "b1"))))
)

val df2 = Seq((Map("a" -> 1, "b" -> 2), Map("a" -> 2, "b" -> 3))).toDF("m1", "m2")

checkAnswer(df2.select(map_zip_with($"m1", $"m2", (k1: Column, ov1: Column, ov2: Column) =>
map_zip_with($"m1", $"m2", (k2: Column, iv1: Column, iv2: Column) =>
ov1 + iv1 + ov2 + iv2))),
Seq(Row(Map("a" -> Map("a" -> 6, "b" -> 8), "b" -> Map("a" -> 8, "b" -> 10))))
)
}
}

object DataFrameFunctionsSuite {
Expand Down