Introduced Krawfish::Compile::Segment::Nowhere

Change-Id: If7f73dd173f1b2dde8b44068ecfdd473513a5018
diff --git a/lib/Krawfish/Compile.pm b/lib/Krawfish/Compile.pm
index 8efd01f..fb46c1a 100644
--- a/lib/Krawfish/Compile.pm
+++ b/lib/Krawfish/Compile.pm
@@ -89,6 +89,10 @@
 };
 
 
+sub clone {
+  warn 'Unimplemented';
+};
+
 # Get maximum frequency
 sub max_freq {
   $_[0]->{query}->max_freq;
diff --git a/lib/Krawfish/Compile/Segment/Nowhere.pm b/lib/Krawfish/Compile/Segment/Nowhere.pm
new file mode 100644
index 0000000..c6c37ed
--- /dev/null
+++ b/lib/Krawfish/Compile/Segment/Nowhere.pm
@@ -0,0 +1,14 @@
+package Krawfish::Compile::Segment::Nowhere;
+use strict;
+use warnings;
+use Role::Tiny;
+
+with 'Krawfish::Query::Nowhere';
+with 'Krawfish::Compile';
+
+
+sub compile {
+  return shift->result;
+};
+
+1;
diff --git a/lib/Krawfish/Corpus.pm b/lib/Krawfish/Corpus.pm
index 6b7d7f9..699efe2 100644
--- a/lib/Krawfish/Corpus.pm
+++ b/lib/Krawfish/Corpus.pm
@@ -93,4 +93,11 @@
 };
 
 
+# Stop compilation of results in non-compile queries
+sub compile {
+  1;
+};
+
+
+
 1;
diff --git a/lib/Krawfish/Koral/Compile.pm b/lib/Krawfish/Koral/Compile.pm
index 2629bee..300a927 100644
--- a/lib/Krawfish/Koral/Compile.pm
+++ b/lib/Krawfish/Koral/Compile.pm
@@ -255,18 +255,6 @@
 };
 
 
-# Send to segments
-sub to_segment {
-  ...
-};
-
-
-# Optimize query
-sub optimize {
-  ...
-};
-
-
 1;
 
 
diff --git a/lib/Krawfish/Koral/Compile/Builder.pm b/lib/Krawfish/Koral/Compile/Builder.pm
index ef1b1c3..f7cb3cc 100644
--- a/lib/Krawfish/Koral/Compile/Builder.pm
+++ b/lib/Krawfish/Koral/Compile/Builder.pm
@@ -155,6 +155,7 @@
   );
 };
 
+
 sub e_inline {
   # TODO:
   #   Add inline annotation information, like
@@ -167,6 +168,7 @@
   ...
 };
 
