7d0743b493
### Why is this change being proposed? This patch adds support for a new "product" aggregation function in `sql.functions` which multiplies-together all values in an aggregation group. This is likely to be useful in statistical applications which involve combining probabilities, or financial applications that involve combining cumulative interest rates, but is also a versatile mathematical operation of similar status to `sum` or `stddev`. Other users [have noted](https://stackoverflow.com/questions/52991640/cumulative-product-in-spark) the absence of such a function in current releases of Spark. This function is both much more concise than an expression of the form `exp(sum(log(...)))`, and avoids awkward edge-cases associated with some values being zero or negative, as well as being less computationally costly. ### Does this PR introduce _any_ user-facing change? No - only adds new function. ### How was this patch tested? Built-in tests have been added for the new `catalyst.expressions.aggregate.Product` class and its invocation via the (scala) `sql.functions.product` function. The latter, and the PySpark wrapper have also been manually tested in spark-shell and pyspark sessions. The SparkR wrapper is currently untested, and may need separate validation (I'm not an "R" user myself). An illustration of the new functionality, within PySpark is as follows: ``` import pyspark.sql.functions as pf, pyspark.sql.window as pw df = sqlContext.range(1, 17).toDF("x") win = pw.Window.partitionBy(pf.lit(1)).orderBy(pf.col("x")) df.withColumn("factorial", pf.product("x").over(win)).show(20, False) +---+---------------+ |x |factorial | +---+---------------+ |1 |1.0 | |2 |2.0 | |3 |6.0 | |4 |24.0 | |5 |120.0 | |6 |720.0 | |7 |5040.0 | |8 |40320.0 | |9 |362880.0 | |10 |3628800.0 | |11 |3.99168E7 | |12 |4.790016E8 | |13 |6.2270208E9 | |14 |8.71782912E10 | |15 |1.307674368E12 | |16 |2.0922789888E13| +---+---------------+ ``` Closes #30745 from rwpenney/feature/agg-product. Lead-authored-by: Richard Penney <rwp@rwpenney.uk> Co-authored-by: Richard Penney <rwpenney@users.noreply.github.com> Signed-off-by: HyukjinKwon <gurwls223@apache.org>
604 lines
10 KiB
ReStructuredText
604 lines
10 KiB
ReStructuredText
.. 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.
|
|
|
|
|
|
=========
|
|
Spark SQL
|
|
=========
|
|
|
|
Core Classes
|
|
------------
|
|
|
|
.. currentmodule:: pyspark.sql
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
SparkSession
|
|
DataFrame
|
|
Column
|
|
Row
|
|
GroupedData
|
|
PandasCogroupedOps
|
|
DataFrameNaFunctions
|
|
DataFrameStatFunctions
|
|
Window
|
|
|
|
|
|
Spark Session APIs
|
|
------------------
|
|
|
|
.. currentmodule:: pyspark.sql
|
|
|
|
The entry point to programming Spark with the Dataset and DataFrame API.
|
|
To create a Spark session, you should use ``SparkSession.builder`` attribute.
|
|
See also :class:`SparkSession`.
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
SparkSession.builder.appName
|
|
SparkSession.builder.config
|
|
SparkSession.builder.enableHiveSupport
|
|
SparkSession.builder.getOrCreate
|
|
SparkSession.builder.master
|
|
SparkSession.catalog
|
|
SparkSession.conf
|
|
SparkSession.createDataFrame
|
|
SparkSession.getActiveSession
|
|
SparkSession.newSession
|
|
SparkSession.range
|
|
SparkSession.read
|
|
SparkSession.readStream
|
|
SparkSession.sparkContext
|
|
SparkSession.sql
|
|
SparkSession.stop
|
|
SparkSession.streams
|
|
SparkSession.table
|
|
SparkSession.udf
|
|
SparkSession.version
|
|
|
|
|
|
Configuration
|
|
-------------
|
|
|
|
.. currentmodule:: pyspark.sql.conf
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
RuntimeConfig
|
|
|
|
|
|
Input and Output
|
|
----------------
|
|
|
|
.. currentmodule:: pyspark.sql
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
DataFrameReader.csv
|
|
DataFrameReader.format
|
|
DataFrameReader.jdbc
|
|
DataFrameReader.json
|
|
DataFrameReader.load
|
|
DataFrameReader.option
|
|
DataFrameReader.options
|
|
DataFrameReader.orc
|
|
DataFrameReader.parquet
|
|
DataFrameReader.schema
|
|
DataFrameReader.table
|
|
DataFrameWriter.bucketBy
|
|
DataFrameWriter.csv
|
|
DataFrameWriter.format
|
|
DataFrameWriter.insertInto
|
|
DataFrameWriter.jdbc
|
|
DataFrameWriter.json
|
|
DataFrameWriter.mode
|
|
DataFrameWriter.option
|
|
DataFrameWriter.options
|
|
DataFrameWriter.orc
|
|
DataFrameWriter.parquet
|
|
DataFrameWriter.partitionBy
|
|
DataFrameWriter.save
|
|
DataFrameWriter.saveAsTable
|
|
DataFrameWriter.sortBy
|
|
DataFrameWriter.text
|
|
|
|
|
|
DataFrame APIs
|
|
--------------
|
|
|
|
.. currentmodule:: pyspark.sql
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
DataFrame.agg
|
|
DataFrame.alias
|
|
DataFrame.approxQuantile
|
|
DataFrame.cache
|
|
DataFrame.checkpoint
|
|
DataFrame.coalesce
|
|
DataFrame.colRegex
|
|
DataFrame.collect
|
|
DataFrame.columns
|
|
DataFrame.corr
|
|
DataFrame.count
|
|
DataFrame.cov
|
|
DataFrame.createGlobalTempView
|
|
DataFrame.createOrReplaceGlobalTempView
|
|
DataFrame.createOrReplaceTempView
|
|
DataFrame.createTempView
|
|
DataFrame.crossJoin
|
|
DataFrame.crosstab
|
|
DataFrame.cube
|
|
DataFrame.describe
|
|
DataFrame.distinct
|
|
DataFrame.drop
|
|
DataFrame.dropDuplicates
|
|
DataFrame.drop_duplicates
|
|
DataFrame.dropna
|
|
DataFrame.dtypes
|
|
DataFrame.exceptAll
|
|
DataFrame.explain
|
|
DataFrame.fillna
|
|
DataFrame.filter
|
|
DataFrame.first
|
|
DataFrame.foreach
|
|
DataFrame.foreachPartition
|
|
DataFrame.freqItems
|
|
DataFrame.groupBy
|
|
DataFrame.head
|
|
DataFrame.hint
|
|
DataFrame.inputFiles
|
|
DataFrame.intersect
|
|
DataFrame.intersectAll
|
|
DataFrame.isLocal
|
|
DataFrame.isStreaming
|
|
DataFrame.join
|
|
DataFrame.limit
|
|
DataFrame.localCheckpoint
|
|
DataFrame.mapInPandas
|
|
DataFrame.na
|
|
DataFrame.orderBy
|
|
DataFrame.persist
|
|
DataFrame.printSchema
|
|
DataFrame.randomSplit
|
|
DataFrame.rdd
|
|
DataFrame.registerTempTable
|
|
DataFrame.repartition
|
|
DataFrame.repartitionByRange
|
|
DataFrame.replace
|
|
DataFrame.rollup
|
|
DataFrame.sameSemantics
|
|
DataFrame.sample
|
|
DataFrame.sampleBy
|
|
DataFrame.schema
|
|
DataFrame.select
|
|
DataFrame.selectExpr
|
|
DataFrame.semanticHash
|
|
DataFrame.show
|
|
DataFrame.sort
|
|
DataFrame.sortWithinPartitions
|
|
DataFrame.stat
|
|
DataFrame.storageLevel
|
|
DataFrame.subtract
|
|
DataFrame.summary
|
|
DataFrame.tail
|
|
DataFrame.take
|
|
DataFrame.toDF
|
|
DataFrame.toJSON
|
|
DataFrame.toLocalIterator
|
|
DataFrame.toPandas
|
|
DataFrame.transform
|
|
DataFrame.union
|
|
DataFrame.unionAll
|
|
DataFrame.unionByName
|
|
DataFrame.unpersist
|
|
DataFrame.where
|
|
DataFrame.withColumn
|
|
DataFrame.withColumnRenamed
|
|
DataFrame.withWatermark
|
|
DataFrame.write
|
|
DataFrame.writeStream
|
|
DataFrame.writeTo
|
|
DataFrameNaFunctions.drop
|
|
DataFrameNaFunctions.fill
|
|
DataFrameNaFunctions.replace
|
|
DataFrameStatFunctions.approxQuantile
|
|
DataFrameStatFunctions.corr
|
|
DataFrameStatFunctions.cov
|
|
DataFrameStatFunctions.crosstab
|
|
DataFrameStatFunctions.freqItems
|
|
DataFrameStatFunctions.sampleBy
|
|
|
|
Column APIs
|
|
-----------
|
|
|
|
.. currentmodule:: pyspark.sql
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
Column.alias
|
|
Column.asc
|
|
Column.asc_nulls_first
|
|
Column.asc_nulls_last
|
|
Column.astype
|
|
Column.between
|
|
Column.bitwiseAND
|
|
Column.bitwiseOR
|
|
Column.bitwiseXOR
|
|
Column.cast
|
|
Column.contains
|
|
Column.desc
|
|
Column.desc_nulls_first
|
|
Column.desc_nulls_last
|
|
Column.dropFields
|
|
Column.endswith
|
|
Column.eqNullSafe
|
|
Column.getField
|
|
Column.getItem
|
|
Column.isNotNull
|
|
Column.isNull
|
|
Column.isin
|
|
Column.like
|
|
Column.name
|
|
Column.otherwise
|
|
Column.over
|
|
Column.rlike
|
|
Column.startswith
|
|
Column.substr
|
|
Column.when
|
|
Column.withField
|
|
|
|
Data Types
|
|
----------
|
|
|
|
.. currentmodule:: pyspark.sql.types
|
|
|
|
.. autosummary::
|
|
:template: autosummary/class_with_docs.rst
|
|
:toctree: api/
|
|
|
|
ArrayType
|
|
BinaryType
|
|
BooleanType
|
|
ByteType
|
|
DataType
|
|
DateType
|
|
DecimalType
|
|
DoubleType
|
|
FloatType
|
|
IntegerType
|
|
LongType
|
|
MapType
|
|
NullType
|
|
ShortType
|
|
StringType
|
|
StructField
|
|
StructType
|
|
TimestampType
|
|
|
|
|
|
Row
|
|
---
|
|
|
|
.. currentmodule:: pyspark.sql
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
Row.asDict
|
|
|
|
|
|
Functions
|
|
---------
|
|
|
|
.. currentmodule:: pyspark.sql.functions
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
abs
|
|
acos
|
|
acosh
|
|
add_months
|
|
aggregate
|
|
approxCountDistinct
|
|
approx_count_distinct
|
|
array
|
|
array_contains
|
|
array_distinct
|
|
array_except
|
|
array_intersect
|
|
array_join
|
|
array_max
|
|
array_min
|
|
array_position
|
|
array_remove
|
|
array_repeat
|
|
array_sort
|
|
array_union
|
|
arrays_overlap
|
|
arrays_zip
|
|
asc
|
|
asc_nulls_first
|
|
asc_nulls_last
|
|
ascii
|
|
asin
|
|
asinh
|
|
assert_true
|
|
atan
|
|
atanh
|
|
atan2
|
|
avg
|
|
base64
|
|
bin
|
|
bitwise_not
|
|
bitwiseNOT
|
|
broadcast
|
|
bround
|
|
bucket
|
|
cbrt
|
|
ceil
|
|
coalesce
|
|
col
|
|
collect_list
|
|
collect_set
|
|
column
|
|
concat
|
|
concat_ws
|
|
conv
|
|
corr
|
|
cos
|
|
cosh
|
|
count
|
|
count_distinct
|
|
countDistinct
|
|
covar_pop
|
|
covar_samp
|
|
crc32
|
|
create_map
|
|
cume_dist
|
|
current_date
|
|
current_timestamp
|
|
date_add
|
|
date_format
|
|
date_sub
|
|
date_trunc
|
|
datediff
|
|
dayofmonth
|
|
dayofweek
|
|
dayofyear
|
|
days
|
|
decode
|
|
degrees
|
|
dense_rank
|
|
desc
|
|
desc_nulls_first
|
|
desc_nulls_last
|
|
element_at
|
|
encode
|
|
exists
|
|
exp
|
|
explode
|
|
explode_outer
|
|
expm1
|
|
expr
|
|
factorial
|
|
filter
|
|
first
|
|
flatten
|
|
floor
|
|
forall
|
|
format_number
|
|
format_string
|
|
from_csv
|
|
from_json
|
|
from_unixtime
|
|
from_utc_timestamp
|
|
get_json_object
|
|
greatest
|
|
grouping
|
|
grouping_id
|
|
hash
|
|
hex
|
|
hour
|
|
hours
|
|
hypot
|
|
initcap
|
|
input_file_name
|
|
instr
|
|
isnan
|
|
isnull
|
|
json_tuple
|
|
kurtosis
|
|
lag
|
|
last
|
|
last_day
|
|
lead
|
|
least
|
|
length
|
|
levenshtein
|
|
lit
|
|
locate
|
|
log
|
|
log10
|
|
log1p
|
|
log2
|
|
lower
|
|
lpad
|
|
ltrim
|
|
map_concat
|
|
map_entries
|
|
map_filter
|
|
map_from_arrays
|
|
map_from_entries
|
|
map_keys
|
|
map_values
|
|
map_zip_with
|
|
max
|
|
md5
|
|
mean
|
|
min
|
|
minute
|
|
monotonically_increasing_id
|
|
month
|
|
months
|
|
months_between
|
|
nanvl
|
|
next_day
|
|
nth_value
|
|
ntile
|
|
overlay
|
|
pandas_udf
|
|
percent_rank
|
|
percentile_approx
|
|
posexplode
|
|
posexplode_outer
|
|
pow
|
|
product
|
|
quarter
|
|
radians
|
|
raise_error
|
|
rand
|
|
randn
|
|
rank
|
|
regexp_extract
|
|
regexp_replace
|
|
repeat
|
|
reverse
|
|
rint
|
|
round
|
|
row_number
|
|
rpad
|
|
rtrim
|
|
schema_of_csv
|
|
schema_of_json
|
|
second
|
|
sequence
|
|
sha1
|
|
sha2
|
|
shiftleft
|
|
shiftright
|
|
shiftrightunsigned
|
|
shuffle
|
|
signum
|
|
sin
|
|
sinh
|
|
size
|
|
skewness
|
|
slice
|
|
sort_array
|
|
soundex
|
|
spark_partition_id
|
|
split
|
|
sqrt
|
|
stddev
|
|
stddev_pop
|
|
stddev_samp
|
|
struct
|
|
substring
|
|
substring_index
|
|
sum
|
|
sum_distinct
|
|
sumDistinct
|
|
tan
|
|
tanh
|
|
timestamp_seconds
|
|
toDegrees
|
|
toRadians
|
|
to_csv
|
|
to_date
|
|
to_json
|
|
to_timestamp
|
|
to_utc_timestamp
|
|
transform
|
|
transform_keys
|
|
transform_values
|
|
translate
|
|
trim
|
|
trunc
|
|
udf
|
|
unbase64
|
|
unhex
|
|
unix_timestamp
|
|
upper
|
|
var_pop
|
|
var_samp
|
|
variance
|
|
weekofyear
|
|
when
|
|
window
|
|
xxhash64
|
|
year
|
|
years
|
|
zip_with
|
|
|
|
|
|
.. currentmodule:: pyspark.sql.avro.functions
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
from_avro
|
|
to_avro
|
|
|
|
Window
|
|
------
|
|
|
|
.. currentmodule:: pyspark.sql
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
Window.currentRow
|
|
Window.orderBy
|
|
Window.partitionBy
|
|
Window.rangeBetween
|
|
Window.rowsBetween
|
|
Window.unboundedFollowing
|
|
Window.unboundedPreceding
|
|
WindowSpec.orderBy
|
|
WindowSpec.partitionBy
|
|
WindowSpec.rangeBetween
|
|
WindowSpec.rowsBetween
|
|
|
|
Grouping
|
|
--------
|
|
|
|
.. currentmodule:: pyspark.sql
|
|
|
|
.. autosummary::
|
|
:toctree: api/
|
|
|
|
GroupedData.agg
|
|
GroupedData.apply
|
|
GroupedData.applyInPandas
|
|
GroupedData.avg
|
|
GroupedData.cogroup
|
|
GroupedData.count
|
|
GroupedData.max
|
|
GroupedData.mean
|
|
GroupedData.min
|
|
GroupedData.pivot
|
|
GroupedData.sum
|
|
PandasCogroupedOps.applyInPandas
|