Skip to content

Commit 16b5c90

Browse files
committed
[SPARK-9085][SQL] Remove LeafNode, UnaryNode, BinaryNode from TreeNode.
1 parent b064519 commit 16b5c90

File tree

9 files changed

+50
-55
lines changed

9 files changed

+50
-55
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ case class UnresolvedRelation(
5050
/**
5151
* Holds the name of an attribute that has yet to be resolved.
5252
*/
53-
case class UnresolvedAttribute(nameParts: Seq[String])
54-
extends Attribute with trees.LeafNode[Expression] {
53+
case class UnresolvedAttribute(nameParts: Seq[String]) extends Attribute {
5554

5655
def name: String =
5756
nameParts.map(n => if (n.contains(".")) s"`$n`" else n).mkString(".")
@@ -96,7 +95,7 @@ case class UnresolvedFunction(name: String, children: Seq[Expression]) extends E
9695
* Represents all of the input attributes to a given relational operator, for example in
9796
* "SELECT * FROM ...". A [[Star]] gets automatically expanded during analysis.
9897
*/
99-
trait Star extends NamedExpression with trees.LeafNode[Expression] {
98+
abstract class Star extends LeafExpression with NamedExpression {
10099
self: Product =>
101100

102101
override def name: String = throw new UnresolvedException(this, "name")
@@ -151,7 +150,7 @@ case class UnresolvedStar(table: Option[String]) extends Star {
151150
* @param names the names to be associated with each output of computing [[child]].
152151
*/
153152
case class MultiAlias(child: Expression, names: Seq[String])
154-
extends NamedExpression with trees.UnaryNode[Expression] {
153+
extends UnaryExpression with NamedExpression {
155154

156155
override def name: String = throw new UnresolvedException(this, "name")
157156

@@ -210,8 +209,7 @@ case class UnresolvedExtractValue(child: Expression, extraction: Expression)
210209
/**
211210
* Holds the expression that has yet to be aliased.
212211
*/
213-
case class UnresolvedAlias(child: Expression) extends NamedExpression
214-
with trees.UnaryNode[Expression] {
212+
case class UnresolvedAlias(child: Expression) extends UnaryExpression with NamedExpression {
215213

216214
override def toAttribute: Attribute = throw new UnresolvedException(this, "toAttribute")
217215
override def qualifiers: Seq[String] = throw new UnresolvedException(this, "qualifiers")

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/BoundAttribute.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import org.apache.spark.sql.types._
3030
* the layout of intermediate tuples, BindReferences should be run after all such transformations.
3131
*/
3232
case class BoundReference(ordinal: Int, dataType: DataType, nullable: Boolean)
33-
extends NamedExpression with trees.LeafNode[Expression] {
33+
extends LeafExpression with NamedExpression {
3434

3535
override def toString: String = s"input[$ordinal]"
3636

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/SortOrder.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ case object Descending extends SortDirection
3030
* An expression that can be used to sort a tuple. This class extends expression primarily so that
3131
* transformations over expression will descend into its child.
3232
*/
33-
case class SortOrder(child: Expression, direction: SortDirection) extends Expression
34-
with trees.UnaryNode[Expression] {
33+
case class SortOrder(child: Expression, direction: SortDirection) extends UnaryExpression {
3534

3635
override def dataType: DataType = child.dataType
3736
override def nullable: Boolean = child.nullable

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregates.scala

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import org.apache.spark.sql.catalyst.util.TypeUtils
2727
import org.apache.spark.sql.types._
2828
import org.apache.spark.util.collection.OpenHashSet
2929

30-
abstract class AggregateExpression extends Expression {
30+
trait AggregateExpression extends Expression {
3131
self: Product =>
3232

3333
/**
@@ -60,7 +60,7 @@ case class SplitEvaluation(
6060
* An [[AggregateExpression]] that can be partially computed without seeing all relevant tuples.
6161
* These partial evaluations can then be combined to compute the actual answer.
6262
*/
63-
abstract class PartialAggregate extends AggregateExpression {
63+
trait PartialAggregate extends AggregateExpression {
6464
self: Product =>
6565

6666
/**
@@ -74,7 +74,7 @@ abstract class PartialAggregate extends AggregateExpression {
7474
* [[AggregateExpression]] with an algorithm that will be used to compute one specific result.
7575
*/
7676
abstract class AggregateFunction
77-
extends AggregateExpression with Serializable with trees.LeafNode[Expression] {
77+
extends LeafExpression with AggregateExpression with Serializable {
7878
self: Product =>
7979

8080
/** Base should return the generic aggregate expression that this function is computing */
@@ -91,7 +91,7 @@ abstract class AggregateFunction
9191
}
9292
}
9393

94-
case class Min(child: Expression) extends PartialAggregate with trees.UnaryNode[Expression] {
94+
case class Min(child: Expression) extends UnaryExpression with PartialAggregate {
9595

9696
override def nullable: Boolean = true
9797
override def dataType: DataType = child.dataType
@@ -124,7 +124,7 @@ case class MinFunction(expr: Expression, base: AggregateExpression) extends Aggr
124124
override def eval(input: InternalRow): Any = currentMin.value
125125
}
126126

127-
case class Max(child: Expression) extends PartialAggregate with trees.UnaryNode[Expression] {
127+
case class Max(child: Expression) extends UnaryExpression with PartialAggregate {
128128

129129
override def nullable: Boolean = true
130130
override def dataType: DataType = child.dataType
@@ -157,7 +157,7 @@ case class MaxFunction(expr: Expression, base: AggregateExpression) extends Aggr
157157
override def eval(input: InternalRow): Any = currentMax.value
158158
}
159159

160-
case class Count(child: Expression) extends PartialAggregate with trees.UnaryNode[Expression] {
160+
case class Count(child: Expression) extends UnaryExpression with PartialAggregate {
161161

162162
override def nullable: Boolean = false
163163
override def dataType: LongType.type = LongType
@@ -310,7 +310,7 @@ private[sql] case object HyperLogLogUDT extends UserDefinedType[HyperLogLog] {
310310
}
311311

312312
case class ApproxCountDistinctPartition(child: Expression, relativeSD: Double)
313-
extends AggregateExpression with trees.UnaryNode[Expression] {
313+
extends UnaryExpression with AggregateExpression {
314314

315315
override def nullable: Boolean = false
316316
override def dataType: DataType = HyperLogLogUDT
@@ -340,7 +340,7 @@ case class ApproxCountDistinctPartitionFunction(
340340
}
341341

342342
case class ApproxCountDistinctMerge(child: Expression, relativeSD: Double)
343-
extends AggregateExpression with trees.UnaryNode[Expression] {
343+
extends UnaryExpression with AggregateExpression {
344344

345345
override def nullable: Boolean = false
346346
override def dataType: LongType.type = LongType
@@ -368,7 +368,7 @@ case class ApproxCountDistinctMergeFunction(
368368
}
369369

370370
case class ApproxCountDistinct(child: Expression, relativeSD: Double = 0.05)
371-
extends PartialAggregate with trees.UnaryNode[Expression] {
371+
extends UnaryExpression with PartialAggregate {
372372

373373
override def nullable: Boolean = false
374374
override def dataType: LongType.type = LongType
@@ -386,7 +386,7 @@ case class ApproxCountDistinct(child: Expression, relativeSD: Double = 0.05)
386386
override def newInstance(): CountDistinctFunction = new CountDistinctFunction(child :: Nil, this)
387387
}
388388

389-
case class Average(child: Expression) extends PartialAggregate with trees.UnaryNode[Expression] {
389+
case class Average(child: Expression) extends UnaryExpression with PartialAggregate {
390390

391391
override def prettyName: String = "avg"
392392

@@ -479,7 +479,7 @@ case class AverageFunction(expr: Expression, base: AggregateExpression)
479479
}
480480
}
481481

482-
case class Sum(child: Expression) extends PartialAggregate with trees.UnaryNode[Expression] {
482+
case class Sum(child: Expression) extends UnaryExpression with PartialAggregate {
483483

484484
override def nullable: Boolean = true
485485

@@ -606,8 +606,7 @@ case class CombineSumFunction(expr: Expression, base: AggregateExpression)
606606
}
607607
}
608608

609-
case class SumDistinct(child: Expression)
610-
extends PartialAggregate with trees.UnaryNode[Expression] {
609+
case class SumDistinct(child: Expression) extends UnaryExpression with PartialAggregate {
611610

612611
def this() = this(null)
613612
override def nullable: Boolean = true
@@ -701,7 +700,7 @@ case class CombineSetsAndSumFunction(
701700
}
702701
}
703702

704-
case class First(child: Expression) extends PartialAggregate with trees.UnaryNode[Expression] {
703+
case class First(child: Expression) extends UnaryExpression with PartialAggregate {
705704
override def nullable: Boolean = true
706705
override def dataType: DataType = child.dataType
707706
override def toString: String = s"FIRST($child)"
@@ -729,7 +728,7 @@ case class FirstFunction(expr: Expression, base: AggregateExpression) extends Ag
729728
override def eval(input: InternalRow): Any = result
730729
}
731730

732-
case class Last(child: Expression) extends PartialAggregate with trees.UnaryNode[Expression] {
731+
case class Last(child: Expression) extends UnaryExpression with PartialAggregate {
733732
override def references: AttributeSet = child.references
734733
override def nullable: Boolean = true
735734
override def dataType: DataType = child.dataType

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/generators.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ import org.apache.spark.sql.types._
4040
* requested. The attributes produced by this function will be automatically copied anytime rules
4141
* result in changes to the Generator or its children.
4242
*/
43-
abstract class Generator extends Expression {
44-
self: Product =>
43+
trait Generator extends Expression { self: Product =>
4544

4645
// TODO ideally we should return the type of ArrayType(StructType),
4746
// however, we don't keep the output field names in the Generator.
@@ -99,8 +98,9 @@ case class UserDefinedGenerator(
9998
/**
10099
* Given an input array produces a sequence of rows for each value in the array.
101100
*/
102-
case class Explode(child: Expression)
103-
extends Generator with trees.UnaryNode[Expression] {
101+
case class Explode(child: Expression) extends UnaryExpression with Generator {
102+
103+
override def children: Seq[Expression] = child :: Nil
104104

105105
override def checkInputDataTypes(): TypeCheckResult = {
106106
if (child.dataType.isInstanceOf[ArrayType] || child.dataType.isInstanceOf[MapType]) {

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/namedExpressions.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ object NamedExpression {
3737
*/
3838
case class ExprId(id: Long)
3939

40-
abstract class NamedExpression extends Expression {
41-
self: Product =>
40+
/**
41+
* An [[Expression]] that is named.
42+
*/
43+
trait NamedExpression extends Expression { self: Product =>
4244

4345
def name: String
4446
def exprId: ExprId
@@ -78,8 +80,7 @@ abstract class NamedExpression extends Expression {
7880
}
7981
}
8082

81-
abstract class Attribute extends NamedExpression {
82-
self: Product =>
83+
abstract class Attribute extends LeafExpression with NamedExpression { self: Product =>
8384

8485
override def references: AttributeSet = AttributeSet(this)
8586

@@ -110,7 +111,7 @@ case class Alias(child: Expression, name: String)(
110111
val exprId: ExprId = NamedExpression.newExprId,
111112
val qualifiers: Seq[String] = Nil,
112113
val explicitMetadata: Option[Metadata] = None)
113-
extends NamedExpression with trees.UnaryNode[Expression] {
114+
extends UnaryExpression with NamedExpression {
114115

115116
// Alias(Generator, xx) need to be transformed into Generate(generator, ...)
116117
override lazy val resolved =
@@ -172,7 +173,8 @@ case class AttributeReference(
172173
nullable: Boolean = true,
173174
override val metadata: Metadata = Metadata.empty)(
174175
val exprId: ExprId = NamedExpression.newExprId,
175-
val qualifiers: Seq[String] = Nil) extends Attribute with trees.LeafNode[Expression] {
176+
val qualifiers: Seq[String] = Nil)
177+
extends Attribute {
176178

177179
/**
178180
* Returns true iff the expression id is the same for both attributes.
@@ -242,7 +244,7 @@ case class AttributeReference(
242244
* A place holder used when printing expressions without debugging information such as the
243245
* expression id or the unresolved indicator.
244246
*/
245-
case class PrettyAttribute(name: String) extends Attribute with trees.LeafNode[Expression] {
247+
case class PrettyAttribute(name: String) extends Attribute {
246248

247249
override def toString: String = name
248250

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/LogicalPlan.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,21 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging {
277277
/**
278278
* A logical plan node with no children.
279279
*/
280-
abstract class LeafNode extends LogicalPlan with trees.LeafNode[LogicalPlan] {
280+
abstract class LeafNode extends LogicalPlan {
281281
self: Product =>
282+
283+
override def children: Seq[LogicalPlan] = Nil
282284
}
283285

284286
/**
285287
* A logical plan node with single child.
286288
*/
287-
abstract class UnaryNode extends LogicalPlan with trees.UnaryNode[LogicalPlan] {
289+
abstract class UnaryNode extends LogicalPlan {
288290
self: Product =>
291+
292+
def child: LogicalPlan
293+
294+
override def children: Seq[LogicalPlan] = child :: Nil
289295
}
290296

291297
/**

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -452,19 +452,3 @@ abstract class TreeNode[BaseType <: TreeNode[BaseType]] {
452452
s"$nodeName(${args.mkString(",")})"
453453
}
454454
}
455-
456-
457-
/**
458-
* A [[TreeNode]] with no children.
459-
*/
460-
trait LeafNode[BaseType <: TreeNode[BaseType]] {
461-
def children: Seq[BaseType] = Nil
462-
}
463-
464-
/**
465-
* A [[TreeNode]] with a single [[child]].
466-
*/
467-
trait UnaryNode[BaseType <: TreeNode[BaseType]] {
468-
def child: BaseType
469-
def children: Seq[BaseType] = child :: Nil
470-
}

sql/core/src/main/scala/org/apache/spark/sql/execution/SparkPlan.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,19 @@ abstract class SparkPlan extends QueryPlan[SparkPlan] with Logging with Serializ
238238
}
239239
}
240240

241-
private[sql] trait LeafNode extends SparkPlan with trees.LeafNode[SparkPlan] {
241+
private[sql] trait LeafNode extends SparkPlan {
242242
self: Product =>
243+
244+
override def children: Seq[SparkPlan] = Nil
243245
}
244246

245-
private[sql] trait UnaryNode extends SparkPlan with trees.UnaryNode[SparkPlan] {
247+
private[sql] trait UnaryNode extends SparkPlan {
246248
self: Product =>
249+
250+
def child: SparkPlan
251+
252+
override def children: Seq[SparkPlan] = child :: Nil
253+
247254
override def outputPartitioning: Partitioning = child.outputPartitioning
248255
}
249256

0 commit comments

Comments
 (0)