+
 # Enrich with Term lists per class
 sub e_terms {
   shift;
diff --git a/lib/Krawfish/Koral/Compile/Node/Aggregate.pm b/lib/Krawfish/Koral/Compile/Node/Aggregate.pm
index 7adaffb..88a83d0 100644
--- a/lib/Krawfish/Koral/Compile/Node/Aggregate.pm
+++ b/lib/Krawfish/Koral/Compile/Node/Aggregate.pm
@@ -1,6 +1,6 @@
 package Krawfish::Koral::Compile::Node::Aggregate;
 use Krawfish::Compile::Segment::Aggregate;
-use Krawfish::Query::Nowhere;
+use Krawfish::Compile::Segment::Nowhere;
 use strict;
 use warnings;
 
@@ -54,7 +54,7 @@
   #   null-values for aggregation that need to
   #   be returned.
   if ($query->max_freq == 0) {
-    return Krawfish::Query::Nowhere->new;
+    return Krawfish::Compile::Segment::Nowhere->new;
   };
 
   # Get all aggregations
diff --git a/lib/Krawfish/Koral/Compile/Node/Enrich/CorpusClasses.pm b/lib/Krawfish/Koral/Compile/Node/Enrich/CorpusClasses.pm
index a06e2a6..f7e14a9 100644
--- a/lib/Krawfish/Koral/Compile/Node/Enrich/CorpusClasses.pm
+++ b/lib/Krawfish/Koral/Compile/Node/Enrich/CorpusClasses.pm
@@ -1,7 +1,7 @@
 package Krawfish::Koral::Compile::Node::Enrich::CorpusClasses;
 use Krawfish::Compile::Segment::Enrich::CorpusClasses;
 use Krawfish::Util::Bits;
-use Krawfish::Query::Nowhere;
+use Krawfish::Compile::Segment::Nowhere;
 use strict;
 use warnings;
 
@@ -40,7 +40,7 @@
   my $query = $self->{query}->optimize($segment);
 
   if ($query->max_freq == 0) {
-    return Krawfish::Query::Nowhere->new;
+    return Krawfish::Compile::Segment::Nowhere->new;
   };
 
   # Create corpus class object
diff --git a/lib/Krawfish/Koral/Compile/Node/Enrich/Fields.pm b/lib/Krawfish/Koral/Compile/Node/Enrich/Fields.pm
index 0dce9f3..387c532 100644
--- a/lib/Krawfish/Koral/Compile/Node/Enrich/Fields.pm
+++ b/lib/Krawfish/Koral/Compile/Node/Enrich/Fields.pm
@@ -1,7 +1,7 @@
 package Krawfish::Koral::Compile::Node::Enrich::Fields;
 use Krawfish::Compile::Segment::Enrich::Fields;
 use Krawfish::Util::String qw/squote/;
-use Krawfish::Query::Nowhere;
+use Krawfish::Compile::Segment::Nowhere;
 use strict;
 use warnings;
 
@@ -63,7 +63,7 @@
   my $query = $self->{query}->optimize($segment);
 
   if ($query->max_freq == 0) {
-    return Krawfish::Query::Nowhere->new;
+    return Krawfish::Compile::Segment::Nowhere->new;
   };
 
   return Krawfish::Compile::Segment::Enrich::Fields->new(
diff --git a/lib/Krawfish/Koral/Compile/Node/Enrich/Snippet.pm b/lib/Krawfish/Koral/Compile/Node/Enrich/Snippet.pm
index 8ac16c7..a4d5d43 100644
--- a/lib/Krawfish/Koral/Compile/Node/Enrich/Snippet.pm
+++ b/lib/Krawfish/Koral/Compile/Node/Enrich/Snippet.pm
@@ -1,7 +1,7 @@
 package Krawfish::Koral::Compile::Node::Enrich::Snippet;
 use Krawfish::Compile::Segment::Enrich::Snippet;
 use Krawfish::Util::String qw/squote/;
-use Krawfish::Query::Nowhere;
+use Krawfish::Compile::Segment::Nowhere;
 use strict;
 use warnings;
 
@@ -64,7 +64,7 @@
   my $query = $self->{query}->optimize($segment);
 
   if ($query->max_freq == 0) {
-    return Krawfish::Query::Nowhere->new;
+    return Krawfish::Compile::Segment::Nowhere->new;
   };
 
   # Create left context object
diff --git a/lib/Krawfish/Koral/Compile/Node/Enrich/Terms.pm b/lib/Krawfish/Koral/Compile/Node/Enrich/Terms.pm
index 618c67f..9d91860 100644
--- a/lib/Krawfish/Koral/Compile/Node/Enrich/Terms.pm
+++ b/lib/Krawfish/Koral/Compile/Node/Enrich/Terms.pm
@@ -1,6 +1,6 @@
 package Krawfish::Koral::Compile::Node::Enrich::Terms;
 use Krawfish::Compile::Segment::Enrich::Terms;
-use Krawfish::Query::Nowhere;
+use Krawfish::Compile::Segment::Nowhere;
 use strict;
 use warnings;
 
@@ -37,7 +37,7 @@
   my $query = $self->{query}->optimize($segment);
 
   if ($query->max_freq == 0) {
-    return Krawfish::Query::Nowhere->new;
+    return Krawfish::Compile::Segment::Nowhere->new;
   };
 
   return Krawfish::Compile::Segment::Enrich::Terms->new(
diff --git a/lib/Krawfish/Koral/Compile/Node/Group/ClassFrequencies.pm b/lib/Krawfish/Koral/Compile/Node/Group/ClassFrequencies.pm
index 5c71ab9..93bf0c7 100644
--- a/lib/Krawfish/Koral/Compile/Node/Group/ClassFrequencies.pm
+++ b/lib/Krawfish/Koral/Compile/Node/Group/ClassFrequencies.pm
@@ -1,7 +1,7 @@
 package Krawfish::Koral::Compile::Node::Group::ClassFrequencies;
 use Krawfish::Compile::Segment::Group::ClassFrequencies;
 use Krawfish::Util::String qw/squote/;
-use Krawfish::Query::Nowhere;
+use Krawfish::Compile::Segment::Nowhere;
 use strict;
 use warnings;
 
@@ -38,7 +38,7 @@
   my $query = $self->{query}->optimize($segment);
 
   if ($query->max_freq == 0) {
-    return Krawfish::Query::Nowhere->new;
+    return Krawfish::Compile::Segment::Nowhere->new;
   };
 
   return Krawfish::Compile::Segment::Group::ClassFrequencies->new(
diff --git a/lib/Krawfish/Koral/Compile/Node/Group/Fields.pm b/lib/Krawfish/Koral/Compile/Node/Group/Fields.pm
index 72329ca..209eeac 100644
--- a/lib/Krawfish/Koral/Compile/Node/Group/Fields.pm
+++ b/lib/Krawfish/Koral/Compile/Node/Group/Fields.pm
@@ -1,7 +1,7 @@
 package Krawfish::Koral::Compile::Node::Group::Fields;
 use Krawfish::Compile::Segment::Group::Fields;
 use Krawfish::Util::String qw/squote/;
-use Krawfish::Query::Nowhere;
+use Krawfish::Compile::Segment::Nowhere;
 use strict;
 use warnings;
 
@@ -63,7 +63,7 @@
   my $query = $self->{query}->optimize($segment);
 
   if ($query->max_freq == 0) {
-    return Krawfish::Query::Nowhere->new;
+    return Krawfish::Compile::Segment::Nowhere->new;
   };
 
   return Krawfish::Compile::Segment::Group::Fields->new(
diff --git a/lib/Krawfish/Koral/Compile/Node/Limit.pm b/lib/Krawfish/Koral/Compile/Node/Limit.pm
index 0196824..deb7463 100644
--- a/lib/Krawfish/Koral/Compile/Node/Limit.pm
+++ b/lib/Krawfish/Koral/Compile/Node/Limit.pm
@@ -4,7 +4,7 @@
 #   Limiting can only be done on the cluster level!
 
 use Krawfish::Compile::Cluster::Limit;
-use Krawfish::Query::Nowhere;
+use Krawfish::Compile::Segment::Nowhere;
 use Krawfish::Log;
 use strict;
 use warnings;
@@ -50,7 +50,7 @@
   my $query = $self->{query}->optimize($segment);
 
   if ($query->max_freq == 0) {
-    return Krawfish::Query::Nowhere->new;
+    return Krawfish::Compile::Segment::Nowhere->new;
   };
 
   return Krawfish::Compile::Limit->new(
diff --git a/lib/Krawfish/Koral/Compile/Node/Sort.pm b/lib/Krawfish/Koral/Compile/Node/Sort.pm
index 395b286..a68a4ea 100644
--- a/lib/Krawfish/Koral/Compile/Node/Sort.pm
+++ b/lib/Krawfish/Koral/Compile/Node/Sort.pm
@@ -2,7 +2,7 @@
 use Krawfish::Compile::Segment::Sort;
 use Krawfish::Compile::Segment::SortAfter;
 use Krawfish::Compile::Segment::BundleDocs;
-use Krawfish::Query::Nowhere;
+use Krawfish::Compile::Segment::Nowhere;
 use Krawfish::Log;
 use strict;
 use warnings;
@@ -68,7 +68,7 @@
   my $query = $self->{query}->optimize($segment);
 
   if ($query->max_freq == 0) {
-    return Krawfish::Query::Nowhere->new;
+    return Krawfish::Compile::Segment::Nowhere->new;
   };
 
   # TODO:
diff --git a/lib/Krawfish/Koral/Compile/Node/Sort/Sample.pm b/lib/Krawfish/Koral/Compile/Node/Sort/Sample.pm
index abedc35..56eada1 100644
--- a/lib/Krawfish/Koral/Compile/Node/Sort/Sample.pm
+++ b/lib/Krawfish/Koral/Compile/Node/Sort/Sample.pm
@@ -1,6 +1,6 @@
 package Krawfish::Koral::Compile::Node::Sort::Sample;
 use Krawfish::Compile::Segment::Sort::Sample;
-use Krawfish::Query::Nowhere;
+use Krawfish::Compile::Segment::Nowhere;
 use strict;
 use warnings;
 
@@ -43,7 +43,7 @@
   my $query = $self->{query}->optimize($segment);
 
   if ($query->max_freq == 0) {
-    return Krawfish::Query::Nowhere->new;
+    return Krawfish::Compile::Segment::Nowhere->new;
   };
 
   return Krawfish::Compile::Segment::Sort::Sample->new(
diff --git a/lib/Krawfish/Query.pm b/lib/Krawfish/Query.pm
index 7b4ea91..2832a09 100644
--- a/lib/Krawfish/Query.pm
+++ b/lib/Krawfish/Query.pm
@@ -85,15 +85,6 @@
 };
 
 
-
-# Clone query
-# (Not implemented yet)
-sub clone {
-  warn $_[0];
-  ...
-};
-
-
 # TODO:
 #   This is a value that should probably be stored
 #   at span-beginnings and can help to jump through very long
@@ -121,10 +112,4 @@
 };
 
 
-# Stop compilation of results in non-compile queries
-sub compile {
-  return 1;
-};
-
-
 1;
diff --git a/lib/Krawfish/Query/Base/Sorted.pm b/lib/Krawfish/Query/Base/Sorted.pm
index d74d6c3..d8ee044 100644
--- a/lib/Krawfish/Query/Base/Sorted.pm
+++ b/lib/Krawfish/Query/Base/Sorted.pm
@@ -46,21 +46,36 @@
 };
 
 
+# Clone query
+sub clone {
+  my $self = shift;
+  return __PACKAGE__->new(
+    $self->{span},
+    $self->{capacity}
+  );
+};
+
+
+# Get maximum frequency
 sub max_freq {
   $_->{span}->max_freq;
 };
 
 
+# Filter query
 sub filter_by {
   my $self = shift;
   $self->{span}->filter_by(@_);
 };
 
 
+# Check if the query requires filtering
 sub requires_filter {
   shift->{span}->requires_filter;
 };
 
+
+# Stringification
 sub to_string {
   my $self = shift;
   return 'sorted(' . $self->{span}->to_string . ')';
diff --git a/lib/Krawfish/Query/Nowhere.pm b/lib/Krawfish/Query/Nowhere.pm
index 89fb1f2..7510055 100644
--- a/lib/Krawfish/Query/Nowhere.pm
+++ b/lib/Krawfish/Query/Nowhere.pm
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 use Krawfish::Util::Constants qw/NOMOREDOCS/;
-use Role::Tiny::With;
+use Role::Tiny;
 
 with 'Krawfish::Query';
 
diff --git a/lib/Test/Krawfish.pm b/lib/Test/Krawfish.pm
index 83a6c2e..7e9ec2a 100644
--- a/lib/Test/Krawfish.pm
+++ b/lib/Test/Krawfish.pm
@@ -272,8 +272,13 @@
   foreach my $key (sort keys %$hash) {
     my $type = 'string';
     my $long_key = $key;
-    if ($key =~ s/^(string|integer|store)_//) {
-      $type = $1;
+    if ($key =~ s/^(string|int(?:eger)?|store)_//) {
+      if ($1 eq 'int') {
+        $type = 'integer';
+      }
+      else {
+        $type = $1;
+      };
     };
 
     push(@fields, {
diff --git a/t/query/clone.t b/t/query/clone.t
index 7ab09c8..96e779a 100644
--- a/t/query/clone.t
+++ b/t/query/clone.t
@@ -10,19 +10,33 @@
 use_ok('Krawfish::Koral');
 
 my $index = Krawfish::Index->new;
+
+ok($index->introduce_field('id', 'NUM'),
+   'Introduce field as sortable');
+ok($index->introduce_field('size', 'NUM'),
+   'Introduce field as sortable');
+ok($index->introduce_field('genre', 'DE'),
+   'Introduce field as sortable');
+
 ok_index($index, {
   id => 1,
+  integer_size => 4,
   genre => 'novel',
 } => '[a|b]<1:x>[a|b|c]</1>[a][b|c]', 'Add complex document');
 ok_index($index, {
   id => 2,
+  integer_size => 5,
   genre => 'news',
 } => '[a|b][a|b|c][a][b|c]', 'Add complex document');
 ok_index($index, {
   id => 3,
+  integer_size => 7,
   genre => 'novel',
 } => '<1:x>[a|b][a|b|c][a]</1>[b|c]', 'Add complex document');
 
+ok($index->commit, 'Commit data');
+
+
 my $koral = Krawfish::Koral->new;
 ok(my $qb = $koral->query_builder, 'Create Koral::Builder');
 ok(my $cb = $koral->corpus_builder, 'Create Koral::Builder');
@@ -82,7 +96,7 @@
 ok($query = $query->identify($index->dict), 'Identify');
 
 is($query->to_string,
-   'filter(unique({3:(#6&#7)|#9}[]#6{1,4}),(#11|#2)&(#13|#4))',
+   'filter(unique({3:#11|(#8&#9)}[]#8{1,4}),(#13|#4)&(#16|#5))',
    'stringification');
 
 ok($query = $query->optimize($index->segment), 'Materialize');
@@ -101,6 +115,50 @@
 is($clone->current->to_string, '[2:0-3$0,3,0,1]', 'Current match');
 ok(!$clone->next, 'Next match found');
 
+
+$koral = Krawfish::Koral->new;
+ok(my $comp = $koral->compilation_builder, 'Create Koral::Builder');
+$koral->query($qb->token('a'));
+$koral->compilation(
+  $comp->aggregate(
+    $comp->a_fields(qw/author/),
+    $comp->a_frequencies,
+    $comp->a_length,
+    $comp->a_values(qw/size/)
+  ),
+  $comp->enrich(
+    $comp->e_fields(qw/size/),
+    $comp->e_corpus_classes(3,4) # TODO: Ignore corpus classes, in case they are not set!
+  ),
+  $comp->sort_by(
+    $comp->s_field('genre')
+  )
+);
+
+# Check stringification
+is($koral->to_string,
+   "compilation=[".
+     "aggr=[fields:['author'],freq,length,values:['size']],".
+     "enrich=[fields:['size'],".
+     "corpusclasses:[3,4]],".
+     "sort=[field='genre'<]".
+     "],".
+     "query=[[a]]",
+   'Serialization');
+
+ok($query = $koral->to_query->identify($index->dict)->optimize($index->segment), 'Query generation');
+#ok($clone = $query->clone, 'Cloning');
+
+#is($query->to_string,
+#  '[0]',
+#  'Stringification');
+
+# Run query
+#is($query->compile->inflate($index->dict)->to_string,
+#  '',
+#  'Stringification');
+
+
 # Test cloning (and running)
 diag 'Check compile queries';