[Groonga-commit] groonga/groonga [master] [select] ignore invalid _score in --sortby for convenience. fixes #1030

Back to archive index

null+****@clear***** null+****@clear*****
2011年 7月 1日 (金) 13:02:49 JST


Kouhei Sutou	2011-07-01 04:02:49 +0000 (Fri, 01 Jul 2011)

  New Revision: 2f4774a6eba462052ea531880f7dfbd119024ce2

  Log:
    [select] ignore invalid _score in --sortby for convenience. fixes #1030

  Modified files:
    lib/db.c
    lib/proc.c
    test/unit/core/test-command-select-sort.c

  Modified: lib/db.c (+15 -7)
===================================================================
--- lib/db.c    2011-07-01 03:02:30 +0000 (4ec2c6f)
+++ lib/db.c    2011-07-01 04:02:49 +0000 (3bd8508)
@@ -7673,7 +7673,7 @@ grn_table_sort_key_from_str_geo(grn_ctx *ctx, const char *str, unsigned str_size
     }
     GRN_FREE(tokbuf);
   }
-  if (!ctx->rc) {
+  if (!ctx->rc && k - keys > 0) {
     *nkeys = k - keys;
   } else {
     grn_table_sort_key_close(ctx, keys, k - keys);
@@ -7709,19 +7709,27 @@ grn_table_sort_key_from_str(grn_ctx *ctx, const char *str, unsigned str_size,
             k->flags = GRN_TABLE_SORT_DESC;
             p++;
           }
-          if (!(k->key = grn_obj_column(ctx, table, p, r - p))) {
-            WARN(GRN_INVALID_ARGUMENT, "invalid sort key: <%.*s>(<%.*s>)",
-                 tokbuf[i] - p, p, str_size, str);
-            break;
+          if ((k->key = grn_obj_column(ctx, table, p, r - p))) {
+            k++;
+          } else {
+            if (r - p == 6 && memcmp(p, "_score", 6) == 0) {
+              GRN_LOG(ctx, GRN_WARN,
+                      "ignore invalid sore key: <%.*s>(<%.*s>)",
+                      r - p, p, str_size, str);
+            } else {
+              WARN(GRN_INVALID_ARGUMENT,
+                   "invalid sort key: <%.*s>(<%.*s>)",
+                   r - p, p, str_size, str);
+              break;
+            }
           }
-          k++;
         }
         p = r;
       }
     }
     GRN_FREE(tokbuf);
   }
