You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "Michael H (JIRA)" <ji...@apache.org> on 2017/11/28 12:04:00 UTC

[jira] [Updated] (SPARK-22629) handling of random in UDFs

     [ https://issues.apache.org/jira/browse/SPARK-22629?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Michael H updated SPARK-22629:
------------------------------
    Description: 
{{df_br = spark.createDataFrame([{'name': 'hello'}])

# create a random int
udf_random_col =  udf(lambda: int(100*random.random()), IntegerType())

# add ten
random.seed(1234)
udf_add_ten =  udf(lambda rand: rand + 10, IntegerType())

# add a column with a random value to that DF
df_br = df_br.withColumn('RAND', udf_random_col())
df_br.show()

+-----+----+
| name|RAND|
+-----+----+
|hello|  68|
+-----+----+

# unexpected result due to re-evaluation
df_br.withColumn('RAND_PLUS_TEN', udf_add_ten('RAND')).show()

+-----+----+-------------+
| name|RAND|RAND_PLUS_TEN|
+-----+----+-------------+
|hello|  72|           87|
+-----+----+-------------+

# workaround: cache the resulst after using the random number generating udf
df_br.withColumn('RAND', udf_random_col()).cache().withColumn('RAND_PLUS_TEN', udf_add_ten('RAND')).show()

+-----+----+-------------+
| name|RAND|RAND_PLUS_TEN|
+-----+----+-------------+
|hello|  68|           78|
+-----+----+-------------+
}}

  was:
df_br = spark.createDataFrame([{'name': 'hello'}])

# create a random int
udf_random_col =  udf(lambda: int(100*random.random()), IntegerType())

# add ten
random.seed(1234)
udf_add_ten =  udf(lambda rand: rand + 10, IntegerType())

# add a column with a random value to that DF
df_br = df_br.withColumn('RAND', udf_random_col())
df_br.show()

+-----+----+
| name|RAND|
+-----+----+
|hello|  68|
+-----+----+

# unexpected result due to re-evaluation
df_br.withColumn('RAND_PLUS_TEN', udf_add_ten('RAND')).show()

+-----+----+-------------+
| name|RAND|RAND_PLUS_TEN|
+-----+----+-------------+
|hello|  72|           87|
+-----+----+-------------+

# workaround: cache the resulst after using the random number generating udf
df_br.withColumn('RAND', udf_random_col()).cache().withColumn('RAND_PLUS_TEN', udf_add_ten('RAND')).show()

+-----+----+-------------+
| name|RAND|RAND_PLUS_TEN|
+-----+----+-------------+
|hello|  68|           78|
+-----+----+-------------+



> handling of random in UDFs
> --------------------------
>
>                 Key: SPARK-22629
>                 URL: https://issues.apache.org/jira/browse/SPARK-22629
>             Project: Spark
>          Issue Type: Bug
>          Components: PySpark
>    Affects Versions: 2.1.0
>            Reporter: Michael H
>
> {{df_br = spark.createDataFrame([{'name': 'hello'}])
> # create a random int
> udf_random_col =  udf(lambda: int(100*random.random()), IntegerType())
> # add ten
> random.seed(1234)
> udf_add_ten =  udf(lambda rand: rand + 10, IntegerType())
> # add a column with a random value to that DF
> df_br = df_br.withColumn('RAND', udf_random_col())
> df_br.show()
> +-----+----+
> | name|RAND|
> +-----+----+
> |hello|  68|
> +-----+----+
> # unexpected result due to re-evaluation
> df_br.withColumn('RAND_PLUS_TEN', udf_add_ten('RAND')).show()
> +-----+----+-------------+
> | name|RAND|RAND_PLUS_TEN|
> +-----+----+-------------+
> |hello|  72|           87|
> +-----+----+-------------+
> # workaround: cache the resulst after using the random number generating udf
> df_br.withColumn('RAND', udf_random_col()).cache().withColumn('RAND_PLUS_TEN', udf_add_ten('RAND')).show()
> +-----+----+-------------+
> | name|RAND|RAND_PLUS_TEN|
> +-----+----+-------------+
> |hello|  68|           78|
> +-----+----+-------------+
> }}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org