activecube 0.1.24 → 0.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e08b7128492c4b84b982715355728b6506c0eb4dc155875ef1323bc88e9c9777
4
- data.tar.gz: 856d25b480a319b11476c9d388f20aa656325f812299018c67df6b44bed322e8
3
+ metadata.gz: 21b32d45c2412d7be46e4f044b3287262b5014ad617ac9929fc0f740c7c64520
4
+ data.tar.gz: f5c97c62544274087c0ca1cfedf403384f3dc04bb05ba327cc2c51a6a5bc9ff0
5
5
  SHA512:
6
- metadata.gz: 76d9c60cfba31ac7f65f819dc1e5b8b0d6689aebbb162f8677f45e7388442dfc7d581e2bec945478d058891f61fc2b7b34de5b7e79f40c510bb88c1f21647547
7
- data.tar.gz: 880550994778371e8dbd416691bc252a51de37123557ee73ad4c4ac5dcdb273a2a29084787c311b7219629cf42ac782302e664a495e5870deb69e4a6a8323730
6
+ metadata.gz: ee2dd6dc3bcb0b4f1b2acc83aceb94d677564d2c70c51646018b0b91c2e168f41459cc0e7541e5b4182aebb8bc4e288d88ae2fc017836ca2d1475afb0c28f019
7
+ data.tar.gz: '0709d3aa9adef226869f016a46d58a11cbfee94315fac2f22c7740ca3bf215c8ab691191a9b6ce50e69ed3ff072de84993bc313cf86f9095b41a71cf5be88072'
@@ -9,7 +9,7 @@ GIT
9
9
  PATH
10
10
  remote: .
11
11
  specs:
12
- activecube (0.1.23)
12
+ activecube (0.1.28)
13
13
  activerecord (>= 5.2)
14
14
 
15
15
  GEM
data/README.md CHANGED
@@ -121,6 +121,12 @@ index ['currency_id'], cardinality: 4
121
121
  index ['currency_id','date'], cardinality: 6
