Skip to content
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 @@ -302,7 +302,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
*/
override def visitQuerySpecification(
ctx: QuerySpecificationContext): LogicalPlan = withOrigin(ctx) {
val from = OneRowRelation.optional(ctx.fromClause) {
val from = OneRowRelation().optional(ctx.fromClause) {
visitFromClause(ctx.fromClause)
}
withQuerySpecification(ctx, from)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,13 @@ case class RepartitionByExpression(
/**
* A relation with one row. This is used in "SELECT ..." without a from clause.
*/
case object OneRowRelation extends LeafNode {
case class OneRowRelation() extends LeafNode {
override def maxRows: Option[Long] = Some(1)
override def output: Seq[Attribute] = Nil
override def computeStats(): Statistics = Statistics(sizeInBytes = 1)

/** [[org.apache.spark.sql.catalyst.trees.TreeNode.makeCopy()]] does not support 0-arg ctor. */
override def makeCopy(newArgs: Array[AnyRef]): OneRowRelation = OneRowRelation()
}

/** A logical plan for `dropDuplicates`. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ class AnalysisSuite extends AnalysisTest with ShouldMatchers {
expression: Expression,
expectedDataType: DataType): Unit = {
val afterAnalyze =
Project(Seq(Alias(expression, "a")()), OneRowRelation).analyze.expressions.head
Project(Seq(Alias(expression, "a")()), OneRowRelation()).analyze.expressions.head
if (!afterAnalyze.dataType.equals(expectedDataType)) {
fail(
s"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ trait ExpressionEvalHelper extends GeneratorDrivenPropertyChecks {
expression: Expression,
expected: Any,
inputRow: InternalRow = EmptyRow): Unit = {
val plan = Project(Alias(expression, s"Optimized($expression)")() :: Nil, OneRowRelation)
val plan = Project(Alias(expression, s"Optimized($expression)")() :: Nil, OneRowRelation())
val optimizedPlan = SimpleTestOptimizer.execute(plan)
checkEvaluationWithoutCodegen(optimizedPlan.expressions.head, expected, inputRow)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
private def checkNaNWithOptimization(
expression: Expression,
inputRow: InternalRow = EmptyRow): Unit = {
val plan = Project(Alias(expression, s"Optimized($expression)")() :: Nil, OneRowRelation)
val plan = Project(Alias(expression, s"Optimized($expression)")() :: Nil, OneRowRelation())
val optimizedPlan = SimpleTestOptimizer.execute(plan)
checkNaNWithoutCodegen(optimizedPlan.expressions.head, inputRow)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class ColumnPruningSuite extends PlanTest {
}

test("Eliminate the Project with an empty projectList") {
val input = OneRowRelation
val input = OneRowRelation()
val expected = Project(Literal(1).as("1") :: Nil, input).analyze

val query1 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class CombineConcatsSuite extends PlanTest {
}

protected def assertEquivalent(e1: Expression, e2: Expression): Unit = {
val correctAnswer = Project(Alias(e2, "out")() :: Nil, OneRowRelation).analyze
val actual = Optimize.execute(Project(Alias(e1, "out")() :: Nil, OneRowRelation).analyze)
val correctAnswer = Project(Alias(e2, "out")() :: Nil, OneRowRelation()).analyze
val actual = Optimize.execute(Project(Alias(e1, "out")() :: Nil, OneRowRelation()).analyze)
comparePlans(actual, correctAnswer)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class EliminateSubqueryAliasesSuite extends PlanTest with PredicateHelper {
}

private def assertEquivalent(e1: Expression, e2: Expression): Unit = {
val correctAnswer = Project(Alias(e2, "out")() :: Nil, OneRowRelation).analyze
val actual = Optimize.execute(Project(Alias(e1, "out")() :: Nil, OneRowRelation).analyze)
val correctAnswer = Project(Alias(e2, "out")() :: Nil, OneRowRelation()).analyze
val actual = Optimize.execute(Project(Alias(e1, "out")() :: Nil, OneRowRelation()).analyze)
comparePlans(actual, correctAnswer)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ class FoldablePropagationSuite extends PlanTest {
val testRelation = LocalRelation('a.int, 'b.int)

test("Propagate from subquery") {
val query = OneRowRelation
val query = OneRowRelation()
.select(Literal(1).as('a), Literal(2).as('b))
.subquery('T)
.select('a, 'b)
val optimized = Optimize.execute(query.analyze)
val correctAnswer = OneRowRelation
val correctAnswer = OneRowRelation()
.select(Literal(1).as('a), Literal(2).as('b))
.subquery('T)
.select(Literal(1).as('a), Literal(2).as('b)).analyze
Expand Down Expand Up @@ -152,7 +152,7 @@ class FoldablePropagationSuite extends PlanTest {
val expand = Expand(
Seq(Seq(Literal(null), 'b), Seq('a, Literal(null))),
Seq(a1, a2),
OneRowRelation.select(c1, c2))
OneRowRelation().select(c1, c2))
val query = expand.where(a1.isNotNull).select(a1, a2).analyze
val optimized = Optimize.execute(query)
val correctExpand = expand.copy(projections = Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class OptimizeCodegenSuite extends PlanTest {
}

protected def assertEquivalent(e1: Expression, e2: Expression): Unit = {
val correctAnswer = Project(Alias(e2, "out")() :: Nil, OneRowRelation).analyze
val actual = Optimize.execute(Project(Alias(e1, "out")() :: Nil, OneRowRelation).analyze)
val correctAnswer = Project(Alias(e2, "out")() :: Nil, OneRowRelation()).analyze
val actual = Optimize.execute(Project(Alias(e1, "out")() :: Nil, OneRowRelation()).analyze)
comparePlans(actual, correctAnswer)
}

Expand All @@ -58,13 +58,13 @@ class OptimizeCodegenSuite extends PlanTest {
}

test("Multiple CaseWhen in one operator.") {
val plan = OneRowRelation
val plan = OneRowRelation()
.select(
CaseWhen(Seq((TrueLiteral, Literal(1))), Literal(2)),
CaseWhen(Seq((FalseLiteral, Literal(3))), Literal(4)),
CaseWhen(List.fill(20)((TrueLiteral, Literal(0))), Literal(0)),
CaseWhen(Seq((TrueLiteral, Literal(5))), Literal(6))).analyze
val correctAnswer = OneRowRelation
val correctAnswer = OneRowRelation()
.select(
CaseWhen(Seq((TrueLiteral, Literal(1))), Literal(2)).toCodegen(),
CaseWhen(Seq((FalseLiteral, Literal(3))), Literal(4)).toCodegen(),
Expand All @@ -75,7 +75,7 @@ class OptimizeCodegenSuite extends PlanTest {
}

test("Multiple CaseWhen in different operators") {
val plan = OneRowRelation
val plan = OneRowRelation()
.select(
CaseWhen(Seq((TrueLiteral, Literal(1))), Literal(2)),
CaseWhen(Seq((FalseLiteral, Literal(3))), Literal(4)),
Expand All @@ -85,7 +85,7 @@ class OptimizeCodegenSuite extends PlanTest {
CaseWhen(Seq((TrueLiteral, Literal(5))), Literal(6)),
CaseWhen(List.fill(20)((TrueLiteral, Literal(0))), Literal(0)))
).analyze
val correctAnswer = OneRowRelation
val correctAnswer = OneRowRelation()
.select(
CaseWhen(Seq((TrueLiteral, Literal(1))), Literal(2)).toCodegen(),
CaseWhen(Seq((FalseLiteral, Literal(3))), Literal(4)).toCodegen(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ class SetOperationSuite extends PlanTest {
}

test("Remove unnecessary distincts in multiple unions") {
val query1 = OneRowRelation
val query1 = OneRowRelation()
.select(Literal(1).as('a))
val query2 = OneRowRelation
val query2 = OneRowRelation()
.select(Literal(2).as('b))
val query3 = OneRowRelation
val query3 = OneRowRelation()
.select(Literal(3).as('c))

// D - U - D - U - query1
Expand Down Expand Up @@ -111,13 +111,13 @@ class SetOperationSuite extends PlanTest {
}

test("Keep necessary distincts in multiple unions") {
val query1 = OneRowRelation
val query1 = OneRowRelation()
.select(Literal(1).as('a))
val query2 = OneRowRelation
val query2 = OneRowRelation()
.select(Literal(2).as('b))
val query3 = OneRowRelation
val query3 = OneRowRelation()
.select(Literal(3).as('c))
val query4 = OneRowRelation
val query4 = OneRowRelation()
.select(Literal(4).as('d))

// U - D - U - query1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class SimplifyConditionalSuite extends PlanTest with PredicateHelper {
}

protected def assertEquivalent(e1: Expression, e2: Expression): Unit = {
val correctAnswer = Project(Alias(e2, "out")() :: Nil, OneRowRelation).analyze
val actual = Optimize.execute(Project(Alias(e1, "out")() :: Nil, OneRowRelation).analyze)
val correctAnswer = Project(Alias(e2, "out")() :: Nil, OneRowRelation()).analyze
val actual = Optimize.execute(Project(Alias(e1, "out")() :: Nil, OneRowRelation()).analyze)
comparePlans(actual, correctAnswer)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,28 @@ class PlanParserSuite extends AnalysisTest {
cte(table("cte1").select(star()), "cte1" -> table("a").select(star())))
assertEqual(
"with cte1 (select 1) select * from cte1",
cte(table("cte1").select(star()), "cte1" -> OneRowRelation.select(1)))
cte(table("cte1").select(star()), "cte1" -> OneRowRelation().select(1)))
assertEqual(
"with cte1 (select 1), cte2 as (select * from cte1) select * from cte2",
cte(table("cte2").select(star()),
"cte1" -> OneRowRelation.select(1),
"cte1" -> OneRowRelation().select(1),
"cte2" -> table("cte1").select(star())))
intercept(
"with cte1 (select 1), cte1 as (select 1 from cte1) select * from cte1",
"Found duplicate keys 'cte1'")
}

test("simple select query") {
assertEqual("select 1", OneRowRelation.select(1))
assertEqual("select a, b", OneRowRelation.select('a, 'b))
assertEqual("select 1", OneRowRelation().select(1))
assertEqual("select a, b", OneRowRelation().select('a, 'b))
assertEqual("select a, b from db.c", table("db", "c").select('a, 'b))
assertEqual("select a, b from db.c where x < 1", table("db", "c").where('x < 1).select('a, 'b))
assertEqual(
"select a, b from db.c having x < 1",
table("db", "c").select('a, 'b).where('x < 1))
assertEqual("select distinct a, b from db.c", Distinct(table("db", "c").select('a, 'b)))
assertEqual("select all a, b from db.c", table("db", "c").select('a, 'b))
assertEqual("select from tbl", OneRowRelation.select('from.as("tbl")))
assertEqual("select from tbl", OneRowRelation().select('from.as("tbl")))
}

test("reverse select query") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ trait PlanTest extends SparkFunSuite with PredicateHelper {

/** Fails the test if the two expressions do not match */
protected def compareExpressions(e1: Expression, e2: Expression): Unit = {
comparePlans(Filter(e1, OneRowRelation), Filter(e2, OneRowRelation), checkAnalysis = false)
comparePlans(Filter(e1, OneRowRelation()), Filter(e2, OneRowRelation()), checkAnalysis = false)
}

/** Fails the test if the join order in the two plans do not match */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder(conf) {
codegen = ctx.CODEGEN != null,
cost = ctx.COST != null)
} else {
ExplainCommand(OneRowRelation)
ExplainCommand(OneRowRelation())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
execution.GenerateExec(
generator, join = join, outer = outer, g.qualifiedGeneratorOutput,
planLater(child)) :: Nil
case logical.OneRowRelation =>
case _: logical.OneRowRelation =>
execution.RDDScanExec(Nil, singleRowRdd, "OneRowRelation") :: Nil
case r: logical.Range =>
execution.RangeExec(r) :: Nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ case class InMemoryRelation(
@transient var _cachedColumnBuffers: RDD[CachedBatch] = null,
val batchStats: LongAccumulator = child.sqlContext.sparkContext.longAccumulator)
extends logical.LeafNode with MultiInstanceRelation {

override def innerChildren: Seq[SparkPlan] = Seq(child)

override def producedAttributes: AttributeSet = outputSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ object PreWriteCheck extends (LogicalPlan => Unit) {
case InsertIntoTable(t, _, _, _, _)
if !t.isInstanceOf[LeafNode] ||
t.isInstanceOf[Range] ||
t == OneRowRelation ||
t.isInstanceOf[OneRowRelation] ||
t.isInstanceOf[LocalRelation] =>
failAnalysis(s"Inserting into an RDD-based table is not allowed.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ class DataFrameSuite extends QueryTest with SharedSQLContext {
assert(e2.getMessage.contains("Inserting into an RDD-based table is not allowed."))

// error case: insert into an OneRowRelation
Dataset.ofRows(spark, OneRowRelation).createOrReplaceTempView("one_row")
Dataset.ofRows(spark, OneRowRelation()).createOrReplaceTempView("one_row")
val e3 = intercept[AnalysisException] {
insertion.write.insertInto("one_row")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class QueryExecutionSuite extends SharedSQLContext {
override def apply(plan: LogicalPlan): Seq[SparkPlan] = Nil
})

def qe: QueryExecution = new QueryExecution(spark, OneRowRelation)
def qe: QueryExecution = new QueryExecution(spark, OneRowRelation())

// Nothing!
assert(qe.toString.contains("OneRowRelation"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ private[hive] class TestHiveSparkSession(
// has already set the execution id.
if (sparkContext.getLocalProperty(SQLExecution.EXECUTION_ID_KEY) == null) {
// We don't actually have a `QueryExecution` here, use a fake one instead.
SQLExecution.withNewExecutionId(this, new QueryExecution(this, OneRowRelation)) {
SQLExecution.withNewExecutionId(this, new QueryExecution(this, OneRowRelation())) {
createCmds.foreach(_())
}
} else {
Expand Down