Skip to content

Commit

Permalink
Add DB_METADATA to INFORMATION_SCHEMA.SCHEMATA
Browse files Browse the repository at this point in the history
Summary: Add DB_METADATA to INFORMATION_SCHEMA.SCHEMATA for each database for easy query by OLM

Test Plan:
1. Test manually by inspecting SCHEMATA

Result:
https://phabricator.intern.facebook.com/P60466869

2. Update test case (TODO)

Reviewers: herman, lcai

Reviewed By: herman

Subscribers: butterflybot, webscalesql-eng@fb.com

Differential Revision: https://phabricator.intern.facebook.com/D13510326

Tasks: T37428915

Tags: bootcamp

Signature: 13510326:1546451545:62ec37297de59dde87229dfa922570fa510c45e3
  • Loading branch information
yizhang82 committed Jan 3, 2019
1 parent 2134da9 commit 64bf72c
Show file tree
Hide file tree
Showing 11 changed files with 473 additions and 15 deletions.
328 changes: 328 additions & 0 deletions mysql-test/r/db_metadata.result

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions mysql-test/r/information_schema-big.result
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ USER_STATISTICS USER_NAME
VIEWS TABLE_SCHEMA
AUTHINFO ID
SLAVE_DB_LOAD DB
SCHEMATA_EXT SCHEMA_NAME
SELECT t.table_name, c1.column_name
FROM information_schema.tables t
INNER JOIN
Expand Down Expand Up @@ -129,3 +130,4 @@ USER_STATISTICS USER_NAME
VIEWS TABLE_SCHEMA
AUTHINFO ID
SLAVE_DB_LOAD DB
SCHEMATA_EXT SCHEMA_NAME
4 changes: 3 additions & 1 deletion mysql-test/r/information_schema.result
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ USER_STATISTICS
VIEWS
AUTHINFO
SLAVE_DB_LOAD
SCHEMATA_EXT
columns_priv
db
event
Expand Down Expand Up @@ -939,7 +940,7 @@ table_schema IN ('mysql', 'INFORMATION_SCHEMA', 'test', 'mysqltest')
AND table_name not like 'ndb%' AND table_name not like 'innodb_%' AND table_name not like 'rocksdb_%'
GROUP BY TABLE_SCHEMA;
table_schema count(*)
information_schema 45
information_schema 46
mysql 27
create table t1 (i int, j int);
create trigger trg1 before insert on t1 for each row
Expand Down Expand Up @@ -1407,6 +1408,7 @@ QUERY_PERF_COUNTER information_schema.QUERY_PERF_COUNTER 1
REFERENTIAL_CONSTRAINTS information_schema.REFERENTIAL_CONSTRAINTS 1
ROUTINES information_schema.ROUTINES 1
SCHEMATA information_schema.SCHEMATA 1
SCHEMATA_EXT information_schema.SCHEMATA_EXT 1
SCHEMA_PRIVILEGES information_schema.SCHEMA_PRIVILEGES 1
SESSION_STATUS information_schema.SESSION_STATUS 1
SESSION_VARIABLES information_schema.SESSION_VARIABLES 1
Expand Down
1 change: 1 addition & 0 deletions mysql-test/r/information_schema_db.result
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ USER_STATISTICS
VIEWS
AUTHINFO
SLAVE_DB_LOAD
SCHEMATA_EXT
show tables from INFORMATION_SCHEMA like 'T%';
Tables_in_information_schema (T%)
TRANSACTION_LIST
Expand Down
2 changes: 2 additions & 0 deletions mysql-test/r/mysqlshow.result
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ DROP TABLE t1, t2;
| ROCKSDB_TRX |
| ROUTINES |
| SCHEMATA |
| SCHEMATA_EXT |
| SCHEMA_PRIVILEGES |
| SESSION_STATUS |
| SESSION_VARIABLES |
Expand Down Expand Up @@ -234,6 +235,7 @@ DROP TABLE t1, t2;
| ROCKSDB_TRX |
| ROUTINES |
| SCHEMATA |
| SCHEMATA_EXT |
| SCHEMA_PRIVILEGES |
| SESSION_STATUS |
| SESSION_VARIABLES |
Expand Down
6 changes: 6 additions & 0 deletions mysql-test/suite/funcs_1/r/is_columns_is.result
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ def information_schema SCHEMATA DEFAULT_CHARACTER_SET_NAME 3 NO varchar 32 96 N
def information_schema SCHEMATA DEFAULT_COLLATION_NAME 4 NO varchar 32 96 NULL NULL NULL utf8 utf8_general_ci varchar(32) select
def information_schema SCHEMATA SCHEMA_NAME 2 NO varchar 64 192 NULL NULL NULL utf8 utf8_general_ci varchar(64) select
def information_schema SCHEMATA SQL_PATH 5 NULL YES varchar 512 1536 NULL NULL NULL utf8 utf8_general_ci varchar(512) select
def information_schema SCHEMATA_EXT CATALOG_NAME 1 NO varchar 512 1536 NULL NULL NULL utf8 utf8_general_ci varchar(512) select
def information_schema SCHEMATA_EXT DB_METADATA 3 NO varchar 1024 3072 NULL NULL NULL utf8 utf8_general_ci varchar(1024) select
def information_schema SCHEMATA_EXT SCHEMA_NAME 2 NO varchar 64 192 NULL NULL NULL utf8 utf8_general_ci varchar(64) select
def information_schema SCHEMA_PRIVILEGES GRANTEE 1 NO varchar 81 243 NULL NULL NULL utf8 utf8_general_ci varchar(81) select
def information_schema SCHEMA_PRIVILEGES IS_GRANTABLE 5 NO varchar 3 9 NULL NULL NULL utf8 utf8_general_ci varchar(3) select
def information_schema SCHEMA_PRIVILEGES PRIVILEGE_TYPE 4 NO varchar 64 192 NULL NULL NULL utf8 utf8_general_ci varchar(64) select
Expand Down Expand Up @@ -918,6 +921,9 @@ NULL information_schema ROUTINES LAST_ALTERED datetime NULL NULL NULL NULL datet
3.0000 information_schema SCHEMATA DEFAULT_CHARACTER_SET_NAME varchar 32 96 utf8 utf8_general_ci varchar(32)
3.0000 information_schema SCHEMATA DEFAULT_COLLATION_NAME varchar 32 96 utf8 utf8_general_ci varchar(32)
3.0000 information_schema SCHEMATA SQL_PATH varchar 512 1536 utf8 utf8_general_ci varchar(512)
3.0000 information_schema SCHEMATA_EXT CATALOG_NAME varchar 512 1536 utf8 utf8_general_ci varchar(512)
3.0000 information_schema SCHEMATA_EXT SCHEMA_NAME varchar 64 192 utf8 utf8_general_ci varchar(64)
3.0000 information_schema SCHEMATA_EXT DB_METADATA varchar 1024 3072 utf8 utf8_general_ci varchar(1024)
3.0000 information_schema SCHEMA_PRIVILEGES GRANTEE varchar 81 243 utf8 utf8_general_ci varchar(81)
3.0000 information_schema SCHEMA_PRIVILEGES TABLE_CATALOG varchar 512 1536 utf8 utf8_general_ci varchar(512)
3.0000 information_schema SCHEMA_PRIVILEGES TABLE_SCHEMA varchar 64 192 utf8 utf8_general_ci varchar(64)
Expand Down
46 changes: 46 additions & 0 deletions mysql-test/suite/funcs_1/r/is_tables_is.result
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,29 @@ user_comment
Separator -----------------------------------------------------
TABLE_CATALOG def
TABLE_SCHEMA information_schema
TABLE_NAME SCHEMATA_EXT
TABLE_TYPE SYSTEM VIEW
ENGINE MEMORY
VERSION 10
ROW_FORMAT Fixed
TABLE_ROWS #TBLR#
AVG_ROW_LENGTH #ARL#
DATA_LENGTH #DL#
MAX_DATA_LENGTH #MDL#
INDEX_LENGTH #IL#
DATA_FREE #DF#
AUTO_INCREMENT NULL
CREATE_TIME #CRT#
UPDATE_TIME #UT#
CHECK_TIME #CT#
TABLE_COLLATION utf8_general_ci
CHECKSUM NULL
CREATE_OPTIONS #CO#
TABLE_COMMENT #TC#
user_comment
Separator -----------------------------------------------------
TABLE_CATALOG def
TABLE_SCHEMA information_schema
TABLE_NAME SCHEMA_PRIVILEGES
TABLE_TYPE SYSTEM VIEW
ENGINE MEMORY
Expand Down Expand Up @@ -1665,6 +1688,29 @@ user_comment
Separator -----------------------------------------------------
TABLE_CATALOG def
TABLE_SCHEMA information_schema
TABLE_NAME SCHEMATA_EXT
TABLE_TYPE SYSTEM VIEW
ENGINE MEMORY
VERSION 10
ROW_FORMAT Fixed
TABLE_ROWS #TBLR#
AVG_ROW_LENGTH #ARL#
DATA_LENGTH #DL#
MAX_DATA_LENGTH #MDL#
INDEX_LENGTH #IL#
DATA_FREE #DF#
AUTO_INCREMENT NULL
CREATE_TIME #CRT#
UPDATE_TIME #UT#
CHECK_TIME #CT#
TABLE_COLLATION utf8_general_ci
CHECKSUM NULL
CREATE_OPTIONS #CO#
TABLE_COMMENT #TC#
user_comment
Separator -----------------------------------------------------
TABLE_CATALOG def
TABLE_SCHEMA information_schema
TABLE_NAME SCHEMA_PRIVILEGES
TABLE_TYPE SYSTEM VIEW
ENGINE MEMORY
Expand Down
21 changes: 21 additions & 0 deletions mysql-test/t/db_metadata.test
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,49 @@ show create database test4;
create database test5 db_metadata = "{\"shard\":\"test5_shard\"}";
show create database test5;
--exec cat $MYSQLD_DATADIR/test5/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# verify show create is valid
--let $db_create=query_get_value(show create database test5, 'Create Database', 1)
drop database test5;
eval $db_create;
show create database test5;
--exec cat $MYSQLD_DATADIR/test5/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# create database with character set and db metadata
create database test6 character set utf8 db_metadata = "{\"shard\":\"test6_shard\"}";
show create database test6;
--exec cat $MYSQLD_DATADIR/test6/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# create database with read only and db metadata
create database test7 read_only = true db_metadata = "{\"shard\":\"test7_shard\"}";
show create database test7;
--exec cat $MYSQLD_DATADIR/test7/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# create database with character set and read only
create database test8 character set utf8 read_only = true;
show create database test8;
--exec cat $MYSQLD_DATADIR/test8/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# create database with character set, read only and db metadata
create database test9 character set utf8 read_only = true db_metadata = "{\"shard\":\"test9_shard\"}";
show create database test9;
--exec cat $MYSQLD_DATADIR/test9/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# create database with invalid JSON string for db_metadata
--error ER_DB_METADATA_INVALID_JSON
create database test10 db_metadata = "invalid_json";
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# create database with single quotation
--error ER_DB_METADATA_INVALID_JSON
create database test11 db_metadata = "{\'shard\':\'test11_shard\'}";
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# create database with double quote around metadata with single and double quotes in value
create database test12 db_metadata = "{\"sha'rd\":\"test12\\\"_shard\"}";
Expand Down Expand Up @@ -107,6 +115,7 @@ show create database test3;
alter database test3 db_metadata = "{\"shard\":\"test3_shard_altered\"}";
show create database test3;
--exec cat $MYSQLD_DATADIR/test3/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# alter database read only
alter database test4 read_only = true;
Expand All @@ -117,6 +126,7 @@ show create database test4;
alter database test4 db_metadata = "{\"shard\":\"test4_shard_altered\"}";
show create database test4;
--exec cat $MYSQLD_DATADIR/test4/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# alter database read only
alter database test4 read_only = false;
Expand All @@ -127,6 +137,7 @@ show create database test4;
alter database test5 db_metadata = "{\"shard\":\"test5_shard_altered\"}";
show create database test5;
--exec cat $MYSQLD_DATADIR/test5/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# alter database character set but keep db metadata intact
alter database test5 character set ascii;
Expand All @@ -147,11 +158,13 @@ show create database test5;
alter database test5 db_metadata "";
show create database test5;
--exec cat $MYSQLD_DATADIR/test5/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# alter database set db metadata to max length string
alter database test5 db_metadata "{\"shard\":\"Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Fin\"}";
show create database test5;
--exec cat $MYSQLD_DATADIR/test5/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# verify show create is valid
--let $db_create=query_get_value(show create database test5, 'Create Database', 1)
Expand All @@ -163,36 +176,43 @@ show create database test5;
# alter database exceed db metadata max length
--error ER_DB_METADATA_TOO_LONG
alter database test5 db_metadata "{\"shard\":\"Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Fin\"}";
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# alter database character set and db metadata
alter database test6 character set ascii db_metadata = "{\"shard\":\"test6_shard_altered\"}";
show create database test6;
--exec cat $MYSQLD_DATADIR/test6/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# alter database read only and db metadata
alter database test7 read_only = true db_metadata = "{\"shard\":\"test7_shard_altered\"}";
show create database test7;
--exec cat $MYSQLD_DATADIR/test7/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# alter database character set and read only
alter database test8 character set ascii read_only = true;
show create database test8;
--exec cat $MYSQLD_DATADIR/test8/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# alter database character set, read only and db metadata
alter database test9 character set ascii read_only = true db_metadata = "{\"shard\":\"test9_shard_altered\"}";
show create database test9;
--exec cat $MYSQLD_DATADIR/test9/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# ensure information_schema and mysql tables are intact
show create database information_schema;
show create database mysql;
--error ER_DBACCESS_DENIED_ERROR
alter database information_schema db_metadata "{\"shard\":\"is_shard\"}";
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# alter database with invalid JSON string for db_metadata
--error ER_DB_METADATA_INVALID_JSON
alter database test9 db_metadata = "invalid_json";
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# create database without any options
create database test10;
Expand All @@ -208,6 +228,7 @@ show create database test10;
alter database test10 db_metadata = "{\"shard\":\"test10_shard_altered\"}";
show create database test10;
--exec cat $MYSQLD_DATADIR/test10/db.opt
select catalog_name, schema_name, db_metadata from information_schema.schemata_ext;

