<commit_before>require 'active_support/core_ext/object/try'
require 'active_record/turntable/sql_tree_patch'

module ActiveRecord::Turntable
  class Mixer
    extend ActiveSupport::Autoload

    eager_autoload do
      autoload :Fader
    end

    delegate :logger, :to => ActiveRecord::Base

    NOT_USED_FOR_SHARDING_OPERATORS_REGEXP = /\A(NOT IN|IS|IS NOT|BETWEEN|LIKE|\!\=|<<|>>|<>|>\=|<=|[\*\+\-\/\%\|\&><])\z/

    def initialize(proxy)
      @proxy = proxy
    end

    def build_fader(method_name, query, *args, &block)
      method = method_name.to_s
      if @proxy.shard_fixed?
        return SpecifiedShard.new(@proxy,
                                  { @proxy.fixed_shard => query },
                                  method, query, *args, &block)
      end
      binds = (method == 'insert') ? args[4] : args[1]
      binded_query = bind_sql(query, binds)

      begin
        tree = SQLTree[binded_query]
      rescue Exception => err
        logger.warn { "[ActiveRecord::Turntable] Error on Parsing SQL: #{binded_query}, on_method: #{method_name}" }
        raise err
      end

      case tree
      when SQLTree::Node::SelectQuery
        build_select_fader(tree, method, query, *args, &block)
      when SQLTree::Node::UpdateQuery, SQLTree::Node::DeleteQuery
        build_update_fader(tree, method, query, *args, &block)
      when SQLTree::Node::InsertQuery
        build_insert_fader(tree, method, query, *args, &block)
      else
        # send to master shard
        Fader::SpecifiedShard.new(@proxy,
                           { @proxy.master => query },
                           method, query, *args, &block)
      end
    rescue Exception => err
      logger.warn { "[ActiveRecord::Turntable] Error on Building Fader: #{binded_query}, on_method: #{method_name}" }
      raise err<commit_msg>Add err detail to Building Fader exception log<commit_after>require 'active_support/core_ext/object/try'
require 'active_record/turntable/sql_tree_patch'

module ActiveRecord::Turntable
  class Mixer
    extend ActiveSupport::Autoload

    eager_autoload do
      autoload :Fader
    end

    delegate :logger, :to => ActiveRecord::Base

    NOT_USED_FOR_SHARDING_OPERATORS_REGEXP = /\A(NOT IN|IS|IS NOT|BETWEEN|LIKE|\!\=|<<|>>|<>|>\=|<=|[\*\+\-\/\%\|\&><])\z/

    def initialize(proxy)
      @proxy = proxy
    end

    def build_fader(method_name, query, *args, &block)
      method = method_name.to_s
      if @proxy.shard_fixed?
        return SpecifiedShard.new(@proxy,
                                  { @proxy.fixed_shard => query },
                                  method, query, *args, &block)
      end
      binds = (method == 'insert') ? args[4] : args[1]
      binded_query = bind_sql(query, binds)

      begin
        tree = SQLTree[binded_query]
      rescue Exception => err
        logger.warn { "[ActiveRecord::Turntable] Error on Parsing SQL: #{binded_query}, on_method: #{method_name}" }
        raise err
      end

      case tree
      when SQLTree::Node::SelectQuery
        build_select_fader(tree, method, query, *args, &block)
      when SQLTree::Node::UpdateQuery, SQLTree::Node::DeleteQuery
        build_update_fader(tree, method, query, *args, &block)
      when SQLTree::Node::InsertQuery
        build_insert_fader(tree, method, query, *args, &block)
      else
        # send to master shard
        Fader::SpecifiedShard.new(@proxy,
                           { @proxy.master => query },
                           method, query, *args, &block)
      end
    rescue Exception => err
      logger.warn { "[ActiveRecord::Turntable] Error on Building Fader: #{binded_query}, on_method: #{method_name}, err: #{err}" }
      raise err
{'fraction_non_alphanumeric': 0.12540628385698807, 'fraction_numerical': 0.0010834236186348862, 'mean_word_length': 2.5037950664136623, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6629120', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1137, 'n_words': 254}