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
10 changes: 5 additions & 5 deletions python/pyspark/sql/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,11 +802,11 @@ def groupBy(self, *cols):
Each element should be a column name (string) or an expression (:class:`Column`).

>>> df.groupBy().avg().collect()
[Row(AVG(age)=3.5)]
[Row(avg(age)=3.5)]
>>> df.groupBy('name').agg({'age': 'mean'}).collect()
[Row(name=u'Alice', AVG(age)=2.0), Row(name=u'Bob', AVG(age)=5.0)]
[Row(name=u'Alice', avg(age)=2.0), Row(name=u'Bob', avg(age)=5.0)]
>>> df.groupBy(df.name).avg().collect()
[Row(name=u'Alice', AVG(age)=2.0), Row(name=u'Bob', AVG(age)=5.0)]
[Row(name=u'Alice', avg(age)=2.0), Row(name=u'Bob', avg(age)=5.0)]
>>> df.groupBy(['name', df.age]).count().collect()
[Row(name=u'Bob', age=5, count=1), Row(name=u'Alice', age=2, count=1)]
"""
Expand Down Expand Up @@ -864,10 +864,10 @@ def agg(self, *exprs):
(shorthand for ``df.groupBy.agg()``).

>>> df.agg({"age": "max"}).collect()
[Row(MAX(age)=5)]
[Row(max(age)=5)]
>>> from pyspark.sql import functions as F
>>> df.agg(F.min(df.age)).collect()
[Row(MIN(age)=2)]
[Row(min(age)=2)]
"""
return self.groupBy().agg(*exprs)

Expand Down
4 changes: 2 additions & 2 deletions python/pyspark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def coalesce(*cols):

>>> cDf.select(coalesce(cDf["a"], cDf["b"])).show()
+-------------+
|Coalesce(a,b)|
|coalesce(a,b)|
+-------------+
| null|
| 1|
Expand All @@ -275,7 +275,7 @@ def coalesce(*cols):

>>> cDf.select('*', coalesce(cDf["a"], lit(0.0))).show()
+----+----+---------------+
| a| b|Coalesce(a,0.0)|
| a| b|coalesce(a,0.0)|
+----+----+---------------+
|null|null| 0.0|
| 1|null| 1.0|
Expand Down
24 changes: 12 additions & 12 deletions python/pyspark/sql/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ def agg(self, *exprs):

>>> gdf = df.groupBy(df.name)
>>> gdf.agg({"*": "count"}).collect()
[Row(name=u'Alice', COUNT(1)=1), Row(name=u'Bob', COUNT(1)=1)]
[Row(name=u'Alice', count(1)=1), Row(name=u'Bob', count(1)=1)]

>>> from pyspark.sql import functions as F
>>> gdf.agg(F.min(df.age)).collect()
[Row(name=u'Alice', MIN(age)=2), Row(name=u'Bob', MIN(age)=5)]
[Row(name=u'Alice', min(age)=2), Row(name=u'Bob', min(age)=5)]
"""
assert exprs, "exprs should not be empty"
if len(exprs) == 1 and isinstance(exprs[0], dict):
Expand Down Expand Up @@ -110,9 +110,9 @@ def mean(self, *cols):
:param cols: list of column names (string). Non-numeric columns are ignored.

>>> df.groupBy().mean('age').collect()
[Row(AVG(age)=3.5)]
[Row(avg(age)=3.5)]
>>> df3.groupBy().mean('age', 'height').collect()
[Row(AVG(age)=3.5, AVG(height)=82.5)]
[Row(avg(age)=3.5, avg(height)=82.5)]
"""

@df_varargs_api
Expand All @@ -125,9 +125,9 @@ def avg(self, *cols):
:param cols: list of column names (string). Non-numeric columns are ignored.

>>> df.groupBy().avg('age').collect()
[Row(AVG(age)=3.5)]
[Row(avg(age)=3.5)]
>>> df3.groupBy().avg('age', 'height').collect()
[Row(AVG(age)=3.5, AVG(height)=82.5)]
[Row(avg(age)=3.5, avg(height)=82.5)]
"""

@df_varargs_api
Expand All @@ -136,9 +136,9 @@ def max(self, *cols):
"""Computes the max value for each numeric columns for each group.

>>> df.groupBy().max('age').collect()
[Row(MAX(age)=5)]
[Row(max(age)=5)]
>>> df3.groupBy().max('age', 'height').collect()
[Row(MAX(age)=5, MAX(height)=85)]
[Row(max(age)=5, max(height)=85)]
"""

@df_varargs_api
Expand All @@ -149,9 +149,9 @@ def min(self, *cols):
:param cols: list of column names (string). Non-numeric columns are ignored.

>>> df.groupBy().min('age').collect()
[Row(MIN(age)=2)]
[Row(min(age)=2)]
>>> df3.groupBy().min('age', 'height').collect()
[Row(MIN(age)=2, MIN(height)=80)]
[Row(min(age)=2, min(height)=80)]
"""

