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 @@ -88,6 +88,9 @@ case class NamedLambdaVariable(
AttributeReference(name, dataType, nullable, Metadata.empty)(exprId, Seq.empty)
}

// Check if this lambda variable is referenced outside the lambda function it is bound to
override def references: AttributeSet = AttributeSet(toAttribute)

override def eval(input: InternalRow): Any = value.get

override def toString: String = s"lambda $name#${exprId.id}$typeSuffix"
Expand All @@ -113,6 +116,13 @@ case class LambdaFunction(
override def nullable: Boolean = function.nullable
final override val nodePatterns: Seq[TreePattern] = Seq(LAMBDA_FUNCTION)

// Check if lambda variables bound to this lambda function are referenced in the wrong scope
override def references: AttributeSet = if (resolved) {
function.references -- AttributeSet(arguments.flatMap(_.references))
} else {
super.references
}

lazy val bound: Boolean = arguments.forall(_.resolved)

override def eval(input: InternalRow): Any = function.eval(input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import org.apache.spark.sql.catalyst.SchemaPruningTest
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.Cross
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules.RuleExecutor
import org.apache.spark.sql.types.{StringType, StructField, StructType}
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}

class NestedColumnAliasingSuite extends SchemaPruningTest {

Expand Down Expand Up @@ -708,6 +709,35 @@ class NestedColumnAliasingSuite extends SchemaPruningTest {
.analyze
comparePlans(optimized, expected)
}

test("SPARK-35636: do not push lambda key out of lambda function") {
val rel = LocalRelation(
'kvs.map(StringType, new StructType().add("v1", IntegerType)), 'keys.array(StringType))
val key = UnresolvedNamedLambdaVariable("key" :: Nil)
val lambda = LambdaFunction('kvs.getItem(key).getField("v1"), key :: Nil)
val query = rel
.limit(5)
.select('keys, 'kvs)
.limit(5)
.select(ArrayTransform('keys, lambda).as("a"))
.analyze
val optimized = Optimize.execute(query)
comparePlans(optimized, query)
}

test("SPARK-35636: do not push down extract value in higher order " +
"function that references both sides of a join") {
val left = LocalRelation('kvs.map(StringType, new StructType().add("v1", IntegerType)))
val right = LocalRelation('keys.array(StringType))
val key = UnresolvedNamedLambdaVariable("key" :: Nil)
val lambda = LambdaFunction('kvs.getItem(key).getField("v1"), key :: Nil)
val query = left
.join(right, Cross, None)
.select(ArrayTransform('keys, lambda).as("a"))
.analyze
val optimized = Optimize.execute(query)
comparePlans(optimized, query)
}
}

object NestedColumnAliasingSuite {
Expand Down