-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathspn_ensemble.py
executable file
·1458 lines (1212 loc) · 73 KB
/
spn_ensemble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import itertools
import logging
import pickle
import random
from collections import deque
from time import perf_counter
import numpy as np
import scipy.stats
from ensemble_compilation.graph_representation import Query, QueryType, AggregationType, AggregationOperationType
from ensemble_compilation.probabilistic_query import IndicatorExpectation, Expectation
from evaluation.utils import parse_what_if_query, all_operations_of_type
from spn.algorithms.Statistics import get_structure_stats
import bz2
np.random.seed(1)
logger = logging.getLogger(__name__)
class CombineSPN:
"""
An SPN built over a sub-schema.
Application a) estimate cardinality for arbitrary acyclic sub-query using
the equation Full_join_size*E(1/multiplier * 1_{c_1 Λ… Λc_n}).
E.g. We have an SPN built over Customer, Orders and Orderline. What is the
cardinality for customers joined with orders made in 2017?
Application b) cardinality per tuple of next neighbour:
Full_join_size*E(1/multiplier * 1_{c_1 Λ… Λc_n}) / next_neighbour_size
This term is required if we merge an SPN into the current cardinality estimation.
E.g. We have an SPN built over Customer and one over Orders and Orderline.
What is the cardinality for Customers, Orders and Orderline joined with
Orders.year = 2019?
Assumption is that is built over acyclic graph for now.
"""
def __init__(self, full_join_size, schema_graph, relationship_list, table_set=None):
self.full_join_size = full_join_size
self.schema_graph = schema_graph
# relationships joined for building the SPN
self.relationship_set = set()
if relationship_list is None:
relationship_list = []
for relationship in relationship_list:
assert (self.schema_graph.relationship_dictionary.get(relationship) is not None)
self.relationship_set.add(relationship)
# set of tables included
if table_set is None:
self.table_set = set()
else:
self.table_set = table_set
for relationship in relationship_list:
relationship_obj = self.schema_graph.relationship_dictionary.get(relationship)
self.table_set.add(relationship_obj.start)
self.table_set.add(relationship_obj.end)
def evaluate_indicator_expectation(self, indicator_expectation):
raise NotImplementedError
def evaluate_expectation(self, expectation):
raise NotImplementedError
def evaluate_indicator_expectation_batch(self, indicator_expectation, group_bys, group_by_tuples,
standard_deviations=False):
raise NotImplementedError
def evaluate_expectation_batch(self, expectation, group_bys, group_by_tuples, standard_deviations=False):
raise NotImplementedError
def relevant_conditions(self, query, merged_tables=None):
"""Compute conditions for E(1/multiplier * 1_{c_1 Λ… Λc_n}) (Application A)."""
condition_dict = query.table_where_condition_dict
conditions = []
if merged_tables is None:
merged_tables = query.table_set.intersection(self.table_set)
# Conditions from Query
for table in condition_dict.keys():
if table in merged_tables:
for condition in condition_dict[table]:
conditions.append((table, condition))
# We have to require the tables of the query to be not null
# This can happen since we learn the SPN on full outer join
for table in merged_tables:
table_obj = self.schema_graph.table_dictionary[table]
condition = table_obj.table_nn_attribute + ' IS NOT NULL'
conditions.append((table, condition))
return conditions
def compute_multipliers(self, query):
"""Compute normalizing multipliers for E(1/multiplier * 1_{c_1 Λ… Λc_n}) (Application A).
Idea: Do a BFS tree search. Only a relevant multiplier if relationship is from
higher degree to lower degree. So we store each table in dict.
"""
queue = deque()
depth_dict = dict()
# Usually for BFS we would need a set of visited nodes. This is not required here.
# We can simply use depth_dict
for table in query.table_set:
queue.append(table)
depth_dict[table] = 0
depth_dict = self.compute_depths(queue, depth_dict)
norm_multipliers = []
# evaluate for every relationship if normalization is necessary
for relationship in self.relationship_set:
if relationship not in query.relationship_set:
relationship_obj = self.schema_graph.relationship_dictionary[relationship]
# only queries directed to query have to be included
if depth_dict[relationship_obj.start] > depth_dict[relationship_obj.end]:
norm_multipliers.append((relationship_obj.end, relationship_obj.multiplier_attribute_name_nn))
return norm_multipliers
def compute_depths(self, queue, depth_dict):
"""
Do a BFS to compute min-distance of tables to set of tables already in queue.
"""
# while not empty
while queue:
# BFS
table = queue.popleft()
# list neighbours
table_obj = self.schema_graph.table_dictionary[table]
for relationship in table_obj.incoming_relationships:
# only consider if part of relationships of combine-SPN
if relationship.identifier in self.relationship_set:
# potentially new table
potential_new_table = relationship.start
if potential_new_table not in depth_dict.keys():
queue.append(potential_new_table)
depth_dict[potential_new_table] = depth_dict[table] + 1
for relationship in table_obj.outgoing_relationships:
# only consider if part of relationships of combine-SPN
if relationship.identifier in self.relationship_set:
# potentially new table
potential_new_table = relationship.end
if potential_new_table not in depth_dict.keys():
queue.append(potential_new_table)
depth_dict[potential_new_table] = depth_dict[table] + 1
return depth_dict
def compute_mergeable_relationships(self, query, start_table):
"""
Compute which relationships are merged starting from a certain table (Application B)
"""
relationships = []
queue = deque()
queue.append(start_table)
while queue:
# BFS
table = queue.popleft()
# list neighbours
table_obj = self.schema_graph.table_dictionary[table]
for relationship in table_obj.incoming_relationships:
# only mergeable if part of SPN and still to be merged in query
if relationship.identifier in self.relationship_set and \
relationship.identifier in query.relationship_set and \
relationship.identifier not in relationships:
relationships.append(relationship.identifier)
queue.append(relationship.start)
for relationship in table_obj.outgoing_relationships:
# only mergeable if part of SPN and still to be merged in query
if relationship.identifier in self.relationship_set and \
relationship.identifier in query.relationship_set and \
relationship.identifier not in relationships:
relationships.append(relationship.identifier)
queue.append(relationship.end)
return relationships
def read_ensemble(ensemble_locations, build_reverse_dict=False):
"""
Creates union of all SPNs in the different ensembles.
:param min_sample_ratio:
:param ensemble_locations: list of file locations of ensembles.
:return:
"""
if not isinstance(ensemble_locations, list):
ensemble_locations = [ensemble_locations]
ensemble = SPNEnsemble(None)
for ensemble_location in ensemble_locations:
with open(ensemble_location, 'rb') as handle:
current_ensemble = pickle.load(handle)
ensemble.schema_graph = current_ensemble.schema_graph
for spn in current_ensemble.spns:
logging.debug(f"Including SPN with table_set {spn.table_set} with sampling ratio"
f"({spn.full_sample_size} / {spn.full_join_size})")
# logging.debug(f"Stats: ({get_structure_stats(spn.mspn)})")
# build reverse dict.
if build_reverse_dict:
_build_reverse_spn_dict(spn)
ensemble.add_spn(spn)
return ensemble
def what_if_scenario(what_if_query, ensemble):
"""
Create SPN ensemble for what if scenario
:param what_if_query: the query specifying the subpopulation and the percentage change
:param ensemble: the original ensemble
:return: new SPN ensemble where the subpopulation that fulfills conditions occurs more or less frequently depending
on the percentage change
"""
what_if_start_t = perf_counter()
conditions, percentage_change = parse_what_if_query(what_if_query, ensemble.schema_graph)
what_if_ensemble = copy.deepcopy(ensemble)
# reset cache to make what if work
what_if_ensemble.cached_expecation_vals = dict()
affected_tables = set([condition[0] for condition in conditions])
for spn in what_if_ensemble.spns:
if len(spn.table_set.intersection(affected_tables)) == 0:
continue
projected_conditions = [condition for condition in conditions if condition[0] in spn.table_set]
spn_conditions = spn._parse_conditions(projected_conditions)
spn_conditions = spn._add_null_values_to_ranges(spn_conditions)
evidence = spn_conditions[0]
# make sure multiplier means remain unchanged
force_equal_mean_cols = []
for i, column_name in enumerate(spn.column_names):
for relationship in what_if_ensemble.schema_graph.relationships:
if relationship.multiplier_attribute_name in column_name or relationship.multiplier_attribute_name_nn in column_name:
force_equal_mean_cols.append(i)
transform_what_if(spn, evidence, percentage_change, force_equal_mean_cols=force_equal_mean_cols,
null_values=spn.null_values, transform_copy=False)
what_if_end_t = perf_counter()
logger.debug(f"Created new what if ensemble for conditions {conditions} ({'+' if percentage_change > 0 else ''}"
f"{percentage_change * 100:.2f}%) in {what_if_end_t - what_if_start_t} secs.")
return what_if_ensemble
def _build_reverse_spn_dict(spn):
spn.table_meta_data['inverted_columns_dict'] = dict()
spn.table_meta_data['inverted_fd_dict'] = dict()
for table in spn.table_meta_data.keys():
if table == 'inverted_columns_dict' or table == 'inverted_fd_dict':
continue
categorical_colums = spn.table_meta_data[table]['categorical_columns_dict']
for categorical_column in categorical_colums.keys():
inverted_dictionary = dict()
for k, v in categorical_colums[categorical_column].items():
inverted_dictionary[v] = k
spn.table_meta_data['inverted_columns_dict'][categorical_column] = inverted_dictionary
if spn.table_meta_data[table].get('fd_dict') is not None:
fd_colums = spn.table_meta_data[table]['fd_dict']
# dwdate.d_dayofweek -> dwdate.d_daynuminweek
for dest_column, source_column_dictionary in fd_colums.items():
for source_column, value_dictionary in source_column_dictionary.items():
if spn.table_meta_data['inverted_fd_dict'].get(source_column) is None:
spn.table_meta_data['inverted_fd_dict'][source_column] = dict()
inverted_dictionary = dict()
for k, v_list in value_dictionary.items():
for v in v_list:
inverted_dictionary[v] = k
spn.table_meta_data['inverted_fd_dict'][source_column][dest_column] = inverted_dictionary
else:
spn.table_meta_data[table]['fd_dict'] = dict()
def evaluate_factors_group_by(artificially_added_conditions, confidence_intervals, debug,
factor_values_full, factors_full, result_tuples, technical_group_by_scopes,
confidence_interval_samples=None):
# see if we can exclude inverted factors, quadratic complexity but usually only very few factors
factors_to_be_deleted = set()
for left_factor in factors_full:
if not isinstance(left_factor, IndicatorExpectation):
continue
for right_factor in factors_full:
if not isinstance(right_factor, IndicatorExpectation):
continue
if left_factor.is_inverse(right_factor):
factors_to_be_deleted.add(left_factor)
factors_to_be_deleted.add(right_factor)
logger.debug("Removed two factors for evaluation.")
factors = [factor for factor in factors_full if factor not in factors_to_be_deleted]
factor_values = [factor_value for i, factor_value in enumerate(factor_values_full) if
not factors_full[i] in factors_to_be_deleted]
card_start_t = perf_counter()
# evaluate factors to obtain cardinality and formula
cardinalities = np.ones((len(result_tuples), 1)) * factors[0]
if confidence_intervals:
# idea: compute indicator stds with bernoulli approach
# store all indicator exps in [:,1]
no_exp = len([factor for factor in factors if isinstance(factor, Expectation)])
factor_exps = np.ones((len(result_tuples), no_exp + 2))
factor_exps[:, 0] = factors[0]
factor_stds = np.zeros((len(result_tuples), no_exp + 2))
exps_counter = 0
for i, factor in enumerate(factors):
if i == 0:
continue
if isinstance(factor, IndicatorExpectation):
def project_tuple(orig_tuple, projection_idxs):
projected_tuple = tuple()
for i in projection_idxs:
if isinstance(orig_tuple[i], list):
orig_tuple[i].sort()
projected_tuple += (tuple(orig_tuple[i]),)
continue
projected_tuple += (orig_tuple[i],)
return projected_tuple
def project_list_tuple(orig_tuple):
projected_tuple = tuple()
for i in range(len(orig_tuple)):
if isinstance(orig_tuple[i], tuple):
projected_tuple += (list(orig_tuple[i]),)
continue
projected_tuple += (orig_tuple[i],)
return projected_tuple
# see which group by conditions apply to this factor
specific_group_by_scopes = []
for group_idx, artificially_added_condition in enumerate(artificially_added_conditions):
if artificially_added_condition in factor.conditions:
factor.conditions.remove(artificially_added_condition)
specific_group_by_scopes.append(group_idx)
# factor has to be recomputed for different group by scopes
if len(specific_group_by_scopes) > 0:
specific_technical_group_by_scopes = [technical_group_by_scopes[i] for i in
specific_group_by_scopes]
specific_result_tuples = [project_tuple(result_tuple, specific_group_by_scopes) for result_tuple in
result_tuples]
# remember which unique projected values appeared and store them in order
different_specific_result_tuples = []
specific_result_dict = dict()
for j, specific_result in enumerate(set(specific_result_tuples)):
specific_result_dict[specific_result] = j
different_specific_result_tuples.append(specific_result)
different_specific_result_tuples_as_list = [project_list_tuple(result_tuple) for result_tuple in
different_specific_result_tuples]
_, unprojected_exps = \
factor.spn.evaluate_indicator_expectation_batch(factor,
specific_technical_group_by_scopes,
different_specific_result_tuples_as_list,
standard_deviations=False)
exps = np.ones((len(result_tuples), 1))
for idx, result_tuple in enumerate(result_tuples):
projected_tuple = project_tuple(result_tuple, specific_group_by_scopes)
unprojected_idx = specific_result_dict[projected_tuple]
exps[idx] = unprojected_exps[unprojected_idx]
cardinalities *= exps
if confidence_intervals:
factor_exps[:, 1] *= np.reshape(exps, len(result_tuples))
elif confidence_intervals:
cardinalities *= factor_values[i]
factor_exps[:, 1] *= factor_values[i]
else:
cardinalities *= factor_values[i]
elif isinstance(factor, Expectation):
for artificially_added_condition in artificially_added_conditions:
if artificially_added_condition in factor.conditions:
factor.conditions.remove(artificially_added_condition)
stds, exps = factor.spn.evaluate_expectation_batch(factor, technical_group_by_scopes, result_tuples,
standard_deviations=confidence_intervals)
cardinalities = np.multiply(exps, cardinalities)
if confidence_intervals:
ci_index = exps_counter + 2
factor_exps[:, ci_index] = np.reshape(exps, len(result_tuples))
factor_stds[:, ci_index] = np.reshape(stds, len(result_tuples))
exps_counter += 1
else:
cardinalities *= factor
if confidence_intervals:
factor_exps[:, 0] *= factor
cardinality_stds = None
if confidence_intervals:
assert confidence_interval_samples is not None, \
"confidence_interval_samples is required for confidence interval calculation"
bernoulli_p = factor_exps[:, 1]
factor_stds[:, 1] = np.reshape(np.sqrt(bernoulli_p * (1 - bernoulli_p) / confidence_interval_samples),
len(result_tuples))
cardinality_stds = std_of_products(factor_exps, factor_stds)
card_end_t = perf_counter()
if debug:
logger.debug(f"\t\tcomputed all cardinalities in {card_end_t - card_start_t} secs.")
logger.debug(f"\t\taverage_cardinality: {cardinalities.mean()}")
return cardinality_stds, cardinalities
def std_of_products(exps, stds):
"""
Computes the std of independent random variables.
:param exps:
:param stds:
:param non_constant_factors:
:return:
"""
# product(var(X_i) + E(X_i)^2)-product(E(X_i))^2
std_shape = (exps.shape[0], 1)
product_left = np.ones(std_shape)
product_right = np.ones(std_shape)
for i in range(exps.shape[1]):
# var(X_i) + E(X_i)^2
product_left *= np.reshape(np.square(stds[:, i]) + np.square(exps[:, i]), std_shape)
# E(X_i)^2
product_right *= np.reshape(np.square(exps[:, i]), std_shape)
return np.sqrt(product_left - product_right)
def evaluate_factors(dry_run, factors_full, cached_expecation_vals, confidence_intervals=False,
confidence_interval_samples=None, gen_code_stats=None):
# see if we can exclude inverted factors, quadratic complexity but usually only very few factors
factors_to_be_deleted = set()
for left_factor in factors_full:
if not isinstance(left_factor, IndicatorExpectation):
continue
for right_factor in factors_full:
if not isinstance(right_factor, IndicatorExpectation):
continue
if left_factor.is_inverse(right_factor):
factors_to_be_deleted.add(left_factor)
factors_to_be_deleted.add(right_factor)
logger.debug("Removed two factors for evaluation.")
factors = [factor for factor in factors_full if factor not in factors_to_be_deleted]
# evaluate factors to obtain cardinality and formula
formula = None
cardinality = None
values = []
non_constant_factors = []
if confidence_intervals:
# idea: compute indicator stds with bernoulli approach
# store all indicator exps in [:,1]
no_exp = len([factor for factor in factors if isinstance(factor, Expectation)])
factor_exps = np.ones((1, no_exp + 2))
factor_exps[:, 0] = factors[0]
factor_stds = np.zeros((1, no_exp + 2))
exps_counter = 0
for i, factor in enumerate(factors):
if formula is None:
assert i == 0
formula = str(factor)
cardinality = factor
values.append(factor)
continue
formula += " * " + str(factor)
if not dry_run:
if isinstance(factor, IndicatorExpectation):
if cached_expecation_vals.get(hash(factor)) is not None:
exp = cached_expecation_vals[hash(factor)]
else:
_, exp = factor.spn.evaluate_indicator_expectation(factor, gen_code_stats=gen_code_stats,
standard_deviations=False)
cached_expecation_vals[hash(factor)] = exp
if confidence_intervals:
factor_exps[:, 1] *= exp
non_constant_factors.append(i)
values.append(exp)
cardinality *= exp
elif isinstance(factor, Expectation):
if not confidence_intervals and cached_expecation_vals.get(hash(factor)) is not None:
std, exp = cached_expecation_vals[hash(factor)]
else:
std, exp = factor.spn.evaluate_expectation(factor, standard_deviations=confidence_intervals,
gen_code_stats=gen_code_stats)
if confidence_intervals:
ci_index = exps_counter + 2
factor_exps[:, ci_index] = exp
factor_stds[:, ci_index] = std
cached_expecation_vals[hash(factor)] = std, exp
non_constant_factors.append(i)
values.append(exp)
cardinality *= exp
exps_counter += 1
else:
cardinality *= factor
values.append(factor)
if confidence_intervals:
factor_exps[:, 0] *= factor
if confidence_intervals:
assert confidence_interval_samples is not None, \
"confidence_interval_samples is required for confidence interval calculation"
bernoulli_p = factor_exps[:, 1]
factor_stds[:, 1] = np.sqrt(bernoulli_p * (1 - bernoulli_p) / confidence_interval_samples)
cardinality_stds = std_of_products(factor_exps, factor_stds)
return cardinality_stds, values, cardinality, formula
else:
return values, cardinality, formula
def infer_column(condition):
column = None
if '<=' in condition:
column, _ = condition.split('<=', 1)
elif '>=' in condition:
column, _ = condition.split('>=', 1)
elif '>' in condition:
column, _ = condition.split('>', 1)
elif '<' in condition:
column, _ = condition.split('<', 1)
elif '=' in condition:
column, _ = condition.split('=', 1)
elif 'NOT IN' in condition:
column, _ = condition.split('NOT IN', 1)
elif 'IN' in condition:
column, _ = condition.split('IN', 1)
assert column is not None, "Condition not recognized"
return column.strip()
class SPNEnsemble:
"""
Several SPNs combined.
Assumptions:
- SPNs do not partition the entire graph.
- SPNs represent trees.
- Queries are trees. (This could be relaxed.)
- For FK relationship referenced entity exists, e.g. every order has a customer. (Not sure about this one)
"""
def __init__(self, schema_graph, spns=None):
self.schema_graph = schema_graph
self.spns = spns
self.cached_expecation_vals = dict()
if self.spns is None:
self.spns = []
def use_generated_code(self):
for spn in self.spns:
assert hasattr(spn, 'id'), "Assigned ids are required to employ generated code. Was this step done?"
spn.use_generated_code = True
# todo. warm start. compute dummy expectation on this spn.
def save(self, ensemble_path, compress=False):
if compress:
with bz2.BZ2File(ensemble_path, 'wb') as f:
pickle.dump(self, f, pickle.HIGHEST_PROTOCOL)
else:
with open(ensemble_path, 'wb') as f:
pickle.dump(self, f, pickle.HIGHEST_PROTOCOL)
def add_spn(self, spn):
"""Add an SPN to ensemble"""
self.spns.append(spn)
def _cardinality_greedy(self, query, rdc_spn_selection=False, rdc_attribute_dict=None, dry_run=False,
merge_indicator_exp=True, exploit_overlapping=False, return_factor_values=False,
exploit_incoming_multipliers=True, prefer_disjunct=False, gen_code_stats=None):
"""
Find first SPN for cardinality estimate.
"""
# Greedily select first SPN
first_spn, next_mergeable_relationships, next_mergeable_tables = self._greedily_select_first_cardinality_spn(
query, rdc_spn_selection=rdc_spn_selection, rdc_attribute_dict=rdc_attribute_dict)
return self._cardinality_with_injected_start(query, first_spn, next_mergeable_relationships,
next_mergeable_tables, rdc_spn_selection=rdc_spn_selection,
rdc_attribute_dict=rdc_attribute_dict,
dry_run=dry_run,
merge_indicator_exp=merge_indicator_exp,
exploit_overlapping=exploit_overlapping,
return_factor_values=return_factor_values,
exploit_incoming_multipliers=exploit_incoming_multipliers,
prefer_disjunct=prefer_disjunct,
gen_code_stats=gen_code_stats)
def _evaluate_group_by_spn_ensembles(self, query):
"""
Go over all Group By attributes, find best SPN with maximal where conditions. Merge features that have same SPN.
"""
spn_group_by_dict = dict()
group_by_list = [table + '.' + attribute for table, attribute in query.group_bys]
for i, grouping_attribute in enumerate(group_by_list):
max_matching_where_cond = -1
grouping_spn = None
# search for spn with maximal matching where conditions
for spn in self.spns:
potential_group_by_columns = set(spn.column_names)
for table in spn.table_set:
potential_group_by_columns = potential_group_by_columns.union(
spn.table_meta_data[table]['fd_dict'].keys())
if grouping_attribute not in potential_group_by_columns:
continue
where_conditions = set(query.table_where_condition_dict.keys()).intersection(spn.table_set)
matching_spns = 0
if spn in spn_group_by_dict.keys():
matching_spns = 1
if len(where_conditions) > max_matching_where_cond or \
(len(where_conditions) == max_matching_where_cond and matching_spns > 0):
max_matching_where_cond = len(where_conditions)
grouping_spn = spn
# use this spn in group by dictionary
if spn_group_by_dict.get(grouping_spn) is None:
spn_group_by_dict[grouping_spn] = []
spn_group_by_dict[grouping_spn].append(grouping_attribute)
group_by_permutation = np.zeros(len(group_by_list), dtype=int)
dict_items = list(spn_group_by_dict.items())
# permutation of group by queries
attribute_counter = 0
for spn, attribute_list in dict_items:
for attribute in attribute_list:
group_by_permutation[group_by_list.index(attribute)] = attribute_counter
attribute_counter += 1
result_tuples = None
result_tuples_translated = None
group_bys_scopes = []
for spn, attribute_list in dict_items:
conditions = spn.relevant_conditions(query)
group_bys_scope, temporary_results, temporary_results_translated = spn.evaluate_group_by_combinations(
attribute_list,
conditions)
group_bys_scopes += group_bys_scope
if result_tuples is None:
result_tuples = temporary_results
result_tuples_translated = temporary_results_translated
else:
result_tuples = [result_tuple + temporary_result for result_tuple in result_tuples for temporary_result
in temporary_results]
result_tuples_translated = [result_tuple + temporary_result for result_tuple in result_tuples_translated
for temporary_result in temporary_results_translated]
# reorder by tuple permutation
# group by scopes do not have to be reordered
group_bys_scopes = [group_bys_scopes[i] for i in group_by_permutation]
result_tuples = [tuple([result_tuple[i] for i in group_by_permutation]) for result_tuple in result_tuples]
result_tuples_translated = [tuple([result_tuple[i] for i in group_by_permutation]) for result_tuple in
result_tuples_translated]
return group_bys_scopes, result_tuples, result_tuples_translated
# def predict(self, conditions, regression_column):
# """
# Conditions
# :param conditions: dictionary of table, tuple condition pairs
# :param feature:
# :return:
# """
#
# max_where_conditions = -1
# prediction_spn = None
#
# for spn in self.spns:
# # if spn contains all features consider it a candidate
# if regression_column in spn.column_names:
#
# where_conditions = [condition for condition in conditions if condition[0] in spn.table_set]
#
# if len(where_conditions) > max_where_conditions:
# prediction_spn = spn
# max_where_conditions = len(where_conditions)
#
# assert prediction_spn is not None, "Did not find SPN offering this feature"
#
# ranges = prediction_spn._parse_conditions(conditions)
#
# return prediction_spn.predict(ranges, regression_column)
def evaluate_query(self, query, rdc_spn_selection=False, pairwise_rdc_path=None,
dry_run=False, merge_indicator_exp=True, max_variants=10,
exploit_overlapping=False, debug=False, display_intermediate_results=False,
exploit_incoming_multipliers=True, confidence_intervals=False,
confidence_sample_size=None, return_expectation=False):
"""
Evaluates any query with or without a group by.
:param query:
:param dry_run:
:param merge_indicator_exp:
:param max_variants:
:param exploit_overlapping:
:return:
"""
result_tuples = None
technical_group_by_scopes = []
if len(query.group_bys) > 0:
group_by_start_t = perf_counter()
# tuples that should appear in the group by clause
group_bys_scopes, result_tuples, result_tuples_translated = self._evaluate_group_by_spn_ensembles(query)
group_by_end_t = perf_counter()
technical_group_by_scopes = [tuple(group_bys_scope.split('.', 1)) for group_bys_scope in group_bys_scopes]
if debug:
logger.debug(f"\t\tcomputed {len(result_tuples)} group by statements "
f"in {group_by_end_t - group_by_start_t} secs.")
# if cardinality query simply return it
if query.query_type == QueryType.CARDINALITY or any(
[aggregation_type == AggregationType.SUM or aggregation_type == AggregationType.COUNT
for _, aggregation_type, _ in query.aggregation_operations]):
prot_card_start_t = perf_counter()
# First get the prototypical factors for concrete group by tuple
prototype_query = copy.deepcopy(query)
artificially_added_conditions = []
for group_by_idx, (table, attribute) in enumerate(query.group_bys):
# add condition for first group bys
condition = attribute + '=' + str(result_tuples_translated[0][group_by_idx])
artificially_added_conditions.append((table, condition,))
prototype_query.add_where_condition(table, condition)
_, factors, cardinalities, factor_values = self.cardinality(prototype_query,
rdc_spn_selection=rdc_spn_selection,
pairwise_rdc_path=pairwise_rdc_path,
dry_run=False,
merge_indicator_exp=merge_indicator_exp,
max_variants=max_variants,
exploit_overlapping=exploit_overlapping,
return_factor_values=True,
exploit_incoming_multipliers=exploit_incoming_multipliers)
prot_card_end_t = perf_counter()
if debug:
if len(query.group_bys) == 0:
logger.debug(f"\t\tpredicted cardinality: {cardinalities}")
logger.debug(f"\t\tcomputed prototypical cardinality in {prot_card_end_t - prot_card_start_t} secs.")
if len(query.group_bys) == 0 and confidence_intervals:
_, factors_no_overlap, _, _ = self.cardinality(prototype_query,
rdc_spn_selection=rdc_spn_selection,
pairwise_rdc_path=pairwise_rdc_path,
dry_run=False,
merge_indicator_exp=False,
max_variants=max_variants,
exploit_overlapping=exploit_overlapping,
return_factor_values=True,
exploit_incoming_multipliers=exploit_incoming_multipliers,
prefer_disjunct=True)
cardinality_stds, _, redundant_cardinality, _ = evaluate_factors(False, factors_no_overlap,
self.cached_expecation_vals,
confidence_intervals=True,
confidence_interval_samples=confidence_sample_size)
if len(query.group_bys) > 0:
_, cardinalities = evaluate_factors_group_by(
artificially_added_conditions, False,
debug, factor_values, factors, result_tuples,
technical_group_by_scopes)
if confidence_intervals:
_, factors_no_overlap, _, factor_values_no_overlap = self.cardinality(prototype_query,
rdc_spn_selection=rdc_spn_selection,
pairwise_rdc_path=pairwise_rdc_path,
dry_run=False,
merge_indicator_exp=False,
max_variants=max_variants,
exploit_overlapping=exploit_overlapping,
return_factor_values=True,
exploit_incoming_multipliers=exploit_incoming_multipliers,
prefer_disjunct=True)
cardinality_stds, _ = evaluate_factors_group_by(
artificially_added_conditions, confidence_intervals,
debug, factor_values_no_overlap, factors_no_overlap, result_tuples,
technical_group_by_scopes, confidence_interval_samples=confidence_sample_size)
# Bernoulli bound
# if confidence_intervals:
# full_join_query = copy.deepcopy(query)
# full_join_query.conditions = []
# full_join_query.table_where_condition_dict = dict()
# _, _, full_join_size = self.cardinality(full_join_query, dry_run=False,
# merge_indicator_exp=merge_indicator_exp,
# max_variants=max_variants,
# exploit_overlapping=exploit_overlapping,
# return_factor_values=False,
# exploit_incoming_multipliers=exploit_incoming_multipliers)
#
# bernoulli_p = cardinalities / full_join_size
# bernoulli_stds = full_join_size * np.sqrt(bernoulli_p * (1 - bernoulli_p) / 10000000)
# cardinality_stds = np.clip(cardinality_stds, bernoulli_stds, np.inf)
def build_confidence_interval(prediction, confidence_interval_std):
z_factor = scipy.stats.norm.ppf(0.95)
lower_bound = prediction - z_factor * confidence_interval_std.item()
upper_bound = prediction + z_factor * confidence_interval_std.item()
return lower_bound, upper_bound
if query.query_type == QueryType.CARDINALITY:
if confidence_intervals:
return build_confidence_interval(cardinalities, cardinality_stds), cardinalities
return None, cardinalities
result_values = None
if all_operations_of_type(AggregationType.SUM, query) or all_operations_of_type(AggregationType.AVG, query):
operation = None
if confidence_intervals:
if result_tuples is not None:
avg_exps = np.zeros((len(result_tuples), 1))
avg_stds = np.zeros((len(result_tuples), 1))
else:
avg_exps = np.zeros((1, 1))
avg_stds = np.zeros((1, 1))
for aggregation_operation_type, aggregation_type, factors in query.aggregation_operations:
# just the operation on the aggregations
if aggregation_operation_type == AggregationOperationType.PLUS or \
aggregation_operation_type == AggregationOperationType.MINUS:
operation = aggregation_operation_type
# which aggregation
elif aggregation_operation_type == AggregationOperationType.AGGREGATION:
# Either sum or avg value. In both cases expectation is required.
exp_start_t = perf_counter()
# todo. incorporate rdc values
expectation_spn, expectation = self._greedily_select_expectation_spn(query, factors)
if confidence_intervals:
current_stds, aggregation_result = expectation_spn.evaluate_expectation_batch(
expectation,
technical_group_by_scopes,
result_tuples,
standard_deviations=True)
avg_stds = np.sqrt(np.square(avg_stds) + np.square(current_stds))
else:
_, aggregation_result = expectation_spn.evaluate_expectation_batch(expectation,
technical_group_by_scopes,
result_tuples)
exp_end_t = perf_counter()
if debug:
logger.debug(f"\t\tcomputed expectation in {exp_end_t - exp_start_t} secs.")
logger.debug(
f"\t\taverage expectation: {np.array([aggregation_result]).mean()} for {expectation.features}")
# add or subtract current SUM or AVG from result
if result_values is None:
result_values = aggregation_result
if confidence_intervals:
avg_exps += aggregation_result
elif operation == AggregationOperationType.PLUS:
result_values += aggregation_result
if confidence_intervals:
avg_exps += aggregation_result
elif operation == AggregationOperationType.MINUS:
result_values -= aggregation_result
if confidence_intervals:
avg_exps -= aggregation_result
else:
raise NotImplementedError
if all_operations_of_type(AggregationType.SUM, query):
if confidence_intervals:
confidence_interval_stds = std_of_products(
np.concatenate((avg_exps, np.reshape(cardinalities, cardinality_stds.shape)), axis=1),
np.concatenate((avg_stds, cardinality_stds), axis=1))
result_values *= cardinalities
elif confidence_intervals:
confidence_interval_stds = avg_stds
# single count
elif all_operations_of_type(AggregationType.COUNT, query):
no_count_ops = len([aggregation_type for aggregation_operation_type, aggregation_type, _ in
query.aggregation_operations if
aggregation_operation_type == AggregationOperationType.AGGREGATION])
assert no_count_ops == 1, "Only a single count operation is supported."
result_values = cardinalities
if confidence_intervals:
confidence_interval_stds = cardinality_stds
# mixed operations
else:
raise NotImplementedError("Mixed operations are currently not implemented.")
# concatenate group by attribute and value if there is a group by
if len(query.group_bys) > 0:
result_tuples = [result_tuple + (result_values[i].item(),) for i, result_tuple in
enumerate(result_tuples_translated)]
if confidence_intervals:
confidence_values = []
for i in range(confidence_interval_stds.shape[0]):
confidence_values.append(
build_confidence_interval(result_values[i][-1], confidence_interval_stds[i]))
return confidence_values, result_tuples
return None, result_tuples
# if no group by queries return single value
if confidence_intervals:
return build_confidence_interval(result_values, confidence_interval_stds), result_values
if return_expectation:
return None, result_values, expectation_spn, expectation
return None, result_values
def cardinality(self, query, rdc_spn_selection=False, pairwise_rdc_path=None,
dry_run=False, merge_indicator_exp=True, max_variants=10, exploit_overlapping=False,
return_factor_values=False, exploit_incoming_multipliers=True, prefer_disjunct=False,
gen_code_stats=None):
"""
Uses several ways to approximate the cardinality and returns the median for cardinality
:param exploit_overlapping:
:param max_variants:
:param query:
:param dry_run:
:param merge_indicator_exp:
:return:
"""
rdc_attribute_dict = None
if rdc_spn_selection:
with open(pairwise_rdc_path, 'rb') as handle:
rdc_attribute_dict = pickle.load(handle)
possible_starts = self._possible_first_spns(query)
# no where conditions given
if len(possible_starts) == 0 or max_variants == 1:
return self._cardinality_greedy(query, rdc_spn_selection=rdc_spn_selection,
rdc_attribute_dict=rdc_attribute_dict,
dry_run=dry_run, merge_indicator_exp=merge_indicator_exp,
exploit_overlapping=exploit_overlapping,
return_factor_values=return_factor_values,
exploit_incoming_multipliers=exploit_incoming_multipliers,
prefer_disjunct=prefer_disjunct, gen_code_stats=gen_code_stats)
if len(possible_starts) > max_variants:
random.shuffle(possible_starts)
possible_starts = possible_starts[:max_variants]
results = []
for first_spn, next_mergeable_relationships, next_mergeable_tables in possible_starts:
results.append(self._cardinality_with_injected_start(query, first_spn, next_mergeable_relationships,
next_mergeable_tables,
rdc_spn_selection=rdc_spn_selection,
rdc_attribute_dict=rdc_attribute_dict,
dry_run=dry_run,
merge_indicator_exp=merge_indicator_exp,
exploit_overlapping=exploit_overlapping,
return_factor_values=return_factor_values,
exploit_incoming_multipliers=exploit_incoming_multipliers,
prefer_disjunct=prefer_disjunct,
gen_code_stats=gen_code_stats))