-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-7248] implemented random number generators for DataFrames #5819
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
Changes from 1 commit
6d43895
c5909eb
03637f0
b453716
7d53953
13cad5c
4234c3a
50d69d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * 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.expressions.randfuncs | ||
|
||
|
|
||
| import org.apache.spark.TaskContext | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.types.{DoubleType, DataType} | ||
| import org.apache.spark.util.random.XORShiftRandom | ||
|
|
||
| /** | ||
| * A Random distribution generating expression. | ||
| * TODO: This can be made generic to generate any type of random distribution, or any type of | ||
| * StructType. | ||
| * | ||
| * Since this expression is stateful, it cannot be a case object. | ||
| */ | ||
| private[sql] abstract class RDG(seed: Long) extends LeafExpression with Serializable { | ||
| self: Product => | ||
|
|
||
| /** | ||
| * Record ID within each partition. By being transient, the Random Number Generator is | ||
| * reset every time we serialize and deserialize it. | ||
| */ | ||
| @transient private[this] lazy val rng = new XORShiftRandom(seed + TaskContext.get().partitionId()) | ||
|
|
||
| override type EvaluatedType = Double | ||
|
|
||
| override def nullable: Boolean = false | ||
|
|
||
| override def dataType: DataType = DoubleType | ||
|
|
||
| def generateNumber(random: XORShiftRandom): Double | ||
|
|
||
| override def eval(input: Row): Double = { | ||
| generateNumber(rng) | ||
|
||
| } | ||
| } | ||
|
|
||
| /** Generate a random column with i.i.d. uniformly distributed values in [0, 1). */ | ||
| private[sql] case class Rand(seed: Long) extends RDG(seed) { | ||
|
||
| override def generateNumber(random: XORShiftRandom): Double = random.nextDouble() | ||
| } | ||
|
|
||
| /** Generate a random column with i.i.d. gaussian random distribution. */ | ||
| private[sql] case class Randn(seed: Long) extends RDG(seed) { | ||
| override def generateNumber(random: XORShiftRandom): Double = random.nextGaussian() | ||
| } | ||
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.
this is actually not zero arg is it?
I'm tempted to not rely on this magic function here, since we only have two functions. Then you can also have docstring test inline.
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.
if we add a lot of functions like this in the future, then we can add this.
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.
There was a lot of scoping issues. We couldn't get it to work with Josh when it wasn't in a function of it's own :/
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.
cov()can provide the covariance between all columns for example. The functions might increase pretty soon!