[Groonga-commit] groonga/groonga at 10109c6 [master] between: support min/max cast

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Dec 20 00:19:41 JST 2013


Kouhei Sutou	2013-12-20 00:19:41 +0900 (Fri, 20 Dec 2013)

  New Revision: 10109c67365e0a6a8f182a4a12d78463c09fdf0e
  https://github.com/groonga/groonga/commit/10109c67365e0a6a8f182a4a12d78463c09fdf0e

  Message:
    between: support min/max cast

  Added files:
    test/command/suite/select/function/between/with_index/cast/max.expected
    test/command/suite/select/function/between/with_index/cast/max.test
    test/command/suite/select/function/between/with_index/cast/max_error.expected
    test/command/suite/select/function/between/with_index/cast/max_error.test
    test/command/suite/select/function/between/with_index/cast/min.expected
    test/command/suite/select/function/between/with_index/cast/min.test
    test/command/suite/select/function/between/with_index/cast/min_error.expected
    test/command/suite/select/function/between/with_index/cast/min_error.test
    test/command/suite/select/function/between/without_index/cast/max.expected
    test/command/suite/select/function/between/without_index/cast/max.test
    test/command/suite/select/function/between/without_index/cast/min.expected
    test/command/suite/select/function/between/without_index/cast/min.test
  Modified files:
    lib/proc.c

  Modified: lib/proc.c (+82 -14)
===================================================================
--- lib/proc.c    2013-12-19 14:29:54 +0900 (5f6cd50)
+++ lib/proc.c    2013-12-20 00:19:41 +0900 (0cff21c)
@@ -4190,6 +4190,41 @@ exit :
   return rc;
 }
 
