Skip to content

Commit

Permalink
fix ordering implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
astudnev committed Feb 24, 2020
1 parent 5cdfb5f commit a806ae1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/activecube/processor/composer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/activecube/query/cube_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/activecube/query/ordering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 18 additions & 5 deletions spec/cases/activecube_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit a806ae1

Please sign in to comment.