Skip to content

Commit

Permalink
[SPARK-13540][SQL] Supports using nested classes within Scala objects…
Browse files Browse the repository at this point in the history
… as Dataset element type

## What changes were proposed in this pull request?

Nested classes defined within Scala objects are translated into Java static nested classes. Unlike inner classes, they don't need outer scopes. But the analyzer still thinks that an outer scope is required.

This PR fixes this issue simply by checking whether a nested class is static before looking up its outer scope.

## How was this patch tested?

A test case is added to `DatasetSuite`. It checks contents of a Dataset whose element type is a nested class declared in a Scala object.

Author: Cheng Lian <[email protected]>

Closes #11421 from liancheng/spark-13540-object-as-outer-scope.
  • Loading branch information
liancheng committed Feb 29, 2016
1 parent ac5c635 commit 916fc34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

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

import java.lang.reflect.Modifier

import scala.collection.mutable.ArrayBuffer

import org.apache.spark.sql.AnalysisException
Expand Down Expand Up @@ -559,7 +561,13 @@ class Analyzer(
}

resolveExpression(unbound, LocalRelation(attributes), throws = true) transform {
case n: NewInstance if n.outerPointer.isEmpty && n.cls.isMemberClass =>
case n: NewInstance
// If this is an inner class of another class, register the outer object in `OuterScopes`.
// Note that static inner classes (e.g., inner classes within Scala objects) don't need
// outer pointer registration.
if n.outerPointer.isEmpty &&
n.cls.isMemberClass &&
!Modifier.isStatic(n.cls.getModifiers) =>
val outer = OuterScopes.outerScopes.get(n.cls.getDeclaringClass.getName)
if (outer == null) {
throw new AnalysisException(
Expand Down
10 changes: 10 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/DatasetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -621,12 +621,22 @@ class DatasetSuite extends QueryTest with SharedSQLContext {
ds.filter(_ => true),
Some(1), Some(2), Some(3))
}

test("SPARK-13540 Dataset of nested class defined in Scala object") {
checkAnswer(
Seq(OuterObject.InnerClass("foo")).toDS(),
OuterObject.InnerClass("foo"))
}
}

class OuterClass extends Serializable {
case class InnerClass(a: String)
}

object OuterObject {
case class InnerClass(a: String)
}

case class ClassData(a: String, b: Int)
case class ClassData2(c: String, d: Int)
case class ClassNullableData(a: String, b: Integer)
Expand Down

0 comments on commit 916fc34

Please sign in to comment.