# alter database read only
alter database test10 read_only = false;
Expand Down
19 changes: 15 additions & 4 deletions sql/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1131,15 +1131,14 @@ enum enum_db_read_only { DB_READ_ONLY_NO = 0,
DB_READ_ONLY_SUPER = 2,
DB_READ_ONLY_NULL = 255 };

typedef struct st_ha_create_information
struct st_ha_create_information_base
{
const CHARSET_INFO *table_charset, *default_table_charset;
bool alter_default_table_charset; /* This is to differentiate whether the
null value for default_table_charset
means it is explicitly set to default or
it is not specified in alter */
enum enum_db_read_only db_read_only;
String db_metadata;
bool alter_db_metadata; /* This is to differentiate whether the null value
for db_metadata means it is explicitly set to
default or it is not specified in alter */
Expand All @@ -1157,7 +1156,6 @@ typedef struct st_ha_create_information
uint stats_sample_pages; /* number of pages to sample during
stats estimation, if used, otherwise 0. */
enum_stats_auto_recalc stats_auto_recalc;
SQL_I_List<TABLE_LIST> merge_list;
handlerton *db_type;
/**
Row type of the table definition.
Expand All @@ -1177,9 +1175,22 @@ typedef struct st_ha_create_information
enum ha_storage_media storage_media; /* DEFAULT, DISK or MEMORY */
bool rbr_column_names; /* If true, column names for this table are logged
in Table_map_log_events */
};

typedef struct st_ha_create_information : public st_ha_create_information_base
{
String db_metadata;
SQL_I_List<TABLE_LIST> merge_list;

/* initialize db_read_only parameter */
st_ha_create_information() : db_read_only(DB_READ_ONLY_NO) {}
st_ha_create_information()
{
// memset the base struct with POD fields
memset(
(st_ha_create_information_base *)this,
0,
sizeof(st_ha_create_information_base));
}
} HA_CREATE_INFO;

/**
Expand Down
1 change: 0 additions & 1 deletion sql/sql_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,6 @@ bool load_db_opt(THD *thd, const char *path, HA_CREATE_INFO *create,
bool error=1;
uint nbytes;

memset(create, 0, sizeof(*create));
create->default_table_charset= thd->variables.collation_server;

/* Check if options for this database are already in the hash */
Expand Down
Loading

0 comments on commit 64bf72c

Please sign in to comment.