[Groonga-mysql-commit] mroonga/mroonga [master] added warning/error handling for insert/update virtual columns.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 11月 16日 (火) 15:35:21 JST


Tetsuro IKEDA	2010-11-16 06:35:21 +0000 (Tue, 16 Nov 2010)

  New Revision: f213fd4472f93e1c44b16d4b8d65e1b414c9e5d4

  Log:
    added warning/error handling for insert/update virtual columns.

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

  Modified: ha_mroonga.cc (+97 -7)
===================================================================
--- ha_mroonga.cc    2010-11-12 10:39:44 +0000 (63acfa7)
+++ ha_mroonga.cc    2010-11-16 06:35:21 +0000 (e8081ca)
@@ -982,6 +982,16 @@ int ha_mroonga::open(const char *name, int mode, uint test_if_locked)
     Field *field = table->field[i];
     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) {
+      col[i] = NULL;
+      continue;
+    }
+    if (strncmp(MRN_SCORE_COL_NAME, col_name, col_name_size) == 0) {
+      col[i] = NULL;
+      continue;
+    }
+
     col[i] = grn_obj_column(ctx, tbl, col_name, col_name_size);
     if (ctx->rc) {
       grn_obj_unlink(ctx, tbl);
@@ -1185,10 +1195,38 @@ int ha_mroonga::write_row(uchar *buf)
   void *pkey = NULL;
   int pkey_size = 0;
   uint pkeynr = table->s->primary_key;
-  GRN_VOID_INIT(&wrapper);
+  THD *thd = ha_thd();
+  int i, col_size;
+  int n_columns = table->s->fields;
 #ifndef DBUG_OFF
   my_bitmap_map *tmp_map = dbug_tmp_use_all_columns(table, table->read_set);
 #endif
+  if (thd->abort_on_warning) {
+    for (i = 0; i < n_columns; i++) {
+      Field *field = table->field[i];
+      const char *col_name = field->field_name;
+      int col_name_size = strlen(col_name);
+
+      if (field->is_null()) continue;
+
+      if (strncmp(MRN_ID_COL_NAME, col_name, col_name_size) == 0) {
+#ifndef DBUG_OFF
+        dbug_tmp_restore_column_map(table->read_set, tmp_map);
+#endif
+        my_message(ER_DATA_TOO_LONG, "cannot insert value to _id column", MYF(0));
+        DBUG_RETURN(ER_DATA_TOO_LONG);
+      } 
+      if (strncmp(MRN_SCORE_COL_NAME, col_name, col_name_size) == 0) {
+#ifndef DBUG_OFF
+        dbug_tmp_restore_column_map(table->read_set, tmp_map);
+#endif
+        my_message(ER_DATA_TOO_LONG, "cannot insert value to _score column", MYF(0));
+        DBUG_RETURN(ER_DATA_TOO_LONG);
+      } 
+    }
+  }
+
+  GRN_VOID_INIT(&wrapper);
   if (pkeynr != MAX_INDEXES) {
     KEY key_info = table->s->key_info[pkeynr];
     // surpose simgle column key
@@ -1216,16 +1254,25 @@ int ha_mroonga::write_row(uchar *buf)
   }
 
   grn_obj colbuf;
-  int i, col_size;
-  int n_columns = table->s->fields;
   GRN_VOID_INIT(&colbuf);
   for (i = 0; i < n_columns; i++) {
     Field *field = table->field[i];
     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) continue;
-    if (strncmp(MRN_SCORE_COL_NAME, col_name, col_name_size) == 0) continue;
+    if (field->is_null()) continue;
+
+    if (strncmp(MRN_ID_COL_NAME, col_name, col_name_size) == 0) {
+      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
+                   "data truncated for  _id column");
+      continue;
+    }
+
+    if (strncmp(MRN_SCORE_COL_NAME, col_name, col_name_size) == 0) {
+      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
+                   "data truncated for  _score column");
+      continue;
+    }
 
     mrn_set_buf(ctx, field, &colbuf, &col_size);
     grn_obj_set_value(ctx, col[i], row_id, &colbuf, GRN_OBJ_SET);
