Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-40963][SQL] Set nullable correctly in project created by ExtractGenerator #38440

Closed
wants to merge 1 commit into from
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 @@ -2910,7 +2910,7 @@ class Analyzer(override val catalogManager: CatalogManager)
generatorOutput = ResolveGenerate.makeGeneratorOutput(generator, names),
child)

(Some(g), res._2 ++ g.generatorOutput)
(Some(g), res._2 ++ g.nullableOutput)
case other =>
(res._1, res._2 :+ other)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,17 @@ case class Generate(

override def producedAttributes: AttributeSet = AttributeSet(generatorOutput)

def nullableOutput: Seq[Attribute] = {
generatorOutput.map { a =>
a.withNullability(outer || a.nullable)
}
}

def qualifiedGeneratorOutput: Seq[Attribute] = {
val qualifiedOutput = qualifier.map { q =>
qualifier.map { q =>
// prepend the new qualifier to the existed one
generatorOutput.map(a => a.withQualifier(Seq(q)))
}.getOrElse(generatorOutput)
val nullableOutput = qualifiedOutput.map {
// if outer, make all attributes nullable, otherwise keep existing nullability
a => a.withNullability(outer || a.nullable)
}
nullableOutput
nullableOutput.map(a => a.withQualifier(Seq(q)))
}.getOrElse(nullableOutput)
}

def output: Seq[Attribute] = requiredChildOutput ++ qualifiedGeneratorOutput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,25 @@ class GeneratorFunctionSuite extends QueryTest with SharedSparkSession {
testNullStruct
}
}

test("SPARK-40963: generator output has correct nullability") {
// This test does not check nullability directly. Before SPARK-40963,
// the below query got wrong results due to incorrect nullability.
val df = sql(
"""select c1, explode(c4) as c5 from (
| select c1, array(c3) as c4 from (
| select c1, explode_outer(c2) as c3
| from values
| (1, array(1, 2)),
| (2, array(2, 3)),
| (3, null)
| as data(c1, c2)
| )
|)
|""".stripMargin)
checkAnswer(df,
Row(1, 1) :: Row(1, 2) :: Row(2, 2) :: Row(2, 3) :: Row(3, null) :: Nil)
}
}

case class EmptyGenerator() extends Generator with LeafLike[Expression] {
Expand Down