Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -2659,32 +2659,31 @@ class Analyzer(override val catalogManager: CatalogManager)
p

case p @ Project(projectList, child) =>
// Holds the resolved generator, if one exists in the project list.
var resolvedGenerator: Generate = null

val newProjectList = projectList
val (resolvedGenerator, newProjectList) = projectList
.map(trimNonTopLevelAliases)
.flatMap {
case AliasedGenerator(generator, names, outer) if generator.childrenResolved =>
// It's a sanity check, this should not happen as the previous case will throw
// exception earlier.
assert(resolvedGenerator == null, "More than one generator found in SELECT.")

resolvedGenerator =
Generate(
.foldLeft((None: Option[Generate], Nil: Seq[NamedExpression])) { (res, e) =>
e match {
case AliasedGenerator(generator, names, outer) if generator.childrenResolved =>
// It's a sanity check, this should not happen as the previous case will throw
// exception earlier.
assert(res._1.isEmpty, "More than one generator found in SELECT.")

val g = Generate(
generator,
unrequiredChildIndex = Nil,
outer = outer,
qualifier = None,
generatorOutput = ResolveGenerate.makeGeneratorOutput(generator, names),
child)

resolvedGenerator.generatorOutput
case other => other :: Nil
(Some(g), res._2 ++ g.generatorOutput)
case other =>
(res._1, res._2 :+ other)
}
}

if (resolvedGenerator != null) {
Project(newProjectList, resolvedGenerator)
if (resolvedGenerator.isDefined) {
Project(newProjectList, resolvedGenerator.get)
} else {
p
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.catalyst.analysis

import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.types._

class ExtractGeneratorSuite extends AnalysisTest {

test("SPARK-34141: ExtractGenerator with lazy project list") {
val b = AttributeReference("b", ArrayType(StringType))()

val columns = AttributeReference("a", StringType)() :: b :: Nil
val explode = Alias(Explode(b), "c")()

// view is a lazy seq
val rel = LocalRelation(output = columns.view)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for end users to get stuck on this issue? If possible, could you add end-2-end tests, too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hit this issue while using spark in java. The step before was an join, where I used JavaConverters.collectionAsScalaIterable(Arrays.asList(columns)).toSeq() as the join condition. The JavaConverters helper returns an lazy collection.
I can take a look at on how to e2e test this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

val plan = Project(rel.output ++ (explode :: Nil), rel)

assertAnalysisSuccess(plan)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.catalyst.analysis

import scala.collection.immutable.LazyList

import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.types._

class ExtractGeneratorSuite extends AnalysisTest {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add comments in each test file noting that there is a parallel one in the other source tree, so people realize that both need to change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you'll add the comments I'll merge it @tanelk


test("SPARK-34141: ExtractGenerator with lazy project list") {
val b = AttributeReference("b", ArrayType(StringType))()

val columns = AttributeReference("a", StringType)() #:: b #:: LazyList.empty
val explode = Alias(Explode(b), "c")()

val rel = LocalRelation(output = columns)
val plan = Project(rel.output ++ (explode :: Nil), rel)

assertAnalysisSuccess(plan)
}
}