ankane / blazer

Business intelligence made simple
MIT License
4.56k stars 474 forks source link

Encoding error dealing with binary data #385

Closed cianmce closed 2 years ago

cianmce commented 2 years ago

We have been using Blazer with some tables that have binary UUID columns. When returning data that has binary data and plain text data it raises an error.

e.g. running the following SQL with a MySQL data source

select unhex("F6"), 123

raises

Encoding::UndefinedConversionError - "\xF6" from ASCII-8BIT to UTF-8:

This only happens if

  1. the binary column is the first column returned
  2. there's no non-binary columns after the binary columns

e.g. these all work

select unhex("F6"); /* this works */
select unhex("F6"), unhex("F6"); /* this works */
select 123, unhex("F6"); /* this works */
select 123, unhex("F6"), 123; /* this works */

We have found this monkey patch fixes the issue but it's not ideal. Adding this to config/initializers/blazer.rb

module SwrveBlazer
  module BaseHelper
    def blazer_format_value(key, value)
      format_value = super(key, value)
      format_value.respond_to?(:force_encoding) ? format_value.force_encoding(Encoding::UTF_8) : format_value
    end
  end
end

Blazer::BaseHelper.prepend(SwrveBlazer::BaseHelper)