[GitHub] [beam] robertwb commented on a change in pull request #11766: [BEAM-10036] More flexible dataframes partitioning.

2020-06-04 Thread GitBox


robertwb commented on a change in pull request #11766:
URL: https://github.com/apache/beam/pull/11766#discussion_r435632933



##
File path: sdks/python/apache_beam/dataframe/expressions.py
##
@@ -85,16 +87,10 @@ def evaluate_at(self, session):  # type: (Session) -> T
 """Returns the result of self with the bindings given in session."""
 raise NotImplementedError(type(self))
 
-  def requires_partition_by_index(self):  # type: () -> bool
-"""Whether this expression requires its argument(s) to be partitioned
-by index."""
-# TODO: It might be necessary to support partitioning by part of the index,
-# for some args, which would require returning more than a boolean here.
+  def requires_partition_by(self):  # type: () -> Partitioning
 raise NotImplementedError(type(self))
 
-  def preserves_partition_by_index(self):  # type: () -> bool
-"""Whether the result of this expression will be partitioned by index
-whenever all of its inputs are partitioned by index."""
+  def preserves_partition_by(self):  # type: () -> Partitioning

Review comment:
   Docstring comments added. 

##
File path: sdks/python/apache_beam/dataframe/partitionings.py
##
@@ -0,0 +1,136 @@
+#
+# 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 __future__ import absolute_import
+
+from typing import Any
+from typing import Iterable
+from typing import TypeVar
+
+import pandas as pd
+
+Frame = TypeVar('Frame', bound=pd.core.generic.NDFrame)
+
+
+class Partitioning(object):
+  """A class representing a (consistent) partitioning of dataframe objects.
+  """
+  def is_subpartitioning_of(self, other):
+# type: (Partitioning) -> bool
+
+"""Returns whether self is a sub-partition of other.
+
+Specifically, returns whether something partitioned by self is necissarily
+also partitioned by other.
+"""
+raise NotImplementedError
+
+  def partition_fn(self, df):
+# type: (Frame) -> Iterable[Tuple[Any, Frame]]
+
+"""A callable that actually performs the partitioning of a Frame df.
+
+This will be invoked via a FlatMap in conjunction with a GroupKey to
+achieve the desired partitioning.
+"""
+raise NotImplementedError
+
+
+class Index(Partitioning):
+  """A partitioning by index (either fully or partially).
+
+  If the set of "levels" of the index to consider is not specified, the entire
+  index is used.
+
+  These form a partial order, given by
+
+  Nothing() < Index([i]) < Index([i, j]) < ... < Index() < Singleton()
+
+  The ordering is implemented via the is_subpartitioning_of method, where the
+  examples on the right are subpartitionings of the examples on the left above.
+  """
+
+  _INDEX_PARTITIONS = 10
+
+  def __init__(self, levels=None):
+self._levels = levels
+
+  def __eq__(self, other):
+return type(self) == type(other) and self._levels == other._levels
+
+  def __hash__(self):
+if self._levels:
+  return hash(tuple(sorted(self._levels)))
+else:
+  return hash(type(self))
+
+  def is_subpartitioning_of(self, other):
+if isinstance(other, Nothing):
+  return True
+elif isinstance(other, Index):
+  if self._levels is None:
+return True
+  elif other._levels is None:
+return False
+  else:
+return all(level in other._levels for level in self._levels)
+else:
+  return False
+
+  def partition_fn(self, df):
+if self._levels is None:
+  levels = list(range(df.index.nlevels))
+else:
+  levels = self._levels
+hashes = sum(
+pd.util.hash_array(df.index.get_level_values(level))
+for level in levels)
+for key in range(self._INDEX_PARTITIONS):
+  yield key, df[hashes % self._INDEX_PARTITIONS == key]
+
+
+class Singleton(Partitioning):
+  """A partitioning co-locating all data to a singleton partition.

Review comment:
   Reworded. 

##
File path: sdks/python/apache_beam/dataframe/partitionings.py
##
@@ -0,0 +1,136 @@
+#
+# 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 

[GitHub] [beam] robertwb commented on a change in pull request #11766: [BEAM-10036] More flexible dataframes partitioning.

2020-06-04 Thread GitBox


robertwb commented on a change in pull request #11766:
URL: https://github.com/apache/beam/pull/11766#discussion_r435373732



##
File path: sdks/python/apache_beam/dataframe/expressions.py
##
@@ -85,16 +87,10 @@ def evaluate_at(self, session):  # type: (Session) -> T
 """Returns the result of self with the bindings given in session."""
 raise NotImplementedError(type(self))
 
-  def requires_partition_by_index(self):  # type: () -> bool
-"""Whether this expression requires its argument(s) to be partitioned
-by index."""
-# TODO: It might be necessary to support partitioning by part of the index,
-# for some args, which would require returning more than a boolean here.
+  def requires_partition_by(self):  # type: () -> Partitioning
 raise NotImplementedError(type(self))
 
-  def preserves_partition_by_index(self):  # type: () -> bool
-"""Whether the result of this expression will be partitioned by index
-whenever all of its inputs are partitioned by index."""
+  def preserves_partition_by(self):  # type: () -> Partitioning

Review comment:
   There are operations, such as setting a column to be an additional level 
of the index, that would do partial preservation. But perhaps that's not worth 
the additional complexity. I can change this to a boolean if you'd rather. 





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:
us...@infra.apache.org




[GitHub] [beam] robertwb commented on a change in pull request #11766: [BEAM-10036] More flexible dataframes partitioning.

2020-06-02 Thread GitBox


robertwb commented on a change in pull request #11766:
URL: https://github.com/apache/beam/pull/11766#discussion_r434217321



##
File path: sdks/python/apache_beam/dataframe/partitionings.py
##
@@ -0,0 +1,133 @@
+#
+# 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 __future__ import absolute_import
+
+from typing import Any
+from typing import Iterable
+from typing import TypeVar
+
+import pandas as pd
+
+Frame = TypeVar('Frame', bound=pd.core.generic.NDFrame)
+
+
+class Partitioning(object):
+  """A class representing a (consistent) partitioning of dataframe objects.
+  """
+  def is_subpartition_of(self, other):

Review comment:
   Done.





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:
us...@infra.apache.org




[GitHub] [beam] robertwb commented on a change in pull request #11766: [BEAM-10036] More flexible dataframes partitioning.

2020-06-02 Thread GitBox


robertwb commented on a change in pull request #11766:
URL: https://github.com/apache/beam/pull/11766#discussion_r434215127



##
File path: sdks/python/apache_beam/dataframe/expressions.py
##
@@ -85,16 +87,10 @@ def evaluate_at(self, session):  # type: (Session) -> T
 """Returns the result of self with the bindings given in session."""
 raise NotImplementedError(type(self))
 
-  def requires_partition_by_index(self):  # type: () -> bool
-"""Whether this expression requires its argument(s) to be partitioned
-by index."""
-# TODO: It might be necessary to support partitioning by part of the index,
-# for some args, which would require returning more than a boolean here.
+  def requires_partition_by(self):  # type: () -> Partitioning
 raise NotImplementedError(type(self))
 
-  def preserves_partition_by_index(self):  # type: () -> bool
-"""Whether the result of this expression will be partitioned by index
-whenever all of its inputs are partitioned by index."""
+  def preserves_partition_by(self):  # type: () -> Partitioning

Review comment:
   Yes, it's a function of both the input and the operation. E.g. an 
elementwise operation preserves all existing partitioning, but does not 
guarantee any. 

##
File path: sdks/python/apache_beam/dataframe/partitionings.py
##
@@ -0,0 +1,133 @@
+#
+# 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 __future__ import absolute_import
+
+from typing import Any
+from typing import Iterable
+from typing import TypeVar
+
+import pandas as pd
+
+Frame = TypeVar('Frame', bound=pd.core.generic.NDFrame)
+
+
+class Partitioning(object):
+  """A class representing a (consistent) partitioning of dataframe objects.
+  """
+  def is_subpartition_of(self, other):
+# type: (Partitioning) -> bool
+
+"""Returns whether self is a sub-partition of other.
+
+Specifically, returns whether something partitioned by self is necissarily
+also partitioned by other.
+"""
+raise NotImplementedError
+
+  def partition_fn(self, df):
+# type: (Frame) -> Iterable[Tuple[Any, Frame]]
+
+"""A callable that actually performs the partitioning of a Frame df.
+
+This will be invoked via a FlatMap in conjunction with a GroupKey to
+achieve the desired partitioning.
+"""
+raise NotImplementedError
+
+
+class Index(Partitioning):
+  """A partitioning by index (either fully or partially).
+
+  If the set of "levels" of the index to consider is not specified, the entire
+  index is used.
+
+  These form a partial order, given by
+
+  Nothing() < Index([i]) < Index([i, j]) < ... < Index() < Singleton()

