-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathproperty_groups.py
More file actions
387 lines (319 loc) · 13.8 KB
/
property_groups.py
File metadata and controls
387 lines (319 loc) · 13.8 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#
# 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.
#
"""Property group classes for property graphs."""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
from pyspark.sql.functions import col, concat, lit, sha2
from pyspark.sql.types import (
ByteType,
DecimalType,
DoubleType,
FloatType,
IntegerType,
LongType,
ShortType,
StringType,
)
from graphframes import GraphFrame
if TYPE_CHECKING:
from pyspark.sql import Column, DataFrame
class InvalidPropertyGroupException(Exception):
"""Exception raised when a property group is invalid."""
pass
class PropertyGroup(ABC):
"""Abstract base class for property groups."""
def __init__(self, name: str, data: DataFrame) -> None:
"""
Initialize a property group.
:param name: The unique identifier for this property group
:param data: The DataFrame containing the property data
"""
self._name = name
self._data = data
self._validate()
@property
def name(self) -> str:
"""Return the name of the property group."""
return self._name
@property
def data(self) -> DataFrame:
"""Return the DataFrame containing the property data."""
return self._data
@abstractmethod
def _validate(self) -> None:
"""Validate the property group. Must be implemented by subclasses."""
pass
def get_data(self, filter_col: Column | None = None) -> DataFrame:
"""
Return a view of the data for the property group.
:param filter_col: An optional filter condition (Column) to apply to the data
:return: A DataFrame containing the filtered and optionally transformed data
"""
if filter_col is None:
filter_col = lit(True)
return self._get_data(filter_col)
@abstractmethod
def _get_data(self, filter_col: Column) -> DataFrame:
"""Internal method to get filtered data. Must be implemented by subclasses."""
pass
class VertexPropertyGroup(PropertyGroup):
"""
Represents a logical group of vertices in a property graph.
A VertexPropertyGroup organizes and manages vertices that share common characteristics
or belong to the same logical group within a property graph. Each group maintains its
own data in the form of a DataFrame and uses a primary key column for unique vertex
identification.
When vertices from different groups are combined into a GraphFrame, their IDs are
hashed with the group name to prevent collisions.
Example:
>>> people_data = spark.createDataFrame([(1, "Alice"), (2, "Bob")], ["id", "name"])
>>> people_group = VertexPropertyGroup("people", people_data, "id")
:param name: The unique identifier for this vertex property group
:param data: The DataFrame containing the vertex data
:param primary_key_column: The column name used to uniquely identify vertices
:param apply_mask_on_id: Whether to hash IDs with group name (default: True)
"""
def __init__(
self,
name: str,
data: DataFrame,
primary_key_column: str = "id",
apply_mask_on_id: bool = True,
) -> None:
"""
Initialize a VertexPropertyGroup.
:param name: Name of the vertex property group
:param data: DataFrame containing vertex data
:param primary_key_column: Name of the column to use as primary key (default: "id")
:param apply_mask_on_id: Whether to apply masking on vertex IDs (default: True)
"""
self._primary_key_column = primary_key_column
self._apply_mask_on_id = apply_mask_on_id
super().__init__(name, data)
@property
def primary_key_column(self) -> str:
"""Return the primary key column name."""
return self._primary_key_column
@property
def apply_mask_on_id(self) -> bool:
"""Return whether ID masking is applied."""
return self._apply_mask_on_id
def _validate(self) -> None:
"""Validate that the primary key column exists in the data."""
if self._primary_key_column not in self._data.columns:
raise InvalidPropertyGroupException(
f"source column {self._primary_key_column} does not exist, "
f"existed columns [{', '.join(self._data.columns)}]"
)
def _get_internal_id_mapping(self) -> DataFrame:
"""
Create a mapping from external IDs to internal hashed IDs.
:return: DataFrame with columns 'external_id' and 'id'
"""
EXTERNAL_ID = "external_id"
return self._data.select(col(self._primary_key_column).alias(EXTERNAL_ID)).withColumn(
GraphFrame.ID,
concat(
lit(self._name),
sha2(col(EXTERNAL_ID).cast(StringType()), 256),
),
)
def _get_data(self, filter_col: Column) -> DataFrame:
"""
Return filtered vertex data with internal IDs and property group column.
:param filter_col: Filter condition to apply
:return: DataFrame with columns 'id' and 'property_group'
"""
PROPERTY_GROUP_COL_NAME = "property_group"
filtered_data = self._data.filter(filter_col)
if self._apply_mask_on_id:
result = filtered_data.select(
concat(
lit(self._name),
sha2(col(self._primary_key_column).cast(StringType()), 256),
).alias(GraphFrame.ID)
)
else:
result = filtered_data.select(
col(self._primary_key_column).cast(StringType()).alias(GraphFrame.ID)
)
return result.select(
col(GraphFrame.ID),
lit(self._name).alias(PROPERTY_GROUP_COL_NAME),
)
class EdgePropertyGroup(PropertyGroup):
"""
Represents a logical group of edges in a property graph.
EdgePropertyGroup encapsulates edge data stored in a DataFrame along with metadata
describing how to interpret the data as graph edges. Each edge group has:
- A unique name identifier
- DataFrame containing the actual edge data
- Source and destination vertex property groups
- Direction flag indicating if edges are directed or undirected
- Column names specifying source vertex, destination vertex, and edge weight
When edges from different groups are combined into a GraphFrame, their src and dst
are hashed with the group name to prevent ID collisions.
Example:
>>> edges_data = spark.createDataFrame([(1, 2, 1.0)], ["src", "dst", "weight"])
>>> edges_group = EdgePropertyGroup(
... "likes", edges_data, people_group, movies_group,
... is_directed=False, src_column="src", dst_column="dst", weight_column="weight"
... )
:param name: Unique identifier for this edge property group
:param data: DataFrame containing the edge data
:param src_property_group: Source vertex property group
:param dst_property_group: Destination vertex property group
:param is_directed: Whether edges should be treated as directed
:param src_column_name: Name of the source vertex column in the data
:param dst_column_name: Name of the destination vertex column in the data
:param weight_column_name: Name of the edge weight column in the data
"""
def __init__(
self,
name: str,
data: DataFrame,
src_property_group: VertexPropertyGroup,
dst_property_group: VertexPropertyGroup,
is_directed: bool,
src_column_name: str,
dst_column_name: str,
weight_column_name: str | None = None,
) -> None:
"""
Initialize an EdgePropertyGroup.
:param name: Unique identifier for this edge property group
:param data: DataFrame containing the edge data with required columns
:param src_property_group: Source vertex property group
:param dst_property_group: Destination vertex property group
:param is_directed: Whether edges are directed (True) or undirected (False)
:param src_column_name: Name of the source vertex column
:param dst_column_name: Name of the destination vertex column
:param weight_column_name: Name of the edge weight column
(None means the lit(1).alias("weight") will be used)
"""
if weight_column_name is None:
data = data.withColumn("weight", lit(1.0))
weight_column_name = "weight"
self._src_property_group = src_property_group
self._dst_property_group = dst_property_group
self._is_directed = is_directed
self._src_column_name = src_column_name
self._dst_column_name = dst_column_name
self._weight_column_name = weight_column_name
super().__init__(name, data)
@property
def src_property_group(self) -> VertexPropertyGroup:
"""Return the source vertex property group."""
return self._src_property_group
@property
def dst_property_group(self) -> VertexPropertyGroup:
"""Return the destination vertex property group."""
return self._dst_property_group
@property
def is_directed(self) -> bool:
"""Return whether edges are directed."""
return self._is_directed
@property
def src_column_name(self) -> str:
"""Return the source column name."""
return self._src_column_name
@property
def dst_column_name(self) -> str:
"""Return the destination column name."""
return self._dst_column_name
@property
def weight_column_name(self) -> str:
"""Return the weight column name."""
return self._weight_column_name
def _validate(self) -> None:
"""Validate that required columns exist and weight column is numeric."""
if self._src_column_name not in self._data.columns:
raise InvalidPropertyGroupException(
f"source column {self._src_column_name} does not exist, "
f"existed columns [{', '.join(self._data.columns)}]"
)
if self._dst_column_name not in self._data.columns:
raise InvalidPropertyGroupException(
f"dest column {self._dst_column_name} does not exist, "
f"existed columns [{', '.join(self._data.columns)}]"
)
if self._weight_column_name not in self._data.columns:
raise InvalidPropertyGroupException(
f"weight column {self._weight_column_name} does not exist, "
f"existed columns [{', '.join(self._data.columns)}]"
)
# Check weight column type
weight_column_type = self._data.schema[self._weight_column_name].dataType
if not self._is_numeric_type(weight_column_type):
_msg = "weight column {} must be numeric type, but was {}"
raise InvalidPropertyGroupException(
_msg.format(self._weight_column_name, weight_column_type)
)
def _is_numeric_type(self, data_type) -> bool:
"""Check if a Spark data type is numeric."""
numeric_types = (
ByteType,
ShortType,
IntegerType,
LongType,
FloatType,
DoubleType,
DecimalType,
)
return isinstance(data_type, numeric_types)
def _hash_src_edge(self) -> Column:
"""Hash the source edge ID based on the source property group settings."""
if self._src_property_group.apply_mask_on_id:
return concat(
lit(self._src_property_group.name),
sha2(col(self._src_column_name).cast(StringType()), 256),
)
else:
return col(self._src_column_name).cast(StringType())
def _hash_dst_edge(self) -> Column:
"""Hash the destination edge ID based on the destination property group settings."""
if self._dst_property_group.apply_mask_on_id:
return concat(
lit(self._dst_property_group.name),
sha2(col(self._dst_column_name).cast(StringType()), 256),
)
else:
return col(self._dst_column_name).cast(StringType())
def _get_data(self, filter_col: Column) -> DataFrame:
"""
Return filtered edge data with hashed IDs and weights.
For undirected edges, creates bidirectional edges.
:param filter_col: Filter condition to apply
:return: DataFrame with columns 'src', 'dst', and 'weight'
"""
filtered_data = self._data.filter(filter_col)
base_edges = filtered_data.select(
self._hash_src_edge().alias(GraphFrame.SRC),
self._hash_dst_edge().alias(GraphFrame.DST),
col(self._weight_column_name).alias(GraphFrame.WEIGHT),
)
if self._is_directed:
return base_edges
else:
# For undirected edges, create bidirectional edges
reverse_edges = base_edges.select(
col(GraphFrame.DST).alias(GraphFrame.SRC),
col(GraphFrame.SRC).alias(GraphFrame.DST),
col(GraphFrame.WEIGHT).alias(GraphFrame.WEIGHT),
)
return base_edges.union(reverse_edges)