DatabaseCreation: dml.sql

File dml.sql, 81.7 KB (added by 231119, 2 days ago)
Line 
1create table if not exists _temp_male_names
2(
3 id bigserial primary key,
4 name text
5);
6
7create table if not exists _temp_female_names
8(
9 id bigserial primary key,
10 name text
11);
12
13create table if not exists _temp_surnames
14(
15 id bigserial primary key,
16 surname text
17);
18
19select count(*)
20from _temp_female_names;
21select count(*)
22from _temp_male_names;
23select count(*)
24from _temp_surnames;
25
26with male_names as (select name, row_number() over () as rn from _temp_male_names order by random()),
27 surnames as (select surname, row_number() over () as rn from _temp_surnames order by random()),
28 combined as (select mn.name as first_name,
29 s.surname as last_name,
30 regexp_replace(lower(mn.name || s.surname), '[^a-zA-Z0-9]', '', 'g') ||
31 row_number() over () as username
32 from male_names mn
33 cross join surnames s
34 limit 500000)
35insert
36into appuser (first_name, last_name, username, password_hash, date_of_birth, phone_number)
37select first_name,
38 last_name,
39 username,
40 substring(md5(random()::text) from 1 for 12),
41 '2020-01-01',
42 '+38975000000'
43from combined
44on conflict (username) do nothing;
45
46with female_names as (select name, row_number() over () as rn from _temp_female_names order by random()),
47 surnames as (select surname, row_number() over () as rn from _temp_surnames order by random()),
48 combined as (select fn.name as first_name,
49 s.surname as last_name,
50 regexp_replace(lower(fn.name || s.surname), '[^a-zA-Z0-9]', '', 'g') ||
51 row_number() over () as username
52 from female_names fn
53 cross join surnames s
54 limit 500000)
55insert
56into appuser (first_name, last_name, username, password_hash, date_of_birth, phone_number)
57select first_name,
58 last_name,
59 username,
60 substring(md5(random()::text) from 1 for 12),
61 '2020-01-01',
62 '+38975000000'
63from combined
64on conflict (username) do nothing;
65-- update appuser
66-- set password_hash = crypt(password_hash, gen_salt('bf')); -- encrypt password
67update appuser
68set email = lower(
69 username || '@' ||
70 (array ['gmail.com', 'yahoo.com', 'outlook.com', 'protonmail.com', 'icloud.com'])[floor(random() * 5 + 1)]
71 );
72
73update appuser
74set phone_number = '+3897'
75 || (array ['0', '1', '2', '5', '7', '8'])[floor(random() * 6 + 1)]
76 || floor(random() * (999999 - 100000 + 1) + 100000)::text;
77
78update appuser
79set date_of_birth = (now() - interval '70 years' * random())::date;
80
81
82insert into customer
83select id
84from appuser
85order by random()
86limit 800000;
87
88insert into admin
89select id
90from appuser
91order by random()
92limit 100;
93
94insert into dispatcher
95select id
96from appuser
97order by random()
98limit 5000;
99
100-- DRIVER License
101create temp table if not exists seed_params as
102select 20000 as total_drivers;
103insert into driverlicense (issue_date, expire_date, license_id)
104select '2024-01-01'::date + (random() * 500)::int as issue_date,
105 '2030-01-01'::date + (random() * 2000)::int as expire_date,
106 'U' || lpad(s.i::text, 7, '0') as license_id
107from generate_series(1, (select total_drivers from seed_params)) as s(i);
108
109with recursive
110 target_pool as (select license_id,
111 issue_date as next_start_date,
112 1 as depth
113 from driverlicense
114 order by random()
115 limit (select 0.3 * total_drivers from seed_params)),
116 license_chain as (select license_id,
117 next_start_date,
118 depth
119 from target_pool
120
121 union all
122
123 select license_id,
124 (next_start_date - 3655 - (random() * 100)::int)::date,
125 depth + 1
126 from license_chain
127 where depth < 3
128 and random() > 0.4)
129insert
130into driverlicense (issue_date, expire_date, license_id)
131select (next_start_date - 3650)::date as issue_date,
132 (next_start_date - (10 + (random() * 30)::int))::date as expire_date,
133 license_id
134from license_chain;
135
136insert into driverlicense_driverlicensecategory (driver_license_id, driver_license_category_id)
137select dl.id, cat.id
138from driverlicense dl
139 cross join driverlicensecategory cat
140where cat.category_name in ('B', 'F', 'G', 'AM')
141on conflict do nothing;
142
143with biker_pool as (select license_id, array_agg(id) as ids
144 from driverlicense
145 group by license_id
146 order by random()
147 limit 3000)
148insert
149into driverlicense_driverlicensecategory (driver_license_id, driver_license_category_id)
150select unnest(ids), cat.id
151from biker_pool
152 cross join driverlicensecategory cat
153where cat.category_name in ('A', 'A1', 'A2')
154on conflict do nothing;
155
156with truck_pool as (select license_id, array_agg(id) as ids
157 from driverlicense
158 group by license_id
159 order by random()
160 limit 2000),
161 truck_base as (
162 insert into driverlicense_driverlicensecategory (driver_license_id, driver_license_category_id)
163 select unnest(ids), cat.id
164 from truck_pool
165 cross join driverlicensecategory cat
166 where cat.category_name in ('C', 'C1')
167 on conflict do nothing
168 returning driver_license_id)
169insert
170into driverlicense_driverlicensecategory (driver_license_id, driver_license_category_id)
171select driver_license_id, cat.id
172from truck_base
173 cross join driverlicensecategory cat
174where cat.category_name in ('CE', 'C1E')
175 and random() > 0.5
176on conflict do nothing;
177
178with bus_pool as (select license_id, array_agg(id) as ids
179 from driverlicense
180 group by license_id
181 order by random()
182 limit 600),
183 bus_base as (
184 insert into driverlicense_driverlicensecategory (driver_license_id, driver_license_category_id)
185 select unnest(ids), cat.id
186 from bus_pool
187 cross join driverlicensecategory cat
188 where cat.category_name in ('D', 'D1')
189 on conflict do nothing
190 returning driver_license_id)
191insert
192into driverlicense_driverlicensecategory (driver_license_id, driver_license_category_id)
193select driver_license_id, cat.id
194from bus_base
195 cross join driverlicensecategory cat
196where cat.category_name in ('DE', 'D1E')
197 and random() > 0.3
198on conflict do nothing;
199
200with special_pool as (select license_id, array_agg(id) as ids from driverlicense group by license_id)
201insert
202into driverlicense_driverlicensecategory (driver_license_id, driver_license_category_id)
203select unnest(ids), (select id from driverlicensecategory where category_name = 'BE')
204from special_pool
205where random() < 0.1
206on conflict do nothing;
207
208with special_pool as (select license_id, array_agg(id) as ids from driverlicense group by license_id)
209insert
210into driverlicense_driverlicensecategory (driver_license_id, driver_license_category_id)
211select unnest(ids), (select id from driverlicensecategory where category_name = 'T')
212from special_pool
213where random() < 0.05
214on conflict do nothing;
215
216insert into driver
217with unique_candidate_users as (select appuser.id
218 from appuser
219 where appuser.date_of_birth <= (current_date - interval '18 days')
220 and not exists(select 1 from admin where admin.user_id = appuser.id)
221 and not exists(select 1 from dispatcher where dispatcher.user_id = appuser.id)
222 order by random()
223 limit (select total_drivers from seed_params)),
224 active_licenses as (select id, row_number() over () as rn
225 from driverlicense
226 where expire_date > current_date
227 and id in (select distinct on (license_id) id
228 from driverlicense
229 order by license_id, expire_date desc)),
230 user_pool as (select id, row_number() over () as rn
231 from unique_candidate_users)
232select user_pool.id as user_id,
233 active_licenses.id as driver_license_id
234from user_pool
235 join active_licenses on active_licenses.rn = user_pool.rn
236on conflict do nothing;
237
238insert into freelancedriver(driver_user_id)
239select driver.user_id
240from driver
241order by random()
242limit (select count(*) * 0.5 from driver);
243
244insert into company (name, date_founded, tax_number, phone_number, email, website, active)
245select
246 (array ['City', 'Classic', 'Lotus', 'Ekstra', 'Global', 'Bel', 'Plav', 'Zolt', 'De Luks', 'Elite', 'Skopje', 'Vardar', 'Prilep', 'Veles', 'Struga', 'Bitola', 'Brajko'])[floor(random() * 14 + 1)] ||
247 ' ' ||
248 (array ['Taxi', 'Prevoz', 'Transport', 'Mobiliti', 'Ekspres'])[floor(random() * 5 + 1)] || ' ' ||
249 (array ['DOOEL', 'DOO'])[floor(random() * 2 + 1)] as name,
250 ('1995-01-01'::date + (random() * 10000)::int * INTERVAL '1 day')::date as date_founded,
251 '4' || floor(random() * (999999999999 - 100000000000 + 1) + 100000000000)::text as tax_number,
252 ('+3897' || (array ['0', '1', '2', '5', '7', '8'])[random() * 5 + 1] ||
253 floor(random() * (99999 - 10000 + 1) + 10000)::text) as phone_number,
254 'contact@tmp.mk' as email,
255 'http://tmp.mk' as website,
256 (random() < 0.95) as active
257from generate_series(1, 300) as s(i);
258
259
260update company
261set email = lower(regexp_replace(name, '[^a-zA-Z0-9]', '', 'g')) || '@' ||
262 (array ['taxi.mk', 'telekom.mk', 'gmail.com', 'outlook.com', 'net.mk'])[floor(random() * 5 + 1)],
263 website = 'https://www.' || lower(regexp_replace(name, '[^a-zA-Z0-9]', '', 'g')) || '.mk';
264
265insert into area (latitude, longitude, radius, name)
266values (41.9961, 21.4317, 1600, 'Centar'),
267 (41.9781, 21.4678, 2500, 'Aerodrom'),
268 (41.9939, 21.3917, 3300, 'Karposh'),
269 (42.0114, 21.4503, 1000, 'Chair'),
270 (41.9492, 21.4533, 3700, 'Kisela Voda'),
271 (42.0039, 21.4939, 5400, 'Gazi Baba'),
272 (42.0536, 21.4447, 4400, 'Butel'),
273 (42.0647, 21.3783, 4600, 'Gjorche Petrov'),
274 (42.0153, 21.2647, 8500, 'Saraj'),
275 (42.0353, 21.4003, 1500, 'Shuto Orizari'),
276
277 -- Eastern Region
278 (41.7022, 22.8575, 13800, 'Berovo'),
279 (41.9142, 22.2272, 6500, 'Cheshinovo-Obleshevo'),
280 (41.9664, 22.7747, 11600, 'Delchevo'),
281 (41.8156, 22.2319, 8600, 'Karbinci'),
282 (41.9167, 22.4125, 10700, 'Kochani'),
283 (42.0192, 22.5878, 7800, 'Makedonska Kamenica'),
284 (41.7619, 22.8875, 8100, 'Pehchevo'),
285 (41.8953, 22.1742, 10200, 'Probistip'),
286 (41.7358, 22.1914, 13600, 'Shtip'),
287 (41.8761, 22.5086, 11700, 'Vinica'),
288 (41.8753, 22.4419, 4000, 'Zrnovci'),
289
290 -- Northeastern Region
291 (42.0792, 22.1750, 10900, 'Kratovo'),
292 (42.2017, 22.3317, 12400, 'Kriva Palanka'),
293 (42.1322, 21.7144, 12700, 'Kumanovo'),
294 (42.1550, 21.5875, 9300, 'Lipkovo'),
295 (42.1697, 22.1153, 8700, 'Rankovce'),
296 (42.1983, 21.8267, 12800, 'Staro Nagorichane'),
297
298 -- Pelagonia Region
299 (41.0319, 21.3347, 15800, 'Bitola'),
300 (41.2208, 21.2031, 12400, 'Demir Hisar'),
301 (41.4253, 21.4533, 11500, 'Dolneni'),
302 (41.3353, 21.3319, 5400, 'Krivogashtani'),
303 (41.3697, 21.2492, 7800, 'Krushevo'),
304 (41.3000, 21.4667, 9000, 'Mogila'),
305 (41.0417, 21.5583, 15500, 'Novaci'),
306 (41.3464, 21.5542, 19500, 'Prilep'),
307 (41.0889, 21.0125, 13200, 'Resen'),
308
309 -- Polog Region
310 (41.9239, 20.9136, 6700, 'Bogovinje'),
311 (41.9406, 20.9822, 7200, 'Brvenica'),
312 (41.7920, 20.9082, 10900, 'Gostivar'),
313 (42.0722, 21.1214, 7500, 'Jegunovce'),
314 (41.6108, 20.5986, 16500, 'Mavrovo and Rostusha'),
315 (42.0772, 21.0533, 6600, 'Tearce'),
316 (42.0103, 20.9714, 9100, 'Tetovo'),
317 (41.8317, 20.8856, 7100, 'Vrapchishte'),
318 (41.9794, 21.0631, 8000, 'Zhelino'),
319
320 -- Southwestern Region
321 (41.5250, 20.5272, 6800, 'Debar'),
322 (41.2144, 20.7497, 11600, 'Debarca'),
323 (41.5142, 20.9631, 16200, 'Kichevo'),
324 (41.0114, 21.1319, 5800, 'Makedonski Brod'),
325 (41.1169, 20.8019, 11100, 'Ohrid'),
326 (41.4667, 20.5333, 4200, 'Plasnica'),
327 (41.1775, 20.6789, 12400, 'Struga'),
328 (41.4939, 20.6019, 5800, 'Centar Zhupa'),
329 (41.2406, 20.5928, 2800, 'Vevchani'),
330
331 -- Southeastern Region
332 (41.2033, 22.5103, 6000, 'Bogdanci'),
333 (41.4300, 22.7275, 7200, 'Bosilovo'),
334 (41.1878, 22.6931, 6400, 'Dojran'),
335 (41.1392, 22.5025, 12400, 'Gevgelija'),
336 (41.6000, 22.5667, 8600, 'Konche'),
337 (41.4111, 22.9736, 8700, 'Novo Selo'),
338 (41.6381, 22.4644, 12600, 'Radovish'),
339 (41.4375, 22.6431, 10100, 'Strumica'),
340 (41.3614, 22.7503, 6500, 'Vasilevo'),
341 (41.3172, 22.5611, 10800, 'Valandovo'),
342
343 -- Vardar Region
344 (41.6431, 21.6911, 16100, 'Chashka'),
345 (41.4111, 22.2417, 9900, 'Demir Kapija'),
346 (41.5833, 21.9000, 8700, 'Gradsko'),
347 (41.4328, 22.0117, 17700, 'Kavadarci'),
348 (41.7803, 21.9000, 7300, 'Lozovo'),
349 (41.4839, 22.0892, 11600, 'Negotino'),
350 (41.5161, 21.9458, 6500, 'Rosoman'),
351 (41.8650, 21.9425, 12400, 'Sveti Nikole'),
352 (41.7153, 21.7753, 11700, 'Veles'),
353
354 -- Additional Skopje Region (Rural)
355 (42.0350, 21.6167, 3500, 'Arachinovo'),
356 (42.1553, 21.4331, 8700, 'Chucher-Sandevo'),
357 (41.9961, 21.5542, 5500, 'Ilinden'),
358 (41.9286, 21.5956, 8400, 'Petrovec'),
359 (41.8953, 21.3117, 8400, 'Sopishte'),
360 (41.9167, 21.5333, 9400, 'Studenichani'),
361 (41.8667, 21.4667, 6100, 'Zelenikovo'),
362 (40.9950, 20.8000, 5000, 'Peshtani & Trpejca Coast'),
363 (40.9150, 21.1500, 6000, 'Lower Prespa'),
364 (41.0000, 21.2000, 7000, 'Pelister National Park'),
365
366 (41.8800, 21.1850, 8000, 'Kozjak & Jasen Reserve'),
367 (41.8950, 21.6150, 5000, 'Taor & Katlanovo Region'),
368 (41.9400, 21.3000, 5000, 'Matka Canyon Deep'),
369
370 (42.0140, 20.8800, 6000, 'Popova Shapka Resort'),
371 (41.5500, 21.1900, 9000, 'Poreche Region'),
372
373 (41.2460, 21.3850, 4500, 'Chepigovo & Stuberra'),
374 (41.5210, 21.3420, 5000, 'Mount Dautica Foothills'),
375
376 (42.0120, 22.2270, 5000, 'Lesnovo & Zletovo Region'),
377 (41.3650, 22.9050, 5000, 'Belasica Foothills');
378
379
380insert into pricinginfo (value, unit, currency_id)
381select val, unt::unit_type, (select id from currencycatalog where currency = 'MKD')
382from (values
383 (25.00, 'kilometer'),
384 (30.00, 'kilometer'),
385 (35.00, 'kilometer'),
386 (45.00, 'kilometer'),
387 (5.00, 'minute'),
388 (8.00, 'minute'),
389 (12.00, 'minute')
390 ) as t(val, unt);
391
392insert into company_area
393select company.id as company_id,
394 area.id as area_id,
395 p.id as pricing_info_id
396from company
397 join area on (company.id % 92) + 1 = (select rn
398 from (select id, row_number() over (order by id) as rn from area) t
399 where t.id = area.id)
400 cross join lateral (
401 select id
402 from pricinginfo
403 where unit = 'kilometer'
404 and (
405 (area.radius < 3000 and value <= 30) or
406 (area.radius >= 3000 and area.radius < 10000) or
407 (area.radius >= 10000 and value >= 35)
408 )
409 order by random()
410 limit 1
411 ) p
412on conflict do nothing;
413
414insert into company_area (company_id, area_id, pricing_info_id)
415select c.id,
416 a.id,
417 p.id
418from company c
419 join area a on a.name in ('Centar', 'Aerodrom', 'Karposh', 'Bitola', 'Tetovo', 'Gostivar', 'Kumanovo')
420 cross join lateral (
421 select pi.id
422 from pricinginfo pi
423 where pi.unit = 'minute'
424 and c.id is not null
425 order by random()
426 limit 1
427 ) p
428where random() < 0.4
429on conflict do nothing;
430
431insert into Brand (name, date_founded, phone_number, email)
432values ('Toyota', '1937-08-28', '+38970100100', 'contact@toyota.mk'),
433 ('Volkswagen', '1937-05-28', '+38971200200', 'info@volkswagen.mk'),
434 ('Ford', '1903-06-16', '+38972300300', 'sales@ford.mk'),
435 ('Honda', '1948-09-24', '+38975400400', 'office@honda.mk'),
436 ('Chevrolet', '1911-11-03', '+38976500500', 'support@chevrolet.mk'),
437 ('Nissan', '1933-12-26', '+38977600600', 'contact@nissan.mk'),
438 ('Hyundai', '1967-12-29', '+38978700700', 'info@hyundai.mk'),
439 ('Kia', '1944-06-09', '+38970800800', 'sales@kia.mk'),
440 ('Mercedes-Benz', '1926-06-28', '+38971900900', 'office@mercedesbenz.mk'),
441 ('BMW', '1916-03-07', '+38972111222', 'contact@bmw.mk'),
442 ('Audi', '1909-07-16', '+38975222333', 'info@audi.mk'),
443 ('Peugeot', '1896-05-09', '+38976333444', 'sales@peugeot.mk'),
444 ('Renault', '1899-02-25', '+38977444555', 'office@renault.mk'),
445 ('Fiat', '1899-07-11', '+38978555666', 'contact@fiat.mk'),
446 ('Skoda', '1895-12-18', '+38970666777', 'info@skoda.mk'),
447 ('Volvo', '1927-04-14', '+38971777888', 'sales@volvo.mk'),
448 ('Mazda', '1920-01-30', '+38972888999', 'office@mazda.mk'),
449 ('Subaru', '1953-07-15', '+38975999000', 'contact@subaru.mk'),
450 ('Porsche', '1931-04-25', '+38976101101', 'info@porsche.mk'),
451 ('Lexus', '1989-09-01', '+38977202202', 'sales@lexus.mk'),
452 ('Jeep', '1941-07-15', '+38978303303', 'office@jeep.mk'),
453 ('Land Rover', '1948-04-30', '+38970404404', 'contact@landrover.mk'),
454 ('Tesla', '2003-07-01', '+38971505505', 'info@tesla.mk'),
455 ('Mitsubishi', '1970-04-22', '+38972606606', 'sales@mitsubishi.mk'),
456 ('Suzuki', '1909-10-01', '+38975707707', 'office@suzuki.mk'),
457 ('Citroen', '1919-03-01', '+38976808808', 'contact@citroen.mk'),
458 ('SEAT', '1950-05-09', '+38977909909', 'info@seat.mk'),
459 ('Dacia', '1966-08-01', '+38978111000', 'sales@dacia.mk'),
460 ('Alfa Romeo', '1910-06-24', '+38970222000', 'office@alfaromeo.mk'),
461 ('Jaguar', '1922-09-04', '+38971333000', 'contact@jaguar.mk');
462
463with brand_models as (select 'Toyota' as b_name,
464 array ['Corolla', 'Camry', 'Yaris', 'RAV4', 'Prius', 'Highlander'] as m_names
465 union all
466 select 'Volkswagen', array ['Golf', 'Polo', 'Passat', 'Tiguan', 'Touareg', 'Arteon']
467 union all
468 select 'Ford', array ['Fiesta', 'Focus', 'Mondeo', 'Mustang', 'Kuga', 'Puma']
469 union all
470 select 'Honda', array ['Civic', 'Accord', 'CR-V', 'HR-V', 'Jazz']
471 union all
472 select 'Chevrolet', array ['Spark', 'Malibu', 'Camaro', 'Tahoe', 'Equinox']
473 union all
474 select 'Nissan', array ['Micra', 'Qashqai', 'X-Trail', 'Altima', 'Leaf', 'Juke']
475 union all
476 select 'Hyundai', array ['i10', 'i20', 'i30', 'Tucson', 'Santa Fe', 'Kona']
477 union all
478 select 'Kia', array ['Picanto', 'Rio', 'Ceed', 'Sportage', 'Sorento', 'Stonic']
479 union all
480 select 'Mercedes-Benz', array ['A-Class', 'C-Class', 'E-Class', 'S-Class', 'GLC', 'GLE']
481 union all
482 select 'BMW', array ['1 Series', '3 Series', '5 Series', '7 Series', 'X3', 'X5']
483 union all
484 select 'Audi', array ['A3', 'A4', 'A6', 'Q3', 'Q5', 'Q7']
485 union all
486 select 'Peugeot', array ['208', '308', '508', '2008', '3008', '5008']
487 union all
488 select 'Renault', array ['Clio', 'Megane', 'Captur', 'Kadjar', 'Scenic', 'Twingo']
489 union all
490 select 'Fiat', array ['500', 'Panda', 'Tipo', 'Punto', 'Ducato']
491 union all
492 select 'Skoda', array ['Fabia', 'Octavia', 'Superb', 'Karoq', 'Kodiaq', 'Kamiq']
493 union all
494 select 'Volvo', array ['XC40', 'XC60', 'XC90', 'S60', 'V60', 'V90']
495 union all
496 select 'Mazda', array ['Mazda2', 'Mazda3', 'Mazda6', 'CX-30', 'CX-5']
497 union all
498 select 'Subaru', array ['Impreza', 'Outback', 'Forester', 'Crosstrek', 'Legacy']
499 union all
500 select 'Porsche', array ['911', 'Cayenne', 'Macan', 'Panamera', 'Taycan']
501 union all
502 select 'Lexus', array ['IS', 'ES', 'RX', 'NX', 'UX', 'LS']
503 union all
504 select 'Jeep', array ['Wrangler', 'Cherokee', 'Grand Cherokee', 'Renegade', 'Compass']
505 union all
506 select 'Land Rover', array ['Defender', 'Discovery', 'Range Rover', 'Evoque', 'Velar']
507 union all
508 select 'Tesla', array ['Model S', 'Model 3', 'Model X', 'Model Y']
509 union all
510 select 'Mitsubishi', array ['Colt', 'Lancer', 'Outlander', 'ASX', 'Eclipse Cross']
511 union all
512 select 'Suzuki', array ['Swift', 'Vitara', 'Jimny', 'Ignis', 'S-Cross']
513 union all
514 select 'Citroen', array ['C3', 'C4', 'C5 Aircross', 'Berlingo']
515 union all
516 select 'SEAT', array ['Ibiza', 'Leon', 'Arona', 'Ateca', 'Tarraco']
517 union all
518 select 'Dacia', array ['Sandero', 'Logan', 'Duster', 'Jogger', 'Spring']
519 union all
520 select 'Alfa Romeo', array ['Giulia', 'Stelvio', 'Tonale', 'Giulietta']
521 union all
522 select 'Jaguar', array ['XE', 'XF', 'F-Pace', 'E-Pace', 'I-Pace']),
523 expanded_models as (
524 select b_name, unnest(m_names) as m_name
525 from brand_models)
526insert
527into Model (name, engine_capacity_cc, body_style, brand_id, model_fuel_type_catalog_id, model_transmission_catalog_id)
528select e.m_name,
529 case
530 when ft.type = 'Electric' then null
531 else (array [998, 1198, 1398, 1498, 1598, 1968, 1998, 2498, 2998])[floor(random() * 9 + 1)]
532 end as engine_capacity_cc,
533
534 case
535 when e.m_name ilike '%X%' or e.m_name ilike '%Q%' or e.m_name ilike '%Cross%' or e.m_name ilike '%Tiguan%' or
536 e.m_name ilike '%Pace%' then 'SUV'
537 when e.m_name in ('Corolla', 'Passat', 'Mondeo', 'Accord', 'A4', '3 Series', 'Octavia', 'Superb', 'Giulia')
538 then 'Sedan'
539 when e.m_name in ('Golf', 'Polo', 'Fiesta', 'Clio', 'A3', '208', 'Sandero', 'Leon') then 'Hatchback'
540 else (array ['Sedan', 'Hatchback', 'SUV', 'Station Wagon', 'Coupe', 'Minivan'])[floor(random() * 6 + 1)]
541 end as body_style,
542
543 b.id as brand_id,
544 ft.id as model_fuel_type_catalog_id,
545 tr.id as model_transmission_catalog_id
546from expanded_models e
547 join Brand b on b.name = e.b_name
548 cross join lateral (
549 select id, type
550 from ModelFuelTypeCatalog
551 where (e.b_name = 'Tesla' and type = 'Electric')
552 or (e.b_name != 'Tesla')
553 order by random()
554 limit 1
555 ) ft
556 cross join lateral (
557 select id
558 from ModelTransmissionCatalog
559 where (ft.type = 'Electric' and transmission = 'Automatic')
560 or (ft.type != 'Electric')
561 order by random()
562 limit 1
563 ) tr;
564
565with model_pool as (select id, body_style, row_number() over () as rn
566 from Model),
567 max_model as (select count(*) as max_val
568 from model_pool)
569insert
570into Vehicle (VIN, passenger_capacity, year, wheelchair_accessible, model_id, category_id)
571select upper(substring(md5(random()::text || s.i::text) from 1 for 17)) as VIN,
572 floor(random() * 4 + 1)::int as passenger_capacity,
573 floor(random() * (2026 - 2010 + 1) + 2010)::int as year,
574 case
575 when mp.body_style in ('Minivan', 'SUV') then random() < 0.15
576 when mp.body_style = 'Station Wagon' then random() < 0.05
577 else random() < 0.01
578 end as wheelchair_accessible,
579
580 mp.id as model_id,
581 (select id from DriverLicenseCategory where category_name = 'B') as category_id
582
583from generate_series(1, 18000) as s(i)
584 join max_model mm on true
585 join model_pool mp on mp.rn = floor(random() * mm.max_val + 1)
586on conflict (VIN) do nothing;
587
588with vehicle_pool as (select VIN, row_number() over () as rn
589 from Vehicle),
590 company_pool as (select id, row_number() over () as rn
591 from Company),
592 freelancer_pool as (select driver_user_id, row_number() over () as rn
593 from FreelanceDriver),
594 counts as (select (select count(*) from Company) as c_count,
595 (select count(*) from FreelanceDriver) as f_count),
596 area_codes as (select id,
597 case
598 when name in
599 ('Centar', 'Aerodrom', 'Karposh', 'Chair', 'Kisela Voda', 'Gazi Baba', 'Butel',
600 'Gjorche Petrov', 'Saraj', 'Shuto Orizari', 'Arachinovo', 'Chucher-Sandevo',
601 'Ilinden', 'Petrovec', 'Sopishte', 'Studenichani', 'Zelenikovo') then 'SK'
602 when name = 'Bitola' then 'BT'
603 when name = 'Tetovo' then 'TE'
604 when name = 'Gostivar' then 'GV'
605 when name = 'Kumanovo' then 'KU'
606 when name = 'Ohrid' then 'OH'
607 when name = 'Struga' then 'SU'
608 when name = 'Prilep' then 'PP'
609 when name = 'Veles' then 'VE'
610 when name = 'Strumica' then 'SR'
611 when name = 'Shtip' then 'ST'
612 when name = 'Kochani' then 'KO'
613 when name = 'Kichevo' then 'KI'
614 when name = 'Kavadarci' then 'KA'
615 when name = 'Gevgelija' then 'GE'
616 when name = 'Radovish' then 'RA'
617 when name = 'Kriva Palanka' then 'KP'
618 when name = 'Debar' then 'DB'
619 when name = 'Resen' then 'RE'
620 when name = 'Sveti Nikole' then 'SN'
621 when name = 'Negotino' then 'NE'
622 when name = 'Delchevo' then 'DE'
623 when name = 'Vinica' then 'VI'
624 when name = 'Probistip' then 'PS'
625 when name = 'Berovo' then 'BE'
626 when name = 'Kratovo' then 'KR'
627 when name = 'Makedonski Brod' then 'MB'
628 when name = 'Valandovo' then 'VA'
629 when name = 'Krushevo' then 'KS'
630 when name = 'Demir Hisar' then 'DH'
631 when name = 'Pehchevo' then 'PE'
632 when name = 'Vevchani' then 'VV'
633 else 'SK'
634 end as prefix
635 from Area),
636 company_primary_area as (select ca.company_id, max(ac.prefix) as prefix
637 from Company_Area ca
638 join area_codes ac on ca.area_id = ac.id
639 group by ca.company_id),
640 base_company as (select cp.id as company_id,
641 null::int as freelance_driver_user_id,
642 vp.VIN,
643 '2023-01-01'::date + (vp.rn % 365)::int as from_date,
644 coalesce(cpa.prefix, 'SK') as plate_prefix,
645 null::date as to_date
646 from company_pool cp
647 join vehicle_pool vp on vp.rn = cp.rn
648 left join company_primary_area cpa on cpa.company_id = cp.id),
649 base_freelancer as (select null::int as company_id,
650 fp.driver_user_id as freelance_driver_user_id,
651 vp.VIN,
652 '2023-01-01'::date + (vp.rn % 365)::int as from_date,
653 (array ['SK', 'SK', 'BT', 'TE', 'KU', 'OH', 'SR', 'ST', 'PP'])[(vp.rn % 9) + 1] as plate_prefix,
654 null::date as to_date
655 from freelancer_pool fp
656 join counts c on true
657 join vehicle_pool vp on vp.rn = fp.rn + c.c_count),
658 base_extra as (select cp.id as company_id,
659 null::int as freelance_driver_user_id,
660 vp.VIN,
661 '2023-01-01'::date + (vp.rn % 365)::int as from_date,
662 COALESCE(cpa.prefix, 'SK') as plate_prefix,
663 null::date as to_date
664 from vehicle_pool vp
665 join counts c on true
666 join company_pool cp on cp.rn = (vp.rn % c.c_count) + 1
667 left join company_primary_area cpa on cpa.company_id = cp.id
668 where vp.rn > (c.c_count + c.f_count)),
669 current_ownerships as (select *, row_number() over () as unique_seq
670 from (select *
671 from base_company
672 union all
673 select *
674 from base_freelancer
675 union all
676 select *
677 from base_extra) t),
678 past_ownership_1 as (select cp.id as company_id,
679 null::int as freelance_driver_user_id,
680 co.VIN,
681 co.from_date - (700 + (co.unique_seq % 500))::int as from_date,
682 co.from_date - (1 + (co.unique_seq % 30))::int as to_date,
683 COALESCE(cpa.prefix, 'SK') as plate_prefix,
684 co.unique_seq
685 from current_ownerships co
686 join counts c on true
687 join company_pool cp on cp.rn = ((co.unique_seq + 15) % c.c_count) + 1
688 left join company_primary_area cpa on cpa.company_id = cp.id
689 where (co.unique_seq % 100) < 75
690 ),
691 past_ownership_2 as (select cp.id as company_id,
692 null::int as freelance_driver_user_id,
693 p1.VIN,
694 p1.from_date - (700 + (p1.unique_seq % 500))::int as from_date,
695 p1.from_date - (1 + (p1.unique_seq % 30))::int as to_date,
696 coalesce(cpa.prefix, 'SK') as plate_prefix,
697 p1.unique_seq
698 from past_ownership_1 p1
699 join counts c on true
700 join company_pool cp on cp.rn = ((p1.unique_seq + 88) % c.c_count) + 1
701 left join company_primary_area cpa on cpa.company_id = cp.id
702 where (p1.unique_seq % 100) < 35
703 ),
704 all_ownerships as (select company_id, freelance_driver_user_id, VIN, from_date, to_date, plate_prefix
705 from current_ownerships
706 union all
707 select company_id, freelance_driver_user_id, VIN, from_date, to_date, plate_prefix
708 from past_ownership_1
709 union all
710 select company_id, freelance_driver_user_id, VIN, from_date, to_date, plate_prefix
711 from past_ownership_2)
712
713insert
714into VehicleOwnership (vehicle_VIN, company_id, freelance_driver_user_id, from_date, to_date, license_plate)
715select VIN,
716 company_id,
717 freelance_driver_user_id,
718 from_date,
719 to_date,
720 plate_prefix || '-' || floor(random() * 9000 + 1000)::text || '-' ||
721 chr((floor(random() * 26) + 65)::int) ||
722 chr((floor(random() * 26) + 65)::int)
723from all_ownerships;
724
725
726with company_pool as (select id, row_number() over () as rn
727 from Company),
728 c_count as (select count(*) as val
729 from company_pool),
730 nf_pool as (select d.user_id, row_number() over () as rn
731 from Driver d
732 left join FreelanceDriver fd on d.user_id = fd.driver_user_id
733 where fd.driver_user_id is null),
734 f_pool as (select driver_user_id as user_id, row_number() over () as rn
735 from FreelanceDriver),
736 dispatcher_pool as (select user_id as user_id, row_number() over () as rn
737 from dispatcher),
738 nf_current as (select np.user_id as driver_user_id,
739 '2022-01-01'::date + (np.rn % 700)::int as start_date,
740 null::date as end_date,
741 cp.id as company_id
742 from nf_pool np
743 cross join c_count c
744 join company_pool cp on cp.rn = (np.rn % c.val) + 1
745 where (np.rn % 100) < 90),
746 nf_past_1 as (select np.user_id as driver_user_id,
747 '2019-01-01'::date + (np.rn % 500)::int as start_date,
748 '2021-12-01'::date - (np.rn % 300)::int as end_date,
749 cp.id as company_id
750 from nf_pool np
751 cross join c_count c
752 join company_pool cp on cp.rn = ((np.rn + 17) % c.val) + 1
753 where (np.rn % 100) < 30),
754 nf_past_2 as (select np.user_id as driver_user_id,
755 '2015-01-01'::date + (np.rn % 600)::int as start_date,
756 '2018-12-01'::date - (np.rn % 300)::int as end_date,
757 cp.id as company_id
758 from nf_pool np
759 cross join c_count c
760 join company_pool cp on cp.rn = ((np.rn + 17 + (np.rn % 2)) % c.val) + 1
761 where (np.rn % 100) < 15),
762 f_past as (select fp.user_id as driver_user_id,
763 '2018-01-01'::date + (fp.rn % 1000)::int as start_date,
764 '2022-12-31'::date - (fp.rn % 500)::int as end_date,
765 cp.id as company_id
766 from f_pool fp
767 cross join c_count c
768 join company_pool cp on cp.rn = (fp.rn % c.val) + 1
769 where (fp.rn % 100) < 40),
770 dispatcher_current as (select dp.user_id as driver_user_id,
771 '2022-01-01'::date + (dp.rn % 700)::int as start_date,
772 null::date as end_date,
773 cp.id as company_id
774 from dispatcher_pool dp
775 cross join c_count c
776 join company_pool cp on cp.rn = (dp.rn % c.val) + 1
777 where (dp.rn % 100) < 90),
778 dispatcher_past as (select dp.user_id as driver_user_id,
779 '2019-01-01'::date + (dp.rn % 700)::int as start_date,
780 '2021-12-31'::date - (dp.rn % 500)::int as end_date,
781 cp.id as compnay_id
782 from dispatcher_pool as dp
783 cross join c_count c
784 join company_pool cp on cp.rn = (dp.rn & c.val) + 1
785 where (dp.rn % 100) < 30),
786 all_employment as (select *
787 from nf_current
788 union all
789 select *
790 from nf_past_1
791 union all
792 select *
793 from nf_past_2
794 union all
795 select *
796 from f_past
797 union all
798 select *
799 from dispatcher_current
800 union all
801 select *
802 from dispatcher_past)
803
804insert
805into EmploymentHistory (employee_user_id, start_date, end_date, company_id)
806select driver_user_id, start_date, end_date, company_id
807from all_employment
808on conflict do nothing;
809
810
811with vehicle_age as (select VIN,
812 extract(year from current_date) - year as age
813 from Vehicle),
814 vehicle_service_counts as (select VIN,
815 case
816 when age >= 10 then floor(random() * 6 + 7)::int
817 when age >= 5 then floor(random() * 4 + 4)::int
818 when age >= 2 then floor(random() * 3 + 1)::int
819 else floor(random() * 2)::int
820 end as num_services
821 from vehicle_age),
822 expanded_services as (
823 select vsc.VIN, s.i as service_index
824 from vehicle_service_counts vsc
825 cross join lateral generate_series(1, vsc.num_services) as s(i)
826 where vsc.num_services > 0),
827 service_records as (select es.VIN,
828 o.service_date,
829 floor(random() * 23500 + 1500)::numeric(19, 2) as service_price,
830 floor(random() * 7 + 1)::int as issue_type
831 from expanded_services es
832 cross join lateral (
833 select vo.from_date +
834 (random() * (coalesce(vo.to_date, current_date) - vo.from_date))::int as service_date
835 from VehicleOwnership vo
836 where vo.vehicle_VIN = es.VIN
837 order by random()
838 limit 1
839 ) o)
840insert
841into ServiceHistory (date, price, currency_catalog_id, fault_description, fix_description, vehicle_VIN)
842select service_date,
843 service_price,
844 (select id from CurrencyCatalog where currency = 'MKD'),
845
846 (array [
847 'Routine maintenance interval reached.',
848 'Squeaking noise when applying brakes.',
849 'Check Engine light illuminated on dashboard.',
850 'Air conditioning system blowing warm air.',
851 'Vehicle pulls to the right during driving.',
852 'Engine oil low and dirty.',
853 'Battery failing to hold charge.'
854 ])[issue_type],
855
856 (array [
857 'Performed comprehensive multipoint inspection, replaced oil and filters.',
858 'Replaced front and rear brake pads, resurfaced rotors.',
859 'Scanned OBD-II, replaced faulty O2 sensor and cleared codes.',
860 'Evacuated and recharged A/C refrigerant, added UV dye to check for leaks.',
861 'Performed 4-wheel alignment and adjusted camber/toe angles.',
862 'Drained engine oil, replaced oil filter, added full synthetic oil.',
863 'Installed new 12V battery and cleaned terminal corrosion.'
864 ])[issue_type],
865
866 VIN
867from service_records;
868
869with ownership_spans as (select vehicle_VIN,
870 from_date,
871 coalesce(to_date, current_date) as end_date
872 from VehicleOwnership),
873 registration_series as (select os.vehicle_VIN,
874 os.from_date as ownership_from_date,
875 (os.from_date + (s.i || ' years')::interval)::date as registration_date,
876 (os.from_date + ((s.i + 1) || ' years')::interval)::date as expiration_date
877 from ownership_spans os
878 cross join lateral generate_series(0,
879 floor((os.end_date - os.from_date) / 365.25)::int) as s(i))
880insert
881into Registration (vehicle_VIN, registration_date, expiration_date, vehicle_ownership_from_date)
882select vehicle_VIN,
883 registration_date,
884 expiration_date,
885 ownership_from_date
886from registration_series
887where registration_date < current_date;
888
889with company_pool as (select id, row_number() over () as rn
890 from Company),
891 freelancer_pool as (select driver_user_id, row_number() over () as rn
892 from FreelanceDriver),
893 counts as (select (select count(*) from Company) as c_count,
894 (select count(*) from FreelanceDriver) as f_count),
895 customer_pool as (
896 select user_id, row_number() over () as rn
897 from Customer),
898 expanded_preferences as (select cp.user_id as customer_user_id,
899 s.i as seqno,
900 cp.rn as customer_rn,
901 case when random() < 0.7 then 1 else 2 end as pref_type
902 from customer_pool cp
903 cross join lateral generate_series(1, floor(random() * 5 + 1 + (cp.rn * 0))::int) as s(i))
904insert
905into CustomerPreference (seqno, customer_user_id, freelance_driver_user_id, company_id)
906select ep.seqno,
907 ep.customer_user_id,
908 fp.driver_user_id as freelance_driver_user_id,
909 cmp.id as company_id
910from expanded_preferences ep
911 cross join counts c
912 left join company_pool cmp
913 on ep.pref_type = 1 and cmp.rn = ((ep.customer_rn + ep.seqno * 13) % c.c_count) + 1
914 left join freelancer_pool fp
915 on ep.pref_type = 2 and fp.rn = ((ep.customer_rn + ep.seqno * 17) % c.f_count) + 1
916on conflict (customer_user_id, seqno) do nothing;
917
918
919create table macedonia_atlas
920(
921 name varchar(100),
922 lat double precision,
923 lon double precision,
924 category varchar(20)
925);
926
927insert into macedonia_atlas (name, lat, lon, category)
928values
929('Monastery of St. George the Victorious, Rajchica', 41.5019, 20.5361, 'tourist'),
930('Church of St. Panteleimon, Gorno Nerezi', 41.9768, 21.3751, 'tourist'),
931('Osogovo Monastery, Kriva Palanka', 42.2081, 22.3619, 'tourist'),
932('Lesnovo Monastery', 42.0119, 22.2272, 'tourist'),
933('Treskavec Monastery, Prilep', 41.3991, 21.5342, 'tourist'),
934('Zrze Monastery', 41.5211, 21.3422, 'tourist'),
935('Arabati Baba Tekke, Tetovo', 42.0044, 20.9631, 'tourist'),
936('Painted Mosque (Sarena Dzamija), Tetovo', 42.0061, 20.9669, 'tourist'),
937('Holy Mother of God (Perivleptos), Ohrid', 41.1147, 20.7956, 'tourist'),
938('St. Clement and Panteleimon (Plaoshnik)', 41.1122, 20.7911, 'tourist'),
939
940('Stobi Ancient City', 41.5519, 21.9744, 'tourist'),
941('Bargala Ancient Site', 41.7911, 22.2844, 'tourist'),
942('Scupi Archaeological Site', 42.0167, 21.3944, 'tourist'),
943('Isar Fortress, Shtip', 41.7375, 22.1869, 'tourist'),
944('Vinica Fortress (Kale)', 41.8822, 22.5036, 'tourist'),
945('Tauresium (Birthplace of Justinian)', 41.8986, 21.6111, 'tourist'),
946('Stuberra Ancient City', 41.2464, 21.3853, 'tourist'),
947
948('Duf Waterfall, Rostusha', 41.6119, 20.5983, 'tourist'),
949('Koleshino Waterfall', 41.3803, 22.8106, 'tourist'),
950('Vevchani Springs', 41.2406, 20.5928, 'tourist'),
951('Cave Vrelo, Matka', 41.9386, 21.2981, 'tourist'),
952('Cave Peshna, Makedonski Brod', 41.5517, 21.1894, 'tourist'),
953('Markovi Kuli (Marko''s Towers)', 41.3622, 21.5394, 'tourist'),
954('Demir Kapija Gorge', 41.4036, 22.2536, 'tourist'),
955('Pelister Eyes (Glacial Lakes)', 41.0022, 21.2011, 'tourist'),
956('Berovo Lake', 41.6833, 22.9167, 'tourist'),
957('Doiran Lake Waterfront', 41.1853, 22.6917, 'tourist'),
958('Kozjak Lake Viewpoint', 41.8797, 21.1844, 'tourist'),
959('Tikvesh Lake', 41.3361, 21.9542, 'tourist'),
960('Shar Planina - Popova Shapka', 42.0142, 20.8842, 'tourist'),
961
962('Memorial House of Mother Teresa', 41.9939, 21.4308, 'tourist'),
963('Makedonium (Ilinden Memorial)', 41.3711, 21.2486, 'tourist'),
964('Millennium Cross Summit', 41.9647, 21.3944, 'tourist'),
965('Clock Tower, Bitola', 41.0308, 21.3353, 'tourist'),
966('Clock Tower, Prilep', 41.3444, 21.5542, 'tourist'),
967('Museum on Water (Bay of Bones)', 40.9942, 20.7994, 'tourist'),
968('Tumba Madzari Neolithic Village', 41.9961, 21.5019, 'tourist'),
969('Aqueduct Skopje', 42.0231, 21.4183, 'tourist'),
970
971('Galichnik Village', 41.5936, 20.6517, 'tourist'),
972('Janche Village', 41.5833, 20.6167, 'tourist'),
973('Lazaropole', 41.5367, 20.7019, 'tourist'),
974('Malovishte Village', 41.0322, 21.1306, 'tourist'),
975('Kratovo Stone Bridges', 42.0792, 22.1753, 'tourist'),
976('Braichino Village', 40.9167, 21.1500, 'tourist'),
977('Zrze Viewpoint', 41.5222, 21.3411, 'tourist'),
978('Smolare Eco-path', 41.3653, 22.9053, 'tourist'),
979('Belchishko Blato (Wetland)', 41.2917, 20.9167, 'tourist'),
980('St. Bogorodica Eleusa, Veljusa', 41.4772, 22.5661, 'tourist'),
981('St. Leontius, Vodocha', 41.4489, 22.5881, 'tourist');
982
983
984select count(*)
985from request;
986
987
988insert into request (customer_user_id, start_latitude, start_longitude, end_latitude, end_longitude,
989 timestamp, number_of_adult_passengers, number_of_children, female_driver,
990 luggage, luggage_count, baby_seat_count)
991with generated_users as (select user_id
992 from customer
993 cross join generate_series(1, ceil(10000000.0 / (select count(*) from customer))::int)
994 order by random()),
995 users as (select user_id, row_number() over () as rn
996 from generated_users
997 limit 10000000),
998
999 user_areas as (select cp.customer_user_id,
1000 array_agg(coalesce(ca.area_id, fd.area_id)) as area_array,
1001 array_length(array_agg(coalesce(ca.area_id, fd.area_id)), 1) as arr_len
1002 from customerpreference cp
1003 left join company_area ca on ca.company_id = cp.company_id
1004 left join freelancedriver fd on cp.freelance_driver_user_id = fd.driver_user_id
1005 group by cp.customer_user_id),
1006
1007 generated_atlas as (select lat, lon
1008 from macedonia_atlas
1009 cross join generate_series(1,
1010 ceil(10000000.0 / (select count(*) from macedonia_atlas))::int)
1011 order by random()),
1012 atlas_pool as (select lat, lon, row_number() over () as rn
1013 from generated_atlas
1014 limit 10000000),
1015
1016 luggage_pool as (select case
1017 when (random() * 3 + 1)::int % 4 = 0 then true
1018 else false
1019 end as luggage,
1020 row_number() over () as rn
1021 from generate_series(1, 10000000)
1022 order by random())
1023
1024select u.user_id,
1025
1026 a.latitude + (a.radius * sqrt(random()) / 111000.0) * cos(random() * 2 * pi()) as start_latitude,
1027 a.longitude + (a.radius * sqrt(random()) / 83000.0) * sin(random() * 2 * pi()) as start_longitude,
1028
1029 e_loc.lat as end_latitude,
1030 e_loc.lon as end_longitude,
1031 (now() - interval '20 days' * random()) as timestamp,
1032 (random() * 3 + 1)::int as number_of_adult_passengers,
1033 (round(random()))::int as number_of_children,
1034 (random() < 0.15) as female_driver,
1035 lp.luggage,
1036 case
1037 when lp.luggage = true then (random() * 2 + 1)
1038 else 0
1039 end as luggage_count,
1040 (round(random()))::int as baby_seat_count
1041from generate_series(1, 10000000) as s(i)
1042 join users u on u.rn = s.i
1043
1044 join user_areas ua on ua.customer_user_id = u.user_id
1045
1046 cross join lateral (
1047 select ua.area_array[floor(random() * ua.arr_len + 1 + (s.i * 0))::int] as selected_area_id
1048 ) rand_pref
1049 join area a on a.id = rand_pref.selected_area_id
1050
1051 join atlas_pool e_loc on e_loc.rn = s.i
1052 join luggage_pool lp on lp.rn = s.i;
1053
1054
1055with days as (select generate_series(
1056 (current_date - interval '30 days')::date,
1057 current_date::date,
1058 '1 day'::interval
1059 )::date as day_date),
1060 active_employment as (select eh.employee_user_id,
1061 eh.company_id,
1062 d.day_date,
1063 row_number()
1064 over (partition by eh.company_id, d.day_date order by eh.employee_user_id) -
1065 1 as driver_rn
1066 from days d
1067 join EmploymentHistory eh
1068 on eh.start_date <= d.day_date and
1069 (eh.end_date is null or eh.end_date >= d.day_date)
1070 join driver on driver.user_id = eh.employee_user_id),
1071 active_company_cars as (select vo.vehicle_VIN,
1072 vo.company_id,
1073 d.day_date,
1074 row_number() over (partition by vo.company_id, d.day_date order by vo.vehicle_VIN) -
1075 1 as car_rn,
1076 count(*) over (partition by vo.company_id, d.day_date) as total_cars
1077 from days d
1078 join VehicleOwnership vo
1079 on vo.company_id is not null and vo.from_date <= d.day_date and
1080 (vo.to_date is null or vo.to_date >= d.day_date)),
1081 company_assignments as (select ae.day_date,
1082 ae.employee_user_id as id_driver,
1083 acc.vehicle_VIN as vin_vehicle,
1084 (ae.driver_rn / acc.total_cars) as shift_id
1085 from active_employment ae
1086 join active_company_cars acc
1087 on ae.company_id = acc.company_id and ae.day_date = acc.day_date
1088 where acc.car_rn = (ae.driver_rn % acc.total_cars)
1089 and (ae.driver_rn / acc.total_cars) < 3),
1090 active_freelancers as (select fd.driver_user_id, d.day_date
1091 from days d
1092 join FreelanceDriver fd on true
1093 left join EmploymentHistory eh
1094 on fd.driver_user_id = eh.employee_user_id and
1095 eh.start_date <= d.day_date and
1096 (eh.end_date is null or eh.end_date >= d.day_date)
1097 where eh.company_id is null),
1098 freelancer_cars as (select vo.vehicle_VIN,
1099 vo.freelance_driver_user_id,
1100 d.day_date,
1101 row_number()
1102 over (partition by vo.freelance_driver_user_id, d.day_date order by vo.vehicle_VIN) as rn
1103 from days d
1104 join VehicleOwnership vo
1105 on vo.freelance_driver_user_id is not null and vo.from_date <= d.day_date and
1106 (vo.to_date is null or vo.to_date >= d.day_date)),
1107 freelancer_assignments as (select af.day_date,
1108 af.driver_user_id as id_driver,
1109 fc.vehicle_VIN as vin_vehicle,
1110 0 as shift_id
1111 from active_freelancers af
1112 join freelancer_cars fc on af.driver_user_id = fc.freelance_driver_user_id and
1113 af.day_date = fc.day_date
1114 where fc.rn = 1
1115 ),
1116 all_assignments as (select *
1117 from company_assignments
1118 union all
1119 select *
1120 from freelancer_assignments)
1121insert
1122into driver_vehicle (vin_vehicle, id_driver, time_from, time_to)
1123select vin_vehicle,
1124 id_driver,
1125 day_date + (6 + shift_id * 8) * interval '1 hour' as time_from,
1126
1127 case
1128 when (day_date + (14 + shift_id * 8) * interval '1 hour') > current_timestamp then null
1129 else (day_date + (14 + shift_id * 8) * interval '1 hour')
1130 end as time_to
1131from all_assignments
1132where (day_date + (6 + shift_id * 8) * interval '1 hour') <= current_timestamp;
1133
1134with randomized_pricing_info as (select id, row_number() over () as rn
1135 from generate_series(1, 1450)
1136 cross join pricinginfo
1137 limit 10000),
1138 freelancers_with_rn as (select driver_user_id, row_number() over () as rn
1139 from freelancedriver
1140 order by random())
1141update freelancedriver
1142set pricing_info_id = (select randomized_pricing_info.id
1143 from freelancers_with_rn
1144 join randomized_pricing_info on freelancers_with_rn.rn = randomized_pricing_info.rn
1145 where freelancers_with_rn.driver_user_id = freelancedriver.driver_user_id);
1146
1147with randomized_area as (select id, row_number() over () as rn
1148 from generate_series(1, 109)
1149 cross join area
1150 limit 10000),
1151 freelancers_with_rn as (select driver_user_id, row_number() over () as rn
1152 from freelancedriver
1153 order by random())
1154update freelancedriver
1155set area_id = (select randomized_area.id
1156 from freelancers_with_rn
1157 join randomized_area on freelancers_with_rn.rn = randomized_area.rn
1158 where freelancers_with_rn.driver_user_id = freelancedriver.driver_user_id);
1159
1160
1161create index idx_temp_driver_car_times on driver_vehicle (id_driver, time_from, time_to);
1162create index idx_customerpreference on customerpreference (customer_user_id);
1163create index idx_employmenthistory on employmenthistory (employee_user_id, company_id);
1164
1165insert into offer(created_at, request_id, dispatcher_user_id, driver_user_id, price, currency_catalog_id, eta,
1166 customer_user_id)
1167with CompanyDispatchers as (select eh.company_id,
1168 array_agg(d.user_id) as disp_array,
1169 array_length(array_agg(d.user_id), 1) as arr_len
1170 from employmenthistory eh
1171 join dispatcher d on d.user_id = eh.employee_user_id
1172 where eh.end_date is null
1173 group by eh.company_id),
1174
1175 CompanyPricing as (select company_id,
1176 array_agg(pricing_info_id) as price_array,
1177 array_length(array_agg(pricing_info_id), 1) as arr_len
1178 from company_area
1179 group by company_id),
1180
1181 CompanyDrivers as (select eh.company_id,
1182 array_agg(eh.employee_user_id) as driver_array,
1183 array_length(array_agg(eh.employee_user_id), 1) as arr_len
1184 from employmenthistory eh
1185 join driver d on d.user_id = eh.employee_user_id
1186 where eh.end_date is null
1187 group by eh.company_id)
1188
1189select (request.timestamp + interval '2 minute' * random()) as created_at,
1190 request.id as request_id,
1191 cd.disp_array[floor(random() * cd.arr_len + 1)::int] as dispatcher_user_id,
1192
1193 cdr.driver_array[floor(random() * cdr.arr_len + 1 + (request.id * 0))::int] as driver_user_id,
1194
1195 greatest(
1196 case
1197 when lower(trim(pricinginfo.unit::text)) = 'kilometer' then
1198 (sqrt(pow(request.start_latitude - request.end_latitude, 2) +
1199 pow(request.start_longitude - request.end_longitude, 2)) * 111.0) * pricinginfo.value
1200 when lower(trim(pricinginfo.unit::text)) = 'minute' then
1201 (sqrt(pow(request.start_latitude - request.end_latitude, 2) +
1202 pow(request.start_longitude - request.end_longitude, 2)) * 111.0 / 50.0 * 60.0) *
1203 pricinginfo.value
1204 else 5.00
1205 end,
1206 1.50)::numeric(19, 2) as price,
1207
1208 pricinginfo.currency_id as currency_catalog_id,
1209 (request.timestamp + interval '15 minute' * random()) as eta,
1210 request.customer_user_id as customer_user_id
1211
1212from request
1213 join customerpreference on customerpreference.customer_user_id = request.customer_user_id
1214 and customerpreference.freelance_driver_user_id is null
1215 join CompanyPricing cpr on cpr.company_id = customerpreference.company_id
1216 cross join lateral (
1217 select cpr.price_array[floor(random() * cpr.arr_len + 1 + (request.id * 0))::int] as pricing_info_id
1218 ) rand_price
1219 join pricinginfo on pricinginfo.id = rand_price.pricing_info_id
1220 join CompanyDispatchers cd on cd.company_id = customerpreference.company_id
1221 join CompanyDrivers cdr on cdr.company_id = customerpreference.company_id
1222
1223union all
1224
1225select (request.timestamp + interval '2 minute' * random()) as created_at,
1226 request.id as request_id,
1227 null as dispatcher_user_id,
1228 freelancedriver.driver_user_id as driver_user_id,
1229 greatest(
1230 case
1231 when lower(trim(pricinginfo.unit::text)) = 'kilometer' then
1232 (sqrt(pow(request.start_latitude - request.end_latitude, 2) +
1233 pow(request.start_longitude - request.end_longitude, 2)) * 111.0) * pricinginfo.value
1234 when lower(trim(pricinginfo.unit::text)) = 'minute' then
1235 (sqrt(pow(request.start_latitude - request.end_latitude, 2) +
1236 pow(request.start_longitude - request.end_longitude, 2)) * 111.0 / 50.0 * 60.0) *
1237 pricinginfo.value
1238 else 5.00
1239 end,
1240 1.50)::numeric(19, 2) as price,
1241 pricinginfo.currency_id as currency_catalog_id,
1242 (request.timestamp + interval '15 minute' * random()) as eta,
1243 request.customer_user_id as customer_user_id
1244from request
1245 join customerpreference on customerpreference.customer_user_id = request.customer_user_id
1246 and customerpreference.freelance_driver_user_id is not null
1247 join freelancedriver on freelancedriver.driver_user_id = customerpreference.freelance_driver_user_id
1248 join pricinginfo on pricinginfo.id = freelancedriver.pricing_info_id;
1249
1250
1251
1252insert into ride(start_time, end_time, distance_traveled, vehicle_vin, driver_user_id, request_id, status, offer_id)
1253with UniqueOffers as (
1254 select id, created_at, driver_user_id, request_id
1255 from (select id,
1256 created_at,
1257 driver_user_id,
1258 request_id,
1259 row_number() over (partition by request_id order by created_at desc) as rn
1260 from offer) tmp
1261 where rn = 1),
1262 ActiveVehicles as (
1263 select distinct on (id_driver) id_driver, vin_vehicle
1264 from driver_vehicle
1265 order by id_driver, time_from desc),
1266 StagedMath as (
1267 select o.driver_user_id,
1268 o.id as offer_id,
1269 r.id as request_id,
1270 av.vin_vehicle,
1271 (sqrt(pow(r.start_latitude - r.end_latitude, 2) + pow(r.start_longitude - r.end_longitude, 2)) *
1272 111.0) as dist_km,
1273 (o.created_at + (interval '15 minute' * random())) as fixed_start_time,
1274 random() as rand_end_delay,
1275 random() as rand_dist_variance
1276 from UniqueOffers o
1277 join request r on r.id = o.request_id
1278 join ActiveVehicles av on av.id_driver = o.driver_user_id)
1279select fixed_start_time as start_time,
1280
1281 fixed_start_time + ((dist_km / 50.0 * 60.0) * interval '1 minute') +
1282 (interval '10 minute' * rand_end_delay) as end_time,
1283
1284 dist_km + (20.0 * rand_dist_variance) as distance_traveled,
1285 vin_vehicle,
1286 driver_user_id,
1287 request_id,
1288 'in_progress' as status,
1289 offer_id
1290from StagedMath;
1291
1292update offer
1293set status = 'rejected';
1294
1295update offer
1296set status = 'accepted'
1297from ride
1298where offer.id = ride.offer_id;
1299
1300insert into ChatMessage (message, timestamp, user_id_from, ride_id)
1301with ride_participants as (
1302 select r.id as ride_id,
1303 req.customer_user_id,
1304 r.driver_user_id,
1305 r.start_time
1306 from Ride r
1307 join Request req on r.request_id = req.id),
1308 ride_participants_randomized as (select *
1309 from ride_participants
1310 order by random())
1311select msg_data.msg,
1312 rp.start_time + (msg_data.offset_mins * interval '1 minute'),
1313 case when msg_data.sender = 'driver' then rp.driver_user_id else rp.customer_user_id end,
1314 rp.ride_id
1315from ride_participants_randomized rp
1316 cross join lateral (
1317 values
1318 ('I have arrived at the pickup location. I am in a silver sedan.', 'driver', -2),
1319 ('Great, I see you! Coming out now.', 'customer', -1),
1320
1321 ('Are you at the main entrance or the side gate?', 'driver', -5),
1322 ('Main entrance, right next to the coffee shop.', 'customer', -4),
1323 ('Got it, see you in a minute.', 'driver', -3),
1324
1325 ('Thank you for the smooth ride!', 'customer', 20),
1326 ('You are welcome! Have a great day.', 'driver', 22)
1327 ) as msg_data(msg, sender, offset_mins)
1328where random() < 0.10;
1329
1330
1331insert into Review (rating, comment, ride_id, customer_user_id)
1332select rating,
1333 case
1334 when rating >= 4 then
1335 (array [
1336 'Great ride, driver was very polite and professional.',
1337 'Everything went smoothly, very satisfied.',
1338 'Clean car and safe driving. Would definitely ride again.',
1339 'Driver arrived on time and the trip was comfortable.',
1340 'Excellent experience, highly recommended.'
1341 ])[floor(random() * 5 + 1)]
1342
1343 when rating >= 2 then
1344 (array [
1345 'Ride was okay, but could be improved.',
1346 'Driver was decent, but communication was lacking.',
1347 'Average experience, nothing special.',
1348 'Car was fine but arrived a bit late.',
1349 'It was fine overall, just expected a bit more.'
1350 ])[floor(random() * 5 + 1)]
1351
1352 else
1353 (array [
1354 'Driver was late and unprofessional.',
1355 'Car was not clean and ride felt unsafe.',
1356 'Very bad experience, would not recommend.',
1357 'Driver canceled last minute and caused issues.',
1358 'Uncomfortable ride and poor communication.'
1359 ])[floor(random() * 5 + 1)]
1360 end,
1361 r.id,
1362 o.customer_user_id
1363from Ride r
1364 inner join Offer o on o.id = r.offer_id
1365 cross join lateral (
1366 select round((random() * 5)::numeric, 2) as rating
1367 )
1368where random() > 0.2;
1369
1370insert into Report (ride_id,
1371 customer_user_id,
1372 message,
1373 title,
1374 created_at,
1375 latitude,
1376 longitude,
1377 reason)
1378select r.id,
1379 req.customer_user_id,
1380
1381 (array [
1382 'The driver was very rude and made the trip uncomfortable.',
1383 'I felt unsafe due to aggressive driving.',
1384 'Driver did not follow the agreed route.',
1385 'The vehicle condition was unacceptable.',
1386 'Driver was late and did not communicate properly.'
1387 ])[floor(random() * 5 + 1)],
1388
1389 (array [
1390 'Unsafe driving',
1391 'Driver behavior issue',
1392 'Route problem',
1393 'Vehicle condition',
1394 'Late arrival'
1395 ])[floor(random() * 5 + 1)],
1396
1397 current_timestamp - (random() * interval '7 days'),
1398
1399 req.start_latitude + (random() - 0.5) * 0.01,
1400 req.start_longitude + (random() - 0.5) * 0.01,
1401
1402 (array [
1403 'Reckless driving',
1404 'Rude behavior',
1405 'Vehicle hygiene',
1406 'Wrong route taken',
1407 'Driver was late'
1408 ])[floor(random() * 5 + 1)]
1409
1410from Ride r
1411 inner join request req on r.request_id = req.id
1412where random() > 0.75;
1413
1414update ride
1415set status = 'completed'
1416where random() < 0.9985;
1417
1418
1419insert into request (customer_user_id, start_latitude, start_longitude, end_latitude, end_longitude,
1420 timestamp, number_of_adult_passengers, number_of_children, female_driver,
1421 luggage, luggage_count, baby_seat_count)
1422with generated_users as (select user_id
1423 from customer
1424 cross join generate_series(1, ceil(500000.0 / (select count(*) from customer))::int)
1425 order by random()),
1426 users as (select user_id, row_number() over () as rn
1427 from generated_users
1428 limit 500000),
1429
1430 user_areas as (select cp.customer_user_id,
1431 array_agg(coalesce(ca.area_id, fd.area_id)) as area_array,
1432 array_length(array_agg(coalesce(ca.area_id, fd.area_id)), 1) as arr_len
1433 from customerpreference cp
1434 left join company_area ca on ca.company_id = cp.company_id
1435 left join freelancedriver fd on cp.freelance_driver_user_id = fd.driver_user_id
1436 group by cp.customer_user_id),
1437
1438 generated_atlas as (select lat, lon
1439 from macedonia_atlas
1440 cross join generate_series(1,
1441 ceil(500000.0 / (select count(*) from macedonia_atlas))::int)
1442 order by random()),
1443 atlas_pool as (select lat, lon, row_number() over () as rn
1444 from generated_atlas
1445 limit 500000),
1446
1447 luggage_pool as (select case
1448 when (random() * 3 + 1)::int % 4 = 0 then true
1449 else false
1450 end as luggage,
1451 row_number() over () as rn
1452 from generate_series(1, 500000)
1453 order by random())
1454
1455select u.user_id,
1456
1457 a.latitude + (a.radius * sqrt(random()) / 111000.0) * cos(random() * 2 * pi()) as start_latitude,
1458 a.longitude + (a.radius * sqrt(random()) / 83000.0) * sin(random() * 2 * pi()) as start_longitude,
1459
1460 e_loc.lat as end_latitude,
1461 e_loc.lon as end_longitude,
1462 (now() - interval '20 days' * random()) as timestamp,
1463 (random() * 3 + 1)::int as number_of_adult_passengers,
1464 (round(random()))::int as number_of_children,
1465 (random() < 0.15) as female_driver,
1466 lp.luggage,
1467 case
1468 when lp.luggage = true then (random() * 2 + 1)
1469 else 0
1470 end as luggage_count,
1471 (round(random()))::int as baby_seat_count
1472from generate_series(1, 500000) as s(i)
1473 join users u on u.rn = s.i
1474
1475 join user_areas ua on ua.customer_user_id = u.user_id
1476
1477 cross join lateral (
1478 select ua.area_array[floor(random() * ua.arr_len + 1 + (s.i * 0))::int] as selected_area_id
1479 ) rand_pref
1480 join area a on a.id = rand_pref.selected_area_id
1481
1482 join atlas_pool e_loc on e_loc.rn = s.i
1483 join luggage_pool lp on lp.rn = s.i;
1484
1485
1486insert into offer(created_at, request_id, dispatcher_user_id, driver_user_id, price, currency_catalog_id, eta,
1487 customer_user_id)
1488with CompanyDispatchers as (select eh.company_id,
1489 array_agg(d.user_id) as disp_array,
1490 array_length(array_agg(d.user_id), 1) as arr_len
1491 from employmenthistory eh
1492 join dispatcher d on d.user_id = eh.employee_user_id
1493 where eh.end_date is null
1494 group by eh.company_id),
1495
1496 CompanyPricing as (select company_id,
1497 array_agg(pricing_info_id) as price_array,
1498 array_length(array_agg(pricing_info_id), 1) as arr_len
1499 from company_area
1500 group by company_id),
1501
1502 CompanyDrivers as (select eh.company_id,
1503 array_agg(eh.employee_user_id) as driver_array,
1504 array_length(array_agg(eh.employee_user_id), 1) as arr_len
1505 from employmenthistory eh
1506 join driver d on d.user_id = eh.employee_user_id
1507 where eh.end_date is null
1508 group by eh.company_id)
1509
1510select (request.timestamp + interval '2 minute' * random()) as created_at,
1511 request.id as request_id,
1512 cd.disp_array[floor(random() * cd.arr_len + 1)::int] as dispatcher_user_id,
1513
1514 cdr.driver_array[floor(random() * cdr.arr_len + 1 + (request.id * 0))::int] as driver_user_id,
1515
1516 greatest(
1517 case
1518 when lower(trim(pricinginfo.unit::text)) = 'kilometer' then
1519 (sqrt(pow(request.start_latitude - request.end_latitude, 2) +
1520 pow(request.start_longitude - request.end_longitude, 2)) * 111.0) * pricinginfo.value
1521 when lower(trim(pricinginfo.unit::text)) = 'minute' then
1522 (sqrt(pow(request.start_latitude - request.end_latitude, 2) +
1523 pow(request.start_longitude - request.end_longitude, 2)) * 111.0 / 50.0 * 60.0) *
1524 pricinginfo.value
1525 else 5.00
1526 end,
1527 1.50)::numeric(19, 2) as price,
1528
1529 pricinginfo.currency_id as currency_catalog_id,
1530 (request.timestamp + interval '15 minute' * random()) as eta,
1531 request.customer_user_id as customer_user_id
1532
1533from (select * from request order by id desc limit 300000) request
1534 join customerpreference on customerpreference.customer_user_id = request.customer_user_id
1535 and customerpreference.freelance_driver_user_id is null
1536 join CompanyPricing cpr on cpr.company_id = customerpreference.company_id
1537 cross join lateral (
1538 select cpr.price_array[floor(random() * cpr.arr_len + 1 + (request.id * 0))::int] as pricing_info_id
1539 ) rand_price
1540 join pricinginfo on pricinginfo.id = rand_price.pricing_info_id
1541 join CompanyDispatchers cd on cd.company_id = customerpreference.company_id
1542 join CompanyDrivers cdr on cdr.company_id = customerpreference.company_id
1543
1544union all
1545
1546select (request.timestamp + interval '2 minute' * random()) as created_at,
1547 request.id as request_id,
1548 null as dispatcher_user_id,
1549 freelancedriver.driver_user_id as driver_user_id,
1550 greatest(
1551 case
1552 when lower(trim(pricinginfo.unit::text)) = 'kilometer' then
1553 (sqrt(pow(request.start_latitude - request.end_latitude, 2) +
1554 pow(request.start_longitude - request.end_longitude, 2)) * 111.0) * pricinginfo.value
1555 when lower(trim(pricinginfo.unit::text)) = 'minute' then
1556 (sqrt(pow(request.start_latitude - request.end_latitude, 2) +
1557 pow(request.start_longitude - request.end_longitude, 2)) * 111.0 / 50.0 * 60.0) *
1558 pricinginfo.value
1559 else 5.00
1560 end,
1561 1.50)::numeric(19, 2) as price,
1562 pricinginfo.currency_id as currency_catalog_id,
1563 (request.timestamp + interval '15 minute' * random()) as eta,
1564 request.customer_user_id as customer_user_id
1565from (select * from request order by id desc limit 300000) request
1566 join customerpreference on customerpreference.customer_user_id = request.customer_user_id
1567 and customerpreference.freelance_driver_user_id is not null
1568 join freelancedriver on freelancedriver.driver_user_id = customerpreference.freelance_driver_user_id
1569 join pricinginfo on pricinginfo.id = freelancedriver.pricing_info_id;
1570
1571
1572insert into waypoints(latitude, longitude, seqno, request_id)
1573select waypoint.latitude as latitude,
1574 waypoint.longitude as longitude,
1575 waypoint.number as seqno,
1576 request.id as request_id
1577from request
1578 cross join lateral (
1579 select request.start_latitude + random(0, 0.2) as latitude,
1580 request.start_longitude + random(0, 0.2) as longitude,
1581 s.i as number
1582 from generate_series(1, (random() * 3 + 1)::int) as s(i)
1583 ) waypoint
1584where random() < 0.3;
1585
1586
1587insert into location(latitude, longitude, timestamp, ride_id)
1588select waypoint.latitude as latitude,
1589 waypoint.longitude as longitude,
1590 waypoint.timestamp as timestamp,
1591 ride.id as ride_id
1592from ride
1593 join request on request.id = ride.request_id
1594 cross join lateral (
1595 select request.start_latitude + random(0, 0.2) as latitude,
1596 request.start_longitude + random(0, 0.2) as longitude,
1597 ride.start_time + s.i * random(1, 3) * interval '1 minute' as timestamp
1598 from generate_series(1, (random() * 2 + 1)::int) as s(i)
1599 ) waypoint;
1600
1601
1602
1603insert into payment(completed_ride_id, total_amount, currency_catalog_id)
1604select ride.id as completed_ride_id,
1605 offer.price as total_amount,
1606 4 as currency_catalog_id
1607from ride
1608 join offer on offer.id = ride.offer_id
1609where ride.status = 'completed';
1610
1611
1612insert into customerpayment(payment_id, customer_user_id, amount, currency_catalog_id, payment_method, transaction_id)
1613select payment.id as payment_id,
1614 request.customer_user_id as customer_user_id,
1615 payment.total_amount as amount,
1616 4 as currency_catalog_id,
1617 case
1618 when random() < 0.5 then 'CASH'::payment_method_type
1619 else 'CARD'::payment_method_type
1620 end as payment_method,
1621 gen_random_uuid()::varchar(64) as transaction_id
1622from payment
1623 join ride on ride.id = payment.completed_ride_id
1624 join request on request.id = ride.request_id;
1625
1626with LastRides as (
1627 select distinct on (r.driver_user_id) r.driver_user_id,
1628 req.end_latitude,
1629 req.end_longitude
1630 from ride r
1631 join request req on req.id = r.request_id
1632 where r.status in ('completed', 'in_progress')
1633 order by r.driver_user_id, r.start_time desc),
1634 TargetLocations as (
1635 select d.user_id,
1636 coalesce(lr.end_latitude, 40.9 + (random() * 1.3)) as target_lat,
1637 coalesce(lr.end_longitude, 20.5 + (random() * 2.5)) as target_lon
1638 from driver d
1639 left join LastRides lr on lr.driver_user_id = d.user_id)
1640update driver d
1641set latitude = tl.target_lat,
1642 longitude = tl.target_lon
1643from TargetLocations tl
1644where d.user_id = tl.user_id;