From a806ae1f7cfc9ecadfa52d54611b10ec27e76f82 Mon Sep 17 00:00:00 2001 From: astudnev Date: Mon, 24 Feb 2020 22:39:26 +0300 Subject: [PATCH] fix ordering implementations --- lib/activecube/processor/composer.rb | 3 ++- lib/activecube/query/cube_query.rb | 4 ++-- lib/activecube/query/ordering.rb | 3 ++- spec/cases/activecube_spec.rb | 23 ++++++++++++++++++----- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/activecube/processor/composer.rb b/lib/activecube/processor/composer.rb index e35664d..9ac7446 100644 --- a/lib/activecube/processor/composer.rb +++ b/lib/activecube/processor/composer.rb @@ -70,7 +70,8 @@ def compose_queries measure_tables @models = [] measure_tables.group_by(&:table).each_pair do |table, list| @models << table.model - reduced = cube_query.reduced list.map(&:measure) + reduce_options = measure_tables.count==1 ? cube_query.options : [] + reduced = cube_query.reduced list.map(&:measure), reduce_options table_query = table.query reduced composed_query = composed_query ? table.join(cube_query, composed_query, table_query) : table_query end diff --git a/lib/activecube/query/cube_query.rb b/lib/activecube/query/cube_query.rb index e37870b..988350d 100644 --- a/lib/activecube/query/cube_query.rb +++ b/lib/activecube/query/cube_query.rb @@ -86,7 +86,7 @@ def selector_column_names measures = self.measures (measures.map(&:selectors) + selectors).flatten.map(&:required_column_names).flatten.uniq end - def reduced other_measures + def reduced other_measures, other_options common_selectors = [] other_measures.each_with_index do |m,i| @@ -113,7 +113,7 @@ def reduced other_measures return self if (reduced_measures == self.measures) && (reduced_selectors == self.selectors) - CubeQuery.new cube, slices, reduced_measures, reduced_selectors + CubeQuery.new cube, slices, reduced_measures, reduced_selectors, other_options end def join_fields diff --git a/lib/activecube/query/ordering.rb b/lib/activecube/query/ordering.rb index cd125be..4b43251 100644 --- a/lib/activecube/query/ordering.rb +++ b/lib/activecube/query/ordering.rb @@ -9,7 +9,8 @@ def initialize argument, direction end def append_query _model, _cube_query, _table, query - query.order(::Arel.sql(argument.to_s).send(direction)) + text = argument.to_s.split(',').map{|s| "`#{s}`"}.join(',') + query.order(::Arel.sql(text).send(direction)) end end diff --git a/spec/cases/activecube_spec.rb b/spec/cases/activecube_spec.rb index 105bb1a..e88de08 100644 --- a/spec/cases/activecube_spec.rb +++ b/spec/cases/activecube_spec.rb @@ -316,7 +316,7 @@ desc(:count_in).desc(:count_out).limit(5).offset(0) .to_sql - expect(sql).to eq("SELECT * FROM (SELECT dictGetString('currency', 'symbol', toUInt64(currency_id)) AS `date`, transfers_to.currency_id, dictGetString('currency', 'address', toUInt64(currency_id)) AS `address`, transfers_to.currency_id, SUM(transfers_to.value) / dictGetUInt64('currency', 'divider', toUInt64(currency_id)) AS `sum_in`, count() AS `count_in` FROM transfers_to WHERE transfers_to.transfer_to_bin = unhex('adr') GROUP BY transfers_to.currency_id ORDER BY `date`, `address`) FULL OUTER JOIN (SELECT dictGetString('currency', 'symbol', toUInt64(currency_id)) AS `date`, transfers_from.currency_id, dictGetString('currency', 'address', toUInt64(currency_id)) AS `address`, transfers_from.currency_id, SUM(transfers_from.value) / dictGetUInt64('currency', 'divider', toUInt64(currency_id)) AS `sum_out`, count() AS `count_out` FROM transfers_from WHERE transfers_from.transfer_from_bin = unhex('adr') GROUP BY transfers_from.currency_id ORDER BY `date`, `address`) USING currency_id ORDER BY count_in DESC, count_out DESC LIMIT 5 OFFSET 0") + expect(sql).to eq("SELECT * FROM (SELECT dictGetString('currency', 'symbol', toUInt64(currency_id)) AS `date`, transfers_to.currency_id, dictGetString('currency', 'address', toUInt64(currency_id)) AS `address`, transfers_to.currency_id, SUM(transfers_to.value) / dictGetUInt64('currency', 'divider', toUInt64(currency_id)) AS `sum_in`, count() AS `count_in` FROM transfers_to WHERE transfers_to.transfer_to_bin = unhex('adr') GROUP BY transfers_to.currency_id ORDER BY `date`, `address`) FULL OUTER JOIN (SELECT dictGetString('currency', 'symbol', toUInt64(currency_id)) AS `date`, transfers_from.currency_id, dictGetString('currency', 'address', toUInt64(currency_id)) AS `address`, transfers_from.currency_id, SUM(transfers_from.value) / dictGetUInt64('currency', 'divider', toUInt64(currency_id)) AS `sum_out`, count() AS `count_out` FROM transfers_from WHERE transfers_from.transfer_from_bin = unhex('adr') GROUP BY transfers_from.currency_id ORDER BY `date`, `address`) USING currency_id ORDER BY `count_in` DESC, `count_out` DESC LIMIT 5 OFFSET 0") end end @@ -333,7 +333,7 @@ offset(5). to_sql - expect(sql).to eq("SELECT transfers_currency.currency_id AS `currency`, transfers_currency.currency_id, count() AS `count` FROM transfers_currency GROUP BY transfers_currency.currency_id ORDER BY count ASC LIMIT 5 OFFSET 5") + expect(sql).to eq("SELECT transfers_currency.currency_id AS `currency`, transfers_currency.currency_id, count() AS `count` FROM transfers_currency GROUP BY transfers_currency.currency_id ORDER BY `count` ASC LIMIT 5 OFFSET 5") end @@ -347,7 +347,7 @@ offset(5). to_sql - expect(sql).to eq("SELECT transfers_currency.currency_id AS `currency`, transfers_currency.currency_id, count() AS `count` FROM transfers_currency GROUP BY transfers_currency.currency_id ORDER BY count ASC LIMIT 5 OFFSET 5") + expect(sql).to eq("SELECT transfers_currency.currency_id AS `currency`, transfers_currency.currency_id, count() AS `count` FROM transfers_currency GROUP BY transfers_currency.currency_id ORDER BY `count` ASC LIMIT 5 OFFSET 5") end @@ -356,12 +356,25 @@ sql = cube. measure(:count). slice(year: cube.dimensions[:date][:year]). - asc('date.year'). + asc('year'). limit(5). offset(5). to_sql - expect(sql).to eq("SELECT toYear(tx_date) AS `year`, count() AS `count` FROM transfers_currency GROUP BY `year` ORDER BY date.year ASC LIMIT 5 OFFSET 5") + expect(sql).to eq("SELECT toYear(tx_date) AS `year`, count() AS `count` FROM transfers_currency GROUP BY `year` ORDER BY `year` ASC LIMIT 5 OFFSET 5") + + end + + + it 'ordering case with internal measure reduction' do + + sql = cube. + measure(count: cube.metrics[:count].when(cube.selectors[:transfer_from].eq('ADR'))). + slice(year: cube.dimensions[:date][:year]). + asc('count'). + to_sql + + expect(sql).to eq("SELECT toYear(tx_date) AS `year`, count() AS `count` FROM transfers_from WHERE transfers_from.transfer_from_bin = unhex('adr') GROUP BY `year` ORDER BY `count` ASC") end