<commit_before>module ROM
  module Plugins
    module Relation
      module SQL
        # @api private
        module AutoCombine
          # @api private
          def self.included(klass)
            super
            klass.class_eval do
              include(InstanceInterface)
              extend(ClassInterface)
            end
          end

          # @api private
          module ClassInterface
            # @api private
            def inherited(klass)
              super
              klass.auto_curry :for_combine
              klass.auto_curry :preload
            end
          end

          # @api private
          module InstanceInterface
            # Default methods for fetching combined relation
            #
            # This method is used by default by `combine`
            #
            # @return [SQL::Relation]
            #
            # @api private
            def for_combine(spec)
              source_key, target_key, target =
                case spec
                when ROM::SQL::Association
                  [*spec.join_keys(__registry__).flatten, spec.call(__registry__, self)]
                else
                  [*spec.flatten, self]
                end

              target.preload(source_key, target_key)
            end

            # @api private
            def preload(source_key, target_key, source)
              where(target_key => source.pluck(source_key.to_sym))
            end
          end
        end
      end
    end
  end
end

ROM.plugins do
  adapter :sql do
    register :auto_combine, ROM::Plugins::Relation::SQL::AutoCombine, type: :relation
  end
end<commit_msg>Avoid passing duped ids in AutoCombine#preload<commit_after>module ROM
  module Plugins
    module Relation
      module SQL
        # @api private
        module AutoCombine
          # @api private
          def self.included(klass)
            super
            klass.class_eval do
              include(InstanceInterface)
              extend(ClassInterface)
            end
          end

          # @api private
          module ClassInterface
            # @api private
            def inherited(klass)
              super
              klass.auto_curry :for_combine
              klass.auto_curry :preload
            end
          end

          # @api private
          module InstanceInterface
            # Default methods for fetching combined relation
            #
            # This method is used by default by `combine`
            #
            # @return [SQL::Relation]
            #
            # @api private
            def for_combine(spec)
              source_key, target_key, target =
                case spec
                when ROM::SQL::Association
                  [*spec.join_keys(__registry__).flatten, spec.call(__registry__, self)]
                else
                  [*spec.flatten, self]
                end

              target.preload(source_key, target_key)
            end

            # @api private
            def preload(source_key, target_key, source)
              target_pks = source.pluck(source_key.to_sym).uniq
              target_pks.uniq!

              where(target_key => target_pks)
            end
          end
        end
      end
    end
  end
end

ROM.plugins do
  adapter :sql do
    register :auto_combine, ROM::Plugins::Relation::SQL::AutoCombine, type: :relation
  end
end
{'fraction_non_alphanumeric': 0.06113408151210868, 'fraction_numerical': 0.0, 'mean_word_length': 1.315105946684894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5002150', 'n_tokens_mistral': 868, 'n_tokens_neox': 844, 'n_words': 219}