aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorq00251598 <qiyadong@huawei.com>2015-03-02 13:16:29 -0800
committerMichael Armbrust <michael@databricks.com>2015-03-02 13:16:29 -0800
commit582e5a24c55e8c876733537c9910001affc8b29b (patch)
treec9be91ac5b541f3b7b58cf8f64aa8035c5fb54df
parent3f9def81170c24f24f4a6b7ca7905de4f75e11e0 (diff)
downloadspark-582e5a24c55e8c876733537c9910001affc8b29b.tar.gz
spark-582e5a24c55e8c876733537c9910001affc8b29b.tar.bz2
spark-582e5a24c55e8c876733537c9910001affc8b29b.zip
[SPARK-6040][SQL] Fix the percent bug in tablesample
HiveQL expression like `select count(1) from src tablesample(1 percent);` means take 1% sample to select. But it means 100% in the current version of the Spark. Author: q00251598 <qiyadong@huawei.com> Closes #4789 from watermen/SPARK-6040 and squashes the following commits: 2453ebe [q00251598] check and adjust the fraction.
-rw-r--r--sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala11
-rw-r--r--sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala1
2 files changed, 11 insertions, 1 deletions
diff --git a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
index 98263f602e..ced99cd082 100644
--- a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
+++ b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveQl.scala
@@ -40,6 +40,7 @@ import org.apache.spark.sql.execution.ExplainCommand
import org.apache.spark.sql.sources.DescribeCommand
import org.apache.spark.sql.hive.execution.{HiveNativeCommand, DropTable, AnalyzeTable, HiveScriptIOSchema}
import org.apache.spark.sql.types._
+import org.apache.spark.util.random.RandomSampler
/* Implicit conversions */
import scala.collection.JavaConversions._
@@ -850,7 +851,15 @@ https://cwiki.apache.org/confluence/display/Hive/Enhanced+Aggregation%2C+Cube%2C
case Token("TOK_TABLESPLITSAMPLE",
Token("TOK_PERCENT", Nil) ::
Token(fraction, Nil) :: Nil) =>
- Sample(fraction.toDouble, withReplacement = false, (math.random * 1000).toInt, relation)
+ // The range of fraction accepted by Sample is [0, 1]. Because Hive's block sampling
+ // function takes X PERCENT as the input and the range of X is [0, 100], we need to
+ // adjust the fraction.
+ require(
+ fraction.toDouble >= (0.0 - RandomSampler.roundingEpsilon)
+ && fraction.toDouble <= (100.0 + RandomSampler.roundingEpsilon),
+ s"Sampling fraction ($fraction) must be on interval [0, 100]")
+ Sample(fraction.toDouble / 100, withReplacement = false, (math.random * 1000).toInt,
+ relation)
case Token("TOK_TABLEBUCKETSAMPLE",
Token(numerator, Nil) ::
Token(denominator, Nil) :: Nil) =>
diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala
index bb0a67dc03..c0d21bc9a8 100644
--- a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala
+++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveQuerySuite.scala
@@ -467,6 +467,7 @@ class HiveQuerySuite extends HiveComparisonTest with BeforeAndAfter {
test("sampling") {
sql("SELECT * FROM src TABLESAMPLE(0.1 PERCENT) s")
+ sql("SELECT * FROM src TABLESAMPLE(100 PERCENT) s")
}
test("DataFrame toString") {