1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-29 05:21:33 +03:00

MDEV-20732 MDB now correctly estimates a length of the FORMAT() result for

doubles in scientific notation with a big integer part.
This commit is contained in:
Roman Nozdrin
2019-10-09 03:13:13 -05:00
parent 6535827885
commit ad8266a5c2
9 changed files with 2041 additions and 8 deletions

View File

@ -1695,6 +1695,7 @@ DO CONVERT(CAST(SUBSTRING_INDEX(FORMAT(1,'1111'), FORMAT('','Zpq'),1)
AS BINARY(0)) USING utf8); AS BINARY(0)) USING utf8);
Warnings: Warnings:
Warning 1292 Truncated incorrect INTEGER value: 'Zpq' Warning 1292 Truncated incorrect INTEGER value: 'Zpq'
Warning 1292 Truncated incorrect INTEGER value: 'Zpq'
Warning 1292 Truncated incorrect DOUBLE value: '' Warning 1292 Truncated incorrect DOUBLE value: ''
Warning 1292 Truncated incorrect BINARY(0) value: '1.' Warning 1292 Truncated incorrect BINARY(0) value: '1.'
# #

View File

@ -225,9 +225,11 @@ DROP TABLE t1;
--echo # Bug#58371 Assertion failed: !s.uses_buffer_owned_by(this) with format string function --echo # Bug#58371 Assertion failed: !s.uses_buffer_owned_by(this) with format string function
--echo # --echo #
--disable_ps_protocol
SET NAMES latin1; SET NAMES latin1;
DO CONVERT(CAST(SUBSTRING_INDEX(FORMAT(1,'1111'), FORMAT('','Zpq'),1) DO CONVERT(CAST(SUBSTRING_INDEX(FORMAT(1,'1111'), FORMAT('','Zpq'),1)
AS BINARY(0)) USING utf8); AS BINARY(0)) USING utf8);
--enable_ps_protocol
--echo # --echo #
--echo # End of 5.1 tests --echo # End of 5.1 tests
--echo # --echo #

View File

