Skip to content

Commit

Permalink
Allow changing scalar_parser to something new
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcq committed Sep 21, 2023
1 parent 8c48501 commit 035679d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/trino/client/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def columns
end

def column_value_parsers
@column_value_parsers ||= columns.map {|column|
@column_value_parsers ||= {}
@column_value_parsers[scalar_parser] ||= columns.map {|column|
ColumnValueParser.new(column, scalar_parser)
}
end
Expand Down
27 changes: 27 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,33 @@
"num_spots" => 2,
},
})

# And to show that you can change the scalar_parser, now we only add 1 to each integer.
query.scalar_parser = ->(data, type) { (type == 'integer') ? data + 1 : data }

transformed_rows = query.transform_rows

expect(transformed_rows[0]).to eq({
"animal" => "dog",
"score" => 2,
"name" => "Lassie",
"foods" => ["kibble", "peanut butter"],
"traits" => {
"breed" => "spaniel",
"num_spots" => 3,
},
})

expect(transformed_rows[1]).to eq({
"animal" => "horse",
"score" => 6,
"name" => "Mr. Ed",
"foods" => ["hay", "sugar cubes"],
"traits" => {
"breed" => "some horse",
"num_spots" => 1,
},
})
end

it 'empty results' do
Expand Down

0 comments on commit 035679d

Please sign in to comment.