Skip to content

Commit

Permalink
fix materialized view aggregated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PNixx committed Sep 2, 2019
1 parent 4d30ad7 commit 56b0f3d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
18 changes: 18 additions & 0 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'clickhouse-activerecord/arel/visitors/to_sql'
require 'clickhouse-activerecord/arel/table'
require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/clickhouse/oid/date'
require 'active_record/connection_adapters/clickhouse/oid/date_time'
Expand Down Expand Up @@ -30,6 +31,14 @@ def clickhouse_connection(config)
end
end

module TypeCaster
class Map
def is_view
types.is_view
end
end
end

module ModelSchema
module ClassMethods
def is_view
Expand All @@ -39,6 +48,11 @@ def is_view
def is_view=(value)
@is_view = value
end

def arel_table # :nodoc:
@arel_table ||= ClickhouseActiverecord::Arel::Table.new(table_name, type_caster: type_caster)
end

end
end

Expand Down Expand Up @@ -130,6 +144,10 @@ def quoted_date(value)
value.to_s(:db)
end

def column_name_for_operation(operation, node) # :nodoc:
column_name_from_arel_node(node)
end

# Executes insert +sql+ statement in the context of this connection using
# +binds+ as the bind substitutes. +name+ is logged along with
# the executed +sql+ statement.
Expand Down
9 changes: 9 additions & 0 deletions lib/clickhouse-activerecord/arel/table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ClickhouseActiverecord
module Arel
class Table < ::Arel::Table
def is_view
type_caster.is_view
end
end
end
end
9 changes: 4 additions & 5 deletions lib/clickhouse-activerecord/arel/visitors/to_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ module Visitors
class ToSql < ::Arel::Visitors::ToSql

def aggregate(name, o, collector)
# todo how get model class from request? This method works with only rails 4.2.
# replacing function name for materialized view
# if o.expressions.first && o.expressions.first != '*' && !o.expressions.first.is_a?(String) && o.expressions.first.relation && o.expressions.first.relation.engine && o.expressions.first.relation.engine.is_view
# super("#{name.downcase}Merge", o, collector)
# else
if o.expressions.first && o.expressions.first != '*' && !o.expressions.first.is_a?(String) && o.expressions.first.relation&.is_view
super("#{name.downcase}Merge", o, collector)
else
super
# end
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/clickhouse-activerecord/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ClickhouseActiverecord
VERSION = '0.3.4'
VERSION = '0.3.5'
end

0 comments on commit 56b0f3d

Please sign in to comment.