[Groonga-mysql-commit] mroonga/mroonga [master] hash index can be defined for _id virtual column.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 11月 18日 (木) 17:55:54 JST


Tetsuro IKEDA	2010-11-18 08:55:54 +0000 (Thu, 18 Nov 2010)

  New Revision: d002fdcef001f30fae4973e33c57d53112a65d50

  Log:
    hash index can be defined for _id virtual column.

  Modified files:
    ha_mroonga.cc
    test/sql/r/create_table.result
    test/sql/t/create_table.test

  Modified: ha_mroonga.cc (+20 -4)
===================================================================
--- ha_mroonga.cc    2010-11-18 08:16:56 +0000 (e2e48db)
+++ ha_mroonga.cc    2010-11-18 08:55:54 +0000 (bd699d7)
@@ -784,8 +784,11 @@ int ha_mroonga::create(const char *name, TABLE *table, HA_CREATE_INFO *info)
     const char *col_name = field->field_name;
     int col_name_size = strlen(col_name);
     if (strncmp(MRN_ID_COL_NAME, col_name, col_name_size) == 0) {
-      GRN_LOG(ctx, GRN_LOG_ERROR, "_id cannot be used for index");
-      my_message(ER_CANT_CREATE_TABLE, "_id cannot be used for index", MYF(0));
+      if (key_info.algorithm == HA_KEY_ALG_HASH) {
+        continue; // hash index is ok
+      }
+      GRN_LOG(ctx, GRN_LOG_ERROR, "only hash index can be defined for _id");
+      my_message(ER_CANT_CREATE_TABLE, "only hash index can be defined for _id", MYF(0));
       DBUG_RETURN(ER_CANT_CREATE_TABLE);
     }
     if (strncmp(MRN_SCORE_COL_NAME, col_name, col_name_size) == 0) {
@@ -844,16 +847,23 @@ int ha_mroonga::create(const char *name, TABLE *table, HA_CREATE_INFO *info)
       DBUG_RETURN(ER_NOT_SUPPORTED_YET);
     }
     Field *pkey_field = key_info.key_part[0].field;
+    const char *col_name = pkey_field->field_name;
+    int col_name_size = strlen(col_name);
+    bool is_id = (strncmp(MRN_ID_COL_NAME, col_name, col_name_size) == 0);
 
     int mysql_field_type = pkey_field->type();
     grn_builtin_type gtype = mrn_get_type(ctx, mysql_field_type);
     pkey_type = grn_ctx_at(ctx, gtype);
 
     // default algorithm is BTREE ==> PAT
-    if (key_info.algorithm == HA_KEY_ALG_HASH) {
+    if (!is_id && key_info.algorithm == HA_KEY_ALG_HASH) {
       tbl_flags |= GRN_OBJ_TABLE_HASH_KEY;
-    } else {
+    } else if (!is_id) {
       tbl_flags |= GRN_OBJ_TABLE_PAT_KEY;
+    } else {
+      // for _id 
+      tbl_flags |= GRN_OBJ_TABLE_NO_KEY;
+      pkey_type = NULL;
     }
 
   } else {
@@ -927,6 +937,12 @@ int ha_mroonga::create(const char *name, TABLE *table, HA_CREATE_INFO *info)
     Field *field = key_info.key_part[0].field;
     const char *col_name = field->field_name;
     int col_name_size = strlen(col_name);
+
+    if (strncmp(MRN_ID_COL_NAME, col_name, col_name_size) == 0) {
+      // skipping _id virtual column
+      continue;
+    }
+
     col_obj = grn_obj_column(ctx, tbl_obj, col_name, col_name_size);
     int mysql_field_type = field->type();
     grn_builtin_type gtype = mrn_get_type(ctx, mysql_field_type);

  Modified: test/sql/r/create_table.result (+13 -1)
===================================================================
--- test/sql/r/create_table.result    2010-11-18 08:16:56 +0000 (3a377cb)
+++ test/sql/r/create_table.result    2010-11-18 08:55:54 +0000 (23a6bec)
@@ -167,7 +167,19 @@ ERROR HY000: _id must be numeric data type
 create table t1 (c1 int, `_score` int) engine = groonga;
 ERROR HY000: _score must be float or double
 create table t1 (c1 int, `_id` int, index(`_id`)) engine = groonga;
-ERROR HY000: _id cannot be used for index
+ERROR HY000: only hash index can be defined for _id
 create table t1 (c1 int, `_score` double, index(`_score`)) engine = groonga;
 ERROR HY000: _score cannot be used for index
+create table t1 (_id int, c1 int, primary key (_id));
+ERROR HY000: only hash index can be defined for _id
+create table t1 (_id int, c1 int, primary key (_id) using hash);
+drop table t1;
+create table t1 (_id int, c1 int, unique key (_id));
+ERROR HY000: only hash index can be defined for _id
+create table t1 (_id int, c1 int, unique key (_id) using hash);
+drop table t1;
+create table t1 (_id int, c1 int, key (_id));
+ERROR HY000: only hash index can be defined for _id
+create table t1 (_id int, c1 int, key (_id) using hash);
+drop table t1;
 uninstall plugin groonga;

  Modified: test/sql/t/create_table.test (+14 -0)
===================================================================
--- test/sql/t/create_table.test    2010-11-18 08:16:56 +0000 (422dac5)
+++ test/sql/t/create_table.test    2010-11-18 08:55:54 +0000 (bc8c388)
@@ -137,6 +137,20 @@ create table t1 (c1 int, `_id` int, index(`_id`)) engine = groonga;
 --error 1005
 create table t1 (c1 int, `_score` double, index(`_score`)) engine = groonga;
 
+# index for _id
+--error 1005
+create table t1 (_id int, c1 int, primary key (_id));
+create table t1 (_id int, c1 int, primary key (_id) using hash);
+drop table t1;
+--error 1005
+create table t1 (_id int, c1 int, unique key (_id));
+create table t1 (_id int, c1 int, unique key (_id) using hash);
+drop table t1;
+--error 1005
+create table t1 (_id int, c1 int, key (_id));
+create table t1 (_id int, c1 int, key (_id) using hash);
+drop table t1;
+
 --disable_warnings
 uninstall plugin groonga;
 --enable_warnings




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