Skip to content

Commit

Permalink
published 0.4.10 - support integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
PNixx committed Mar 10, 2021
1 parent 5f221fe commit 42f6077
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### Version 0.4.10 (Mar 10, 2021)

* Support ClickHouse 20.9+
* Fix schema create / dump
* Support all integer types through :limit and :unsigned [@bdevel](https://github.com/bdevel)

### Version 0.4.4 (Sep 23, 2020)

* Full support migration and rollback database
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Clickhouse::Activerecord

A Ruby database ActiveRecord driver for ClickHouse. Support Rails >= 5.2.
Support ClickHouse version from 19.14 LTS.
Support ClickHouse version from 20.9 LTS.

## Installation

Expand Down Expand Up @@ -170,10 +170,10 @@ false`. The default integer is `UInt32`

| Type (bit size) | Range | :limit (byte size) |
| :--- | :----: | ---: |
| Int8 | -128 to 127 | limit: 1 |
| Int8 | -128 to 127 | 1 |
| Int16 | -32768 to 32767 | 2 |
| Int32 | -2147483648 to 2,147,483,647 | 3, 4 |
| Int64 | -9223372036854775808 to 9223372036854775807] | 5,6,7, 8 |
| Int32 | -2147483648 to 2,147,483,647 | 3,4 |
| Int64 | -9223372036854775808 to 9223372036854775807] | 5,6,7,8 |
| Int128 | ... | 9 - 15 |
| Int256 | ... | 16+ |
| UInt8 | 0 to 255 | 1 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def visit_TableDefinition(o)
statements << accept(o.primary_keys) if o.primary_keys

create_sql << "(#{statements.join(', ')})" if statements.present?
add_table_options!(create_sql, table_options(o))
# Attach options for only table or materialized view
add_table_options!(create_sql, table_options(o)) if !o.view || o.view && o.materialized

create_sql << " AS #{to_sql(o.as)}" if o.as
create_sql
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def tables(name = nil)

def table_options(table)
sql = show_create_table(table)
{ options: sql.gsub(/^(?:.*?)ENGINE = (.*?)$/, '\\1') }
{ options: sql.gsub(/^(?:.*?)(?:ENGINE = (.*?))?( AS SELECT .*?)?$/, '\\1').presence, as: sql.match(/^CREATE (?:.*?) AS (SELECT .*?)$/).try(:[], 1) }.compact
end

# Not indexes on clickhouse
Expand Down
21 changes: 9 additions & 12 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ClickhouseAdapter < AbstractAdapter
datetime: { name: 'DateTime' },
date: { name: 'Date' },
boolean: { name: 'UInt8' },

int8: { name: 'Int8' },
int16: { name: 'Int16' },
int32: { name: 'Int32' },
Expand Down Expand Up @@ -152,10 +152,12 @@ def extract_limit(sql_type) # :nodoc:
when /(Nullable)?\(?String\)?/
super('String')
when /(Nullable)?\(?U?Int8\)?/
super('int2')
when /(Nullable)?\(?U?Int(16|32)\)?/
super('int4')
when /(Nullable)?\(?U?Int(64)\)?/
1
when /(Nullable)?\(?U?Int16\)?/
2
when /(Nullable)?\(?U?Int32\)?/
nil
when /(Nullable)?\(?U?Int64\)?/
8
else
super
Expand All @@ -167,25 +169,20 @@ def initialize_type_map(m) # :nodoc:
register_class_with_limit m, %r(String), Type::String
register_class_with_limit m, 'Date', Clickhouse::OID::Date
register_class_with_limit m, 'DateTime', Clickhouse::OID::DateTime

register_class_with_limit m, %r(Int8), Type::Integer
register_class_with_limit m, %r(Int16), Type::Integer
register_class_with_limit m, %r(Int32), Type::Integer
register_class_with_limit m, %r(Int64), Type::Integer
register_class_with_limit m, %r(Int128), Type::Integer
register_class_with_limit m, %r(Int256), Type::Integer

register_class_with_limit m, %r(Uint8), Type::UnsignedInteger
register_class_with_limit m, %r(UInt16), Type::UnsignedInteger
register_class_with_limit m, %r(UInt32), Type::UnsignedInteger
register_class_with_limit m, %r(UInt64), Type::UnsignedInteger
#register_class_with_limit m, %r(UInt128), Type::UnsignedInteger #not implemnted in clickhouse
register_class_with_limit m, %r(UInt256), Type::UnsignedInteger

m.alias_type 'Int16', 'Int8'
m.alias_type 'Int32', 'Int8'
m.alias_type 'UInt16', 'UInt8'
m.alias_type 'UInt32', 'UInt8'
end

# Quoting time without microseconds
Expand Down
15 changes: 13 additions & 2 deletions lib/clickhouse-activerecord/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def header(stream)
end

def tables(stream)
sorted_tables = @connection.tables.sort {|a,b| @connection.show_create_table(a).match(/^CREATE\s+(MATERIALIZED)\s+VIEW/) ? 1 : a <=> b }
sorted_tables = @connection.tables.sort {|a,b| @connection.show_create_table(a).match(/^CREATE\s+(MATERIALIZED\s+)?VIEW/) ? 1 : a <=> b }

sorted_tables.each do |table_name|
table(table_name, stream) unless ignored?(table_name)
Expand All @@ -50,7 +50,7 @@ def table(table, stream)
# super(table.gsub(/^\.inner\./, ''), stream)

# detect view table
match = sql.match(/^CREATE\s+(MATERIALIZED)\s+VIEW/)
match = sql.match(/^CREATE\s+(MATERIALIZED\s+)?VIEW/)
end

# Copy from original dumper
Expand Down Expand Up @@ -138,5 +138,16 @@ def schema_limit(column)
return nil if column.type == :float
super
end

def schema_unsigned(column)
return nil unless column.type == :integer && !simple
(column.sql_type =~ /(Nullable)?\(?UInt\d+\)?/).nil? ? false : nil
end

def prepare_column_options(column)
spec = {}
spec[:unsigned] = schema_unsigned(column)
spec.merge(super).compact
end
end
end
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.4.9'
VERSION = '0.4.10'
end

0 comments on commit 42f6077

Please sign in to comment.