-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathsideinputs.py
111 lines (84 loc) · 3.64 KB
/
sideinputs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# 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.
#
"""Internal side input transforms and implementations.
For internal use only; no backwards-compatibility guarantees.
Important: this module is an implementation detail and should not be used
directly by pipeline writers. Instead, users should use the helper methods
AsSingleton, AsIter, AsList and AsDict in apache_beam.pvalue.
"""
# pytype: skip-file
import re
from collections.abc import Callable
from typing import TYPE_CHECKING
from typing import Any
from apache_beam.transforms import window
if TYPE_CHECKING:
from apache_beam import pvalue
WindowMappingFn = Callable[[window.BoundedWindow], window.BoundedWindow]
SIDE_INPUT_PREFIX = 'python_side_input'
SIDE_INPUT_REGEX = SIDE_INPUT_PREFIX + '([0-9]+)(-.*)?$'
# Top-level function so we can identify it later.
def _global_window_mapping_fn(
w, global_window=window.GlobalWindow()) -> window.GlobalWindow:
return global_window
def default_window_mapping_fn(
target_window_fn: window.WindowFn) -> WindowMappingFn:
if target_window_fn == window.GlobalWindows():
return _global_window_mapping_fn
if isinstance(target_window_fn, window.Sessions):
raise RuntimeError("Sessions is not allowed in side inputs")
def map_via_end(source_window: window.BoundedWindow) -> window.BoundedWindow:
return list(
target_window_fn.assign(
window.WindowFn.AssignContext(source_window.max_timestamp())))[-1]
return map_via_end
def get_sideinput_index(tag: str) -> int:
match = re.match(SIDE_INPUT_REGEX, tag, re.DOTALL)
if match:
return int(match.group(1))
else:
raise RuntimeError("Invalid tag %r" % tag)
class SideInputMap(object):
"""Represents a mapping of windows to side input values."""
def __init__(self, view_class: 'pvalue.AsSideInput', view_options, iterable):
self._window_mapping_fn = view_options.get(
'window_mapping_fn', _global_window_mapping_fn)
self._view_class = view_class
self._view_options = view_options
self._iterable = iterable
self._cache: dict[window.BoundedWindow, Any] = {}
def __getitem__(self, window: window.BoundedWindow) -> Any:
if window not in self._cache:
target_window = self._window_mapping_fn(window)
self._cache[window] = self._view_class._from_runtime_iterable(
_FilteringIterable(self._iterable, target_window), self._view_options)
return self._cache[window]
def is_globally_windowed(self) -> bool:
return self._window_mapping_fn == _global_window_mapping_fn
class _FilteringIterable(object):
"""An iterable containing only those values in the given window.
"""
def __init__(self, iterable, target_window):
self._iterable = iterable
self._target_window = target_window
def __iter__(self):
for wv in self._iterable:
if self._target_window in wv.windows:
yield wv.value
def __reduce__(self):
# Pickle self as an already filtered list.
return list, (list(self), )