This repository was archived by the owner on Jul 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_generator.sql
More file actions
286 lines (241 loc) · 8.96 KB
/
Copy pathdata_generator.sql
File metadata and controls
286 lines (241 loc) · 8.96 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
-- Data generation function using plpython3u
CREATE OR REPLACE FUNCTION generate_data()
RETURNS text
AS $$
import random
from datetime import datetime, timedelta
import traceback
import time
def generate_retail_centers(num_centers=10):
retail_center_types = [
"Distribution Center",
"Warehouse",
"Store",
"Pickup Point",
"Sorting Facility",
]
addresses = [
"123 Main St, New York, NY",
"456 Broad Ave, Los Angeles, CA",
"789 Oak Dr, Chicago, IL",
"101 Pine Rd, Houston, TX",
"202 Maple Ln, Phoenix, AZ",
"303 Cedar Blvd, Philadelphia, PA",
"404 Elm St, San Antonio, TX",
"505 Willow Ave, San Diego, CA",
"606 Birch Rd, Dallas, TX",
"707 Spruce Ct, San Jose, CA",
]
for i in range(num_centers):
plan = plpy.prepare(
"INSERT INTO retail_center (type, address) VALUES ($1, $2) RETURNING id",
["text", "text"],
)
plpy.execute(plan, [random.choice(retail_center_types), addresses[i]])
def generate_transport_events(num_events=10):
transport_types = ["Air", "Ground", "Sea", "Rail", "Express"]
delivery_routes = [
"East Coast Route",
"West Coast Route",
"Central Route",
"Northern Route",
"Southern Route",
"International Route",
"Overnight Express",
"Standard Delivery",
"Priority Route",
"Economy Route",
]
for i in range(num_events):
plan = plpy.prepare(
"INSERT INTO transport_event (type, delivery_rout) VALUES ($1, $2) RETURNING seq_number",
["text", "text"],
)
plpy.execute(plan, [random.choice(transport_types), delivery_routes[i]])
def generate_shipped_items(num_items=1_000_000, batch_size=10_000):
destinations = [
"New York",
"Los Angeles",
"Chicago",
"Houston",
"Phoenix",
"Philadelphia",
"San Antonio",
"San Diego",
"Dallas",
"San Jose",
"Austin",
"Jacksonville",
"Fort Worth",
"Columbus",
"Indianapolis",
"Charlotte",
"Seattle",
"Denver",
"Washington",
"Boston",
]
# Current date for final delivery date
today = datetime.now()
# Get all retail center IDs - no need for foreign key validation during insert
retail_center_ids = range(1, 11) # Assuming we have 10 retail centers with IDs 1-10
start_time = time.time()
generated = 0
for batch in range(0, num_items, batch_size):
# Prepare batch values
values = []
for i in range(batch_size):
if batch + i >= num_items:
break
retail_center_id = random.choice(retail_center_ids)
weight = round(random.uniform(0.1, 100.0), 2)
dimension = round(random.uniform(1.0, 50.0), 2)
insurance_amt = round(random.uniform(10.0, 1_000.0), 2)
destination = random.choice(destinations)
days_to_add = random.randint(1, 120)
final_delivery_date = (today + timedelta(days=days_to_add)).strftime(
"%Y-%m-%d"
)
values.append(
f"({retail_center_id}, {weight}, {dimension}, {insurance_amt}, '{destination}', '{final_delivery_date}')"
)
# Execute batch insert
sql = f"INSERT INTO shipped_item (retail_center_id, weight, dimension, insurance_amt, destination, final_delivery_date) VALUES {','.join(values)};"
plpy.execute(sql)
generated += len(values)
elapsed = time.time() - start_time
rate = generated / elapsed if elapsed > 0 else 0
if batch % 100_000 == 0 or batch + batch_size >= num_items:
plpy.info(
f"Generated {generated}/{num_items} shipped items. Rate: {int(rate)} rows/sec"
)
def generate_item_transportation(num_records=10_000_000, batch_size=50_000):
# Generate comments for full-text search
comment_templates = [
"Package {status} at {location} on {date}",
"Shipment {status} and {action} at {time}",
"Item {status} for delivery on {date}",
"Delivery {status} at {location}, reason: {reason}",
"Package {status} by carrier, {action} at {location}",
"Item {action} at {location} for {reason}",
"Shipment {status} at {time}, {action} needed",
"Delivery attempt {status}, {action} required",
]
status_options = [
"received",
"processed",
"shipped",
"delayed",
"delivered",
"returned",
"damaged",
"lost",
"on hold",
]
action_options = [
"signature required",
"reschedule needed",
"pickup available",
"contact customer",
"verify address",
"awaiting instructions",
]
location_options = [
"distribution center",
"local hub",
"delivery vehicle",
"customer address",
"pickup point",
"warehouse",
"sorting facility",
]
reason_options = [
"weather delay",
"address issue",
"customer request",
"customs clearance",
"technical problem",
"carrier delay",
"missing information",
]
time_options = ["morning", "afternoon", "evening", "overnight"]
# We'll use a range of item_nums and seq_nums rather than fetching them
# This is much faster since no foreign key checks during insert
max_item_num = plpy.execute("SELECT MAX(item_num) as max FROM shipped_item")[0][
"max"
]
max_seq_num = plpy.execute("SELECT MAX(seq_number) as max FROM transport_event")[0][
"max"
]
start_time = time.time()
generated = 0
today = datetime.now()
for batch in range(0, num_records, batch_size):
values = []
for i in range(batch_size):
if batch + i >= num_records:
break
transportation_event_seq_number = random.randint(1, max_seq_num)
shipped_item_item_num = random.randint(1, max_item_num)
# Generate a comment for full-text search
template = random.choice(comment_templates)
status = random.choice(status_options)
action = random.choice(action_options)
location = random.choice(location_options)
reason = random.choice(reason_options)
time_of_day = random.choice(time_options)
days_offset = random.randint(-30, 30)
date = (today + timedelta(days=days_offset)).strftime("%Y-%m-%d")
comment = template.format(
status=status,
action=action,
location=location,
reason=reason,
time=time_of_day,
date=date,
)
# Escape single quotes in comment
comment = comment.replace("'", "''")
# Note: We don't need to specify the id as it's automatically filled by the serial column
values.append(
f"(DEFAULT, {transportation_event_seq_number}, '{comment}', {shipped_item_item_num})"
)
# Execute batch insert with DEFAULT for id column
sql = f"INSERT INTO item_transportation (id, transportation_event_seq_number, comment, shipped_item_item_num) VALUES {','.join(values)};"
plpy.execute(sql)
generated += len(values)
elapsed = time.time() - start_time
rate = generated / elapsed if elapsed > 0 else 0
if batch % 1_000_000 == 0 or batch + batch_size >= num_records:
plpy.info(
f"Generated {generated}/{num_records} transportation records. Rate: {int(rate)} rows/sec"
)
def main():
total_start_time = time.time()
plpy.info("Starting data generation process...")
plpy.info("1. Generating retail centers...")
generate_retail_centers(10)
plpy.info("Retail centers generated successfully.")
plpy.info("2. Generating transport events...")
generate_transport_events(10)
plpy.info("Transport events generated successfully.")
plpy.info("3. Generating shipped items (1M records)...")
generate_shipped_items(num_items=1_000_000, batch_size=10_000)
plpy.info("Shipped items generated successfully.")
plpy.info("4. Generating item transportation records (10M records)...")
generate_item_transportation(num_records=10_000_000, batch_size=50_000)
plpy.info("Item transportation records generated successfully.")
plpy.info("5. Adding constraints to tables...")
plpy.execute("SELECT add_constraints()")
plpy.info("Constraints added successfully.")
total_time = time.time() - total_start_time
return (
f"Data generation completed successfully! Total time: {total_time:.2f} seconds!"
)
try:
return main()
except Exception as e:
error_details = traceback.format_exc()
return f"Error during data generation: {str(e)}\n{error_details}"
$$
LANGUAGE plpython3u;