-  if (!ctx->rc) {
+  if (!ctx->rc && k - keys > 0) {
     *nkeys = k - keys;
   } else {
     grn_table_sort_key_close(ctx, keys, k - keys);

  Modified: lib/proc.c (+21 -20)
===================================================================
--- lib/proc.c    2011-07-01 03:02:30 +0000 (df4bb3c)
+++ lib/proc.c    2011-07-01 04:02:49 +0000 (df56479)
@@ -199,31 +199,32 @@ grn_select(grn_ctx *ctx, const char *table, unsigned table_len,
 
       grn_normalize_offset_and_limit(ctx, nhits, &offset, &limit);
 
-      if (sortby_len) {
+      if (sortby_len &&
+          (keys = grn_table_sort_key_from_str(ctx, sortby, sortby_len, res, &nkeys))) {
         if ((sorted = grn_table_create(ctx, NULL, 0, NULL,
                                        GRN_OBJ_TABLE_NO_KEY, NULL, res))) {
-          if ((keys = grn_table_sort_key_from_str(ctx, sortby, sortby_len, res, &nkeys))) {
-            grn_table_sort(ctx, res, offset, limit, sorted, keys, nkeys);
-            LAP(":", "sort(%d)", limit);
-            GRN_OBJ_FORMAT_INIT(&format, nhits, 0, limit, offset);
-            format.flags =
-              GRN_OBJ_FORMAT_WITH_COLUMN_NAMES|
-              GRN_OBJ_FORMAT_XML_ELEMENT_RESULTSET;
-            grn_obj_columns(ctx, sorted, output_columns, output_columns_len, &format.columns);
-            GRN_OUTPUT_OBJ(sorted, &format);
-            GRN_OBJ_FORMAT_FIN(ctx, &format);
-            grn_table_sort_key_close(ctx, keys, nkeys);
-          }
+          grn_table_sort(ctx, res, offset, limit, sorted, keys, nkeys);
+          LAP(":", "sort(%d)", limit);
+          GRN_OBJ_FORMAT_INIT(&format, nhits, 0, limit, offset);
+          format.flags =
+            GRN_OBJ_FORMAT_WITH_COLUMN_NAMES|
+            GRN_OBJ_FORMAT_XML_ELEMENT_RESULTSET;
+          grn_obj_columns(ctx, sorted, output_columns, output_columns_len, &format.columns);
+          GRN_OUTPUT_OBJ(sorted, &format);
+          GRN_OBJ_FORMAT_FIN(ctx, &format);
           grn_obj_unlink(ctx, sorted);
         }
+        grn_table_sort_key_close(ctx, keys, nkeys);
       } else {
-        GRN_OBJ_FORMAT_INIT(&format, nhits, offset, limit, offset);
-        format.flags =
-          GRN_OBJ_FORMAT_WITH_COLUMN_NAMES|
-          GRN_OBJ_FORMAT_XML_ELEMENT_RESULTSET;
-        grn_obj_columns(ctx, res, output_columns, output_columns_len, &format.columns);
-        GRN_OUTPUT_OBJ(res, &format);
-        GRN_OBJ_FORMAT_FIN(ctx, &format);
+        if (!ctx->rc) {
+          GRN_OBJ_FORMAT_INIT(&format, nhits, offset, limit, offset);
+          format.flags =
+            GRN_OBJ_FORMAT_WITH_COLUMN_NAMES|
+            GRN_OBJ_FORMAT_XML_ELEMENT_RESULTSET;
+          grn_obj_columns(ctx, res, output_columns, output_columns_len, &format.columns);
+          GRN_OUTPUT_OBJ(res, &format);
+          GRN_OBJ_FORMAT_FIN(ctx, &format);
+        }
       }
       LAP(":", "output(%d)", limit);
       if (!ctx->rc && drilldown_len) {

  Modified: test/unit/core/test-command-select-sort.c (+54 -15)
===================================================================
--- test/unit/core/test-command-select-sort.c    2011-07-01 03:02:30 +0000 (1e68e3b)
+++ test/unit/core/test-command-select-sort.c    2011-07-01 04:02:49 +0000 (13c68f2)
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
 /*
-  Copyright (C) 2010  Kouhei Sutou <kou****@clear*****>
+  Copyright (C) 2010-2011  Kouhei Sutou <kou****@clear*****>
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -27,6 +27,7 @@ void test_int(void);
 void test_drilldown(void);
 void test_score_without_query(void);
 void test_score_drilldown_without_query(void);
+void test_nonexistent(void);
 void test_index(void);
 
 static gchar *tmp_directory;
@@ -200,28 +201,66 @@ test_drilldown(void)
 void
 test_score_without_query(void)
 {
-  grn_test_assert_send_command_error(
-    context,
-    GRN_INVALID_ARGUMENT,
-    "invalid sort key: <_score>(<_score>)",
-    "select Sites "
-    "--sortby \"_score\" "
-    "--output_columns \"_key\"");
+  cut_assert_equal_string(
+    "[[[3],"
+      "[[\"_key\",\"ShortText\"]],"
+      "[\"2ch.net\"],"
+      "[\"groonga.org\"],"
+      "[\"qwik.jp/senna/FrontPageJ.html\"]]]",
+    send_command("select Sites "
+                 "--sortby \"_score\" "
+                 "--output_columns \"_key\""));
 }
 
 void
 test_score_drilldown_without_query(void)
 {
+  cut_assert_equal_string(
+    "[[[5],"
+      "[[\"_id\",\"UInt32\"],"
+       "[\"site._key\",\"ShortText\"],"
+       "[\"user._key\",\"ShortText\"]],"
+      "[5,\"2ch.net\",\"yu\"],"
+      "[4,\"2ch.net\",\"gunyara-kun\"],"
+      "[3,\"groonga.org\",\"yu\"],"
+      "[2,\"groonga.org\",\"gunyara-kun\"],"
+      "[1,\"groonga.org\",\"morita\"]],"
+     "[[2],"
+      "[[\"_key\",\"ShortText\"],"
+       "[\"_nsubrecs\",\"Int32\"]],"
+      "[\"2ch.net\",2],"
+      "[\"groonga.org\",3]],"
+     "[[3],"
+      "[[\"_key\",\"ShortText\"],"
+       "[\"_nsubrecs\",\"Int32\"]],"
+      "[\"gunyara-kun\",2],"
+      "[\"morita\",1],"
+      "[\"yu\",2]],"
+    "[[4],"
+     "[[\"_key\",\"Int32\"],"
+      "[\"_nsubrecs\",\"Int32\"]],"
+     "[0,1],"
+     "[10,1],"
+     "[50,1],"
+     "[100,2]]]",
+    send_command("select Bookmarks "
+                 "--sortby \"_score,-_id\" "
+                 "--output_columns \"_id, site._key, user._key\" "
+                 "--drilldown \"site user rank\" "
+                 "--drilldown_output_columns \"_key, _nsubrecs\" "
+                 "--drilldown_sortby \"_key\""));
+}
+
+void
+test_nonexistent(void)
+{
   grn_test_assert_send_command_error(
     context,
     GRN_INVALID_ARGUMENT,
-    "invalid sort key: <_score>(<_score>)",
-    "select Bookmarks "
-    "--sortby \"_score\" "
-    "--output_columns \"site._key, user._key\" "
-    "--drilldown \"site user rank\" "
-    "--drilldown_output_columns \"_key, _nsubrecs\" "
-    "--drilldown_sortby \"_key\"");
+    "invalid sort key: <nonexistent>(<_score,nonexistent>)",
+    "select Sites "
+    "--sortby \"_score,nonexistent\" "
+    "--output_columns \"_key\"");
 }
 
 void




Groonga-commit メーリングリストの案内
Back to archive index