@ -1588,7 +1588,7 @@ CREATE TABLE t1 AS SELECT format(123,2,'no_NO');
SHOW CREATE TABLE t1; SHOW CREATE TABLE t1;
Table Create Table Table Create Table
t1 CREATE TABLE `t1` ( t1 CREATE TABLE `t1` (
`format(123,2,'no_NO')` varchar(45) CHARACTER SET utf32 DEFAULT NULL `format(123,2,'no_NO')` varchar(17) CHARACTER SET utf32 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ) ENGINE=MyISAM DEFAULT CHARSET=latin1
SELECT * FROM t1; SELECT * FROM t1;
format(123,2,'no_NO') format(123,2,'no_NO')

File diff suppressed because it is too large Load Diff

View File

@ -1001,3 +1001,713 @@ DROP TABLE t1;
--echo # --echo #
--echo # End of 10.3 tests --echo # End of 10.3 tests
--echo # --echo #
--echo #
--echo # Start of 10.4 tests
--echo #
--echo #
--echo # MDEV-20732 max_char_length() doesn't account for FORMAT() with doubles in scientific notation
--echo #
CREATE OR REPLACE TABLE t1 AS SELECT FORMAT(-1e308,2);
SELECT * FROM t1;
SHOW CREATE TABLE t1;
DROP TABLE t1;
CREATE OR REPLACE TABLE t1 AS SELECT FORMAT('-1e308',2);
SELECT * FROM t1;
SHOW CREATE TABLE t1;
DROP TABLE t1;
CREATE OR REPLACE TABLE t1 AS SELECT FORMAT(DATE'20191231',0),FORMAT(TIME'99:05:00',0),FORMAT(TIMESTAMP'2019-12-31 23:59:59.123456',0);
SELECT * FROM t1;
SHOW CREATE TABLE t1;
DROP TABLE t1;
CREATE OR REPLACE TABLE t1 (y YEAR);
INSERT INTO t1 VALUES ('2099'),('99');
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(y,0) FROM t1;
SELECT * FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1, t2;
CREATE OR REPLACE TABLE t1 (bi BIGINT UNSIGNED, bis BIGINT);
INSERT INTO t1 VALUES (18446744073709551615,-9223372036854775808),(0,0);
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(bi,0),FORMAT(bis,0) FROM t1;
SELECT * FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (d DECIMAL(65,0));
INSERT INTO t1 VALUES (99999999999999999999999999999999999999999999999999999999999999999),(-99999999999999999999999999999999999999999999999999999999999999999),(0);
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(d,0) FROM t1;
SELECT * FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 AS SELECT -99999999999999999999999999999999999999999999999999999999999999999 as c1;
SELECT * FROM t1;
SHOW CREATE TABLE t1;
DROP TABLE t1;
CREATE OR REPLACE TABLE t1 (b1 BIT(1),
b2 BIT(2),
b3 BIT(3),
b4 BIT(4),
b5 BIT(5),
b6 BIT(6),
b7 BIT(7),
b8 BIT(8),
b9 BIT(9),
b10 BIT(10),
b11 BIT(11),
b12 BIT(12),
b13 BIT(13),
b14 BIT(14),
b15 BIT(15),
b16 BIT(16),
b17 BIT(17),
b18 BIT(18),
b19 BIT(19),
b20 BIT(20),
b21 BIT(21),
b22 BIT(22),
b23 BIT(23),
b24 BIT(24),
b25 BIT(25),
b26 BIT(26),
b27 BIT(27),
b28 BIT(28),
b29 BIT(29),
b30 BIT(30),
b31 BIT(31),
b32 BIT(32),
b33 BIT(33),
b34 BIT(34),
b35 BIT(35),
b36 BIT(36),
b37 BIT(37),
b38 BIT(38),
b39 BIT(39),
b40 BIT(40),
b41 BIT(41),
b42 BIT(42),
b43 BIT(43),
b44 BIT(44),
b45 BIT(45),
b46 BIT(46),
b47 BIT(47),
b48 BIT(48),
b49 BIT(49),
b50 BIT(50),
b51 BIT(51),
b52 BIT(52),
b53 BIT(53),
b54 BIT(54),
b55 BIT(55),
b56 BIT(56),
b57 BIT(57),
b58 BIT(58),
b59 BIT(59),
b60 BIT(60),
b61 BIT(61),
b62 BIT(62),
b63 BIT(63),
b64 BIT(64));
INSERT INTO t1 VALUES(1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,
32767,65535,131071,262143,524287,1048575,2097151,4194303,8388607,16777215,
33554431,67108863,134217727,268435455,536870911,1073741823,2147483647,
4294967295,8589934591,17179869183,34359738367,68719476735,137438953471,
274877906943,549755813887,1099511627775,2199023255551,4398046511103,
8796093022207,17592186044415,35184372088831,70368744177663,140737488355327,
281474976710655,562949953421311,1125899906842623,2251799813685247,
4503599627370495,9007199254740991,18014398509481983,36028797018963967,
72057594037927935,144115188075855871,288230376151711743,576460752303423487,
1152921504606846975,2305843009213693951,4611686018427387903,
9223372036854775807,18446744073709551615);
--vertical_results
SELECT FORMAT(b1,0),
FORMAT(b2,0),
FORMAT(b3,0),
FORMAT(b4,0),
FORMAT(b5,0),
FORMAT(b6,0),
FORMAT(b7,0),
FORMAT(b8,0),
FORMAT(b9,0),
FORMAT(b10,0),
FORMAT(b11,0),
FORMAT(b12,0),
FORMAT(b13,0),
FORMAT(b14,0),
FORMAT(b15,0),
FORMAT(b16,0),
FORMAT(b17,0),
FORMAT(b18,0),
FORMAT(b19,0),
FORMAT(b20,0),
FORMAT(b21,0),
FORMAT(b22,0),
FORMAT(b23,0),
FORMAT(b24,0),
FORMAT(b25,0),
FORMAT(b26,0),
FORMAT(b27,0),
FORMAT(b28,0),
FORMAT(b29,0),
FORMAT(b30,0),
FORMAT(b31,0),
FORMAT(b32,0),
FORMAT(b33,0),
FORMAT(b34,0),
FORMAT(b35,0),
FORMAT(b36,0),
FORMAT(b37,0),
FORMAT(b38,0),
FORMAT(b39,0),
FORMAT(b40,0),
FORMAT(b41,0),
FORMAT(b42,0),
FORMAT(b43,0),
FORMAT(b44,0),
FORMAT(b45,0),
FORMAT(b46,0),
FORMAT(b47,0),
FORMAT(b48,0),
FORMAT(b49,0),
FORMAT(b50,0),
FORMAT(b51,0),
FORMAT(b52,0),
FORMAT(b53,0),
FORMAT(b54,0),
FORMAT(b55,0),
FORMAT(b56,0),
FORMAT(b57,0),
FORMAT(b58,0),
FORMAT(b59,0),
FORMAT(b60,0),
FORMAT(b61,0),
FORMAT(b62,0),
FORMAT(b63,0),
FORMAT(b64,0)
FROM t1;
--horizontal_results
DROP TABLE t1;
CREATE OR REPLACE TABLE t1 (c1 BIT(1));
INSERT INTO t1 VALUES (b'1');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(2));
INSERT INTO t1 VALUES (b'11');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(3));
INSERT INTO t1 VALUES (b'111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(4));
INSERT INTO t1 VALUES (b'1111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(5));
INSERT INTO t1 VALUES (b'11111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(6));
INSERT INTO t1 VALUES (b'111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(7));
INSERT INTO t1 VALUES (b'1111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(8));
INSERT INTO t1 VALUES (b'11111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(9));
INSERT INTO t1 VALUES (b'111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(10));
INSERT INTO t1 VALUES (b'1111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(11));
INSERT INTO t1 VALUES (b'11111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(12));
INSERT INTO t1 VALUES (b'111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(13));
INSERT INTO t1 VALUES (b'1111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(14));
INSERT INTO t1 VALUES (b'11111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(15));
INSERT INTO t1 VALUES (b'111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(16));
INSERT INTO t1 VALUES (b'1111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(17));
INSERT INTO t1 VALUES (b'11111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(18));
INSERT INTO t1 VALUES (b'111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(19));
INSERT INTO t1 VALUES (b'1111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(20));
INSERT INTO t1 VALUES (b'11111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(21));
INSERT INTO t1 VALUES (b'111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(22));
INSERT INTO t1 VALUES (b'1111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(23));
INSERT INTO t1 VALUES (b'11111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(24));
INSERT INTO t1 VALUES (b'111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(25));
INSERT INTO t1 VALUES (b'1111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(26));
INSERT INTO t1 VALUES (b'11111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(27));
INSERT INTO t1 VALUES (b'111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(28));
INSERT INTO t1 VALUES (b'1111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(29));
INSERT INTO t1 VALUES (b'11111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(30));
INSERT INTO t1 VALUES (b'111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(31));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(32));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(33));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(34));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(35));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(36));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(37));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(38));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(39));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(40));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(41));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(42));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(43));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(44));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(45));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(46));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(47));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(48));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(49));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(50));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(51));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(52));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(53));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(54));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(55));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(56));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(57));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(58));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(59));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(60));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(61));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(62));
INSERT INTO t1 VALUES (b'11111111111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(63));
INSERT INTO t1 VALUES (b'111111111111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
CREATE OR REPLACE TABLE t1 (c1 BIT(64));
INSERT INTO t1 VALUES (b'1111111111111111111111111111111111111111111111111111111111111111');
SELECT HEX(c1) FROM t1;
CREATE OR REPLACE TABLE t2 AS SELECT FORMAT(c1,0) as c1 FROM t1;
SELECT HEX(c1) FROM t2;
SHOW CREATE TABLE t2;
DROP TABLE t1,t2;
--echo #
--echo # End of 10.4 tests
--echo #

View File

@ -745,7 +745,7 @@ t1 CREATE TABLE `t1` (
`conv(130,16,10)` varchar(64) DEFAULT NULL, `conv(130,16,10)` varchar(64) DEFAULT NULL,
`hex(130)` varchar(6) DEFAULT NULL, `hex(130)` varchar(6) DEFAULT NULL,
`char(130)` varbinary(4) DEFAULT NULL, `char(130)` varbinary(4) DEFAULT NULL,
`format(130,10)` varchar(45) DEFAULT NULL, `format(130,10)` varchar(25) DEFAULT NULL,
`left(_latin2'a',1)` varchar(1) CHARACTER SET latin2 DEFAULT NULL, `left(_latin2'a',1)` varchar(1) CHARACTER SET latin2 DEFAULT NULL,
`right(_latin2'a',1)` varchar(1) CHARACTER SET latin2 DEFAULT NULL, `right(_latin2'a',1)` varchar(1) CHARACTER SET latin2 DEFAULT NULL,
`lcase(_latin2'a')` varchar(1) CHARACTER SET latin2 DEFAULT NULL, `lcase(_latin2'a')` varchar(1) CHARACTER SET latin2 DEFAULT NULL,
@ -2708,7 +2708,7 @@ create table t1(a float);
insert into t1 values (1.33); insert into t1 values (1.33);
select format(a, 2) from t1; select format(a, 2) from t1;
Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr
def format(a, 2) 253 57 4 Y 0 39 8 def format(a, 2) 253 4 4 Y 0 39 8
format(a, 2) format(a, 2)
1.33 1.33
drop table t1; drop table t1;
@ -3042,7 +3042,7 @@ CREATE TABLE t1 AS SELECT format(123,2,'no_NO');
SHOW CREATE TABLE t1; SHOW CREATE TABLE t1;
Table Create Table Table Create Table
t1 CREATE TABLE `t1` ( t1 CREATE TABLE `t1` (
`format(123,2,'no_NO')` varchar(45) DEFAULT NULL `format(123,2,'no_NO')` varchar(17) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ) ENGINE=MyISAM DEFAULT CHARSET=latin1
SELECT * FROM t1; SELECT * FROM t1;
format(123,2,'no_NO') format(123,2,'no_NO')

View File

@ -2666,10 +2666,17 @@ const int FORMAT_MAX_DECIMALS= 30;
bool Item_func_format::fix_length_and_dec() bool Item_func_format::fix_length_and_dec()
{ {
uint32 char_length= args[0]->max_char_length(); uint32 char_length= args[0]->type_handler()->Item_decimal_notation_int_digits(args[0]);
uint32 max_sep_count= (char_length / 3) + (decimals ? 1 : 0) + /*sign*/1; uint dec= FORMAT_MAX_DECIMALS;
if (args[1]->const_item() && !args[1]->is_expensive())
{
Longlong_hybrid tmp= args[1]->to_longlong_hybrid();
if (!args[1]->null_value)
dec= tmp.to_uint(FORMAT_MAX_DECIMALS);
}
uint32 max_sep_count= (char_length / 3) + (dec ? 1 : 0) + /*sign*/1;
collation.set(default_charset()); collation.set(default_charset());
fix_char_length(char_length + max_sep_count + decimals); fix_char_length(char_length + max_sep_count + dec);
if (arg_count == 3) if (arg_count == 3)
locale= args[2]->basic_const_item() ? args[2]->locale_from_val_str() : NULL; locale= args[2]->basic_const_item() ? args[2]->locale_from_val_str() : NULL;
else else

View File

@ -3490,7 +3490,6 @@ uint32 Type_handler_temporal_result::max_display_length(const Item *item) const
return item->max_length; return item->max_length;
} }
uint32 Type_handler_string_result::max_display_length(const Item *item) const uint32 Type_handler_string_result::max_display_length(const Item *item) const
{ {
return item->max_length; return item->max_length;
@ -3515,6 +3514,50 @@ uint32 Type_handler_general_purpose_int::max_display_length(const Item *item)
return type_limits_int_by_unsigned_flag(item->unsigned_flag)->char_length(); return type_limits_int_by_unsigned_flag(item->unsigned_flag)->char_length();
} }
/*************************************************************************/
uint32
Type_handler_decimal_result::Item_decimal_notation_int_digits(const Item *item)
const
{
return item->decimal_int_part();
}
uint32
Type_handler_temporal_result::Item_decimal_notation_int_digits(const Item *item)
const
{
return item->decimal_int_part();
}
uint32
Type_handler_bit::Item_decimal_notation_int_digits(const Item *item)
const
{
return Bit_decimal_notation_int_digits(item);
}
uint32
Type_handler_general_purpose_int::Item_decimal_notation_int_digits(
const Item *item) const
{
return type_limits_int_by_unsigned_flag(item->unsigned_flag)->precision();
}
/*************************************************************************/
/*
Decimal to binary digits ratio converges to log2(10) thus using 3 as
a divisor.
*/
uint32
Type_handler_bit::Bit_decimal_notation_int_digits(const Item *item)
{
return item->max_length/3+1;
}
/*************************************************************************/ /*************************************************************************/

View File

@ -3488,6 +3488,7 @@ public:
SORT_FIELD_ATTR *attr) const= 0; SORT_FIELD_ATTR *attr) const= 0;
virtual uint32 max_display_length(const Item *item) const= 0; virtual uint32 max_display_length(const Item *item) const= 0;
virtual uint32 Item_decimal_notation_int_digits(const Item *item) const { return 0; }
virtual uint32 calc_pack_length(uint32 length) const= 0; virtual uint32 calc_pack_length(uint32 length) const= 0;
virtual void Item_update_null_value(Item *item) const= 0; virtual void Item_update_null_value(Item *item) const= 0;
virtual bool Item_save_in_value(THD *thd, Item *item, st_value *value) const= 0; virtual bool Item_save_in_value(THD *thd, Item *item, st_value *value) const= 0;
@ -4236,6 +4237,7 @@ public:
const Type_std_attributes *item, const Type_std_attributes *item,
SORT_FIELD_ATTR *attr) const; SORT_FIELD_ATTR *attr) const;
uint32 max_display_length(const Item *item) const; uint32 max_display_length(const Item *item) const;
uint32 Item_decimal_notation_int_digits(const Item *item) const;
Item *create_typecast_item(THD *thd, Item *item, Item *create_typecast_item(THD *thd, Item *item,
const Type_cast_attributes &attr) const; const Type_cast_attributes &attr) const;
bool Item_const_eq(const Item_const *a, const Item_const *b, bool Item_const_eq(const Item_const *a, const Item_const *b,
@ -4529,6 +4531,7 @@ public:
virtual const Type_limits_int * virtual const Type_limits_int *
type_limits_int_by_unsigned_flag(bool unsigned_flag) const= 0; type_limits_int_by_unsigned_flag(bool unsigned_flag) const= 0;
uint32 max_display_length(const Item *item) const; uint32 max_display_length(const Item *item) const;
uint32 Item_decimal_notation_int_digits(const Item *item) const;
bool Vers_history_point_resolve_unit(THD *thd, Vers_history_point *p) const; bool Vers_history_point_resolve_unit(THD *thd, Vers_history_point *p) const;
}; };
@ -4554,6 +4557,7 @@ public:
const Type_all_attributes *attr, const Type_all_attributes *attr,
const st_value *value) const; const st_value *value) const;
uint32 max_display_length(const Item *item) const; uint32 max_display_length(const Item *item) const;
uint32 Item_decimal_notation_int_digits(const Item *item) const;
bool can_change_cond_ref_to_const(Item_bool_func2 *target, bool can_change_cond_ref_to_const(Item_bool_func2 *target,
Item *target_expr, Item *target_value, Item *target_expr, Item *target_value,
Item_bool_func2 *source, Item_bool_func2 *source,
@ -4638,6 +4642,11 @@ public:
const Schema_specification_st *schema) const Schema_specification_st *schema)
const; const;
uint32 max_display_length(const Item *item) const; uint32 max_display_length(const Item *item) const;
/*
The next method returns 309 for long stringified doubles in scientific
notation, e.g. FORMAT('1e308', 2).
*/
uint32 Item_decimal_notation_int_digits(const Item *item) const { return 309; }
bool Item_const_eq(const Item_const *a, const Item_const *b, bool Item_const_eq(const Item_const *a, const Item_const *b,
bool binary_cmp) const; bool binary_cmp) const;
bool Item_eq_value(THD *thd, const Type_cmp_attributes *attr, bool Item_eq_value(THD *thd, const Type_cmp_attributes *attr,
@ -5024,6 +5033,7 @@ public:
return PROTOCOL_SEND_SHORT; return PROTOCOL_SEND_SHORT;
} }
uint32 max_display_length(const Item *item) const; uint32 max_display_length(const Item *item) const;
uint32 Item_decimal_notation_int_digits(const Item *item) const { return 4; };
uint32 calc_pack_length(uint32 length) const { return 1; } uint32 calc_pack_length(uint32 length) const { return 1; }
bool Item_send(Item *item, Protocol *protocol, st_value *buf) const bool Item_send(Item *item, Protocol *protocol, st_value *buf) const
{ {
@ -5073,6 +5083,8 @@ public:
return PROTOCOL_SEND_STRING; return PROTOCOL_SEND_STRING;
} }
uint32 max_display_length(const Item *item) const; uint32 max_display_length(const Item *item) const;
uint32 Item_decimal_notation_int_digits(const Item *item) const;
static uint32 Bit_decimal_notation_int_digits(const Item *item);
uint32 calc_pack_length(uint32 length) const { return length / 8; } uint32 calc_pack_length(uint32 length) const { return length / 8; }
bool Item_send(Item *item, Protocol *protocol, st_value *buf) const bool Item_send(Item *item, Protocol *protocol, st_value *buf) const
{ {
@ -5175,6 +5187,7 @@ public:
} }
bool type_can_have_auto_increment_attribute() const { return true; } bool type_can_have_auto_increment_attribute() const { return true; }
uint32 max_display_length(const Item *item) const { return 53; } uint32 max_display_length(const Item *item) const { return 53; }
uint32 Item_decimal_notation_int_digits(const Item *item) const { return 309; }
uint32 calc_pack_length(uint32 length) const { return sizeof(double); } uint32 calc_pack_length(uint32 length) const { return sizeof(double); }
Item *create_typecast_item(THD *thd, Item *item, Item *create_typecast_item(THD *thd, Item *item,
const Type_cast_attributes &attr) const; const Type_cast_attributes &attr) const;