<commit_before>module ClosureTree
  module HashTreeSupport
    def default_tree_scope(limit_depth = nil)
        # Deepest generation, within limit, for each descendant
        # NOTE: Postgres requires HAVING clauses to always contains aggregate functions (!!)
        having_clause = limit_depth ? "HAVING MAX(generations) <= #{limit_depth - 1}" : ''
        generation_depth = <<-SQL.strip_heredoc
          INNER JOIN (
            SELECT descendant_id, MAX(generations) as depth
            FROM #{quoted_hierarchy_table_name}
            INNER JOIN (#{self.all.to_sql}) AS outer_table
              ON outer_table.#{primary_key} = #{quoted_hierarchy_table_name}.descendant_id
            GROUP BY descendant_id
            #{having_clause}
          ) AS generation_depth
            ON #{quoted_table_name}.#{model_class.primary_key} = generation_depth.descendant_id
        SQL
        scope_with_order(model_class.joins(generation_depth), 'generation_depth.depth')
      end

    def hash_tree(tree_scope, limit_depth = nil)
      limited_scope = if tree_scope
        limit_depth ? tree_scope.where("#{quoted_hierarchy_table_name}.generations <= #{limit_depth - 1}") : tree_scope
      else
        default_tree_scope(limit_depth)<commit_msg>Fixing sql<commit_after>module ClosureTree
  module HashTreeSupport
    def default_tree_scope(limit_depth = nil)
        # Deepest generation, within limit, for each descendant
        # NOTE: Postgres requires HAVING clauses to always contains aggregate functions (!!)
        having_clause = limit_depth ? "HAVING MAX(generations) <= #{limit_depth - 1}" : ''
        generation_depth = <<-SQL.strip_heredoc
          INNER JOIN (
            SELECT descendant_id, MAX(generations) as depth
            FROM #{quoted_hierarchy_table_name}
            INNER JOIN (#{model_class.all.to_sql}) AS outer_table
              ON outer_table.#{model_class.primary_key} = #{quoted_hierarchy_table_name}.descendant_id
            GROUP BY descendant_id
            #{having_clause}
          ) AS generation_depth
            ON #{quoted_table_name}.#{model_class.primary_key} = generation_depth.descendant_id
        SQL
        scope_with_order(model_class.joins(generation_depth), 'generation_depth.depth')
      end

    def hash_tree(tree_scope, limit_depth = nil)
      limited_scope = if tree_scope
        limit_depth ? tree_scope.where("#{quoted_hierarchy_table_name}.generations <= #{limit_depth - 1}") : tree_scope
      else
        default_tree_scope(limit_depth)
{'fraction_non_alphanumeric': 0.08373015873015872, 'fraction_numerical': 0.0015873015873015873, 'mean_word_length': 3.0465489566613164, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '4424497', 'n_tokens_mistral': 753, 'n_tokens_neox': 733, 'n_words': 152}