mariadb/sql/lex_charset.h

467 lines
13 KiB
C
Raw Normal View History

/* Copyright (c) 2021, 2022, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
#ifndef LEX_CHARSET_INCLUDED
#define LEX_CHARSET_INCLUDED
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
/*
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
An exact character set, e.g:
CHARACTER SET latin1
*/
class Lex_exact_charset
{
CHARSET_INFO *m_ci;
public:
explicit Lex_exact_charset(CHARSET_INFO *ci)
:m_ci(ci)
{
DBUG_ASSERT(m_ci);
DBUG_ASSERT(m_ci->state & MY_CS_PRIMARY);
}
CHARSET_INFO *charset_info() const { return m_ci; }
bool raise_if_not_equal(const Lex_exact_charset &rhs) const;
bool raise_if_not_applicable(const class Lex_exact_collation &cl) const;
};
/*
A contextually typed collation, e.g.:
COLLATE DEFAULT
CHAR(10) BINARY
*/
class Lex_context_collation
{
CHARSET_INFO *m_ci;
public:
explicit Lex_context_collation(CHARSET_INFO *ci)
:m_ci(ci)
{
DBUG_ASSERT(ci);
}
CHARSET_INFO *charset_info() const { return m_ci; }
bool is_contextually_typed_collate_default() const
{
return m_ci == &my_collation_contextually_typed_default;
}
bool is_contextually_typed_binary_style() const
{
return m_ci == &my_collation_contextually_typed_binary;
}
bool raise_if_not_equal(const Lex_context_collation &cl) const;
};
/*
An exact collation, e.g.
COLLATE latin1_swedish_ci
*/
class Lex_exact_collation
{
CHARSET_INFO *m_ci;
public:
explicit Lex_exact_collation(CHARSET_INFO *ci)
:m_ci(ci)
{
DBUG_ASSERT(ci);
}
CHARSET_INFO *charset_info() const { return m_ci; }
// EXACT + EXACT
bool raise_if_not_equal(const Lex_exact_collation &cl) const;
// EXACT + CONTEXT
// CONTEXT + EXACT
bool raise_if_conflicts_with_context_collation(const Lex_context_collation &,
bool reverse_order) const;
};
/*
Parse time COLLATE clause:
COLLATE colation_name
The collation can be either exact or contextual:
COLLATE latin1_bin
COLLATE DEFAULT
*/
class Lex_extended_collation_st
{
public:
enum Type
{
TYPE_EXACT,
TYPE_CONTEXTUALLY_TYPED
};
protected:
CHARSET_INFO *m_ci;
Type m_type;
public:
void init(CHARSET_INFO *ci, Type type)
{
m_ci= ci;
m_type= type;
}
CHARSET_INFO *charset_info() const { return m_ci; }
Type type() const { return m_type; }
void set_collate_default()
{
m_ci= &my_collation_contextually_typed_default;
m_type= TYPE_CONTEXTUALLY_TYPED;
}
bool raise_if_conflicts_with_context_collation(const Lex_context_collation &)
const;
bool merge_exact_charset(const Lex_exact_charset &rhs);
bool merge_exact_collation(const Lex_exact_collation &rhs);
bool merge(const Lex_extended_collation_st &rhs);
};
class Lex_extended_collation: public Lex_extended_collation_st
{
public:
Lex_extended_collation(CHARSET_INFO *ci, Type type)
{
init(ci, type);
}
Lex_extended_collation(const Lex_exact_collation &rhs)
{
init(rhs.charset_info(), TYPE_EXACT);
}
};
/*
CHARACTER SET cs_exact [COLLATE cl_exact_or_context]
*/
class Lex_exact_charset_opt_extended_collate
{
CHARSET_INFO *m_ci;
bool m_with_collate;
public:
Lex_exact_charset_opt_extended_collate(CHARSET_INFO *ci, bool with_collate)
:m_ci(ci), m_with_collate(with_collate)
{
DBUG_ASSERT(m_ci);
DBUG_ASSERT((m_ci->state & MY_CS_PRIMARY) || m_with_collate);
}
Lex_exact_charset_opt_extended_collate(const Lex_exact_charset &cs)
:m_ci(cs.charset_info()), m_with_collate(false)
{
DBUG_ASSERT(m_ci);
DBUG_ASSERT(m_ci->state & MY_CS_PRIMARY);
}
Lex_exact_charset_opt_extended_collate(const Lex_exact_collation &cl)
:m_ci(cl.charset_info()), m_with_collate(true)
{
DBUG_ASSERT(m_ci);
}
bool with_collate() const { return m_with_collate; }
CHARSET_INFO *find_bin_collation() const;
CHARSET_INFO *find_default_collation() const;
bool raise_if_not_applicable(const Lex_exact_collation &cl) const;
/*
Add another COLLATE clause (exact or context).
So the full syntax looks like:
CHARACTER SET cs [COLLATE cl] ... COLLATE cl2
*/
bool merge_collation(const Lex_extended_collation_st &cl)
{
switch (cl.type()) {
case Lex_extended_collation_st::TYPE_EXACT:
return merge_exact_collation(Lex_exact_collation(cl.charset_info()));
case Lex_extended_collation_st::TYPE_CONTEXTUALLY_TYPED:
return merge_context_collation(Lex_context_collation(cl.charset_info()));
}
DBUG_ASSERT(0);
return false;
}
/*
Add a context collation:
CHARACTER SET cs [COLLATE cl] ... COLLATE DEFAULT
*/
bool merge_context_collation(const Lex_context_collation &cl);
bool merge_context_collation_override(const Lex_context_collation &cl);
/*
Add an exact collation:
CHARACTER SET cs [COLLATE cl] ... COLLATE latin1_bin
*/
bool merge_exact_collation(const Lex_exact_collation &cl);
Lex_exact_collation collation() const
{
return Lex_exact_collation(m_ci);
}
Lex_exact_charset charset() const
{
if ((m_ci->state & MY_CS_PRIMARY))
return Lex_exact_charset(m_ci);
return Lex_exact_charset(find_default_collation());
}
};
/*
Parse time character set and collation for:
[CHARACTER SET cs_exact] [COLLATE cl_exact_or_context]
Can be:
1. Empty (not specified on the column level):
CREATE TABLE t1 (a CHAR(10)) CHARACTER SET latin2; -- (1a)
CREATE TABLE t1 (a CHAR(10)); -- (1b)
2. Precisely typed:
CREATE TABLE t1 (a CHAR(10) COLLATE latin1_bin); -- (2a)
CREATE TABLE t1 (
a CHAR(10) CHARACTER SET latin1 COLLATE latin1_bin); -- (2b)
3. Contextually typed:
CREATE TABLE t2 (a CHAR(10) BINARY) CHARACTER SET latin2; -- (3a)
CREATE TABLE t2 (a CHAR(10) BINARY); -- (3b)
CREATE TABLE t2 (a CHAR(10) COLLATE DEFAULT)
CHARACER SET latin2 COLLATE latin2_bin; -- (3c)
In case of an empty or a contextually typed collation,
it is a subject to later resolution, when the context
character set becomes known in the end of the CREATE statement:
- either after the explicit table level CHARACTER SET, like in (1a,3a,3c)
- or by the inhereted database level CHARACTER SET, like in (1b,3b)
Resolution happens in Type_handler::Column_definition_prepare_stage1().
*/
struct Lex_exact_charset_extended_collation_attrs_st
{
public:
enum Type
{
TYPE_EMPTY= 0,
TYPE_CHARACTER_SET= 1,
TYPE_COLLATE_EXACT= 2,
TYPE_COLLATE_CONTEXTUALLY_TYPED= 3
};
// Number of bits required to store enum Type values
#define LEX_CHARSET_COLLATION_TYPE_BITS 2
static_assert(((1<<LEX_CHARSET_COLLATION_TYPE_BITS)-1) >=
TYPE_COLLATE_CONTEXTUALLY_TYPED,
"Lex_exact_charset_extended_collation_attrs_st::Type bits");
protected:
CHARSET_INFO *m_ci;
Type m_type;
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
protected:
static Type type_from_lex_collation_type(Lex_extended_collation_st::Type type)
{
switch (type) {
case Lex_extended_collation_st::TYPE_EXACT:
return TYPE_COLLATE_EXACT;
case Lex_extended_collation_st::TYPE_CONTEXTUALLY_TYPED:
return TYPE_COLLATE_CONTEXTUALLY_TYPED;
}
DBUG_ASSERT(0);
return TYPE_COLLATE_EXACT;
}
public:
void init()
{
m_ci= NULL;
m_type= TYPE_EMPTY;
}
void init(CHARSET_INFO *cs, Type type)
{
DBUG_ASSERT(cs || type == TYPE_EMPTY);
m_ci= cs;
m_type= type;
}
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
void init(const Lex_exact_charset &cs)
{
m_ci= cs.charset_info();
m_type= TYPE_CHARACTER_SET;
}
void init(const Lex_exact_collation &cs)
{
m_ci= cs.charset_info();
m_type= TYPE_COLLATE_EXACT;
}
void init(const Lex_exact_charset_opt_extended_collate &cscl)
{
cscl.with_collate() ? init(cscl.collation()) :
init(cscl.charset());
}
bool is_empty() const
{
return m_type == TYPE_EMPTY;
}
void set_charset(CHARSET_INFO *cs)
{
DBUG_ASSERT(cs);
m_ci= cs;
m_type= TYPE_CHARACTER_SET;
}
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
bool set_charset_collate_default(CHARSET_INFO *cs)
{
DBUG_ASSERT(cs);
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
if (!(cs= Lex_exact_charset_opt_extended_collate(cs, true).
find_default_collation()))
return true;
m_ci= cs;
m_type= TYPE_COLLATE_EXACT;
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
return false;
}
bool set_charset_collate_binary(CHARSET_INFO *cs)
{
DBUG_ASSERT(cs);
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
if (!(cs= Lex_exact_charset_opt_extended_collate(cs, true).
find_bin_collation()))
return true;
m_ci= cs;
m_type= TYPE_COLLATE_EXACT;
return false;
}
void set_collate_default()
{
m_ci= &my_collation_contextually_typed_default;
m_type= TYPE_COLLATE_CONTEXTUALLY_TYPED;
}
void set_contextually_typed_binary_style()
{
m_ci= &my_collation_contextually_typed_binary;
m_type= TYPE_COLLATE_CONTEXTUALLY_TYPED;
}
bool is_contextually_typed_collate_default() const
{
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
return Lex_context_collation(m_ci).is_contextually_typed_collate_default();
}
CHARSET_INFO *charset_info() const
{
return m_ci;
}
Type type() const
{
return m_type;
}
bool is_contextually_typed_collation() const
{
return m_type == TYPE_COLLATE_CONTEXTUALLY_TYPED;
}
CHARSET_INFO *resolved_to_character_set(CHARSET_INFO *cs) const;
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
/*
Merge the column CHARACTER SET clause to:
- an exact collation name
- a contextually typed collation
"this" corresponds to `CHARACTER SET xxx [BINARY]`
"cl" corresponds to the COLLATE clause
*/
bool merge_column_charset_clause_and_collate_clause(
const Lex_exact_charset_extended_collation_attrs_st &cl)
{
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
switch (cl.type()) {
case TYPE_EMPTY:
return false;
case TYPE_COLLATE_EXACT:
return merge_exact_collation(Lex_exact_collation(cl.charset_info()));
case TYPE_COLLATE_CONTEXTUALLY_TYPED:
case TYPE_CHARACTER_SET:
break;
}
DBUG_ASSERT(0);
return false;
}
/*
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
This method is used in the "attribute_list" rule to merge two independent
COLLATE clauses (not belonging to a CHARACTER SET clause).
"BINARY" and "COLLATE DEFAULT" are not possible
in an independent COLLATE clause in a column attribute.
*/
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
bool merge_column_collate_clause_and_collate_clause(
const Lex_exact_charset_extended_collation_attrs_st &cl)
{
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
DBUG_ASSERT(m_type != TYPE_COLLATE_CONTEXTUALLY_TYPED);
DBUG_ASSERT(m_type != TYPE_CHARACTER_SET);
switch (cl.type()) {
case TYPE_EMPTY:
return false;
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
case TYPE_COLLATE_EXACT:
return merge_exact_collation(Lex_exact_collation(cl.charset_info()));
case TYPE_COLLATE_CONTEXTUALLY_TYPED:
case TYPE_CHARACTER_SET:
break;
}
DBUG_ASSERT(0);
return false;
}
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
bool merge_exact_charset(const Lex_exact_charset &cs);
bool merge_exact_collation(const Lex_exact_collation &cl);
bool merge_context_collation(const Lex_context_collation &cl);
bool merge_collation(const Lex_extended_collation_st &cl);
};
class Lex_exact_charset_extended_collation_attrs:
public Lex_exact_charset_extended_collation_attrs_st
{
public:
Lex_exact_charset_extended_collation_attrs()
{
init();
}
Lex_exact_charset_extended_collation_attrs(CHARSET_INFO *collation, Type type)
{
init(collation, type);
}
Step#3 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level Splitting Lex_exact_charset_extended_collation_attrs_st into small components. - Adding classes: * Lex_exact_charset * Lex_context_collation * Lex_exact_collation * Lex_extended_collation_st * Lex_extended_collation and moving pieces of the code from methods * merge_charset_clause_and_collate_clause() * merge_collate_clause_and_collate_clause() into smaller methods in the new classes. It's easier to read, handle and reuse the code this way. - Moving static methods find_default_collation() and find_binary_collation() from Lex_exact_charset_extended_collation_attrs_st to non-static methods in Lex_exact_charset_opt_extended_collate, as now it's a better place for them. - Using Lex_extended_collation_st in sql_yacc.yy to handle COLLATE clauses, to handle both context and extended collations (instead of the previous notation with NULL CHARSET_INFO pointer meaning DEFAULT, and not-NULL meaning an exact collation). This change will also help to add more context (UCA1400) collations soon. The old notation with CHARSET_INFO won't be enough. - Adding LEX::set_names() and reusing it in two places in sql_yacc.yy - Removing the opt_collate_or_default rule. It's was used only to handle the CONVERT TO related grammar. Had to add some code duplication, but it will be gone in one of the next commits. This change will also soon help to add Lex_extended_charset_extended_collation_attrs_st - a new class to handle table and database level CHARACTER SET and COLLATE clauses easier.
2022-05-23 09:05:33 +02:00
explicit
Lex_exact_charset_extended_collation_attrs(const Lex_exact_charset &cs)
{
init(cs.charset_info(), TYPE_CHARACTER_SET);
}
explicit
Lex_exact_charset_extended_collation_attrs(const Lex_exact_collation &cl)
{
init(cl.charset_info(), TYPE_COLLATE_EXACT);
}
explicit
Lex_exact_charset_extended_collation_attrs(const Lex_context_collation &cl)
{
init(cl.charset_info(), TYPE_COLLATE_CONTEXTUALLY_TYPED);
}
explicit
Lex_exact_charset_extended_collation_attrs(
const Lex_exact_charset_opt_extended_collate &cscl)
{
init(cscl);
}
explicit
Lex_exact_charset_extended_collation_attrs(const Lex_extended_collation_st &cl)
{
init(cl.charset_info(), type_from_lex_collation_type(cl.type()));
}
static Lex_exact_charset_extended_collation_attrs national(bool bin_mod)
{
return bin_mod ?
Lex_exact_charset_extended_collation_attrs(&my_charset_utf8mb3_bin,
TYPE_COLLATE_EXACT) :
Lex_exact_charset_extended_collation_attrs(&my_charset_utf8mb3_general_ci,
TYPE_CHARACTER_SET);
}
};
using Lex_column_charset_collation_attrs_st =
Lex_exact_charset_extended_collation_attrs_st;
using Lex_column_charset_collation_attrs =
Lex_exact_charset_extended_collation_attrs;
Step#1 MDEV-27896 Wrong result upon `COLLATE latin1_bin CHARACTER SET latin1` on the table or the database level - Adding data type aliases: using Lex_column_charset_collation_attrs_st = Lex_charset_collation_st; using Lex_column_charset_collation_attrs = Lex_charset_collation; and using them all around the code (except lex_charset.*) instead of the original names. - Renaming Lex_field_type_st::lex_charset_collation() to charset_collation_attrs() - Renaming Column_definition::set_lex_charset_collation() to set_charset_collation_attrs() - Renaming Column_definition::lex_charset_collation() to charset_collation_attrs() Rationale: The name "Lex_charset_collation" was a not very good name. It does not tell details about its properties: 1. if the charset is optional (yes) 2. if the collation is optional (yes) 3. if the charset can be exact (yes) or context (no) 4. if the collation can be: exact (yes) or context (yes) 5. if the clauses can be repeated multiple times (yes) We'll need a few new data types soon with different properties. For example, to fix MDEV-27896 and MDEV-27782, we'll need a new data type which is very like Lex_charset_collation, but additionally supports CHARACTER SET DEFAULT (which is allowed on table and database level, but is not allowed on the column level yet), i.e. with: "the charset can be exact (yes) or context (yes)" in N3. So we'll have to rename Lex_charset_collation to something else, e.g.: Lex_exact_charset_extended_collation_attrs, and add a new data type: e.g. Lex_extended_charset_extended_collation_attrs Also, we'll possibly allow CHARACTER SET DEFAULT at the column level for consistency with other places. So the storge on the column level can change: - from Lex_exact_charset_extended_collation_attrs - to Lex_extended_charset_extended_collation_attrs Adding the aliases introduces a convenient abstraction against upcoming renames and c++ data type changes.
2022-05-22 19:25:31 +02:00
#endif // LEX_CHARSET_INCLUDED