Github user zsxwing commented on a diff in the pull request:
https://github.com/apache/spark/pull/8573#discussion_r39256156
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/local/SampleNode.scala
---
@@ -0,0 +1,79 @@
+/*
+ * 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.execution.local
+
+import java.util.Random
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.util.random.{BernoulliCellSampler, PoissonSampler}
+
+/**
+ * Sample the dataset.
+ *
+ * @param lowerBound Lower-bound of the sampling probability (usually 0.0)
+ * @param upperBound Upper-bound of the sampling probability. The expected
fraction sampled
+ * will be ub - lb.
+ * @param withReplacement Whether to sample with replacement.
+ * @param seed the random seed
+ * @param child the LocalNode
+ */
+case class SampleNode(
+ lowerBound: Double,
+ upperBound: Double,
+ withReplacement: Boolean,
+ seed: Long,
+ child: LocalNode) extends UnaryLocalNode {
+
+ override def output: Seq[Attribute] = child.output
+
+ private[this] var iterator: Iterator[InternalRow] = _
+
+ private[this] var currentRow: InternalRow = _
+
+ override def open(): Unit = {
+ child.open()
+ val (sampler, _seed) = if (withReplacement) {
+ val random = new Random(seed)
+ // Disable gap sampling since the gap sampling method buffers two
rows internally,
+ // requiring us to copy the row, which is more expensive than the
random number generator.
+ (new PoissonSampler[InternalRow](upperBound - lowerBound,
useGapSamplingIfPossible = false),
+ // Use the seed for partition 0 like PartitionwiseSampledRDD to
generate the same result
+ // of DataFrame
+ random.nextLong())
--- End diff --
[PartitionwiseSampledRDD](https://github.com/apache/spark/blob/c1bc4f439f54625c01a585691e5293cd9961eb0c/core/src/main/scala/org/apache/spark/rdd/PartitionwiseSampledRDD.scala#L57)
doesn't use the provided seed directly, it calls `random.nextLong` to create
`seed` for each partition. Here I want to make `SampleNode` generate the same
result like the first partition of `PartitionwiseSampledRDD`, so I don't use
the provided seed directly.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]