Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def add_column_options!(sql, options)
if options[:aggregate_function]
sql.gsub!(/(\w+)\s+(.*)/, "\\1 AggregateFunction(#{options[:aggregate_function]}, \\2)")
end
if options[:simple_aggregate_function]
sql.gsub!(/(\w+)\s+(.*)/, "\\1 SimpleAggregateFunction(#{options[:simple_aggregate_function]}, \\2)")
end
sql.gsub!(/(\sString)\(\d+\)/, '\1')
sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def column(name, type, index: nil, **options)
private

def valid_column_definition_options
super + [:array, :low_cardinality, :fixed_string, :value, :type, :map, :codec, :unsigned, :aggregate_function]
super + [:array, :low_cardinality, :fixed_string, :value, :type, :map, :codec, :unsigned, :aggregate_function, :simple_aggregate_function]
end
end

Expand Down
11 changes: 6 additions & 5 deletions lib/clickhouse-activerecord/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,14 @@ def schema_low_cardinality(column)
end

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

return {} if match.nil? || match.size != 3
return {} if match.nil? || match.size != 4

{ aggregate_function: match[1].inspect }.tap do |spec|
spec[:limit] = 4 if match[2] == "Float32"
spec[:limit] = 8 if match[2] == "Float64"
type = match[1] == "AggregateFunction" ? :aggregate_function : :simple_aggregate_function
{ type => match[2].inspect }.tap do |spec|
spec[:limit] = 4 if match[3] == "Float32"
spec[:limit] = 8 if match[3] == "Float64"
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def up
t.column :col1, "AggregateFunction(sum, Float32)", null: false
t.column :col2, "AggregateFunction(anyLast, Float64)", null: false
t.column :col3, "AggregateFunction(anyLast, DateTime64)", null: false
t.column :col4, "SimpleAggregateFunction(anyLast, DateTime64)", null: false
end
end
end
11 changes: 11 additions & 0 deletions spec/single/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@
end
).to_stdout_from_any_process
end

it 'creates a table with simple aggregate function column for an DateTime64' do
expect { subject }.to output(
satisfy do |schema|
expect(schema).to match(/t\.datetime "col3"/)
expect(schema).to match(/"col3"[^\n]+aggregate_function: "anyLast"/)
expect(schema).to match(/"col3"[^\n]+precision: 3/)
expect(schema).to match(/t\.datetime "col4", simple_aggregate_function: "anyLast"/)
end
).to_stdout_from_any_process
end
end
end
end