+static grn_rc
+between_cast(grn_ctx *ctx, grn_obj *source, grn_obj *destination, grn_id domain,
+             const char *target_argument_name)
+{
+  grn_rc rc;
+
+  GRN_OBJ_INIT(destination, GRN_BULK, 0, domain);
+  rc = grn_obj_cast(ctx, source, destination, GRN_FALSE);
+  if (rc != GRN_SUCCESS) {
+    grn_obj inspected_source;
+    grn_obj *domain_object;
+    char domain_name[GRN_TABLE_MAX_KEY_SIZE];
+    int domain_name_length;
+
+    GRN_TEXT_INIT(&inspected_source, 0);
+    grn_inspect(ctx, &inspected_source, source);
+
+    domain_object = grn_ctx_at(ctx, domain);
+    domain_name_length =
+      grn_obj_name(ctx, domain_object, domain_name, GRN_TABLE_MAX_KEY_SIZE);
+
+    ERR(rc, "between(): failed to cast %s: <%.*s> -> <%.*s>",
+        target_argument_name,
+        (int)GRN_TEXT_LEN(&inspected_source),
+        GRN_TEXT_VALUE(&inspected_source),
+        domain_name_length,
+        domain_name);
+
+    grn_obj_unlink(ctx, &inspected_source);
+    grn_obj_unlink(ctx, domain_object);
+  }
+
+  return rc;
+}
+
 static grn_obj *
 func_between(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 {
@@ -4274,7 +4309,10 @@ selector_between(grn_ctx *ctx, grn_obj *table, grn_obj *index,
   int limit = -1;
   int flags = GRN_CURSOR_ASCENDING | GRN_CURSOR_BY_ID;
   between_data data;
-  grn_obj *index_table;
+  grn_obj casted_min, casted_max;
+  grn_obj *used_min = NULL;
+  grn_obj *used_max = NULL;
+  grn_obj *index_table = NULL;
   grn_table_cursor *cursor;
   grn_id id;
 
@@ -4295,23 +4333,53 @@ selector_between(grn_ctx *ctx, grn_obj *table, grn_obj *index,
   }
 
   index_table = grn_ctx_at(ctx, index->header.domain);
-  /* TODO: min/max cast */
-  cursor = grn_table_cursor_open(ctx, index_table,
-                                 GRN_BULK_HEAD(data.min),
-                                 GRN_BULK_VSIZE(data.min),
-                                 GRN_BULK_HEAD(data.max),
-                                 GRN_BULK_VSIZE(data.max),
-                                 offset, limit, flags);
-  if (cursor) {
-    while ((id = grn_table_cursor_next(ctx, cursor))) {
-      grn_ii_at(ctx, (grn_ii *)index, id, (grn_hash *)res, op);
+  if (data.min->header.type == index_table->header.domain) {
+    used_min = data.min;
+  } else {
+    used_min = &casted_min;
+    rc = between_cast(ctx, data.min, &casted_min, index_table->header.domain,
+                      "min");
+    if (rc != GRN_SUCCESS) {
+      goto exit;
     }
-    grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op);
-    grn_table_cursor_close(ctx, cursor);
+  }
+  if (data.max->header.type == index_table->header.domain) {
+    used_max = data.max;
   } else {
+    used_max = &casted_max;
+    rc = between_cast(ctx, data.max, &casted_max, index_table->header.domain,
+                      "max");
+    if (rc != GRN_SUCCESS) {
+      goto exit;
+    }
+  }
+  cursor = grn_table_cursor_open(ctx, index_table,
+                                 GRN_BULK_HEAD(used_min),
+                                 GRN_BULK_VSIZE(used_min),
+                                 GRN_BULK_HEAD(used_max),
+                                 GRN_BULK_VSIZE(used_max),
+                                 offset, limit, flags);
+  if (!cursor) {
     rc = ctx->rc;
+    goto exit;
+  }
+
+  while ((id = grn_table_cursor_next(ctx, cursor))) {
+    grn_ii_at(ctx, (grn_ii *)index, id, (grn_hash *)res, op);
+  }
+  grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op);
+  grn_table_cursor_close(ctx, cursor);
+
+exit :
+  if (used_min == &casted_min) {
+    grn_obj_unlink(ctx, &casted_min);
+  }
+  if (used_max == &casted_max) {
+    grn_obj_unlink(ctx, &casted_max);
+  }
+  if (index_table) {
+    grn_obj_unlink(ctx, index_table);
   }
-  grn_obj_unlink(ctx, index_table);
 
   return rc;
 }

  Added: test/command/suite/select/function/between/with_index/cast/max.expected (+61 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/with_index/cast/max.expected    2013-12-20 00:19:41 +0900 (8c13dee)
@@ -0,0 +1,61 @@
+table_create Users TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Users age COLUMN_SCALAR Int32
+[[0,0.0,0.0],true]
+table_create Ages TABLE_PAT_KEY Int32
+[[0,0.0,0.0],true]
+column_create Ages users_age COLUMN_INDEX Users age
+[[0,0.0,0.0],true]
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+[[0,0.0,0.0],5]
+select Users --filter 'between(age, 18, "include", "20", "include")'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        3
+      ],
+      [
+        [
+          "_id",
+          "UInt32"
+        ],
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "age",
+          "Int32"
+        ]
+      ],
+      [
+        2,
+        "bob",
+        18
+      ],
+      [
+        3,
+        "calros",
+        19
+      ],
+      [
+        4,
+        "dave",
+        20
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/between/with_index/cast/max.test (+16 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/with_index/cast/max.test    2013-12-20 00:19:41 +0900 (35b65fd)
@@ -0,0 +1,16 @@
+table_create Users TABLE_HASH_KEY ShortText
+column_create Users age COLUMN_SCALAR Int32
+
+table_create Ages TABLE_PAT_KEY Int32
+column_create Ages users_age COLUMN_INDEX Users age
+
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+
+select Users --filter 'between(age, 18, "include", "20", "include")'

  Added: test/command/suite/select/function/between/with_index/cast/max_error.expected (+20 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/with_index/cast/max_error.expected    2013-12-20 00:19:41 +0900 (e08d202)
@@ -0,0 +1,20 @@
+table_create Users TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Users age COLUMN_SCALAR Int32
+[[0,0.0,0.0],true]
+table_create Ages TABLE_PAT_KEY Int32
+[[0,0.0,0.0],true]
+column_create Ages users_age COLUMN_INDEX Users age
+[[0,0.0,0.0],true]
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+[[0,0.0,0.0],5]
+select Users --filter 'between(age, 18, "include", "XXX", "include")'
+[[[-22,0.0,0.0],"between(): failed to cast max: <\"XXX\"> -> <Int32>"],[]]
+#|e| between(): failed to cast max: <"XXX"> -> <Int32>

  Added: test/command/suite/select/function/between/with_index/cast/max_error.test (+16 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/with_index/cast/max_error.test    2013-12-20 00:19:41 +0900 (20c775d)
@@ -0,0 +1,16 @@
+table_create Users TABLE_HASH_KEY ShortText
+column_create Users age COLUMN_SCALAR Int32
+
+table_create Ages TABLE_PAT_KEY Int32
+column_create Ages users_age COLUMN_INDEX Users age
+
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+
+select Users --filter 'between(age, 18, "include", "XXX", "include")'

  Added: test/command/suite/select/function/between/with_index/cast/min.expected (+61 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/with_index/cast/min.expected    2013-12-20 00:19:41 +0900 (51e5681)
@@ -0,0 +1,61 @@
+table_create Users TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Users age COLUMN_SCALAR Int32
+[[0,0.0,0.0],true]
+table_create Ages TABLE_PAT_KEY Int32
+[[0,0.0,0.0],true]
+column_create Ages users_age COLUMN_INDEX Users age
+[[0,0.0,0.0],true]
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+[[0,0.0,0.0],5]
+select Users --filter 'between(age, "18", "include", 20, "include")'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        3
+      ],
+      [
+        [
+          "_id",
+          "UInt32"
+        ],
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "age",
+          "Int32"
+        ]
+      ],
+      [
+        2,
+        "bob",
+        18
+      ],
+      [
+        3,
+        "calros",
+        19
+      ],
+      [
+        4,
+        "dave",
+        20
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/between/with_index/cast/min.test (+16 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/with_index/cast/min.test    2013-12-20 00:19:41 +0900 (81d29f3)
@@ -0,0 +1,16 @@
+table_create Users TABLE_HASH_KEY ShortText
+column_create Users age COLUMN_SCALAR Int32
+
+table_create Ages TABLE_PAT_KEY Int32
+column_create Ages users_age COLUMN_INDEX Users age
+
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+
+select Users --filter 'between(age, "18", "include", 20, "include")'

  Added: test/command/suite/select/function/between/with_index/cast/min_error.expected (+20 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/with_index/cast/min_error.expected    2013-12-20 00:19:41 +0900 (a56c9e3)
@@ -0,0 +1,20 @@
+table_create Users TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Users age COLUMN_SCALAR Int32
+[[0,0.0,0.0],true]
+table_create Ages TABLE_PAT_KEY Int32
+[[0,0.0,0.0],true]
+column_create Ages users_age COLUMN_INDEX Users age
+[[0,0.0,0.0],true]
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+[[0,0.0,0.0],5]
+select Users --filter 'between(age, "XXX", "include", 20, "include")'
+[[[-22,0.0,0.0],"between(): failed to cast min: <\"XXX\"> -> <Int32>"],[]]
+#|e| between(): failed to cast min: <"XXX"> -> <Int32>

  Added: test/command/suite/select/function/between/with_index/cast/min_error.test (+16 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/with_index/cast/min_error.test    2013-12-20 00:19:41 +0900 (2896cf9)
@@ -0,0 +1,16 @@
+table_create Users TABLE_HASH_KEY ShortText
+column_create Users age COLUMN_SCALAR Int32
+
+table_create Ages TABLE_PAT_KEY Int32
+column_create Ages users_age COLUMN_INDEX Users age
+
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+
+select Users --filter 'between(age, "XXX", "include", 20, "include")'

  Added: test/command/suite/select/function/between/without_index/cast/max.expected (+57 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/without_index/cast/max.expected    2013-12-20 00:19:41 +0900 (27b2953)
@@ -0,0 +1,57 @@
+table_create Users TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Users age COLUMN_SCALAR Int32
+[[0,0.0,0.0],true]
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+[[0,0.0,0.0],5]
+select Users --filter 'between(age, 18, "include", "20", "include")'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        3
+      ],
+      [
+        [
+          "_id",
+          "UInt32"
+        ],
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "age",
+          "Int32"
+        ]
+      ],
+      [
+        2,
+        "bob",
+        18
+      ],
+      [
+        3,
+        "calros",
+        19
+      ],
+      [
+        4,
+        "dave",
+        20
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/between/without_index/cast/max.test (+13 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/without_index/cast/max.test    2013-12-20 00:19:41 +0900 (7170b7c)
@@ -0,0 +1,13 @@
+table_create Users TABLE_HASH_KEY ShortText
+column_create Users age COLUMN_SCALAR Int32
+
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+
+select Users --filter 'between(age, 18, "include", "20", "include")'

  Added: test/command/suite/select/function/between/without_index/cast/min.expected (+57 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/without_index/cast/min.expected    2013-12-20 00:19:41 +0900 (6b383c4)
@@ -0,0 +1,57 @@
+table_create Users TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+column_create Users age COLUMN_SCALAR Int32
+[[0,0.0,0.0],true]
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+[[0,0.0,0.0],5]
+select Users --filter 'between(age, "18", "include", 20, "include")'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        3
+      ],
+      [
+        [
+          "_id",
+          "UInt32"
+        ],
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "age",
+          "Int32"
+        ]
+      ],
+      [
+        2,
+        "bob",
+        18
+      ],
+      [
+        3,
+        "calros",
+        19
+      ],
+      [
+        4,
+        "dave",
+        20
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/between/without_index/cast/min.test (+13 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/between/without_index/cast/min.test    2013-12-20 00:19:41 +0900 (51f4476)
@@ -0,0 +1,13 @@
+table_create Users TABLE_HASH_KEY ShortText
+column_create Users age COLUMN_SCALAR Int32
+
+load --table Users
+[
+{"_key": "alice",  "age": 17},
+{"_key": "bob",    "age": 18},
+{"_key": "calros", "age": 19},
+{"_key": "dave",   "age": 20},
+{"_key": "eric",   "age": 21}
+]
+
+select Users --filter 'between(age, "18", "include", 20, "include")'
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index