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
25 changes: 24 additions & 1 deletion python/pyspark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,37 @@ def _(col):
'sumDistinct': 'Aggregate function: returns the sum of distinct values in the expression.',
}


for _name, _doc in _functions.items():
globals()[_name] = _create_function(_name, _doc)
del _name, _doc
__all__ += _functions.keys()
__all__.sort()


def rand(seed=None):
"""
Generate a random column with i.i.d. samples from U[0.0, 1.0].
"""
sc = SparkContext._active_spark_context
if seed:
jc = sc._jvm.functions.rand(seed)
else:
jc = sc._jvm.functions.rand()
return Column(jc)


def randn(seed=None):
"""
Generate a column with i.i.d. samples from the standard normal distribution.
"""
sc = SparkContext._active_spark_context
if seed:
jc = sc._jvm.functions.randn(seed)
else:
jc = sc._jvm.functions.randn()
return Column(jc)


def approxCountDistinct(col, rsd=None):
"""Returns a new :class:`Column` for approximate distinct count of ``col``.

Expand Down
10 changes: 10 additions & 0 deletions python/pyspark/sql/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ def assert_close(a, b):
assert_close([math.hypot(i, 2 * i) for i in range(10)],
df.select(functions.hypot(df.a, df.b)).collect())

def test_rand_functions(self):
df = self.df
from pyspark.sql import functions
rnd = df.select('key', functions.rand()).collect()
for row in rnd:
assert row[1] >= 0.0 and row[1] <= 1.0, "got: %s" % row[1]
rndn = df.select('key', functions.randn(5)).collect()
for row in rndn:
assert row[1] >= -4.0 and row[1] <= 4.0, "got: %s" % row[1]

def test_save_and_load(self):
df = self.df
tmpPath = tempfile.mkdtemp()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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

import org.apache.spark.TaskContext
import org.apache.spark.sql.types.{DataType, DoubleType}
import org.apache.spark.util.Utils
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.
*/
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 protected lazy val rng = new XORShiftRandom(seed + TaskContext.get().partitionId())

override type EvaluatedType = Double

override def nullable: Boolean = false

override def dataType: DataType = DoubleType
}

/** Generate a random column with i.i.d. uniformly distributed values in [0, 1). */
case class Rand(seed: Long = Utils.random.nextLong()) extends RDG(seed) {
override def eval(input: Row): Double = rng.nextDouble()
}

/** Generate a random column with i.i.d. gaussian random distribution. */
case class Randn(seed: Long = Utils.random.nextLong()) extends RDG(seed) {
override def eval(input: Row): Double = rng.nextGaussian()
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ class ConstantFoldingSuite extends PlanTest {
val originalQuery =
testRelation
.select(
Rand + Literal(1) as Symbol("c1"),
Rand(5L) + Literal(1) as Symbol("c1"),
Sum('a) as Symbol("c2"))

val optimized = Optimize.execute(originalQuery.analyze)

val correctAnswer =
testRelation
.select(
Rand + Literal(1.0) as Symbol("c1"),
Rand(5L) + Literal(1.0) as Symbol("c1"),
Sum('a) as Symbol("c2"))
.analyze

Expand Down
30 changes: 29 additions & 1 deletion sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.apache.spark.sql.catalyst.ScalaReflection
import org.apache.spark.sql.catalyst.analysis.{UnresolvedFunction, Star}
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.types._

import org.apache.spark.util.Utils

/**
* :: Experimental ::
Expand Down Expand Up @@ -346,6 +346,34 @@ object functions {
*/
def not(e: Column): Column = !e

/**
* Generate a random column with i.i.d. samples from U[0.0, 1.0].
*
* @group normal_funcs
*/
def rand(seed: Long): Column = Rand(seed)

/**
* Generate a random column with i.i.d. samples from U[0.0, 1.0].
*
* @group normal_funcs
*/
def rand(): Column = rand(Utils.random.nextLong)

/**
* Generate a column with i.i.d. samples from the standard normal distribution.
*
* @group normal_funcs
*/
def randn(seed: Long): Column = Randn(seed)

/**
* Generate a column with i.i.d. samples from the standard normal distribution.
*
* @group normal_funcs
*/
def randn(): Column = randn(Utils.random.nextLong)

/**
* Partition ID of the Spark task.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import org.apache.spark.sql.functions.lit
/**
* :: Experimental ::
* Mathematical Functions available for [[DataFrame]].
*
* @groupname double_funcs Functions that require DoubleType as an input
*/
@Experimental
// scalastyle:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public void testVarargMethods() {
df2.select(pow("a", "a"), pow("b", 2.0));
df2.select(pow(col("a"), col("b")), exp("b"));
df2.select(sin("a"), acos("b"));

df2.select(rand(), acos("b"));
df2.select(col("*"), randn(5L));
}

@Ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.spark.sql

import org.scalatest.Matchers._

import org.apache.spark.sql.functions._
import org.apache.spark.sql.test.TestSQLContext
import org.apache.spark.sql.test.TestSQLContext.implicits._
Expand Down Expand Up @@ -349,4 +351,24 @@ class ColumnExpressionSuite extends QueryTest {
assert(schema("value").metadata === Metadata.empty)
assert(schema("abc").metadata === metadata)
}

test("rand") {
val randCol = testData.select('key, rand(5L).as("rand"))
randCol.columns.length should be (2)
val rows = randCol.collect()
rows.foreach { row =>
assert(row.getDouble(1) <= 1.0)
assert(row.getDouble(1) >= 0.0)
}
}

test("randn") {
val randCol = testData.select('key, randn(5L).as("rand"))
randCol.columns.length should be (2)
val rows = randCol.collect()
rows.foreach { row =>
assert(row.getDouble(1) <= 4.0)
assert(row.getDouble(1) >= -4.0)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ package org.apache.spark.sql.hive

import java.sql.Date


import org.apache.hadoop.hive.ql.exec.{FunctionRegistry, FunctionInfo}

import scala.collection.mutable.ArrayBuffer

import org.apache.hadoop.hive.conf.HiveConf
import org.apache.hadoop.hive.ql.Context
import org.apache.hadoop.hive.ql.exec.{FunctionRegistry, FunctionInfo}
import org.apache.hadoop.hive.ql.lib.Node
import org.apache.hadoop.hive.ql.metadata.Table
import org.apache.hadoop.hive.ql.parse._
Expand Down Expand Up @@ -1244,7 +1242,8 @@ https://cwiki.apache.org/confluence/display/Hive/Enhanced+Aggregation%2C+Cube%2C
/* Other functions */
case Token("TOK_FUNCTION", Token(ARRAY(), Nil) :: children) =>
CreateArray(children.map(nodeToExpr))
case Token("TOK_FUNCTION", Token(RAND(), Nil) :: Nil) => Rand
case Token("TOK_FUNCTION", Token(RAND(), Nil) :: Nil) => Rand()
case Token("TOK_FUNCTION", Token(RAND(), Nil) :: seed :: Nil) => Rand(seed.toString.toLong)
case Token("TOK_FUNCTION", Token(SUBSTR(), Nil) :: string :: pos :: Nil) =>
Substring(nodeToExpr(string), nodeToExpr(pos), Literal.create(Integer.MAX_VALUE, IntegerType))
case Token("TOK_FUNCTION", Token(SUBSTR(), Nil) :: string :: pos :: length :: Nil) =>
Expand Down