122
122
  ```
123
123
 
124
+ You can require using index in some cases. If required: true added, the table will be used **only** in case when this field is used
125
+ in query metric, dimension or selector.
126
+ ```ruby
127
+ index ['currency_id'], cardinality: 4, required: true
128
+ ```
129
+
124
130
  ### Query language
125
131
 
126
132
  You use the cube class to create and execute queries.
@@ -2,7 +2,7 @@ module Activecube::Common
2
2
 
3
3
  module Metrics
4
4
 
5
- METHODS = [:count, :minimum,:maximum,:average,:sum,:uniqueExact,:unique,:median,:any,:anyLast]
5
+ METHODS = [:count, :minimum,:maximum,:average,:sum,:uniqueExact,:unique,:median,:medianExact,:any,:anyLast]
6
6
 
7
7
  METHODS.each do |fname|
8
8
 
@@ -52,7 +52,7 @@ module Activecube
52
52
 
53
53
  include DefinitionMethods
54
54
 
55
- attr_reader :modifiers
55
+ attr_reader :modifiers, :tuple
56
56
 
57
57
  private
58
58
 
@@ -60,6 +60,9 @@ module Activecube
60
60
  (@modifiers ||= {} )[args.first.to_sym] = Modifier.new( *args)
61
61
  end
62
62
 
63
+ def tuple_fields *args
64
+ @tuple = args
65
+ end
63
66
  end
64
67
 
65
68
  end
@@ -41,7 +41,7 @@ module Activecube::Processor
41
41
  end
42
42
  after = total_cost measure_tables
43
43
 
44
- raise "Optimizer made it worth #{before} -> #{after} for #{cost_matrix}" unless after <= before
44
+ raise "Optimizer made it worse #{before} -> #{after} for #{cost_matrix}" unless after <= before
45
45
  measure_tables
46
46
 
47
47
  end
@@ -2,16 +2,20 @@ module Activecube
2
2
  module Processor
3
3
  class Index
4
4
 
5
- attr_reader :fields, :cardinality
5
+ attr_reader :fields, :cardinality, :required
6
6
  def initialize name, *args
7
7
  @fields = [name].flatten
8
8
  @cardinality = args.first && args.first[:cardinality]
9
+ @required = args.first && args.first[:required]
9
10
  end
10
11
 
11
12
  def indexes? query, measures
12
13
  (fields - query.selector_column_names(measures)).empty?
13
14
  end
14
15
 
16
+ def matches? query, measures
17
+ !required || (fields - query.column_names(measures)).empty?
18
+ end
15
19
  end
16
20
  end
17
21
  end
@@ -18,7 +18,7 @@ module Activecube::Processor
18
18
  @tables_count = cost_matrix.map(&:count).max
19
19
  @metrics_count = cost_matrix.count
20
20
 
21
- tables_count==1 ? [0]*metrics_count : do_optimize
21
+ (tables_count==1 || metrics_count==0) ? [0]*metrics_count : do_optimize
22
22
 
23
23
  end
24
24
 
@@ -27,95 +27,41 @@ module Activecube::Processor
27
27
 
28
28
  private
29
29
 
30
+ def generate_variants vs, metric_i
30
31
 
31
- def do_optimize
32
- @tables_by_metrics = []
32
+ return vs if metric_i==metrics_count
33
33
 
34
- # sort metrics from low min cost to higher min costs ( by all applicable tables )
35
- sort_metrics
34
+ metric_tables = cost_matrix[metric_i].map.with_index do |c, index|
35
+ [index] if c
36
+ end.compact
36
37
 
37
- # fill initial @tables_by_metrics by selecting tables with minimum cost for metrics.
38
- # If there are more than one table with this minimum cost, then select already selected table with maximum cost
39
- select_min_cost_by_metric
38
+ vsnew = if metric_i==0
39
+ metric_tables
40
+ else
41
+ arry = []
42
+ vs.each do |v|
43
+ metric_tables.each{|newv|
44
+ arry << (v + newv)
45
+ }
46
+ end
47
+ arry
48
+ end
40
49
 
41
- # make iterations over @tables_by_metrics ( max MAX_ITERATIONS)
42
- iterates
50
+ generate_variants vsnew, metric_i+1
43
51
 
44
- @tables_by_metrics
45
52
  end
46
53
 
47
- def sort_metrics
48
- @metrics_index_sorted = (0...metrics_count).sort_by{|m_i| cost_matrix[m_i].compact.min || UNLIM_COST }
54
+ def cost_for variant
55
+ variant.each_with_index.group_by(&:first).collect do |table_index, arry|
56
+ arry.map(&:second).map{|metric_index| cost_matrix[metric_index][table_index] }.max
57
+ end.sum
49
58
  end
50
59
 
51
- def select_min_cost_by_metric
52
-
53
- @metrics_index_sorted.collect do |m_i|
54
-
55
- table_index_cost = (0...tables_count).map{|c_i| [c_i,
56
- cost_matrix[m_i][c_i] || UNLIM_COST,
57
- (@tables_by_metrics.include?(c_i) ? -cost_matrix[@tables_by_metrics.index(c_i)][c_i] : 0)
58
- ]}.sort_by(&:third).sort_by(&:second)
59
-
60
- @tables_by_metrics[m_i] = table_index_cost.first.first
61
-
62
- end
63
- end
64
-
65
- def iterates
66
-
67
- steps = [@tables_by_metrics]
68
-
69
- (1..MAX_ITERATIONS).each do |iteration|
70
-
71
- step = []
72
- prev_step = steps.last
73
-
74
- prev_step.each_with_index {|c_i, m_i|
75
-
76
- table_included_times = prev_step.select{|c| c==c_i }.count
77
- old_cost = cost_matrix[m_i][c_i]
78
- new_c_i = (0...tables_count).detect{|c_n|
79
- new_cost = cost_matrix[m_i][c_n]
80
- next if c_i==c_n || new_cost.nil?
81
- new_table_included_times = prev_step.select{|c| c==c_n }.count
82
-
83
- if old_cost.nil?
84
- # if we have non indexed table now
85
- true
86
- elsif table_included_times>1
87
- if new_table_included_times>0
88
- # table to used table if
89
- # cost now > new cost
90
- old_cost > new_cost
91
- else
92
- # table to unused table if
93
- # cost now > new cost + max other cost in table now
94
- old_cost > new_cost + ( prev_step.select.with_index{|c,i| c==c_i && i!=m_i }.max || UNLIM_COST )
95
- end
96
- else
97
- if new_table_included_times>0
98
- # unused table to table if
99
- # new cost < cost now + max other cost in new table
100
- old_cost > new_cost - ( prev_step.select{|c| c==c_n }.max || UNLIM_COST )
101
- else
102
- # unused to unused
103
- # cost now > new cost
104
- old_cost > new_cost
105
- end
106
- end
107
-
108
- }
109
-
110
- step << c_i || new_c_i
111
-
112
- }
113
-
114
- break if steps.include? step
115
- steps << step
116
- end
60
+ def do_optimize
117
61
 
118
- @tables_by_metrics = steps.last
62
+ variants = generate_variants [], 0
63
+ variant_costs = variants.map{|v| cost_for v}
64
+ variants[variant_costs.each_with_index.min.second]
119
65
 
120
66
  end
121
67
 
@@ -12,7 +12,8 @@ module Activecube::Processor
12
12
  end
13
13
 
14
14
  def matches? query, measures = query.measures
15
- (query.column_names(measures)-model.attribute_types.keys).empty?
15
+ (query.column_names(measures)-model.attribute_types.keys).empty? &&
16
+ !model.activecube_indexes.detect{|index| !index.matches?(query, measures) }
16
17
  end
17
18
 
18
19
  def measures? measure
@@ -1,3 +1,3 @@
1
1
  module Activecube
2
- VERSION = "0.1.24"
2
+ VERSION = "0.1.29"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activecube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.24
4
+ version: 0.1.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksey Studnev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-10 00:00:00.000000000 Z
11
+ date: 2020-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord