-
Notifications
You must be signed in to change notification settings - Fork 472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for ClickHouse #377
Open
foxy-eyed
wants to merge
6
commits into
ankane:master
Choose a base branch
from
foxy-eyed:clickhouse_adapter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
41a6b49
Added support for ClickHouse
foxy-eyed 3910dee
Add support for ActiveRecord driver for ClickHouse
foxy-eyed cc7fe03
Fix Date and Time columns typecasting
foxy-eyed 57d08da
Drop clickhouse-activerecord support; extend config
foxy-eyed dc4b17f
Convert DateTime columns to Time objects
foxy-eyed bfba2a4
Fix charts
foxy-eyed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module Blazer | ||
module Adapters | ||
class ClickhouseAdapter < BaseAdapter | ||
DATE_TIME_TYPES = ["DateTime", "DateTime(%s)", "DateTime64(%d, %s)"].freeze | ||
|
||
def run_statement(statement, _comment) | ||
columns = [] | ||
rows = [] | ||
error = nil | ||
|
||
begin | ||
result = connection.select_all(statement) | ||
unless result.data.blank? | ||
date_time_columns = result.meta | ||
.select { |column| column["type"].in?(DATE_TIME_TYPES) } | ||
.map { |column| column["name"] } | ||
columns = result.data.first.keys | ||
rows = result.data.map { |row| convert_time_columns(row, date_time_columns).values } | ||
end | ||
rescue => e | ||
error = e.message | ||
end | ||
|
||
[columns, rows, error] | ||
end | ||
|
||
def tables | ||
connection.tables | ||
end | ||
|
||
def schema | ||
statement = <<-SQL | ||
SELECT table, name, type | ||
FROM system.columns | ||
WHERE database = currentDatabase() | ||
ORDER BY table, position | ||
SQL | ||
|
||
response = connection.post(query: { query: statement, default_format: "CSV" }) | ||
response.body | ||
.group_by { |row| row[0] } | ||
.transform_values { |columns| columns.map { |c| { name: c[1], data_type: c[2] } } } | ||
.map { |table, columns| { schema: "public", table: table, columns: columns } } | ||
end | ||
|
||
def preview_statement | ||
"SELECT * FROM {table} LIMIT 10" | ||
end | ||
|
||
def explain(statement) | ||
connection.explain(statement) | ||
end | ||
|
||
protected | ||
|
||
def connection | ||
@connection ||= ClickHouse::Connection.new(config) | ||
end | ||
|
||
def config | ||
@config ||= begin | ||
uri = URI.parse(settings["url"]) | ||
options = { | ||
scheme: uri.scheme, | ||
host: uri.host, | ||
port: uri.port, | ||
username: uri.user, | ||
password: uri.password, | ||
database: uri.path.sub(/\A\//, ""), | ||
ssl_verify: settings.fetch("ssl_verify", false) | ||
}.compact | ||
ClickHouse::Config.new(**options) | ||
end | ||
end | ||
|
||
def convert_time_columns(row, date_time_columns) | ||
time_values = row.slice(*date_time_columns).transform_values!(&:to_time) | ||
row.merge(time_values) | ||
rescue NoMethodError | ||
row | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot be absolutely sure that we handle
DateTime
object here, because built-in types can be overwritten. If conversion toTime
fails, we return initial object.