Skip to content

Commit 6bb65b0

Browse files
committed
Support SimpleAggregateFunctions (#21)
1 parent b7ac986 commit 6bb65b0

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

lib/active_record/connection_adapters/clickhouse/schema_creation.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def add_column_options!(sql, options)
4545
if options[:aggregate_function]
4646
sql.gsub!(/(\w+)\s+(.*)/, "\\1 AggregateFunction(#{options[:aggregate_function]}, \\2)")
4747
end
48+
if options[:simple_aggregate_function]
49+
sql.gsub!(/(\w+)\s+(.*)/, "\\1 SimpleAggregateFunction(#{options[:simple_aggregate_function]}, \\2)")
50+
end
4851
sql.gsub!(/(\sString)\(\d+\)/, '\1')
4952
sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
5053
sql

lib/active_record/connection_adapters/clickhouse/table_definition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def column(name, type, index: nil, **options)
110110
private
111111

112112
def valid_column_definition_options
113-
super + [:array, :low_cardinality, :fixed_string, :value, :type, :map, :codec, :unsigned, :aggregate_function]
113+
super + [:array, :low_cardinality, :fixed_string, :value, :type, :map, :codec, :unsigned, :aggregate_function, :simple_aggregate_function]
114114
end
115115
end
116116

lib/clickhouse-activerecord/schema_dumper.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,14 @@ def schema_low_cardinality(column)
177177
end
178178

179179
def schema_aggregate_function(column)
180-
match = column.sql_type.match(/AggregateFunction\((.+), (\S+)\)/)
180+
match = column.sql_type.match(/((?:Simple|)AggregateFunction)\((.+), (\S+)\)/)
181181

182-
return {} if match.nil? || match.size != 3
182+
return {} if match.nil? || match.size != 4
183183

184-
{ aggregate_function: match[1].inspect }.tap do |spec|
185-
spec[:limit] = 4 if match[2] == "Float32"
186-
spec[:limit] = 8 if match[2] == "Float64"
184+
type = match[1] == "AggregateFunction" ? :aggregate_function : :simple_aggregate_function
185+
{ type => match[2].inspect }.tap do |spec|
186+
spec[:limit] = 4 if match[3] == "Float32"
187+
spec[:limit] = 8 if match[3] == "Float64"
187188
end
188189
end
189190

spec/fixtures/migrations/schema_table_with_aggregate_function_creation/1_create_some_table.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def up
66
t.column :col1, "AggregateFunction(sum, Float32)", null: false
77
t.column :col2, "AggregateFunction(anyLast, Float64)", null: false
88
t.column :col3, "AggregateFunction(anyLast, DateTime64)", null: false
9+
t.column :col4, "SimpleAggregateFunction(anyLast, DateTime64)", null: false
910
end
1011
end
1112
end

spec/single/schema_dumper_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@
4848
end
4949
).to_stdout_from_any_process
5050
end
51+
52+
it 'creates a table with simple aggregate function column for an DateTime64' do
53+
expect { subject }.to output(
54+
satisfy do |schema|
55+
expect(schema).to match(/t\.datetime "col3"/)
56+
expect(schema).to match(/"col3"[^\n]+aggregate_function: "anyLast"/)
57+
expect(schema).to match(/"col3"[^\n]+precision: 3/)
58+
expect(schema).to match(/t\.datetime "col4", simple_aggregate_function: "anyLast"/)
59+
end
60+
).to_stdout_from_any_process
61+
end
5162
end
5263
end
5364
end

0 commit comments

Comments
 (0)