In a fixed-width file every field occupies the same number of bytes, so no separator is needed and the load logic gets simpler — at the cost of wasted space. When the data is complex and no safe delimiter can be chosen, fixed width may be the only option. This post covers how the China-domestically developed GBase 8a MPP database from GBASE exports fixed-width data, with the exact errors and truncation behavior seen in the source examples.

Reference

The companion note on loading fixed-width data with LOAD.

Syntax

select XX into outfile 'XXX' fields length(XX,XX,XX....) select XX into outfile 'XXX' fields FIELDS TERMINATED BY '' ENCLOSED BY '' ESCAPED BY '' select XX into outfile 'XXX' fields FIELDS TERMINATED BY '' ESCAPED BY '' 

Once the first length form is used, the field options of forms two and three must not be added, or the statement fails with a syntax error:

gbase> SELECT * FROM t1 INTO OUTFILE '/home/gbase/t1.zxq' FIELDS length '10,20' TERMINATED BY '' ENCLOSED BY '' ESCAPED BY '' writemode by overwrites; ERROR 1755 (HY000): Fields/columns length conllided with fields/columns enclosed by. 

Method one: specifying length

Every exported field needs a length, and for numbers it cannot be smaller than the default width — the display width in the column definition is ignored here.

  • tinyint: 4
  • smallint: 6
  • int: 11
  • bigint: 21
  • decimal with a scale: at least the defined length +2 (decimal point and minus sign); with no scale, +1

The following errors show the minimum widths in practice:

gbase> SELECT * FROM t1 INTO OUTFILE '/home/gbase/t1.zxq' FIELDS length '10,20' writemode by overwrites; ERROR 1149 (42000): Invalid length field, need length 11 for field 'id'. gbase> select id,f1 from tsi into outfile '/home/gbase/t.out' fields length '1,5'; ERROR 1149 (42000): Invalid length field, need length 6 for field 'id'. gbase> gbase> create table tti(id tinyint,f1 varchar(10)); Query OK, 0 rows affected (Elapsed: 00:00:00.14) gbase> select id,f1 from tti into outfile '/home/gbase/t.out' fields length '1,5'; ERROR 1149 (42000): Invalid length field, need length 4 for field 'id'. gbase> select id,f1 from ttt into outfile '/home/gbase/t.out' fields length '6,5'; ERROR 1149 (42000): Invalid length field, need length 21 for field 'id'. gbase> create table tdecimal(id decimal(32,5),f1 varchar(10)); Query OK, 0 rows affected (Elapsed: 00:00:00.13) gbase> select id,f1 from tdecimal into outfile '/home/gbase/t.out' fields length '1,5'; ERROR 1149 (42000): Invalid length field, need length 34 for field 'id'. gbase> create table tdecimal2(id decimal(32,0),f1 varchar(10)); Query OK, 0 rows affected (Elapsed: 00:00:00.16) gbase> select id,f1 from tdecimal2 into outfile '/home/gbase/t.out' fields length '1,5'; ERROR 1149 (42000): Invalid length field, need length 33 for field 'id'. 

Lengths are byte lengths, while a varchar column is defined in characters. Convert by charset: 3 bytes per character for UTF8 and 4 bytes for utf8mb4, so a UTF8 varchar(10) is really 30 bytes. A configured length smaller than the actual value's byte length silently truncates:

gbase> SELECT * FROM t1 INTO OUTFILE '/home/gbase/t1.zxq' FIELDS length '11,2' writemode by overwrites; Query OK, 1 row affected (Elapsed: 00:00:00.03) Normal output: [root@gbase_rh7_001 t1.zxq]# cat t1.zxq 1 From vc1 Truncated output: [root@gbase_rh7_001 t1.zxq]# cat t1.zxq 1 Fr [root@gbase_rh7_001 t1.zxq]# 

Methods two and three: empty separators

If any of TERMINATED BY, ENCLOSED BY or ESCAPED BY is set to a non-empty value, the result is not a fixed-width export. The marker is that the field separator and the escape character are both empty:

gbase> SELECT * FROM t1 INTO OUTFILE '/home/gbase/t1.zxq' FIELDS TERMINATED BY '' ENCLOSED BY '' ESCAPED BY '' writemode by overwrites; Query OK, 1 row affected (Elapsed: 00:00:00.03) gbase> SELECT * FROM t1 INTO OUTFILE '/home/gbase/t1.zxq' FIELDS TERMINATED BY '' ESCAPED BY '' writemode by overwrites; Query OK, 1 row affected (Elapsed: 00:00:00.02) 

Going forward, compute widths in bytes from the charset before an export on a gbase cluster, and keep the length form and the empty-separator forms apart: a width that is too small either raises Invalid length field or quietly cuts off the tail of a gbase database value.