|
18 | 18 | logger = logging.getLogger(__name__) |
19 | 19 |
|
20 | 20 |
|
| 21 | +class SelectTagColumns(UnaryOperator): |
| 22 | + """ |
| 23 | + Operator that selects specified columns from a stream. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, columns: str | Collection[str], strict: bool = True, **kwargs): |
| 27 | + if isinstance(columns, str): |
| 28 | + columns = [columns] |
| 29 | + self.columns = columns |
| 30 | + self.strict = strict |
| 31 | + super().__init__(**kwargs) |
| 32 | + |
| 33 | + def op_forward(self, stream: cp.Stream) -> cp.Stream: |
| 34 | + tag_columns, packet_columns = stream.keys() |
| 35 | + tags_to_drop = [c for c in tag_columns if c not in self.columns] |
| 36 | + new_tag_columns = [c for c in tag_columns if c not in tags_to_drop] |
| 37 | + |
| 38 | + if len(new_tag_columns) == len(tag_columns): |
| 39 | + logger.info("All tag columns are selected. Returning stream unaltered.") |
| 40 | + return stream |
| 41 | + |
| 42 | + table = stream.as_table( |
| 43 | + include_source=True, include_system_tags=True, sort_by_tags=False |
| 44 | + ) |
| 45 | + |
| 46 | + modified_table = table.drop_columns(list(tags_to_drop)) |
| 47 | + |
| 48 | + return TableStream( |
| 49 | + modified_table, |
| 50 | + tag_columns=new_tag_columns, |
| 51 | + source=self, |
| 52 | + upstreams=(stream,), |
| 53 | + ) |
| 54 | + |
| 55 | + def op_validate_inputs(self, stream: cp.Stream) -> None: |
| 56 | + """ |
| 57 | + This method should be implemented by subclasses to validate the inputs to the operator. |
| 58 | + It takes two streams as input and raises an error if the inputs are not valid. |
| 59 | + """ |
| 60 | + # TODO: remove redundant logic |
| 61 | + tag_columns, packet_columns = stream.keys() |
| 62 | + columns_to_select = self.columns |
| 63 | + missing_columns = set(columns_to_select) - set(tag_columns) |
| 64 | + if missing_columns and self.strict: |
| 65 | + raise InputValidationError( |
| 66 | + f"Missing tag columns: {missing_columns}. Make sure all specified columns to select are present or use strict=False to ignore missing columns" |
| 67 | + ) |
| 68 | + |
| 69 | + def op_output_types( |
| 70 | + self, stream: cp.Stream, include_system_tags: bool = False |
| 71 | + ) -> tuple[PythonSchema, PythonSchema]: |
| 72 | + tag_schema, packet_schema = stream.types( |
| 73 | + include_system_tags=include_system_tags |
| 74 | + ) |
| 75 | + tag_columns, _ = stream.keys() |
| 76 | + tags_to_drop = [tc for tc in tag_columns if tc not in self.columns] |
| 77 | + |
| 78 | + # this ensures all system tag columns are preserved |
| 79 | + new_tag_schema = {k: v for k, v in tag_schema.items() if k not in tags_to_drop} |
| 80 | + |
| 81 | + return new_tag_schema, packet_schema |
| 82 | + |
| 83 | + def op_identity_structure(self, stream: cp.Stream | None = None) -> Any: |
| 84 | + return ( |
| 85 | + self.__class__.__name__, |
| 86 | + self.columns, |
| 87 | + self.strict, |
| 88 | + ) + ((stream,) if stream is not None else ()) |
| 89 | + |
| 90 | + |
| 91 | +class SelectPacketColumns(UnaryOperator): |
| 92 | + """ |
| 93 | + Operator that selects specified columns from a stream. |
| 94 | + """ |
| 95 | + |
| 96 | + def __init__(self, columns: str | Collection[str], strict: bool = True, **kwargs): |
| 97 | + if isinstance(columns, str): |
| 98 | + columns = [columns] |
| 99 | + self.columns = columns |
| 100 | + self.strict = strict |
| 101 | + super().__init__(**kwargs) |
| 102 | + |
| 103 | + def op_forward(self, stream: cp.Stream) -> cp.Stream: |
| 104 | + tag_columns, packet_columns = stream.keys() |
| 105 | + packet_columns_to_drop = [c for c in packet_columns if c not in self.columns] |
| 106 | + new_packet_columns = [ |
| 107 | + c for c in packet_columns if c not in packet_columns_to_drop |
| 108 | + ] |
| 109 | + |
| 110 | + if len(new_packet_columns) == len(packet_columns): |
| 111 | + logger.info("All packet columns are selected. Returning stream unaltered.") |
| 112 | + return stream |
| 113 | + |
| 114 | + table = stream.as_table( |
| 115 | + include_source=True, include_system_tags=True, sort_by_tags=False |
| 116 | + ) |
| 117 | + # make sure to drop associated source fields |
| 118 | + associated_source_fields = [ |
| 119 | + f"{constants.SOURCE_PREFIX}{c}" for c in packet_columns_to_drop |
| 120 | + ] |
| 121 | + packet_columns_to_drop.extend(associated_source_fields) |
| 122 | + |
| 123 | + modified_table = table.drop_columns(packet_columns_to_drop) |
| 124 | + |
| 125 | + return TableStream( |
| 126 | + modified_table, |
| 127 | + tag_columns=tag_columns, |
| 128 | + source=self, |
| 129 | + upstreams=(stream,), |
| 130 | + ) |
| 131 | + |
| 132 | + def op_validate_inputs(self, stream: cp.Stream) -> None: |
| 133 | + """ |
| 134 | + This method should be implemented by subclasses to validate the inputs to the operator. |
| 135 | + It takes two streams as input and raises an error if the inputs are not valid. |
| 136 | + """ |
| 137 | + # TODO: remove redundant logic |
| 138 | + tag_columns, packet_columns = stream.keys() |
| 139 | + columns_to_select = self.columns |
| 140 | + missing_columns = set(columns_to_select) - set(packet_columns) |
| 141 | + if missing_columns and self.strict: |
| 142 | + raise InputValidationError( |
| 143 | + f"Missing packet columns: {missing_columns}. Make sure all specified columns to select are present or use strict=False to ignore missing columns" |
| 144 | + ) |
| 145 | + |
| 146 | + def op_output_types( |
| 147 | + self, stream: cp.Stream, include_system_tags: bool = False |
| 148 | + ) -> tuple[PythonSchema, PythonSchema]: |
| 149 | + tag_schema, packet_schema = stream.types( |
| 150 | + include_system_tags=include_system_tags |
| 151 | + ) |
| 152 | + _, packet_columns = stream.keys() |
| 153 | + packets_to_drop = [pc for pc in packet_columns if pc not in self.columns] |
| 154 | + |
| 155 | + # this ensures all system tag columns are preserved |
| 156 | + new_packet_schema = { |
| 157 | + k: v for k, v in packet_schema.items() if k not in packets_to_drop |
| 158 | + } |
| 159 | + |
| 160 | + return tag_schema, new_packet_schema |
| 161 | + |
| 162 | + def op_identity_structure(self, stream: cp.Stream | None = None) -> Any: |
| 163 | + return ( |
| 164 | + self.__class__.__name__, |
| 165 | + self.columns, |
| 166 | + self.strict, |
| 167 | + ) + ((stream,) if stream is not None else ()) |
| 168 | + |
| 169 | + |
21 | 170 | class DropTagColumns(UnaryOperator): |
22 | 171 | """ |
23 | 172 | Operator that drops specified columns from a stream. |
@@ -64,11 +213,10 @@ def op_validate_inputs(self, stream: cp.Stream) -> None: |
64 | 213 | tag_columns, packet_columns = stream.keys() |
65 | 214 | columns_to_drop = self.columns |
66 | 215 | missing_columns = set(columns_to_drop) - set(tag_columns) |
67 | | - if missing_columns: |
68 | | - if self.strict: |
69 | | - raise InputValidationError( |
70 | | - f"Missing tag columns: {missing_columns}. Make sure all specified columns to drop are present or use strict=False to ignore missing columns" |
71 | | - ) |
| 216 | + if missing_columns and self.strict: |
| 217 | + raise InputValidationError( |
| 218 | + f"Missing tag columns: {missing_columns}. Make sure all specified columns to drop are present or use strict=False to ignore missing columns" |
| 219 | + ) |
72 | 220 |
|
73 | 221 | def op_output_types( |
74 | 222 | self, stream: cp.Stream, include_system_tags: bool = False |
@@ -105,19 +253,25 @@ def __init__(self, columns: str | Collection[str], strict: bool = True, **kwargs |
105 | 253 |
|
106 | 254 | def op_forward(self, stream: cp.Stream) -> cp.Stream: |
107 | 255 | tag_columns, packet_columns = stream.keys() |
108 | | - columns_to_drop = self.columns |
| 256 | + columns_to_drop = list(self.columns) |
109 | 257 | if not self.strict: |
110 | 258 | columns_to_drop = [c for c in columns_to_drop if c in packet_columns] |
111 | 259 |
|
112 | 260 | if len(columns_to_drop) == 0: |
113 | 261 | logger.info("No packet columns to drop. Returning stream unaltered.") |
114 | 262 | return stream |
115 | 263 |
|
| 264 | + # make sure all associated source columns are dropped too |
| 265 | + associated_source_columns = [ |
| 266 | + f"{constants.SOURCE_PREFIX}{c}" for c in columns_to_drop |
| 267 | + ] |
| 268 | + columns_to_drop.extend(associated_source_columns) |
| 269 | + |
116 | 270 | table = stream.as_table( |
117 | 271 | include_source=True, include_system_tags=True, sort_by_tags=False |
118 | 272 | ) |
119 | 273 |
|
120 | | - modified_table = table.drop_columns(list(columns_to_drop)) |
| 274 | + modified_table = table.drop_columns(columns_to_drop) |
121 | 275 |
|
122 | 276 | return TableStream( |
123 | 277 | modified_table, |
|
0 commit comments