ueshin commented on a change in pull request #32469:
URL: https://github.com/apache/spark/pull/32469#discussion_r633786522



##########
File path: python/pyspark/pandas/data_type_ops/base.py
##########
@@ -0,0 +1,121 @@
+#
+# 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.
+#
+
+from abc import ABCMeta, abstractmethod
+
+from pandas.api.types import CategoricalDtype
+
+from pyspark.sql.types import (
+    BooleanType,
+    DataType,
+    DateType,
+    DecimalType,
+    DoubleType,
+    FloatType,
+    IntegralType,
+    StringType,
+    TimestampType,
+)
+
+from pyspark.pandas.typedef import Dtype
+
+
+class DataTypeOps(object, metaclass=ABCMeta):
+    """The base class for binary operations of pandas-on-Spark objects (of 
different data types)."""
+
+    def __new__(cls, dtype: Dtype, spark_type: DataType):
+        from pyspark.pandas.data_type_ops.boolean_ops import BooleanOps
+        from pyspark.pandas.data_type_ops.categorical_ops import CategoricalOps
+        from pyspark.pandas.data_type_ops.date_ops import DateOps
+        from pyspark.pandas.data_type_ops.datetime_ops import DatetimeOps
+        from pyspark.pandas.data_type_ops.num_ops import (
+            IntegralOps,
+            FractionalOps,
+        )
+        from pyspark.pandas.data_type_ops.string_ops import StringOps
+
+        if isinstance(dtype, CategoricalDtype):
+            return object.__new__(CategoricalOps)
+        elif (
+                isinstance(spark_type, FloatType)
+                or isinstance(spark_type, DoubleType)
+                or isinstance(spark_type, DecimalType)

Review comment:
       nit: style. 4 spaces indent.

##########
File path: python/pyspark/pandas/data_type_ops/num_ops.py
##########
@@ -0,0 +1,297 @@
+#
+# 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.
+#
+
+import numbers
+
+import numpy as np
+from pandas.api.types import CategoricalDtype
+
+from pyspark.sql import Column, functions as F
+from pyspark.sql.types import (
+    NumericType,
+    StringType,
+    TimestampType,
+)
+
+from pyspark.pandas.base import column_op, IndexOpsMixin, numpy_column_op
+from pyspark.pandas.data_type_ops.base import DataTypeOps
+from pyspark.pandas.spark import functions as SF
+
+
+class NumericOps(DataTypeOps):
+    """
+    The class for binary operations of numeric pandas-on-Spark objects.
+    """
+
+    @property
+    def pretty_name(self):
+        return 'numerics'
+
+    def __add__(self, left, right):
+        if (
+            isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType)
+        ) or isinstance(right, str):
+            raise TypeError("string addition can only be applied to string 
series or literals.")
+
+        if (
+            isinstance(right, IndexOpsMixin)
+            and (
+                isinstance(right.dtype, CategoricalDtype)
+                or (not isinstance(right.spark.data_type, NumericType))
+            )
+        ) and not isinstance(right, numbers.Number):
+            raise TypeError("addition can not be applied to given types.")
+
+        return column_op(Column.__add__)(left, right)
+
+    def __sub__(self, left, right):
+        if (
+            isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType)
+        ) or isinstance(right, str):
+            raise TypeError("subtraction can not be applied to string series 
or literals.")
+
+        if (
+            isinstance(right, IndexOpsMixin)
+            and (
+                isinstance(right.dtype, CategoricalDtype)
+                or (not isinstance(right.spark.data_type, NumericType))
+            )
+        ) and not isinstance(right, numbers.Number):
+            raise TypeError("subtraction can not be applied to given types.")
+
+        return column_op(Column.__sub__)(left, right)
+
+    def __mul__(self, left, right):

Review comment:
       Is this `__mul__` for `FractionalOps`? If so, we should have this in it?

##########
File path: python/pyspark/pandas/data_type_ops/string_ops.py
##########
@@ -0,0 +1,98 @@
+#
+# 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.
+#
+
+from pandas.api.types import CategoricalDtype
+
+from pyspark.sql import functions as F
+from pyspark.sql.types import IntegralType, StringType
+
+from pyspark.pandas.base import column_op, IndexOpsMixin
+from pyspark.pandas.data_type_ops.base import DataTypeOps
+from pyspark.pandas.spark import functions as SF
+
+
+class StringOps(DataTypeOps):
+    """
+    The class for binary operations of pandas-on-Spark objects with spark 
type: StringType.
+    """
+
+    @property
+    def pretty_name(self):
+        return 'strings'
+
+    def __add__(self, left, right):
+        if isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType):
+            return column_op(F.concat)(left, right)
+        elif isinstance(right, str):
+            return column_op(F.concat)(left, F.lit(right))
+        else:
+            raise TypeError("string addition can only be applied to string 
series or literals.")
+
+    def __sub__(self, left, right):
+        raise TypeError("subtraction can not be applied to string series or 
literals.")
+
+    def __mul__(self, left, right):
+        if isinstance(right, str):
+            raise TypeError("multiplication can not be applied to a string 
literal.")
+
+        if (
+                isinstance(right, IndexOpsMixin)
+                and isinstance(right.spark.data_type, IntegralType)
+                and not isinstance(right.dtype, CategoricalDtype)

Review comment:
       nit: style. 4 spaces indent.

##########
File path: python/pyspark/pandas/tests/data_type_ops/test_boolean_ops.py
##########
@@ -0,0 +1,150 @@
+#
+# 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.
+#
+
+import datetime
+import pandas as pd
+
+from pyspark import pandas as ps
+from pyspark.pandas.config import option_context
+from pyspark.pandas.tests.data_type_ops.testing_utils import TestCasesUtils
+from pyspark.testing.pandasutils import PandasOnSparkTestCase
+
+
+class BooleanOpsTest(PandasOnSparkTestCase, TestCasesUtils):

Review comment:
       We have to add these new tests in `dev/sparktestsupport/modules.py`?

##########
File path: python/pyspark/pandas/data_type_ops/base.py
##########
@@ -0,0 +1,121 @@
+#
+# 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.
+#
+
+from abc import ABCMeta, abstractmethod
+
+from pandas.api.types import CategoricalDtype
+
+from pyspark.sql.types import (
+    BooleanType,
+    DataType,
+    DateType,
+    DecimalType,
+    DoubleType,
+    FloatType,
+    IntegralType,
+    StringType,
+    TimestampType,
+)
+
+from pyspark.pandas.typedef import Dtype
+
+
+class DataTypeOps(object, metaclass=ABCMeta):
+    """The base class for binary operations of pandas-on-Spark objects (of 
different data types)."""
+
+    def __new__(cls, dtype: Dtype, spark_type: DataType):
+        from pyspark.pandas.data_type_ops.boolean_ops import BooleanOps
+        from pyspark.pandas.data_type_ops.categorical_ops import CategoricalOps
+        from pyspark.pandas.data_type_ops.date_ops import DateOps
+        from pyspark.pandas.data_type_ops.datetime_ops import DatetimeOps
+        from pyspark.pandas.data_type_ops.num_ops import (
+            IntegralOps,
+            FractionalOps,
+        )
+        from pyspark.pandas.data_type_ops.string_ops import StringOps
+
+        if isinstance(dtype, CategoricalDtype):
+            return object.__new__(CategoricalOps)
+        elif (
+                isinstance(spark_type, FloatType)
+                or isinstance(spark_type, DoubleType)
+                or isinstance(spark_type, DecimalType)

Review comment:
       btw, I guess we can use `FractionalType` instead of those three?

##########
File path: python/pyspark/pandas/data_type_ops/num_ops.py
##########
@@ -0,0 +1,297 @@
+#
+# 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.
+#
+
+import numbers
+
+import numpy as np
+from pandas.api.types import CategoricalDtype
+
+from pyspark.sql import Column, functions as F
+from pyspark.sql.types import (
+    NumericType,
+    StringType,
+    TimestampType,
+)
+
+from pyspark.pandas.base import column_op, IndexOpsMixin, numpy_column_op
+from pyspark.pandas.data_type_ops.base import DataTypeOps
+from pyspark.pandas.spark import functions as SF
+
+
+class NumericOps(DataTypeOps):
+    """
+    The class for binary operations of numeric pandas-on-Spark objects.
+    """
+
+    @property
+    def pretty_name(self):
+        return 'numerics'
+
+    def __add__(self, left, right):
+        if (
+            isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType)
+        ) or isinstance(right, str):
+            raise TypeError("string addition can only be applied to string 
series or literals.")
+
+        if (
+            isinstance(right, IndexOpsMixin)
+            and (
+                isinstance(right.dtype, CategoricalDtype)
+                or (not isinstance(right.spark.data_type, NumericType))
+            )
+        ) and not isinstance(right, numbers.Number):
+            raise TypeError("addition can not be applied to given types.")
+
+        return column_op(Column.__add__)(left, right)
+
+    def __sub__(self, left, right):
+        if (
+            isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType)
+        ) or isinstance(right, str):
+            raise TypeError("subtraction can not be applied to string series 
or literals.")
+
+        if (
+            isinstance(right, IndexOpsMixin)
+            and (
+                isinstance(right.dtype, CategoricalDtype)
+                or (not isinstance(right.spark.data_type, NumericType))
+            )
+        ) and not isinstance(right, numbers.Number):
+            raise TypeError("subtraction can not be applied to given types.")
+
+        return column_op(Column.__sub__)(left, right)
+
+    def __mul__(self, left, right):
+        if isinstance(right, str):
+            raise TypeError("multiplication can not be applied to a string 
literal.")
+
+        if isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, TimestampType):
+            raise TypeError("multiplication can not be applied to date times.")
+
+        if (
+            isinstance(right, IndexOpsMixin)
+            and (
+                isinstance(right.dtype, CategoricalDtype)
+                or not isinstance(right.spark.data_type, NumericType)
+            )
+        ) and not isinstance(right, numbers.Number):
+            raise TypeError("multiplication can not be applied to given 
types.")
+
+        return column_op(Column.__mul__)(left, right)
+
+    def __truediv__(self, left, right):
+        if (
+            isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType)
+        ) or isinstance(right, str):
+            raise TypeError("division can not be applied on string series or 
literals.")
+
+        if (
+            isinstance(right, IndexOpsMixin)
+            and (
+                isinstance(right.dtype, CategoricalDtype)
+                or (not isinstance(right.spark.data_type, NumericType))
+            )
+        ) and not isinstance(right, numbers.Number):
+            raise TypeError("division can not be applied to given types.")
+
+        def truediv(left, right):
+            return F.when(F.lit(right != 0) | F.lit(right).isNull(), 
left.__div__(right)).otherwise(
+                F.when(F.lit(left == np.inf) | F.lit(left == -np.inf), 
left).otherwise(
+                    F.lit(np.inf).__div__(left)
+                )
+            )
+
+        return numpy_column_op(truediv)(left, right)
+
+    def __floordiv__(self, left, right):
+        if (
+            isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType)
+        ) or isinstance(right, str):
+            raise TypeError("division can not be applied on string series or 
literals.")
+
+        if (
+            isinstance(right, IndexOpsMixin)
+            and (
+                isinstance(right.dtype, CategoricalDtype)
+                or (not isinstance(right.spark.data_type, NumericType))
+            )
+        ) and not isinstance(right, numbers.Number):
+            raise TypeError("division can not be applied to given types.")
+
+        def floordiv(left, right):
+            return F.when(F.lit(right is np.nan), np.nan).otherwise(
+                F.when(
+                    F.lit(right != 0) | F.lit(right).isNull(), 
F.floor(left.__div__(right))
+                ).otherwise(
+                    F.when(F.lit(left == np.inf) | F.lit(left == -np.inf), 
left).otherwise(
+                        F.lit(np.inf).__div__(left)
+                    )
+                )
+            )
+
+        return numpy_column_op(floordiv)(left, right)
+
+    def __mod__(self, left, right):
+        if (
+            isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType)
+        ) or isinstance(right, str):
+            raise TypeError("modulo can not be applied on string series or 
literals.")
+
+        if (
+            isinstance(right, IndexOpsMixin)
+            and (
+                isinstance(right.dtype, CategoricalDtype)
+                or (not isinstance(right.spark.data_type, NumericType))
+            )
+        ) and not isinstance(right, numbers.Number):
+            raise TypeError("modulo can not be applied to given types.")
+
+        def mod(left, right):
+            return ((left % right) + right) % right
+
+        return column_op(mod)(left, right)
+
+    def __pow__(self, left, right):
+        if (
+            isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType)
+        ) or isinstance(right, str):
+            raise TypeError("exponentiation can not be applied on string 
series or literals.")
+
+        if (
+            isinstance(right, IndexOpsMixin)
+            and (
+                isinstance(right.dtype, CategoricalDtype)
+                or (not isinstance(right.spark.data_type, NumericType))
+            )
+        ) and not isinstance(right, numbers.Number):
+            raise TypeError("exponentiation can not be applied to given 
types.")
+
+        def pow_func(left, right):
+            return F.when(left == 1, left).otherwise(Column.__pow__(left, 
right))
+
+        return column_op(pow_func)(left, right)
+
+    def __radd__(self, left, right=None):
+        if isinstance(right, str):
+            raise TypeError("string addition can only be applied to string 
series or literals.")
+        if not isinstance(right, numbers.Number):
+            raise TypeError("addition can not be applied to given types.")
+
+        return column_op(Column.__radd__)(left, right)
+
+    def __rsub__(self, left, right=None):
+        if isinstance(right, str):
+            raise TypeError("subtraction can not be applied to string series 
or literals.")
+        if not isinstance(right, numbers.Number):
+            raise TypeError("subtraction can not be applied to given types.")
+        return column_op(Column.__rsub__)(left, right)
+
+    def __rmul__(self, left, right=None):
+        if isinstance(right, str):
+            raise TypeError("multiplication can not be applied to a string 
literal.")
+        if not isinstance(right, numbers.Number):
+            raise TypeError("multiplication can not be applied to given 
types.")
+        return column_op(Column.__rmul__)(left, right)
+
+    def __rtruediv__(self, left, right=None):
+        if isinstance(right, str):
+            raise TypeError("division can not be applied on string series or 
literals.")
+        if not isinstance(right, numbers.Number):
+            raise TypeError("division can not be applied to given types.")
+
+        def rtruediv(left, right):
+            return F.when(left == 0, F.lit(np.inf).__div__(right)).otherwise(
+                F.lit(right).__truediv__(left)
+            )
+
+        return numpy_column_op(rtruediv)(left, right)
+
+    def __rfloordiv__(self, left, right=None):
+        if isinstance(right, str):
+            raise TypeError("division can not be applied on string series or 
literals.")
+        if not isinstance(right, numbers.Number):
+            raise TypeError("division can not be applied to given types.")
+
+        def rfloordiv(left, right):
+            return F.when(F.lit(left == 0), 
F.lit(np.inf).__div__(right)).otherwise(
+                F.when(F.lit(left) == np.nan, 
np.nan).otherwise(F.floor(F.lit(right).__div__(left)))
+            )
+
+        return numpy_column_op(rfloordiv)(left, right)
+
+    def __rpow__(self, left, right=None):
+        if isinstance(right, str):
+            raise TypeError("exponentiation can not be applied on string 
series or literals.")
+        if not isinstance(right, numbers.Number):
+            raise TypeError("exponentiation can not be applied to given 
types.")
+
+        def rpow_func(left, right):
+            return F.when(F.lit(right == 1), 
right).otherwise(Column.__rpow__(left, right))
+
+        return column_op(rpow_func)(left, right)
+
+    def __rmod__(self, left, right=None):
+        if isinstance(right, str):
+            raise TypeError("modulo can not be applied on string series or 
literals.")
+        if not isinstance(right, numbers.Number):
+            raise TypeError("modulo can not be applied to given types.")
+
+        def rmod(left, right):
+            return ((right % left) + left) % left
+
+        return column_op(rmod)(left, right)
+
+
+class IntegralOps(NumericOps):
+    """
+    The class for binary operations of pandas-on-Spark objects with spark 
types:
+    LongType, IntegerType, ByteType and ShortType.
+    """
+
+    @property
+    def pretty_name(self):
+        return 'integrals'
+
+    def __mul__(self, left, right):
+        if isinstance(right, str):
+            raise TypeError("multiplication can not be applied to a string 
literal.")
+
+        if isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, TimestampType):
+            raise TypeError("multiplication can not be applied to date times.")
+
+        if isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType):
+            return column_op(SF.repeat)(right, left)
+
+        if (
+            isinstance(right, IndexOpsMixin)
+            and (
+                isinstance(right.dtype, CategoricalDtype)
+                or not isinstance(right.spark.data_type, NumericType)
+            )
+        ) and not isinstance(right, numbers.Number):
+            raise TypeError("multiplication can not be applied to given 
types.")
+
+        return column_op(Column.__mul__)(left, right)
+
+
+class FractionalOps(NumericOps):
+    """
+    The class for binary operations of pandas-on-Spark objects with spark 
types:
+    FloatType, DoubleType and DecimalType.
+    """
+
+    pass

Review comment:
       This should have `pretty_name`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to