Review comment:
   Clarified. 

##
File path: sdks/python/apache_beam/dataframe/frames_test.py
##
@@ -23,6 +23,7 @@
 
 from apache_beam.dataframe import expressions
 from apache_beam.dataframe import frame_base
+from apache_beam.dataframe import frames  # pylint: disable=unused-import

Review comment:
   This ensures the deferred dataframe subclasses are registered for 
wrapping.

##
File path: sdks/python/apache_beam/dataframe/frames_test.py
##
@@ -23,6 +23,7 @@
 
 from apache_beam.dataframe import expressions
 from apache_beam.dataframe import frame_base
+from apache_beam.dataframe import frames  # pylint: disable=unused-import

Review comment:
   It makes sure the wrapper code is populated with the various types of 
frames. 





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:
us...@infra.apache.org




[GitHub] [beam] robertwb commented on a change in pull request #11766: [BEAM-10036] More flexible dataframes partitioning.

2020-06-02 Thread GitBox


robertwb commented on a change in pull request #11766:
URL: https://github.com/apache/beam/pull/11766#discussion_r434214501



##
File path: sdks/python/apache_beam/dataframe/partitionings.py
##
@@ -0,0 +1,133 @@
+#
+# 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 __future__ import absolute_import
+
+from typing import Any
+from typing import Iterable
+from typing import TypeVar
+
+import pandas as pd
+
+Frame = TypeVar('Frame', bound=pd.core.generic.NDFrame)
+
+
+class Partitioning(object):
+  """A class representing a (consistent) partitioning of dataframe objects.
+  """
+  def is_subpartition_of(self, other):
+# type: (Partitioning) -> bool
+
+"""Returns whether self is a sub-partition of other.
+
+Specifically, returns whether something partitioned by self is necissarily
+also partitioned by other.
+"""
+raise NotImplementedError
+
+  def partition_fn(self, df):
+# type: (Frame) -> Iterable[Tuple[Any, Frame]]
+
+"""A callable that actually performs the partitioning of a Frame df.
+
+This will be invoked via a FlatMap in conjunction with a GroupKey to
+achieve the desired partitioning.
+"""
+raise NotImplementedError
+
+
+class Index(Partitioning):
+  """A partitioning by index (either fully or partially).
+
+  If the set of "levels" of the index to consider is not specified, the entire
+  index is used.
+
+  These form a partial order, given by
+
+  Nothing() < Index([i]) < Index([i, j]) < ... < Index() < Singleton()
+  """
+
+  _INDEX_PARTITIONS = 100

Review comment:
   Oh, I was just testing things. I'll change it back. (It would be great 
to get rid of this altogether, as it limits parallelism, but that's not part of 
this change.)





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:
us...@infra.apache.org