-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-19350] [SQL] Cardinality estimation of Limit and Sample #16696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
.../test/scala/org/apache/spark/sql/catalyst/statsEstimation/BasicStatsEstimationSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * 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.statsEstimation | ||
|
|
||
| import org.apache.spark.sql.catalyst.CatalystConf | ||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap, AttributeReference, Literal} | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.types.IntegerType | ||
|
|
||
|
|
||
| class BasicStatsEstimationSuite extends StatsEstimationTestBase { | ||
| val attribute = attr("key") | ||
| val colStat = ColumnStat(distinctCount = 10, min = Some(1), max = Some(10), | ||
| nullCount = 0, avgLen = 4, maxLen = 4) | ||
|
|
||
| val plan = StatsTestPlan( | ||
| outputList = Seq(attribute), | ||
| attributeStats = AttributeMap(Seq(attribute -> colStat)), | ||
| rowCount = 10, | ||
| // row count * (overhead + column size) | ||
| size = Some(10 * (8 + 4))) | ||
|
|
||
| test("limit estimation: limit < child's rowCount") { | ||
| val localLimit = LocalLimit(Literal(2), plan) | ||
| val globalLimit = GlobalLimit(Literal(2), plan) | ||
| // LocalLimit's stats is just its child's stats except column stats | ||
| checkStats(localLimit, plan.stats(conf).copy(attributeStats = AttributeMap(Nil))) | ||
| checkStats(globalLimit, Statistics(sizeInBytes = 24, rowCount = Some(2))) | ||
| } | ||
|
|
||
| test("limit estimation: limit > child's rowCount") { | ||
| val localLimit = LocalLimit(Literal(20), plan) | ||
| val globalLimit = GlobalLimit(Literal(20), plan) | ||
| checkStats(localLimit, plan.stats(conf).copy(attributeStats = AttributeMap(Nil))) | ||
| // Limit is larger than child's rowCount, so GlobalLimit's stats is equal to its child's stats. | ||
| checkStats(globalLimit, plan.stats(conf).copy(attributeStats = AttributeMap(Nil))) | ||
| } | ||
|
|
||
| test("limit estimation: limit = 0") { | ||
| val localLimit = LocalLimit(Literal(0), plan) | ||
| val globalLimit = GlobalLimit(Literal(0), plan) | ||
| val stats = Statistics(sizeInBytes = 1, rowCount = Some(0)) | ||
| checkStats(localLimit, stats) | ||
| checkStats(globalLimit, stats) | ||
| } | ||
|
|
||
| test("sample estimation") { | ||
| val sample = Sample(0.0, 0.5, withReplacement = false, (math.random * 1000).toLong, plan)() | ||
| checkStats(sample, Statistics(sizeInBytes = 60, rowCount = Some(5))) | ||
|
|
||
| // Child doesn't have rowCount in stats | ||
| val childStats = Statistics(sizeInBytes = 120) | ||
| val childPlan = DummyLogicalPlan(childStats, childStats) | ||
| val sample2 = | ||
| Sample(0.0, 0.11, withReplacement = false, (math.random * 1000).toLong, childPlan)() | ||
| checkStats(sample2, Statistics(sizeInBytes = 14)) | ||
| } | ||
|
|
||
| test("estimate statistics when the conf changes") { | ||
| val expectedDefaultStats = | ||
| Statistics( | ||
| sizeInBytes = 40, | ||
| rowCount = Some(10), | ||
| attributeStats = AttributeMap(Seq( | ||
| AttributeReference("c1", IntegerType)() -> ColumnStat(10, Some(1), Some(10), 0, 4, 4))), | ||
| isBroadcastable = false) | ||
| val expectedCboStats = | ||
| Statistics( | ||
| sizeInBytes = 4, | ||
| rowCount = Some(1), | ||
| attributeStats = AttributeMap(Seq( | ||
| AttributeReference("c1", IntegerType)() -> ColumnStat(1, Some(5), Some(5), 0, 4, 4))), | ||
| isBroadcastable = false) | ||
|
|
||
| val plan = DummyLogicalPlan(defaultStats = expectedDefaultStats, cboStats = expectedCboStats) | ||
| checkStats( | ||
| plan, expectedStatsCboOn = expectedCboStats, expectedStatsCboOff = expectedDefaultStats) | ||
| } | ||
|
|
||
| /** Check estimated stats when cbo is turned on/off. */ | ||
| private def checkStats( | ||
| plan: LogicalPlan, | ||
| expectedStatsCboOn: Statistics, | ||
| expectedStatsCboOff: Statistics): Unit = { | ||
| assert(plan.stats(conf.copy(cboEnabled = true)) == expectedStatsCboOn) | ||
| // Invalidate statistics | ||
| plan.invalidateStatsCache() | ||
| assert(plan.stats(conf.copy(cboEnabled = false)) == expectedStatsCboOff) | ||
| } | ||
|
|
||
| /** Check estimated stats when it's the same whether cbo is turned on or off. */ | ||
| private def checkStats(plan: LogicalPlan, expectedStats: Statistics): Unit = | ||
| checkStats(plan, expectedStats, expectedStats) | ||
| } | ||
|
|
||
| /** | ||
| * This class is used for unit-testing the cbo switch, it mimics a logical plan which computes | ||
| * a simple statistics or a cbo estimated statistics based on the conf. | ||
| */ | ||
| private case class DummyLogicalPlan( | ||
| defaultStats: Statistics, | ||
| cboStats: Statistics) extends LogicalPlan { | ||
| override def output: Seq[Attribute] = Nil | ||
| override def children: Seq[LogicalPlan] = Nil | ||
| override def computeStats(conf: CatalystConf): Statistics = | ||
| if (conf.cboEnabled) cboStats else defaultStats | ||
| } |
64 changes: 0 additions & 64 deletions
64
...atalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/StatsConfSuite.scala
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make the stats more accurate, yes, we can use a smaller number between
childStats.rowCountsandlimitasoutputRowCountofgetOutputSize