<commit_before>require_relative 'pass_thru'

module ConceptQL
  module Nodes
    class Complement < PassThru
      def query(db)
        child = children.first
        child.types.map do |type|
          positive_query = db.from(child.evaluate(db))
            .select(:criterion_id)
            .exclude(:criterion_id => nil)
          query = db.from(make_table_name(type))
            .exclude(type_id(type) => positive_query)
          db.from(select_it(query, type))
        end.inject do |union_query, q|
          union_query.union(q, all: true)
        end
      end
    end
  end
end
<commit_msg>Complement: fix bugs intro'd by 5 column changes<commit_after>require_relative 'pass_thru'

module ConceptQL
  module Nodes
    class Complement < PassThru
      def query(db)
        child = children.first
        child.types.map do |type|
          positive_query = db.from(child.evaluate(db))
            .select(:criterion_id)
            .exclude(:criterion_id => nil)
            .where(:criterion_type => type.to_s)
          query = db.from(make_table_name(type))
            .exclude(type_id(type) => positive_query)
          db.from(select_it(query, type))
        end.inject do |union_query, q|
          union_query.union(q, all: true)
        end
      end

=begin
This is an alternate, but equally accurate way to do complement.
We'll need to benchmark which is faster.
      def query2(db)
        child = children.first
        froms = child.types.map do |type|
          select_it(db.from(make_table_name(type)), type)
        end
        big_from = froms.inject { |union_query, q| union_query.union(q, all:true) }
        db.from(big_from).except(child.evaluate(db))
      end
=end
    end
  end
end

{'fraction_non_alphanumeric': 0.09918793503480279, 'fraction_numerical': 0.001160092807424594, 'mean_word_length': 2.298279158699809, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '4102412', 'n_tokens_mistral': 549, 'n_tokens_neox': 535, 'n_words': 126}