Skip to content

Commit

Permalink
MDEV-12478 CONCAT function inside view casts values incorrectly with …
Browse files Browse the repository at this point in the history
…Oracle sql_mode
  • Loading branch information
Alexander Barkov committed Apr 11, 2017
1 parent 5bf7046 commit 012fbc1
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 12 deletions.
55 changes: 53 additions & 2 deletions mysql-test/suite/compat/oracle/r/func_concat.result
Expand Up @@ -3,12 +3,12 @@ EXPLAIN EXTENDED SELECT 'a'||'b'||'c';
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used
Warnings:
Note 1003 select 'a' || 'b' || 'c' AS "'a'||'b'||'c'"
Note 1003 select concat_operator_oracle(concat_operator_oracle('a','b'),'c') AS "'a'||'b'||'c'"
EXPLAIN EXTENDED SELECT CONCAT('a'||'b'||'c');
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used
Warnings:
Note 1003 select 'a' || 'b' || 'c' AS "CONCAT('a'||'b'||'c')"
Note 1003 select concat_operator_oracle(concat_operator_oracle(concat_operator_oracle('a','b'),'c')) AS "CONCAT('a'||'b'||'c')"
SELECT '' || '';
'' || ''

Expand Down Expand Up @@ -204,3 +204,54 @@ NULL NULL
2 ab
3 abc
DROP TABLE t1;
#
# MDEV-12478 CONCAT function inside view casts values incorrectly with Oracle sql_mode
#
SET sql_mode=ORACLE;
CREATE VIEW v1 AS SELECT 'foo'||NULL||'bar' AS test;
SHOW CREATE VIEW v1;
View Create View character_set_client collation_connection
v1 CREATE VIEW "v1" AS select concat_operator_oracle(concat_operator_oracle('foo',NULL),'bar') AS "test" latin1 latin1_swedish_ci
SELECT * FROM v1;
test
foobar
SET sql_mode=DEFAULT;
SHOW CREATE VIEW v1;
View Create View character_set_client collation_connection
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select concat_operator_oracle(concat_operator_oracle('foo',NULL),'bar') AS `test` latin1 latin1_swedish_ci
SELECT * FROM v1;
test
foobar
DROP VIEW v1;
SET sql_mode=DEFAULT;
CREATE VIEW v1 AS SELECT CONCAT('foo',NULL,'bar') AS test;
SHOW CREATE VIEW v1;
View Create View character_set_client collation_connection
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select concat('foo',NULL,'bar') AS `test` latin1 latin1_swedish_ci
SELECT * FROM v1;
test
NULL
SET sql_mode=ORACLE;
SHOW CREATE VIEW v1;
View Create View character_set_client collation_connection
v1 CREATE VIEW "v1" AS select concat('foo',NULL,'bar') AS "test" latin1 latin1_swedish_ci
SELECT * FROM v1;
test
NULL
DROP VIEW v1;
SET sql_mode=DEFAULT;
CREATE VIEW v1 AS SELECT '0'||'1' AS test;
SHOW CREATE VIEW v1;
View Create View character_set_client collation_connection
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select '0' or '1' AS `test` latin1 latin1_swedish_ci
SELECT * FROM v1;
test
1
SET sql_mode=ORACLE;
SHOW CREATE VIEW v1;
View Create View character_set_client collation_connection
v1 CREATE VIEW "v1" AS select '0' or '1' AS "test" latin1 latin1_swedish_ci
SELECT * FROM v1;
test
1
DROP VIEW v1;
4 changes: 2 additions & 2 deletions mysql-test/suite/compat/oracle/r/ps.result
Expand Up @@ -178,9 +178,9 @@ EXECUTE IMMEDIATE 'SELECT :1 FROM DUAL' USING 10;
# Testing erroneous and diallowed prepare source
#
EXECUTE IMMEDIATE _latin1'SELECT 1 AS c FROM ' || _latin2 'DUAL';
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,COERCIBLE) and (latin2_general_ci,COERCIBLE) for operation '||'
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,COERCIBLE) and (latin2_general_ci,COERCIBLE) for operation 'concat_operator_oracle'
PREPARE stmt FROM _latin1'SELECT 1 AS c FROM ' || _latin2 'DUAL';
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,COERCIBLE) and (latin2_general_ci,COERCIBLE) for operation '||'
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,COERCIBLE) and (latin2_general_ci,COERCIBLE) for operation 'concat_operator_oracle'
EXECUTE IMMEDIATE (SELECT 'SELECT 1');
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 'SELECT 1')' at line 1
PREPARE stmt FROM (SELECT 'SELECT 1');
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/suite/compat/oracle/r/sp-cursor-rowtype.result
Expand Up @@ -758,7 +758,7 @@ END;
END;
$$
CALL p1();
ERROR HY000: Illegal mix of collations (latin1_bin,EXPLICIT) and (latin1_swedish_ci,EXPLICIT) for operation '||'
ERROR HY000: Illegal mix of collations (latin1_bin,EXPLICIT) and (latin1_swedish_ci,EXPLICIT) for operation 'concat_operator_oracle'
DROP PROCEDURE p1;
#
# Non-existing field
Expand Down
31 changes: 31 additions & 0 deletions mysql-test/suite/compat/oracle/t/func_concat.test
Expand Up @@ -83,3 +83,34 @@ SELECT LENGTH(a||b||c), a||b||c FROM t1 ORDER BY a,b,c;
SELECT LENGTH(CONCAT(a||b||c)), CONCAT(a||b||c) FROM t1 ORDER BY a,b,c;

