Skip to content

Commit 92e381b

Browse files
committed
Reduce constructors
1 parent 7617ec3 commit 92e381b

File tree

10 files changed

+39
-29
lines changed

10 files changed

+39
-29
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/dsl/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ package object dsl {
173173
def approxCountDistinct(e: Expression, rsd: Double = 0.05): Expression =
174174
HyperLogLogPlusPlus(e, rsd).toAggregateExpression()
175175
def avg(e: Expression): Expression = Average(e).toAggregateExpression()
176-
def first(e: Expression): Expression = new First(e).toAggregateExpression()
177-
def last(e: Expression): Expression = new Last(e).toAggregateExpression()
176+
def first(e: Expression): Expression = First(e).toAggregateExpression()
177+
def last(e: Expression): Expression = Last(e).toAggregateExpression()
178178
def min(e: Expression): Expression = Min(e).toAggregateExpression()
179179
def minDistinct(e: Expression): Expression = Min(e).toAggregateExpression(isDistinct = true)
180180
def max(e: Expression): Expression = Max(e).toAggregateExpression()

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/First.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ import org.apache.spark.sql.types._
4747
case class First(funcName: String, child: Expression, ignoreNullsExpr: Expression)
4848
extends DeclarativeAggregate with ExpectsInputTypes {
4949

50-
def this(child: Expression, ignoreNullsExpr: Expression) = this("first", child, ignoreNullsExpr)
51-
52-
def this(child: Expression) = this(child, Literal.create(false, BooleanType))
53-
5450
def this(funcName: String, child: Expression) =
5551
this(funcName, child, Literal.create(false, BooleanType))
5652

@@ -122,3 +118,12 @@ case class First(funcName: String, child: Expression, ignoreNullsExpr: Expressio
122118

123119
override def toString: String = s"$prettyName($child)${if (ignoreNulls) " ignore nulls"}"
124120
}
121+
122+
object First {
123+
124+
def apply(child: Expression, ignoreNullsExpr: Expression): First =
125+
First("first", child, ignoreNullsExpr)
126+
127+
def apply(child: Expression): First =
128+
First("first", child)
129+
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Last.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ import org.apache.spark.sql.types._
4747
case class Last(funcName: String, child: Expression, ignoreNullsExpr: Expression)
4848
extends DeclarativeAggregate with ExpectsInputTypes {
4949

50-
def this(child: Expression, ignoreNullsExpr: Expression) = this("last", child, ignoreNullsExpr)
51-
52-
def this(child: Expression) = this(child, Literal.create(false, BooleanType))
53-
5450
def this(funcName: String, child: Expression) =
5551
this(funcName, child, Literal.create(false, BooleanType))
5652

@@ -118,3 +114,12 @@ case class Last(funcName: String, child: Expression, ignoreNullsExpr: Expression
118114

119115
override def toString: String = s"$funcName($child)${if (ignoreNulls) " ignore nulls"}"
120116
}
117+
118+
object Last {
119+
120+
def apply(child: Expression, ignoreNullsExpr: Expression): Last =
121+
Last("last", child, ignoreNullsExpr)
122+
123+
def apply(child: Expression): Last =
124+
Last("last", child)
125+
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ object ReplaceDeduplicateWithAggregate extends Rule[LogicalPlan] {
15391539
if (keyExprIds.contains(attr.exprId)) {
15401540
attr
15411541
} else {
1542-
Alias(new First(attr).toAggregateExpression(), attr.name)(attr.exprId)
1542+
Alias(First(attr).toAggregateExpression(), attr.name)(attr.exprId)
15431543
}
15441544
}
15451545
// SPARK-22951: Physical aggregate operators distinguishes global aggregation and grouping

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDistinctAggregates.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ object RewriteDistinctAggregates extends Rule[LogicalPlan] {
198198

199199
// Select the result of the first aggregate in the last aggregate.
200200
val result = AggregateExpression(
201-
new aggregate.First(evalWithinGroup(regularGroupId, operator.toAttribute), Literal(true)),
201+
aggregate.First(evalWithinGroup(regularGroupId, operator.toAttribute), Literal(true)),
202202
mode = Complete,
203203
isDistinct = false)
204204

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,15 +1510,15 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
15101510
*/
15111511
override def visitFirst(ctx: FirstContext): Expression = withOrigin(ctx) {
15121512
val ignoreNullsExpr = ctx.IGNORE != null
1513-
new First(expression(ctx.expression), Literal(ignoreNullsExpr)).toAggregateExpression()
1513+
First(expression(ctx.expression), Literal(ignoreNullsExpr)).toAggregateExpression()
15141514
}
15151515

15161516
/**
15171517
* Create a [[Last]] expression.
15181518
*/
15191519
override def visitLast(ctx: LastContext): Expression = withOrigin(ctx) {
15201520
val ignoreNullsExpr = ctx.IGNORE != null
1521-
new Last(expression(ctx.expression), Literal(ignoreNullsExpr)).toAggregateExpression()
1521+
Last(expression(ctx.expression), Literal(ignoreNullsExpr)).toAggregateExpression()
15221522
}
15231523

15241524
/**

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/aggregate/LastTestSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import org.apache.spark.sql.types.IntegerType
2323

2424
class LastTestSuite extends SparkFunSuite {
2525
val input = AttributeReference("input", IntegerType, nullable = true)()
26-
val evaluator = DeclarativeAggregateEvaluator(new Last(input, Literal(false)), Seq(input))
26+
val evaluator = DeclarativeAggregateEvaluator(Last(input, Literal(false)), Seq(input))
2727
val evaluatorIgnoreNulls = DeclarativeAggregateEvaluator(
28-
new Last(input, Literal(true)), Seq(input))
28+
Last(input, Literal(true)), Seq(input))
2929

3030
test("empty buffer") {
3131
assert(evaluator.initialize() === InternalRow(null, false))

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/ReplaceOperatorSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class ReplaceOperatorSuite extends PlanTest {
203203
Seq(attrA),
204204
Seq(
205205
attrA,
206-
Alias(new First(attrB).toAggregateExpression(), attrB.name)(attrB.exprId)
206+
Alias(First(attrB).toAggregateExpression(), attrB.name)(attrB.exprId)
207207
),
208208
input)
209209

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ExpressionParserSuite.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -787,25 +787,25 @@ class ExpressionParserSuite extends AnalysisTest {
787787
}
788788

789789
test("SPARK-19526 Support ignore nulls keywords for first and last") {
790-
assertEqual("first(a ignore nulls)", new First('a, Literal(true)).toAggregateExpression())
791-
assertEqual("first(a)", new First('a, Literal(false)).toAggregateExpression())
792-
assertEqual("last(a ignore nulls)", new Last('a, Literal(true)).toAggregateExpression())
793-
assertEqual("last(a)", new Last('a, Literal(false)).toAggregateExpression())
790+
assertEqual("first(a ignore nulls)", First('a, Literal(true)).toAggregateExpression())
791+
assertEqual("first(a)", First('a, Literal(false)).toAggregateExpression())
792+
assertEqual("last(a ignore nulls)", Last('a, Literal(true)).toAggregateExpression())
793+
assertEqual("last(a)", Last('a, Literal(false)).toAggregateExpression())
794794
}
795795

796796
test("Support respect nulls keywords for first_value and last_value") {
797797
assertEqual("first_value(a ignore nulls)",
798-
new First('a, Literal(true)).toAggregateExpression())
798+
First('a, Literal(true)).toAggregateExpression())
799799
assertEqual("first_value(a respect nulls)",
800-
new First('a, Literal(false)).toAggregateExpression())
800+
First('a, Literal(false)).toAggregateExpression())
801801
assertEqual("first_value(a)",
802-
new First('a, Literal(false)).toAggregateExpression())
802+
First('a, Literal(false)).toAggregateExpression())
803803
assertEqual("last_value(a ignore nulls)",
804-
new Last('a, Literal(true)).toAggregateExpression())
804+
Last('a, Literal(true)).toAggregateExpression())
805805
assertEqual("last_value(a respect nulls)",
806-
new Last('a, Literal(false)).toAggregateExpression())
806+
Last('a, Literal(false)).toAggregateExpression())
807807
assertEqual("last_value(a)",
808-
new Last('a, Literal(false)).toAggregateExpression())
808+
Last('a, Literal(false)).toAggregateExpression())
809809
}
810810

811811
test("timestamp literals") {

sql/core/src/main/scala/org/apache/spark/sql/functions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ object functions {
431431
* @since 2.0.0
432432
*/
433433
def first(e: Column, ignoreNulls: Boolean): Column = withAggregateFunction {
434-
new First(e.expr, Literal(ignoreNulls))
434+
First(e.expr, Literal(ignoreNulls))
435435
}
436436

437437
/**
@@ -556,7 +556,7 @@ object functions {
556556
* @since 2.0.0
557557
*/
558558
def last(e: Column, ignoreNulls: Boolean): Column = withAggregateFunction {
559-
new Last(e.expr, Literal(ignoreNulls))
559+
Last(e.expr, Literal(ignoreNulls))
560560
}
561561

562562
/**

0 commit comments

Comments
 (0)