summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Cueva <alan.cuevamr@gmail.com>2021-06-15 22:52:51 +0200
committerSergei Golubchik <vuvova@gmail.com>2021-10-12 13:27:36 +0200
commite214e60201216b2e6330f86a84fec8ae14aa2887 (patch)
tree848f5181dd5eead43473ee66c99c67b751cab533
parentcd390af982753fadd83b339536ce4d24ea8e5fd0 (diff)
downloadmariadb-git-e214e60201216b2e6330f86a84fec8ae14aa2887.tar.gz
MDEV-25015 Custom formatting of strings in MariaDB queries
SFORMAT() SQL function that uses fmtlib (https://fmt.dev/) for python-like (also Rust, C++20, etc) string formatting Only fmtlib 7.0.0+ is supported, older fmtlib produces different results in the test. No native support for temporal and decimal values, * TIME_RESULT is handled as STRING_RESULT * DECIMAL_RESULT as REAL_RESULT
-rw-r--r--.gitignore1
-rw-r--r--CMakeLists.txt2
-rw-r--r--cmake/libfmt.cmake59
-rw-r--r--libmysqld/CMakeLists.txt4
-rw-r--r--mysql-test/main/sformat.result476
-rw-r--r--mysql-test/main/sformat.test224
-rw-r--r--sql/CMakeLists.txt3
-rw-r--r--sql/item_create.cc31
-rw-r--r--sql/item_strfunc.cc103
-rw-r--r--sql/item_strfunc.h16
-rw-r--r--sql/share/errmsg-utf8.txt2
11 files changed, 919 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index dff2e179d87..e2a227492d3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -103,6 +103,7 @@ packaging/rpm-oel/mysql.spec
packaging/rpm-uln/mysql.10.0.11.spec
packaging/solaris/postinstall-solaris
extra/pcre2
+extra/libfmt
plugin/auth_pam/auth_pam_tool
plugin/auth_pam/config_auth_pam.h
plugin/aws_key_management/aws-sdk-cpp
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 00007f92f57..144d092149f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -158,6 +158,7 @@ INCLUDE(readline)
INCLUDE(libutils)
INCLUDE(dtrace)
INCLUDE(pcre)
+INCLUDE(libfmt)
INCLUDE(ctest)
INCLUDE(plugin)
INCLUDE(install_macros)
@@ -382,6 +383,7 @@ MYSQL_CHECK_READLINE()
SET(MALLOC_LIBRARY "system")
CHECK_PCRE()
+CHECK_LIBFMT()
ADD_SUBDIRECTORY(tpool)
CHECK_SYSTEMD()
diff --git a/cmake/libfmt.cmake b/cmake/libfmt.cmake
new file mode 100644
index 00000000000..57d173307d2
--- /dev/null
+++ b/cmake/libfmt.cmake
@@ -0,0 +1,59 @@
+INCLUDE (CheckCXXSourceCompiles)
+INCLUDE (ExternalProject)
+
+SET(WITH_LIBFMT "auto" CACHE STRING
+ "Which libfmt to use (possible values are 'bundled', 'system', or 'auto')")
+
+MACRO(BUNDLE_LIBFMT)
+ SET(dir "${CMAKE_BINARY_DIR}/extra/libfmt")
+ SET(LIBFMT_INCLUDE_DIR "${dir}/src/libfmt/include")
+ ADD_LIBRARY(fmt STATIC IMPORTED GLOBAL)
+ SET(file ${dir}/src/libfmt-build/${CMAKE_CFG_INTDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}fmt${CMAKE_STATIC_LIBRARY_SUFFIX})
+ SET_TARGET_PROPERTIES(fmt PROPERTIES IMPORTED_LOCATION ${file})
+
+ ExternalProject_Add(
+ libfmt
+ PREFIX "${dir}"
+ URL "https://github.com/fmtlib/fmt/archive/refs/tags/8.0.1.zip"
+ URL_MD5 e77873199e897ca9f780479ad68e25b1
+ INSTALL_COMMAND ""
+ CMAKE_ARGS
+ "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
+ "-DBUILD_SHARED_LIBS=OFF"
+ "-DFMT_DEBUG_POSTFIX="
+ "-DFMT_DOC=OFF"
+ "-DFMT_TEST=OFF"
+ "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
+ "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} ${PIC_FLAG}"
+ "-DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}"
+ "-DCMAKE_CXX_FLAGS_RELWITHDEBINFO=${CMAKE_CXX_FLAGS_RELWITHDEBINFO}"
+ "-DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}"
+ "-DCMAKE_CXX_FLAGS_MINSIZEREL=${CMAKE_CXX_FLAGS_MINSIZEREL}"
+ "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
+ BUILD_BYPRODUCTS ${file}
+ )
+ SET_TARGET_PROPERTIES(fmt PROPERTIES EXCLUDE_FROM_ALL TRUE)
+ENDMACRO()
+
+MACRO (CHECK_LIBFMT)
+ IF(WITH_LIBFMT STREQUAL "system" OR WITH_LIBFMT STREQUAL "auto")
+ SET(CMAKE_REQUIRED_LIBRARIES fmt)
+ CHECK_CXX_SOURCE_COMPILES(
+ "#include <fmt/core.h>
+ #include <iostream>
+ int main() {
+ std::cout << fmt::format(\"The answer is {}.\", 42);
+ }" HAVE_SYSTEM_LIBFMT)
+ SET(CMAKE_REQUIRED_LIBRARIES)
+ ENDIF()
+ IF(NOT HAVE_SYSTEM_LIBFMT OR WITH_LIBFMT STREQUAL "bundled")
+ IF (WITH_LIBFMT STREQUAL "system")
+ MESSAGE(FATAL_ERROR "system libfmt library is not found")
+ ENDIF()
+ BUNDLE_LIBFMT()
+ ELSE()
+ FIND_FILE(Libfmt_core_h fmt/core.h) # for build_depends.cmake
+ ENDIF()
+ENDMACRO()
+
+MARK_AS_ADVANCED(LIBFMT_INCLUDE_DIR)
diff --git a/libmysqld/CMakeLists.txt b/libmysqld/CMakeLists.txt
index d2cbb9a85ee..9f3ccb4cd63 100644
--- a/libmysqld/CMakeLists.txt
+++ b/libmysqld/CMakeLists.txt
@@ -24,6 +24,7 @@ ${CMAKE_SOURCE_DIR}/sql
${CMAKE_SOURCE_DIR}/tpool
${CMAKE_BINARY_DIR}/sql
${PCRE_INCLUDES}
+${LIBFMT_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
${SSL_INCLUDE_DIRS}
${SSL_INTERNAL_INCLUDE_DIRS}
@@ -395,9 +396,10 @@ IF(NOT DISABLE_SHARED)
# libmysqld
SET_TARGET_PROPERTIES(libmysqld PROPERTIES CLEAN_DIRECT_OUTPUT 1)
SET_TARGET_PROPERTIES(mysqlserver PROPERTIES CLEAN_DIRECT_OUTPUT 1)
- TARGET_LINK_LIBRARIES(mysqlserver LINK_PRIVATE tpool ${CRC32_LIBRARY})
+ TARGET_LINK_LIBRARIES(mysqlserver LINK_PRIVATE tpool fmt ${CRC32_LIBRARY})
IF(LIBMYSQLD_SO_EXTRA_LIBS)
TARGET_LINK_LIBRARIES(libmysqld LINK_PRIVATE ${LIBMYSQLD_SO_EXTRA_LIBS})
ENDIF()
ENDIF()
ENDIF()
+
diff --git a/mysql-test/main/sformat.result b/mysql-test/main/sformat.result
new file mode 100644
index 00000000000..2d453a92628
--- /dev/null
+++ b/mysql-test/main/sformat.result
@@ -0,0 +1,476 @@
+#
+# Normal Test Cases
+#
+select sformat('string test');
+sformat('string test')
+string test
+select sformat(0);
+sformat(0)
+0
+select sformat('C');
+sformat('C')
+C
+select sformat(-4.2);
+sformat(-4.2)
+-4.2
+select sformat(5, 5, 5);
+sformat(5, 5, 5)
+5
+select sformat('{} {}', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+sformat('{} {}', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+0 0
+select sformat('{{{}}}', 0);
+sformat('{{{}}}', 0)
+{0}
+select sformat('{{{}{{', 0);
+sformat('{{{}{{', 0)
+{0{
+select sformat('{{{{{}{{', 'param1');
+sformat('{{{{{}{{', 'param1')
+{{param1{
+select sformat(' {{ {{ {} {{ ', 'param1');
+sformat(' {{ {{ {} {{ ', 'param1')
+ { { param1 {
+select sformat(' {{ {} {}', 'param1', 'param2');
+sformat(' {{ {} {}', 'param1', 'param2')
+ { param1 param2
+select sformat('A{}C{}E{}', 'B', 'D', 'F');
+sformat('A{}C{}E{}', 'B', 'D', 'F')
+ABCDEF
+select sformat('{} {}', FALSE, TRUE);
+sformat('{} {}', FALSE, TRUE)
+0 1
+select sformat('Add € != {} != {}?', '$', '£');
+sformat('Add € != {} != {}?', '$', '£')
+Add € != $ != £?
+select sformat('Check {} != {} != {}?', '€', '$', '£');
+sformat('Check {} != {} != {}?', '€', '$', '£')
+Check € != $ != £?
+select sformat('{}{}{}', 1, 2, 3);
+sformat('{}{}{}', 1, 2, 3)
+123
+select sformat('Float {} Boolean {} Number {}', 3.14159, True, -50);
+sformat('Float {} Boolean {} Number {}', 3.14159, True, -50)
+Float 3.14159 Boolean 1 Number -50
+select sformat('SUM {} + {} = {}', 2, 3, 2+3);
+sformat('SUM {} + {} = {}', 2, 3, 2+3)
+SUM 2 + 3 = 5
+select sformat('Numbers {} {} {}', 1, 1.11, 1.111);
+sformat('Numbers {} {} {}', 1, 1.11, 1.111)
+Numbers 1 1.11 1.111
+select sformat('what {} is {}?', 'time', 'it');
+sformat('what {} is {}?', 'time', 'it')
+what time is it?
+select sformat('{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}', 1, 2, 3, 4, 5, 6,
+7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
+33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
+58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
+83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
+106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120);
+sformat('{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
+ 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
+ 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
+ 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
+select sformat('{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}', '1', '2', '3 ',
+'4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20',
+'21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36',
+'37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52',
+'53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68',
+'69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84',
+'85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100',
+'101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114',
+'115', '116', '117', '118', '119', '120');
+sformat('{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
+ 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
+ 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
+ 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
+#
+# Error Test Cases
+#
+select sformat('R={ }', 42);
+sformat('R={ }', 42)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid format string
+select sformat(NULL, 'Null', 'Test');
+sformat(NULL, 'Null', 'Test')
+NULL
+select sformat('Null Test {} {} ', 0, NULL);
+sformat('Null Test {} {} ', 0, NULL)
+NULL
+select sformat(NULL);
+sformat(NULL)
+NULL
+select sformat('My { Test');
+sformat('My { Test')
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid format string
+select sformat('Test { {');
+sformat('Test { {')
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid format string
+select sformat('A{}C{}E{}', 'B', 'D');
+sformat('A{}C{}E{}', 'B', 'D')
+NULL
+Warnings:
+Warning 4183 Wrong format exception: argument not found
+select sformat();
+ERROR 42000: Incorrect parameter count in the call to native function 'sformat'
+#
+# Table Test Cases
+#
+create table t1 (param1 text, param2 text, param3 int);
+insert into t1 values ('string: {}, number: {}', 'xyz', 123),
+('something {} went {} wrong {}', 'foo', 0),
+('Test case {} -> {}', 'Print', -32);
+select sformat(param1, param2, param3) from t1;
+sformat(param1, param2, param3)
+string: xyz, number: 123
+NULL
+Test case Print -> -32
+Warnings:
+Warning 4183 Wrong format exception: argument not found
+drop table t1;
+create table t2 (param1 FLOAT, param2 SMALLINT, param3 CHAR, param4 DATE);
+insert into t2 values (0.0025, 25, 'A', DATE('2020-06-29')),
+(0.0005, 5, 'B', DATE('2020-6-29')),
+(5.5555, -5, 'C', DATE('200629')),
+(-9, -9, 'D', DATE('20*06*29'));
+select sformat('p1 {:.4f} p2 {} p3 {} p4 {}', param1, param2, param3, param4) from t2;
+sformat('p1 {:.4f} p2 {} p3 {} p4 {}', param1, param2, param3, param4)
+p1 0.0025 p2 25 p3 A p4 2020-06-29
+p1 0.0005 p2 5 p3 B p4 2020-06-29
+p1 5.5555 p2 -5 p3 C p4 2020-06-29
+p1 -9.0000 p2 -9 p3 D p4 2020-06-29
+drop table t2;
+set names utf8;
+create table t3 (format_str text character set 'latin1',
+first_param text character set 'utf8',
+second_param text character set 'latin1');
+insert into t3 values ('test 1 {} {}', UNHEX('C3A5'), UNHEX('E5'));
+select sformat(format_str, first_param, second_param) from t3;
+sformat(format_str, first_param, second_param)
+test 1 å å
+select HEX(sformat(format_str, first_param, second_param)) from t3;
+HEX(sformat(format_str, first_param, second_param))
+74657374203120C3A520C3A5
+drop table t3;
+set names latin1;
+create table t4 (p1 bit(8), p2 boolean, p3 DECIMAL, p4 TIMESTAMP);
+insert into t4 values (42, TRUE, 42, '2021-08-18 16:50:07'),
+(24, FALSE, 24, '0000-00-00 00:00:00');
+select sformat('{}: {} {} {} {}', 'Data', p1, p2, p3, p4) from t4;
+sformat('{}: {} {} {} {}', 'Data', p1, p2, p3, p4)
+Data: 42 1 42 2021-08-18 16:50:07
+Data: 24 0 24 0000-00-00 00:00:00
+drop table t4;
+set names utf8;
+create table t5 (param text character SET utf8 COLLATE utf8_general_ci);
+insert into t5 values (UNHEX('C3A5')),(UNHEX('C5BB')),(UNHEX('e0b1bb')),
+(UNHEX('C38A')), (NULL);
+select sformat('{}', param) from t5;
+sformat('{}', param)
+౻
+NULL
+select HEX(sformat('{}', param)) from t5;
+HEX(sformat('{}', param))
+C3A5
+C5BB
+E0B1BB
+C38A
+NULL
+drop table t5;
+set names latin1;
+#
+# Format Test Cases
+#
+select sformat('Num {:L}', 13800000000);
+sformat('Num {:L}', 13800000000)
+Num 13800000000
+select sformat('Num [{:20}]', 42);
+sformat('Num [{:20}]', 42)
+Num [ 42]
+select sformat('Number: {:*^{}}', 4, 5);
+sformat('Number: {:*^{}}', 4, 5)
+Number: **4**
+select sformat('{:02} - {:02} - {:02}', 1, 2, 3);
+sformat('{:02} - {:02} - {:02}', 1, 2, 3)
+01 - 02 - 03
+select sformat('Character {:c}', 'h');
+sformat('Character {:c}', 'h')
+Character h
+select sformat('String {:s}', 'hello');
+sformat('String {:s}', 'hello')
+String hello
+select sformat('Large {0:+010.4g}', 392.64);
+sformat('Large {0:+010.4g}', 392.64)
+Large +0000392.6
+select sformat('Large {:g}', 392.65);
+sformat('Large {:g}', 392.65)
+Large 392.65
+select sformat('Float {:.2f}', 42.0);
+sformat('Float {:.2f}', 42.0)
+Float 42.00
+select sformat('Float {:f}', 42.0);
+sformat('Float {:f}', 42.0)
+Float 42.000000
+select sformat('Number {:d}', 42);
+sformat('Number {:d}', 42)
+Number 42
+select sformat('Number {:{}}', 5, 5);
+sformat('Number {:{}}', 5, 5)
+Number 5
+select sformat('Number [{:10}]', 9999);
+sformat('Number [{:10}]', 9999)
+Number [ 9999]
+select sformat('Number {:.3}', 3.1416);
+sformat('Number {:.3}', 3.1416)
+Number 3.14
+select sformat('int: {0:d}; hex: {0:x}; oct: {0:o}', 42);
+sformat('int: {0:d}; hex: {0:x}; oct: {0:o}', 42)
+int: 42; hex: 2a; oct: 52
+select sformat('int: {0:d}; hex: {0:#x}; oct: {0:#o}', 42);
+sformat('int: {0:d}; hex: {0:#x}; oct: {0:#o}', 42)
+int: 42; hex: 0x2a; oct: 052
+select sformat('The Hexadecimal version of {0} is {0:X}', 255);
+sformat('The Hexadecimal version of {0} is {0:X}', 255)
+The Hexadecimal version of 255 is FF
+select sformat('The Hexadecimal version of {0} is {0:x}', 255);
+sformat('The Hexadecimal version of {0} is {0:x}', 255)
+The Hexadecimal version of 255 is ff
+select sformat('The octal version of {0} is {0:o}', 10);
+sformat('The octal version of {0} is {0:o}', 10)
+The octal version of 10 is 12
+select sformat('The binary version of {0} is {0:b}', 5);
+sformat('The binary version of {0} is {0:b}', 5)
+The binary version of 5 is 101
+select sformat('{:+f}; {:+f}', 3.14, -3.14);
+sformat('{:+f}; {:+f}', 3.14, -3.14)
++3.140000; -3.140000
+select sformat('{: f}; {: f}', 3.14, -3.14);
+sformat('{: f}; {: f}', 3.14, -3.14)
+ 3.140000; -3.140000
+select sformat('{:-f}; {:-f}', 3.14, -3.14);
+sformat('{:-f}; {:-f}', 3.14, -3.14)
+3.140000; -3.140000
+select sformat('The temperature is between {: } and {: } degrees celsius.', -3, 7);
+sformat('The temperature is between {: } and {: } degrees celsius.', -3, 7)
+The temperature is between -3 and 7 degrees celsius.
+select sformat('The temperature is between {:-} and {:-} degrees celsius.', -3, 7);
+sformat('The temperature is between {:-} and {:-} degrees celsius.', -3, 7)
+The temperature is between -3 and 7 degrees celsius.
+select sformat('The temperature is between {:+} and {:+} degrees celsius.', -3, 7);
+sformat('The temperature is between {:+} and {:+} degrees celsius.', -3, 7)
+The temperature is between -3 and +7 degrees celsius.
+select sformat('We have {:<8} chickens.', 49);
+sformat('We have {:<8} chickens.', 49)
+We have 49 chickens.
+select sformat('Center alimgn [{:*^10}]', 'data');
+sformat('Center alimgn [{:*^10}]', 'data')
+Center alimgn [***data***]
+select sformat('Center aling [{:^10}].', 'data');
+sformat('Center aling [{:^10}].', 'data')
+Center aling [ data ].
+select sformat('Right aling [{:>10}].', 'data');
+sformat('Right aling [{:>10}].', 'data')
+Right aling [ data].
+select sformat('Left align [{:<10}].', 'data');
+sformat('Left align [{:<10}].', 'data')
+Left align [data ].
+select sformat('{0}{1}{0}', 'abra', 'cad');
+sformat('{0}{1}{0}', 'abra', 'cad')
+abracadabra
+select sformat('Change Order {1} {0}', 'second', 'first');
+sformat('Change Order {1} {0}', 'second', 'first')
+Change Order first second
+#
+# Failed Format Test Cases
+#
+select sformat('Test {:c}', 'word');
+sformat('Test {:c}', 'word')
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid type specifier
+select sformat('Test {one} {two} {three}', 1, 2, 3);
+sformat('Test {one} {two} {three}', 1, 2, 3)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: argument not found
+select sformat('Number {:{<}', 8);
+sformat('Number {:{<}', 8)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid fill character '{'
+select sformat('Number {:10000000000}', 5);
+sformat('Number {:10000000000}', 5)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: number is too big
+select sformat('1={1} 2={2} 0={0}', 0, 1);
+sformat('1={1} 2={2} 0={0}', 0, 1)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: argument not found
+select sformat('Number {:.2d}', 42);
+sformat('Number {:.2d}', 42)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: precision not allowed for this argument type
+select sformat('You scored {:.0%}', 0.25);
+sformat('You scored {:.0%}', 0.25)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid type specifier
+select sformat('You scored {:%}', 0.25);
+sformat('You scored {:%}', 0.25)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid type specifier
+select sformat('The price is {:f} dollars.', 45);
+sformat('The price is {:f} dollars.', 45)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid type specifier
+select sformat('The price is {:.2f} dollars.', 45);
+sformat('The price is {:.2f} dollars.', 45)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: precision not allowed for this argument type
+select sformat('We have {:E} chickens.', 5);
+sformat('We have {:E} chickens.', 5)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid type specifier
+select sformat('We have {:e} chickens.', 5);
+sformat('We have {:e} chickens.', 5)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid type specifier
+select sformat('The universe is {:,} years old.', 13800000000);
+sformat('The universe is {:,} years old.', 13800000000)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid type specifier
+select sformat('The universe is {:_} years old.', 13800000000);
+sformat('The universe is {:_} years old.', 13800000000)
+NULL
+Warnings:
+Warning 4183 Wrong format exception: invalid type specifier
+select sformat('String {:-}', 'hello');
+sformat('String {:-}', 'hello')
+NULL
+Warnings:
+Warning 4183 Wrong format exception: format specifier requires numeric argument
+#
+# Table Format Test Cases
+#
+create table t1 (pformat text, data1 FLOAT, data2 INT,
+data3 VARCHAR(30), data4 CHAR);
+insert into t1 values ('{:.2f} {: } |{:*^15}| {:c}', 1.11, 1, 'example I', 'A'),
+('{:.1f} {:-} |{:^15}| {:c}', -2.22, -2, 'example II', 'B'),
+('{:.2f} {:+} |{:<15}| {:c}', 3.33, 3, 'example III', 'C'),
+('{:.4f} {:d} |{:^15}| {:c}', -4.44, -4, 'example IV', 'D');
+select sformat(pformat, data1, data2, data3, data4) from t1;
+sformat(pformat, data1, data2, data3, data4)
+1.11 1 |***example I***| A
+-2.2 -2 | example II | B
+3.33 +3 |example III | C
+-4.4400 -4 | example IV | D
+drop table t1;
+create table t2 (a double);
+insert into t2 values (3.14159265358979323846);
+select a, sformat('{:.15f}', a) from t2;
+a sformat('{:.15f}', a)
+3.141592653589793 3.141592653589793
+drop table t2;
+#
+# Table Test Cases
+#
+create table t1 (param1 text, param2 text, param3 int);
+insert into t1 values ('string: {}, number: {}', 'xyz', 123),
+('something {} went {} wrong {}', 'foo', 0),
+('Test case {} -> {}', 'Print', -32);
+select sformat(param1, param2, param3) from t1;
+sformat(param1, param2, param3)
+string: xyz, number: 123
+NULL
+Test case Print -> -32
+Warnings:
+Warning 4183 Wrong format exception: argument not found
+drop table t1;
+create table t2 (param1 FLOAT, param2 SMALLINT, param3 CHAR, param4 DATE);
+insert into t2 values (0.0025, 25, 'A', DATE('2020-06-29')),
+(0.0005, 5, 'B', DATE('2020-6-29')),
+(5.5555, -5, 'C', DATE('200629')),
+(-9, -9, 'D', DATE('20*06*29'));
+select sformat('p1 {:.4f} p2 {} p3 {} p4 {}', param1, param2, param3, param4) from t2;
+sformat('p1 {:.4f} p2 {} p3 {} p4 {}', param1, param2, param3, param4)
+p1 0.0025 p2 25 p3 A p4 2020-06-29
+p1 0.0005 p2 5 p3 B p4 2020-06-29
+p1 5.5555 p2 -5 p3 C p4 2020-06-29
+p1 -9.0000 p2 -9 p3 D p4 2020-06-29
+drop table t2;
+set names utf8;
+create table t3 (format_str text character set 'latin1',
+first_param text character set 'utf8',
+second_param text character set 'latin1');
+insert into t3 values ('test 1 {} {}', UNHEX('C3A5'), UNHEX('E5'));
+select sformat(format_str, first_param, second_param) from t3;
+sformat(format_str, first_param, second_param)
+test 1 å å
+select HEX(sformat(format_str, first_param, second_param)) from t3;
+HEX(sformat(format_str, first_param, second_param))
+74657374203120C3A520C3A5
+drop table t3;
+set names latin1;
+create table t4 (p1 bit(8), p2 boolean, p3 DECIMAL, p4 TIMESTAMP);
+insert into t4 values (42, TRUE, 42, '2021-08-18 16:50:07'),
+(24, FALSE, 24, '0000-00-00 00:00:00');
+select sformat('{}: {} {} {} {}', 'Data', p1, p2, p3, p4) from t4;
+sformat('{}: {} {} {} {}', 'Data', p1, p2, p3, p4)
+Data: 42 1 42 2021-08-18 16:50:07
+Data: 24 0 24 0000-00-00 00:00:00
+drop table t4;
+set names utf8;
+create table t5 (param text character SET utf8 COLLATE utf8_general_ci);
+insert into t5 values (UNHEX('C3A5')),(UNHEX('C5BB')),(UNHEX('e0b1bb')),
+(UNHEX('C38A')), (NULL);
+select sformat('{}', param) from t5;
+sformat('{}', param)
+౻
+NULL
+select HEX(sformat('{}', param)) from t5;
+HEX(sformat('{}', param))
+C3A5
+C5BB
+E0B1BB
+C38A
+NULL
+drop table t5;
+set names latin1;
diff --git a/mysql-test/main/sformat.test b/mysql-test/main/sformat.test
new file mode 100644
index 00000000000..c48de5b8cde
--- /dev/null
+++ b/mysql-test/main/sformat.test
@@ -0,0 +1,224 @@
+# Description
+# -----------
+# Test cases for the sformat function
+
+echo #;
+echo # Normal Test Cases;
+echo #;
+select sformat('string test');
+select sformat(0);
+select sformat('C');
+select sformat(-4.2);
+select sformat(5, 5, 5);
+select sformat('{} {}', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+select sformat('{{{}}}', 0);
+select sformat('{{{}{{', 0);
+select sformat('{{{{{}{{', 'param1');
+select sformat(' {{ {{ {} {{ ', 'param1');
+select sformat(' {{ {} {}', 'param1', 'param2');
+select sformat('A{}C{}E{}', 'B', 'D', 'F');
+select sformat('{} {}', FALSE, TRUE);
+select sformat('Add € != {} != {}?', '$', '£');
+select sformat('Check {} != {} != {}?', '€', '$', '£');
+select sformat('{}{}{}', 1, 2, 3);
+select sformat('Float {} Boolean {} Number {}', 3.14159, True, -50);
+select sformat('SUM {} + {} = {}', 2, 3, 2+3);
+select sformat('Numbers {} {} {}', 1, 1.11, 1.111);
+select sformat('what {} is {}?', 'time', 'it');
+select sformat('{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}', 1, 2, 3, 4, 5, 6,
+ 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
+ 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
+ 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
+ 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
+ 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120);
+select sformat('{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
+ {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}', '1', '2', '3 ',
+ '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20',
+ '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36',
+ '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52',
+ '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68',
+ '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84',
+ '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100',
+ '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114',
+ '115', '116', '117', '118', '119', '120');
+
+echo #;
+echo # Error Test Cases;
+echo #;
+select sformat('R={ }', 42);
+select sformat(NULL, 'Null', 'Test');
+select sformat('Null Test {} {} ', 0, NULL);
+select sformat(NULL);
+select sformat('My { Test');
+select sformat('Test { {');
+select sformat('A{}C{}E{}', 'B', 'D');
+--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
+select sformat();
+--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
+echo #;
+echo # Table Test Cases;
+echo #;
+create table t1 (param1 text, param2 text, param3 int);
+insert into t1 values ('string: {}, number: {}', 'xyz', 123),
+ ('something {} went {} wrong {}', 'foo', 0),
+ ('Test case {} -> {}', 'Print', -32);
+select sformat(param1, param2, param3) from t1;
+drop table t1;
+
+create table t2 (param1 FLOAT, param2 SMALLINT, param3 CHAR, param4 DATE);
+insert into t2 values (0.0025, 25, 'A', DATE('2020-06-29')),
+ (0.0005, 5, 'B', DATE('2020-6-29')),
+ (5.5555, -5, 'C', DATE('200629')),
+ (-9, -9, 'D', DATE('20*06*29'));
+select sformat('p1 {:.4f} p2 {} p3 {} p4 {}', param1, param2, param3, param4) from t2;
+drop table t2;
+
+set names utf8;
+create table t3 (format_str text character set 'latin1',
+ first_param text character set 'utf8',
+ second_param text character set 'latin1');
+insert into t3 values ('test 1 {} {}', UNHEX('C3A5'), UNHEX('E5'));
+select sformat(format_str, first_param, second_param) from t3;
+select HEX(sformat(format_str, first_param, second_param)) from t3;
+drop table t3;
+set names latin1;
+
+create table t4 (p1 bit(8), p2 boolean, p3 DECIMAL, p4 TIMESTAMP);
+insert into t4 values (42, TRUE, 42, '2021-08-18 16:50:07'),
+ (24, FALSE, 24, '0000-00-00 00:00:00');
+select sformat('{}: {} {} {} {}', 'Data', p1, p2, p3, p4) from t4;
+drop table t4;
+
+set names utf8;
+create table t5 (param text character SET utf8 COLLATE utf8_general_ci);
+insert into t5 values (UNHEX('C3A5')),(UNHEX('C5BB')),(UNHEX('e0b1bb')),
+ (UNHEX('C38A')), (NULL);
+select sformat('{}', param) from t5;
+select HEX(sformat('{}', param)) from t5;
+drop table t5;
+set names latin1;
+
+echo #;
+echo # Format Test Cases;
+echo #;
+select sformat('Num {:L}', 13800000000);
+select sformat('Num [{:20}]', 42);
+select sformat('Number: {:*^{}}', 4, 5);
+select sformat('{:02} - {:02} - {:02}', 1, 2, 3);
+select sformat('Character {:c}', 'h');
+select sformat('String {:s}', 'hello');
+select sformat('Large {0:+010.4g}', 392.64);
+select sformat('Large {:g}', 392.65);
+select sformat('Float {:.2f}', 42.0);
+select sformat('Float {:f}', 42.0);
+select sformat('Number {:d}', 42);
+select sformat('Number {:{}}', 5, 5);
+select sformat('Number [{:10}]', 9999);
+select sformat('Number {:.3}', 3.1416);
+select sformat('int: {0:d}; hex: {0:x}; oct: {0:o}', 42);
+select sformat('int: {0:d}; hex: {0:#x}; oct: {0:#o}', 42);
+select sformat('The Hexadecimal version of {0} is {0:X}', 255);
+select sformat('The Hexadecimal version of {0} is {0:x}', 255);
+select sformat('The octal version of {0} is {0:o}', 10);
+select sformat('The binary version of {0} is {0:b}', 5);
+select sformat('{:+f}; {:+f}', 3.14, -3.14);
+select sformat('{: f}; {: f}', 3.14, -3.14);
+select sformat('{:-f}; {:-f}', 3.14, -3.14);
+select sformat('The temperature is between {: } and {: } degrees celsius.', -3, 7);
+select sformat('The temperature is between {:-} and {:-} degrees celsius.', -3, 7);
+select sformat('The temperature is between {:+} and {:+} degrees celsius.', -3, 7);
+select sformat('We have {:<8} chickens.', 49);
+select sformat('Center alimgn [{:*^10}]', 'data');
+select sformat('Center aling [{:^10}].', 'data');
+select sformat('Right aling [{:>10}].', 'data');
+select sformat('Left align [{:<10}].', 'data');
+select sformat('{0}{1}{0}', 'abra', 'cad');
+select sformat('Change Order {1} {0}', 'second', 'first');
+
+echo #;
+echo # Failed Format Test Cases;
+echo #;
+select sformat('Test {:c}', 'word');
+select sformat('Test {one} {two} {three}', 1, 2, 3);
+select sformat('Number {:{<}', 8);
+select sformat('Number {:10000000000}', 5);
+select sformat('1={1} 2={2} 0={0}', 0, 1);
+select sformat('Number {:.2d}', 42);
+select sformat('You scored {:.0%}', 0.25);
+select sformat('You scored {:%}', 0.25);
+select sformat('The price is {:f} dollars.', 45);
+select sformat('The price is {:.2f} dollars.', 45);
+select sformat('We have {:E} chickens.', 5);
+select sformat('We have {:e} chickens.', 5);
+select sformat('The universe is {:,} years old.', 13800000000);
+select sformat('The universe is {:_} years old.', 13800000000);
+select sformat('String {:-}', 'hello');
+
+echo #;
+echo # Table Format Test Cases;
+echo #;
+create table t1 (pformat text, data1 FLOAT, data2 INT,
+ data3 VARCHAR(30), data4 CHAR);
+insert into t1 values ('{:.2f} {: } |{:*^15}| {:c}', 1.11, 1, 'example I', 'A'),
+ ('{:.1f} {:-} |{:^15}| {:c}', -2.22, -2, 'example II', 'B'),
+ ('{:.2f} {:+} |{:<15}| {:c}', 3.33, 3, 'example III', 'C'),
+ ('{:.4f} {:d} |{:^15}| {:c}', -4.44, -4, 'example IV', 'D');
+select sformat(pformat, data1, data2, data3, data4) from t1;
+drop table t1;
+
+create table t2 (a double);
+insert into t2 values (3.14159265358979323846);
+select a, sformat('{:.15f}', a) from t2;
+drop table t2;
+
+echo #;
+echo # Table Test Cases;
+echo #;
+create table t1 (param1 text, param2 text, param3 int);
+insert into t1 values ('string: {}, number: {}', 'xyz', 123),
+ ('something {} went {} wrong {}', 'foo', 0),
+ ('Test case {} -> {}', 'Print', -32);
+select sformat(param1, param2, param3) from t1;
+drop table t1;
+
+create table t2 (param1 FLOAT, param2 SMALLINT, param3 CHAR, param4 DATE);
+insert into t2 values (0.0025, 25, 'A', DATE('2020-06-29')),
+ (0.0005, 5, 'B', DATE('2020-6-29')),
+ (5.5555, -5, 'C', DATE('200629')),
+ (-9, -9, 'D', DATE('20*06*29'));
+select sformat('p1 {:.4f} p2 {} p3 {} p4 {}', param1, param2, param3, param4) from t2;
+drop table t2;
+
+set names utf8;
+create table t3 (format_str text character set 'latin1',
+ first_param text character set 'utf8',
+ second_param text character set 'latin1');
+insert into t3 values ('test 1 {} {}', UNHEX('C3A5'), UNHEX('E5'));
+select sformat(format_str, first_param, second_param) from t3;
+select HEX(sformat(format_str, first_param, second_param)) from t3;
+drop table t3;
+set names latin1;
+
+create table t4 (p1 bit(8), p2 boolean, p3 DECIMAL, p4 TIMESTAMP);
+insert into t4 values (42, TRUE, 42, '2021-08-18 16:50:07'),
+ (24, FALSE, 24, '0000-00-00 00:00:00');
+select sformat('{}: {} {} {} {}', 'Data', p1, p2, p3, p4) from t4;
+drop table t4;
+
+set names utf8;
+create table t5 (param text character SET utf8 COLLATE utf8_general_ci);
+insert into t5 values (UNHEX('C3A5')),(UNHEX('C5BB')),(UNHEX('e0b1bb')),
+ (UNHEX('C38A')), (NULL);
+select sformat('{}', param) from t5;
+select HEX(sformat('{}', param)) from t5;
+drop table t5;
+set names latin1;
diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt
index 5a8ea9ea2d2..7318667901f 100644
--- a/sql/CMakeLists.txt
+++ b/sql/CMakeLists.txt
@@ -52,6 +52,7 @@ ENDIF()
INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/sql
+${LIBFMT_INCLUDE_DIR}
${PCRE_INCLUDES}
${ZLIB_INCLUDE_DIR}
${SSL_INCLUDE_DIRS}
@@ -207,7 +208,7 @@ MAYBE_DISABLE_IPO(sql)
DTRACE_INSTRUMENT(sql)
TARGET_LINK_LIBRARIES(sql
mysys mysys_ssl dbug strings vio pcre2-8
- tpool
+ tpool fmt
${LIBWRAP} ${LIBCRYPT} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}
${SSL_LIBRARIES}
${LIBSYSTEMD})
diff --git a/sql/item_create.cc b/sql/item_create.cc
index e019ac6f8bb..1d78afed34a 100644
--- a/sql/item_create.cc
+++ b/sql/item_create.cc
@@ -1922,6 +1922,16 @@ protected:
virtual ~Create_func_sec_to_time() {}
};
+class Create_func_sformat : public Create_native_func
+{
+public:
+ virtual Item *create_native(THD *thd, LEX_CSTRING *name,
+ List<Item> *item_list);
+ static Create_func_sformat s_singleton;
+protected:
+ Create_func_sformat() {}
+ virtual ~Create_func_sformat() {}
+};
class Create_func_sha : public Create_func_arg1
{
@@ -5010,6 +5020,26 @@ Create_func_sec_to_time::create_1_arg(THD *thd, Item *arg1)
return new (thd->mem_root) Item_func_sec_to_time(thd, arg1);
}
+Create_func_sformat Create_func_sformat::s_singleton;
+
+Item*
+Create_func_sformat::create_native(THD *thd, LEX_CSTRING *name,
+ List<Item> *item_list)
+{
+ int arg_count= 0;
+
+ if (item_list != NULL)
+ arg_count= item_list->elements;
+
+ if (unlikely(arg_count < 1))
+ {
+ my_error(ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT, MYF(0), name->str);
+ return NULL;
+ }
+
+ return new (thd->mem_root) Item_func_sformat(thd, *item_list);
+}
+
Create_func_sha Create_func_sha::s_singleton;
@@ -5677,6 +5707,7 @@ Native_func_registry func_array[] =
{ { STRING_WITH_LEN("RTRIM") }, BUILDER(Create_func_rtrim)},
{ { STRING_WITH_LEN("RTRIM_ORACLE") }, BUILDER(Create_func_rtrim_oracle)},
{ { STRING_WITH_LEN("SEC_TO_TIME") }, BUILDER(Create_func_sec_to_time)},
+ { { STRING_WITH_LEN("SFORMAT") }, BUILDER(Create_func_sformat)},
{ { STRING_WITH_LEN("SHA") }, BUILDER(Create_func_sha)},
{ { STRING_WITH_LEN("SHA1") }, BUILDER(Create_func_sha)},
{ { STRING_WITH_LEN("SHA2") }, BUILDER(Create_func_sha2)},
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc
index 93353e2a925..46f6423d440 100644
--- a/sql/item_strfunc.cc
+++ b/sql/item_strfunc.cc
@@ -55,6 +55,9 @@ C_MODE_END
#include <sql_repl.h>
#include "sql_statistics.h"
+/* fmtlib include (https://fmt.dev/). */
+#include "fmt/format.h"
+
size_t username_char_length= USERNAME_CHAR_LENGTH;
/*
@@ -1302,6 +1305,106 @@ bool Item_func_replace::fix_length_and_dec()
}
+bool Item_func_sformat::fix_length_and_dec()
+{
+ ulonglong char_length= 0;
+
+ if (agg_arg_charsets_for_string_result(collation, args, arg_count))
+ return TRUE;
+
+ for (uint i=0 ; i < arg_count ; i++)
+ char_length+= args[i]->max_char_length();
+
+ fix_char_length_ulonglong(char_length);
+ return FALSE;
+}
+
+/*
+ * SFORMAT(format_string, ...)
+ * This function receives a formatting specification string and N parameters
+ * (N >= 0), and it returns string formatted using the rules the user passed
+ * in the specification. It uses fmtlib (https://fmt.dev/).
+ */
+String *Item_func_sformat::val_str(String *res)
+{
+ DBUG_ASSERT(fixed());
+ uint carg= 1;
+ using ctx= fmt::format_context;
+ String *fmt_arg= NULL;
+ String *parg= NULL;
+ String *val_arg= NULL;
+ fmt::format_args::format_arg *vargs= NULL;
+
+ null_value= true;
+ if (!(fmt_arg= args[0]->val_str(res)))
+ return NULL;
+
+ if (!(vargs= new fmt::format_args::format_arg[arg_count - 1]))
+ return NULL;
+
+ if (!(val_arg= new String[arg_count - 1]))
+ {
+ delete [] vargs;
+ return NULL;
+ }
+
+ /* Creates the array of arguments for vformat */
+ while (carg < arg_count)
+ {
+ switch (args[carg]->result_type())
+ {
+ case INT_RESULT:
+ vargs[carg-1]= fmt::detail::make_arg<ctx>(args[carg]->val_int());
+ break;
+ case DECIMAL_RESULT:
+ case REAL_RESULT:
+ vargs[carg-1]= fmt::detail::make_arg<ctx>(args[carg]->val_real());
+ break;
+ case STRING_RESULT:
+ if (!(parg= args[carg]->val_str(&val_arg[carg-1])))
+ {
+ delete [] vargs;
+ delete [] val_arg;
+ return NULL;
+ }
+ if (parg->length() == 1)
+ vargs[carg-1]= fmt::detail::make_arg<ctx>(parg->ptr()[0]);
+ else
+ vargs[carg-1]= fmt::detail::make_arg<ctx>(parg->c_ptr_safe());
+ break;
+ default:
+ DBUG_ASSERT(0);
+ delete [] vargs;
+ delete [] val_arg;
+ return NULL;
+ }
+ carg++;
+ }
+
+ null_value= false;
+ /* Create the string output */
+ try
+ {
+ auto text = fmt::vformat(fmt_arg->c_ptr_safe(),
+ fmt::format_args(vargs, arg_count-1));
+ res->set("", 0, collation.collation);
+ res->append(text.c_str(), text.size());
+ }
+ catch (const fmt::format_error &ex)
+ {
+ THD *thd= current_thd;
+ push_warning_printf(thd,
+ Sql_condition::WARN_LEVEL_WARN,
+ WARN_WRONG_FMTLIB_FORMAT,
+ ER_THD(thd, WARN_WRONG_FMTLIB_FORMAT),
+ ex.what());
+ null_value= true;
+ }
+ delete [] vargs;
+ delete [] val_arg;
+ return null_value ? NULL : res;
+}
+
/*********************************************************************/
bool Item_func_regexp_replace::fix_length_and_dec()
{
diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h
index 3384cf5b369..d06f07d8c34 100644
--- a/sql/item_strfunc.h
+++ b/sql/item_strfunc.h
@@ -586,6 +586,22 @@ public:
{ return get_item_copy<Item_func_substr>(thd, this); }
};
+class Item_func_sformat :public Item_str_func
+{
+public:
+ Item_func_sformat(THD *thd, List<Item> &list):
+ Item_str_func(thd, list) { }
+ String *val_str(String*) override;
+ bool fix_length_and_dec() override;
+ LEX_CSTRING func_name_cstring() const override
+ {
+ static LEX_CSTRING name= {STRING_WITH_LEN("sformat") };
+ return name;
+ }
+ Item *get_copy(THD *thd) override
+ { return get_item_copy<Item_func_sformat>(thd, this); }
+};
+
class Item_func_substr_oracle :public Item_func_substr
{
protected:
diff --git a/sql/share/errmsg-utf8.txt b/sql/share/errmsg-utf8.txt
index 205f42f50a0..a05fbe04b77 100644
--- a/sql/share/errmsg-utf8.txt
+++ b/sql/share/errmsg-utf8.txt
@@ -7992,3 +7992,5 @@ ER_REMOVED_ORPHAN_TRIGGER
eng "Dropped orphan trigger '%-.64s', originally created for table: '%-.192s'"
ER_STORAGE_ENGINE_DISABLED
eng "Storage engine %s is disabled"
+WARN_WRONG_FMTLIB_FORMAT
+ eng "Wrong format exception: %-.64s"