Skip to content

Commit

Permalink
Fix selective for non JSON formats
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Shilov <[email protected]>
  • Loading branch information
shlima committed Nov 20, 2022
1 parent 6f9b0f1 commit 035b7d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
11 changes: 10 additions & 1 deletion lib/click_house/extend/connection_selective.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 11,16 @@ def select_all(sql)

def select_value(sql)
response = get(body: sql, query: { default_format: 'JSON' })
Array(Response::Factory.response(response, config).first).dig(0, -1)
got = Response::Factory.response(response, config).first

case got
when Hash
Array(got).dig(0, -1) # get a value of a first key for JSON format
when Array
got[0] # for CSV format
else
got # for RowBinary format
end
end

def select_one(sql)
Expand Down
20 changes: 15 additions & 5 deletions spec/click_house/integration/formats.rb
Original file line number Diff line number Diff line change
@@ -1,29 1,39 @@
# frozen_string_literal: true

RSpec.describe 'Different formats' do
RSpec.describe ClickHouse::Extend::ConnectionSelective do
subject do
ClickHouse.connection
end

context 'when RowBinary' do
it 'works' do
it '#select_one' do
got = subject.select_one('SELECT 1 FORMAT RowBinary')
expect(got).to eq("\u0001")
end

it 'has summary' do
it '#select_value' do
got = subject.select_value('SELECT 1 FORMAT RowBinary')
expect(got).to eq("\u0001")
end

it '#summary' do
got = subject.select_all('SELECT 1 FORMAT RowBinary')
expect(got.summary.read_rows).to eq(1)
end
end

context 'when CSB' do
it 'works' do
it '#select_one' do
got = subject.select_one('SELECT 1 FORMAT CSV')
expect(got).to eq(['1'])
end

it 'has summary' do
it '#select_value' do
got = subject.select_value('SELECT 1 FORMAT CSV')
expect(got).to eq('1')
end

it '#summary' do
got = subject.select_all('SELECT 1 FORMAT CSV')
expect(got.summary.read_rows).to eq(1)
end
Expand Down

0 comments on commit 035b7d9

Please sign in to comment.