@@ -1251,15 +1298,58 @@ int ha_mroonga::update_row(const uchar *old_data, uchar *new_data)
   grn_obj colbuf;
   int i, col_size;
   int n_columns = table->s->fields;
+  THD *thd = ha_thd();
+
+  if (thd->abort_on_warning) {
+    for (i = 0; i < n_columns; i++) {
+      Field *field = table->field[i];
+      const char *col_name = field->field_name;
+      int col_name_size = strlen(col_name);
+
+      if (field->is_null()) continue;
+
+      if (strncmp(MRN_ID_COL_NAME, col_name, col_name_size) == 0) {
+        my_message(ER_DATA_TOO_LONG, "cannot update value to _id column", MYF(0));
+        DBUG_RETURN(ER_DATA_TOO_LONG);
+      } 
+      if (strncmp(MRN_SCORE_COL_NAME, col_name, col_name_size) == 0) {
+        my_message(ER_DATA_TOO_LONG, "cannot update value to _score column", MYF(0));
+        DBUG_RETURN(ER_DATA_TOO_LONG);
+      } 
+    }
+  }
+
   GRN_VOID_INIT(&colbuf);
   for (i = 0; i < n_columns; i++) {
     Field *field = table->field[i];
+    const char *col_name = field->field_name;
+    int col_name_size = strlen(col_name);
     if (bitmap_is_set(table->write_set, field->field_index)) {
 #ifndef DBUG_OFF
-      my_bitmap_map *tmp_map = dbug_tmp_use_all_columns(table,
-        table->read_set);
+      my_bitmap_map *tmp_map = dbug_tmp_use_all_columns(table, table->read_set);
 #endif
       DBUG_PRINT("info",("mroonga update column %d(%d)",i,field->field_index));
+
+      if (field->is_null()) continue;
+
+      if (strncmp(MRN_ID_COL_NAME, col_name, col_name_size) == 0) {
+        push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
+                     "data truncated for  _id column");
+#ifndef DBUG_OFF
+        dbug_tmp_restore_column_map(table->read_set, tmp_map);
+#endif
+        continue;
+      }
+
+      if (strncmp(MRN_SCORE_COL_NAME, col_name, col_name_size) == 0) {
+        push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
+                     "data truncated for  _score column");
+#ifndef DBUG_OFF
+        dbug_tmp_restore_column_map(table->read_set, tmp_map);
+#endif
+        continue;
+      }
+
       mrn_set_buf(ctx, field, &colbuf, &col_size);
       grn_obj_set_value(ctx, col[i], row_id, &colbuf, GRN_OBJ_SET);
       if (ctx->rc) {

  Modified: test/sql/r/insert.result (+25 -0)
===================================================================
--- test/sql/r/insert.result    2010-11-12 10:39:44 +0000 (5182a7b)
+++ test/sql/r/insert.result    2010-11-16 06:35:21 +0000 (984d3ee)
@@ -67,4 +67,29 @@ select * from t1;
 c1
 2010-03-26 11:22:33
 drop table t1;
+create table t1 (c1 int, _id int, _score float);
+set sql_mode="";
+insert into t1 (c1,_id) values (1,1);
+Warnings:
+Warning	1265	data truncated for  _id column
+insert into t1 (c1,_score) values (2,2);
+Warnings:
+Warning	1265	data truncated for  _score column
+insert into t1 (c1,_id,_score) values (3,1,2);
+Warnings:
+Warning	1265	data truncated for  _id column
+Warning	1265	data truncated for  _score column
+set sql_mode="strict_all_tables";
+insert into t1 (c1,_id) values (4,1);
+ERROR 22001: cannot insert value to _id column
+insert into t1 (c1,_score) values (5,2);
+ERROR 22001: cannot insert value to _score column
+insert into t1 (c1,_id,_score) values (6,1,2);
+ERROR 22001: cannot insert value to _id column
+select * from t1;
+c1	_id	_score
+1	1	0
+2	2	0
+3	3	0
+drop table t1;
 uninstall plugin groonga;

  Modified: test/sql/r/update.result (+38 -0)
===================================================================
--- test/sql/r/update.result    2010-11-12 10:39:44 +0000 (b5a3411)
+++ test/sql/r/update.result    2010-11-16 06:35:21 +0000 (0aea290)
@@ -63,4 +63,42 @@ c1	c2
 select * from t1 where match(c2) against("ki");
 c1	c2
 drop table t1;
+create table t1 (c1 int, _id int, _score float);
+insert into t1 values(1,null,null);
+insert into t1 values(2,null,null);
+insert into t1 values(3,null,null);
+select * from t1;
+c1	_id	_score
+1	1	0
+2	2	0
+3	3	0
+set sql_mode="";
+update t1 set _id = 10 where c1 = 1;
+Warnings:
+Warning	1265	data truncated for  _id column
+update t1 set _score = 20 where c1 = 2;
+Warnings:
+Warning	1265	data truncated for  _score column
+update t1 set _id = 10, _score = 20 where c1 = 3;
+Warnings:
+Warning	1265	data truncated for  _id column
+Warning	1265	data truncated for  _score column
+select * from t1;
+c1	_id	_score
+1	1	0
+2	2	0
+3	3	0
+set sql_mode="strict_all_tables";
+update t1 set _id = 11 where c1 = 1;
+ERROR 22001: cannot update value to _id column
+update t1 set _score = 22 where c1 = 2;
+ERROR 22001: cannot update value to _score column
+update t1 set _id = 11, _score = 22 where c1 = 3;
+ERROR 22001: cannot update value to _id column
+select * from t1;
+c1	_id	_score
+1	1	0
+2	2	0
+3	3	0
+drop table t1;
 uninstall plugin groonga;

  Modified: test/sql/t/insert.test (+20 -0)
===================================================================
--- test/sql/t/insert.test    2010-11-12 10:39:44 +0000 (376deff)
+++ test/sql/t/insert.test    2010-11-16 06:35:21 +0000 (fe96d8f)
@@ -76,6 +76,26 @@ insert into t1 values("2010/03/26 11:22:33");
 select * from t1;
 drop table t1;
 
+
+# for virtual columns
+create table t1 (c1 int, _id int, _score float);
+set sql_mode="";
+# warning 1265
+insert into t1 (c1,_id) values (1,1);
+# warning 1265
+insert into t1 (c1,_score) values (2,2);
+# warning 1265
+insert into t1 (c1,_id,_score) values (3,1,2);
+set sql_mode="strict_all_tables";
+--error 1406
+insert into t1 (c1,_id) values (4,1);
+--error 1406
+insert into t1 (c1,_score) values (5,2);
+--error 1406
+insert into t1 (c1,_id,_score) values (6,1,2);
+select * from t1;
+drop table t1;
+
 --disable_warnings
 uninstall plugin groonga;
 --enable_warnings

  Modified: test/sql/t/update.test (+24 -0)
===================================================================
--- test/sql/t/update.test    2010-11-12 10:39:44 +0000 (e2d6f4f)
+++ test/sql/t/update.test    2010-11-16 06:35:21 +0000 (34f8e9c)
@@ -54,6 +54,30 @@ select * from t1 where match(c2) against("ki");
 
 drop table t1;
 
+# for virtual columns
+create table t1 (c1 int, _id int, _score float);
+insert into t1 values(1,null,null);
+insert into t1 values(2,null,null);
+insert into t1 values(3,null,null);
+select * from t1;
+set sql_mode="";
+# warning 1265
+update t1 set _id = 10 where c1 = 1;
+# warning 1265
+update t1 set _score = 20 where c1 = 2;
+# warning 1265
+update t1 set _id = 10, _score = 20 where c1 = 3;
+select * from t1;
+set sql_mode="strict_all_tables";
+--error 1406
+update t1 set _id = 11 where c1 = 1;
+--error 1406
+update t1 set _score = 22 where c1 = 2;
+--error 1406
+update t1 set _id = 11, _score = 22 where c1 = 3;
+select * from t1;
+drop table t1;
+
 --disable_warnings
 uninstall plugin groonga;
 --enable_warnings




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