Search code examples
mysqlviewgroup-concat

GROUP_CONCAT cut error on view when creating to table


I have a view defined as:

CREATE OR REPLACE VIEW my_view 
AS SELECT some_table.some_id AS id,
SUBSTRING(GROUP_CONCAT(some_table.value), 1, 1000) AS other_field 
FROM some_table 
WHERE some_table.some_other_id = 5 GROUP BY some_table.some_id;

If I query the whole table, I get all the expected results, truncated to 1000 characters.

However, if I try to do:

CREATE TABLE my_table SELECT * FROM my_view LIMIT 1;

I get the error:

Row 254468 was cut by GROUP_CONCAT()

Checking CHAR_LENGTH of each other_field shows they are all 1000 characters or fewer.

group_concat_max_len is currently set to 200,000.

Why does the CREATE TABLE give the error, but the SELECT * ... by itself not?


CREATE TABLE `some_table` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `some_other_id` int(11) DEFAULT NULL,
  `some_id` int(11) NOT NULL,
  `value` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=48190231 DEFAULT CHARSET=utf8;

Solution

  • Modify view to...

    CREATE OR REPLACE VIEW my_view 
    AS SELECT some_table.some_id AS id,
    cast(substring(GROUP_CONCAT(some_table.value),1,1000) as char(1000)) AS other_field 
    FROM some_table 
    WHERE some_table.some_other_id = 5 
    GROUP BY some_table.some_id;
    

    or alter the create table to select fields individually and cast other_field to varchar(1000).

    What I believe is happening is the sampling the engine does to evaluate what datatype and size to use for the table only samples a few of the rows. Size may be larger on later records causing the insert to fail. To resolve explicitly define the column size for the concatenated field. By default I believe the engine will use blob in the view which makes it ok; but the create table samples the data to try and determine data type and since size isn't blob; I believe it's trying to create a varchar data type but sizes it incorrectly