@df_varargs_api
Expand All @@ -162,9 +162,9 @@ def sum(self, *cols):
:param cols: list of column names (string). Non-numeric columns are ignored.

>>> df.groupBy().sum('age').collect()
[Row(SUM(age)=7)]
[Row(sum(age)=7)]
>>> df3.groupBy().sum('age', 'height').collect()
[Row(SUM(age)=7, SUM(height)=165)]
[Row(sum(age)=7, sum(height)=165)]
"""


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ object HiveTypeCoercion {
* Converts string "NaN"s that are in binary operators with a NaN-able types (Float / Double) to
* the appropriate numeric equivalent.
*/
// TODO: remove this rule and make Cast handle Nan.
object ConvertNaNs extends Rule[LogicalPlan] {
private val StringNaN = Literal("NaN")

Expand All @@ -159,19 +160,19 @@ object HiveTypeCoercion {
case e if !e.childrenResolved => e

/* Double Conversions */
case b @ BinaryExpression(StringNaN, right @ DoubleType()) =>
case b @ BinaryOperator(StringNaN, right @ DoubleType()) =>
b.makeCopy(Array(Literal(Double.NaN), right))
case b @ BinaryExpression(left @ DoubleType(), StringNaN) =>
case b @ BinaryOperator(left @ DoubleType(), StringNaN) =>
b.makeCopy(Array(left, Literal(Double.NaN)))

/* Float Conversions */
case b @ BinaryExpression(StringNaN, right @ FloatType()) =>
case b @ BinaryOperator(StringNaN, right @ FloatType()) =>
b.makeCopy(Array(Literal(Float.NaN), right))
case b @ BinaryExpression(left @ FloatType(), StringNaN) =>
case b @ BinaryOperator(left @ FloatType(), StringNaN) =>
b.makeCopy(Array(left, Literal(Float.NaN)))

/* Use float NaN by default to avoid unnecessary type widening */
case b @ BinaryExpression(left @ StringNaN, StringNaN) =>
case b @ BinaryOperator(left @ StringNaN, StringNaN) =>
b.makeCopy(Array(left, Literal(Float.NaN)))
}
}
Expand Down Expand Up @@ -245,12 +246,12 @@ object HiveTypeCoercion {

Union(newLeft, newRight)

// Also widen types for BinaryExpressions.
// Also widen types for BinaryOperator.
case q: LogicalPlan => q transformExpressions {
// Skip nodes who's children have not been resolved yet.
case e if !e.childrenResolved => e

case b @ BinaryExpression(left, right) if left.dataType != right.dataType =>
case b @ BinaryOperator(left, right) if left.dataType != right.dataType =>
findTightestCommonTypeOfTwo(left.dataType, right.dataType).map { widestType =>
val newLeft = if (left.dataType == widestType) left else Cast(left, widestType)
val newRight = if (right.dataType == widestType) right else Cast(right, widestType)
Expand Down Expand Up @@ -478,7 +479,7 @@ object HiveTypeCoercion {

// Promote integers inside a binary expression with fixed-precision decimals to decimals,
// and fixed-precision decimals in an expression with floats / doubles to doubles
case b @ BinaryExpression(left, right) if left.dataType != right.dataType =>
case b @ BinaryOperator(left, right) if left.dataType != right.dataType =>
(left.dataType, right.dataType) match {
case (t, DecimalType.Fixed(p, s)) if intTypeToFixed.contains(t) =>
b.makeCopy(Array(Cast(left, intTypeToFixed(t)), right))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.sql.catalyst.analysis.TypeCheckResult
import org.apache.spark.sql.types.DataType


/**
* An trait that gets mixin to define the expected input types of an expression.
*/
trait ExpectsInputTypes { self: Expression =>

/**
* Expected input types from child expressions. The i-th position in the returned seq indicates
* the type requirement for the i-th child.
*
* The possible values at each position are:
* 1. a specific data type, e.g. LongType, StringType.
* 2. a non-leaf data type, e.g. NumericType, IntegralType, FractionalType.
* 3. a list of specific data types, e.g. Seq(StringType, BinaryType).
*/
def inputTypes: Seq[Any]

override def checkInputDataTypes(): TypeCheckResult = {
// We will do the type checking in `HiveTypeCoercion`, so always returning success here.
TypeCheckResult.TypeCheckSuccess
}
}

/**
* Expressions that require a specific `DataType` as input should implement this trait
* so that the proper type conversions can be performed in the analyzer.
*/
trait AutoCastInputTypes { self: Expression =>

def inputTypes: Seq[DataType]

override def checkInputDataTypes(): TypeCheckResult = {
// We will always do type casting for `AutoCastInputTypes` in `HiveTypeCoercion`,
// so type mismatch error won't be reported here, but for underling `Cast`s.
TypeCheckResult.TypeCheckSuccess
}
}
Loading