DROP TABLE t1;

--echo #
--echo # MDEV-12478 CONCAT function inside view casts values incorrectly with Oracle sql_mode
--echo #

SET sql_mode=ORACLE;
CREATE VIEW v1 AS SELECT 'foo'||NULL||'bar' AS test;
SHOW CREATE VIEW v1;
SELECT * FROM v1;
SET sql_mode=DEFAULT;
SHOW CREATE VIEW v1;
SELECT * FROM v1;
DROP VIEW v1;

SET sql_mode=DEFAULT;
CREATE VIEW v1 AS SELECT CONCAT('foo',NULL,'bar') AS test;
SHOW CREATE VIEW v1;
SELECT * FROM v1;
SET sql_mode=ORACLE;
SHOW CREATE VIEW v1;
SELECT * FROM v1;
DROP VIEW v1;

SET sql_mode=DEFAULT;
CREATE VIEW v1 AS SELECT '0'||'1' AS test;
SHOW CREATE VIEW v1;
SELECT * FROM v1;
SET sql_mode=ORACLE;
SHOW CREATE VIEW v1;
SELECT * FROM v1;
DROP VIEW v1;
35 changes: 35 additions & 0 deletions sql/item_create.cc
Expand Up @@ -644,6 +644,19 @@ class Create_func_concat : public Create_native_func
};


class Create_func_concat_operator_oracle : public Create_native_func
{
public:
virtual Item *create_native(THD *thd, LEX_STRING name, List<Item> *item_list);

static Create_func_concat_operator_oracle s_singleton;

protected:
Create_func_concat_operator_oracle() {}
virtual ~Create_func_concat_operator_oracle() {}
};


class Create_func_decode_histogram : public Create_func_arg2
{
public:
Expand Down Expand Up @@ -3862,6 +3875,27 @@ Create_func_concat::create_native(THD *thd, LEX_STRING name,
new (thd->mem_root) Item_func_concat(thd, *item_list);
}

Create_func_concat_operator_oracle
Create_func_concat_operator_oracle::s_singleton;

Item*
Create_func_concat_operator_oracle::create_native(THD *thd, LEX_STRING name,
List<Item> *item_list)
{
int arg_count= 0;

if (item_list != NULL)
arg_count= item_list->elements;

if (arg_count < 1)
{
my_error(ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT, MYF(0), name.str);
return NULL;
}

return new (thd->mem_root) Item_func_concat_operator_oracle(thd, *item_list);
}

Create_func_decode_histogram Create_func_decode_histogram::s_singleton;

Item *
Expand Down Expand Up @@ -6737,6 +6771,7 @@ static Native_func_registry func_array[] =
{ { C_STRING_WITH_LEN("COLUMN_JSON") }, BUILDER(Create_func_dyncol_json)},
{ { C_STRING_WITH_LEN("COMPRESS") }, BUILDER(Create_func_compress)},
{ { C_STRING_WITH_LEN("CONCAT") }, BUILDER(Create_func_concat)},
{ { C_STRING_WITH_LEN("CONCAT_OPERATOR_ORACLE") }, BUILDER(Create_func_concat_operator_oracle)},
{ { C_STRING_WITH_LEN("CONCAT_WS") }, BUILDER(Create_func_concat_ws)},
{ { C_STRING_WITH_LEN("CONNECTION_ID") }, BUILDER(Create_func_connection_id)},
{ { C_STRING_WITH_LEN("CONV") }, BUILDER(Create_func_conv)},
Expand Down
6 changes: 1 addition & 5 deletions sql/item_strfunc.h
Expand Up @@ -303,12 +303,8 @@ class Item_func_concat_operator_oracle :public Item_func_concat
Item_func_concat_operator_oracle(THD *thd, Item *a, Item *b)
:Item_func_concat(thd, a, b)
{ }
void print(String *str, enum_query_type query_type)
{
print_op(str, query_type);
}
String *val_str(String *);
const char *func_name() const { return "||"; }
const char *func_name() const { return "concat_operator_oracle"; }
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{
return get_item_copy<Item_func_concat_operator_oracle>(thd, mem_root, this);
Expand Down
5 changes: 3 additions & 2 deletions sql/sql_view.cc
Expand Up @@ -1363,7 +1363,7 @@ bool mysql_make_view(THD *thd, TABLE_SHARE *share, TABLE_LIST *table,
* MODE_NO_UNSIGNED_SUBTRACTION affect execution
- MODE_NO_DIR_IN_CREATE affect table creation only
- MODE_POSTGRESQL compounded from other modes
- MODE_ORACLE compounded from other modes
- MODE_ORACLE affects Item creation (e.g for CONCAT)
- MODE_MSSQL compounded from other modes
- MODE_DB2 compounded from other modes
- MODE_MAXDB affect only CREATE TABLE parsing
Expand All @@ -1378,7 +1378,8 @@ bool mysql_make_view(THD *thd, TABLE_SHARE *share, TABLE_LIST *table,
+ MODE_NO_BACKSLASH_ESCAPES affect expression parsing
*/
thd->variables.sql_mode&= ~(MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
MODE_IGNORE_SPACE | MODE_NO_BACKSLASH_ESCAPES);
MODE_IGNORE_SPACE | MODE_NO_BACKSLASH_ESCAPES |
MODE_ORACLE);

/* Parse the query. */

Expand Down

0 comments on commit 012fbc1

Please sign in to comment.