github.com/matrixorigin/matrixone@v1.2.0/pkg/frontend/predefined.go (about)

     1  // Copyright 2024 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package frontend
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/catalog"
    21  )
    22  
    23  var (
    24  	// the sqls creating many tables for the tenant.
    25  	// Wrap them in a transaction
    26  	MoCatalogMoUserDDL = `create table mo_catalog.mo_user (
    27  				user_id int signed auto_increment primary key,
    28  				user_host varchar(100),
    29  				user_name varchar(300) unique key,
    30  				authentication_string varchar(100),
    31  				status   varchar(8),
    32  				created_time  timestamp,
    33  				expired_time timestamp,
    34  				login_type  varchar(16),
    35  				creator int signed,
    36  				owner int signed,
    37  				default_role int signed
    38      		)`
    39  
    40  	MoCatalogMoAccountDDL = `create table mo_catalog.mo_account (
    41  				account_id int signed auto_increment primary key,
    42  				account_name varchar(300) unique key,
    43  				admin_name varchar(300),
    44  				status varchar(300),
    45  				created_time timestamp,
    46  				comments varchar(256),
    47  				version bigint unsigned auto_increment,
    48  				suspended_time timestamp default NULL,
    49  				create_version varchar(50) default '1.2.0'
    50  			)`
    51  
    52  	MoCatalogMoRoleDDL = `create table mo_catalog.mo_role (
    53  				role_id int signed auto_increment primary key,
    54  				role_name varchar(300) unique key,
    55  				creator int signed,
    56  				owner int signed,
    57  				created_time timestamp,
    58  				comments text
    59  			)`
    60  
    61  	MoCatalogMoUserGrantDDL = `create table mo_catalog.mo_user_grant (
    62  				role_id int signed,
    63  				user_id int signed,
    64  				granted_time timestamp,
    65  				with_grant_option bool,
    66  				primary key(role_id, user_id)
    67  			)`
    68  
    69  	MoCatalogMoRoleGrantDDL = `create table mo_catalog.mo_role_grant (
    70  				granted_id int signed,
    71  				grantee_id int signed,
    72  				operation_role_id int signed,
    73  				operation_user_id int signed,
    74  				granted_time timestamp,
    75  				with_grant_option bool,
    76  				primary key(granted_id, grantee_id)
    77  			)`
    78  
    79  	MoCatalogMoRolePrivsDDL = `create table mo_catalog.mo_role_privs (
    80  				role_id int signed,
    81  				role_name  varchar(100),
    82  				obj_type  varchar(16),
    83  				obj_id bigint unsigned,
    84  				privilege_id int,
    85  				privilege_name varchar(100),
    86  				privilege_level varchar(100),
    87  				operation_user_id int unsigned,
    88  				granted_time timestamp,
    89  				with_grant_option bool,
    90  				primary key(role_id, obj_type, obj_id, privilege_id, privilege_level)
    91  			)`
    92  
    93  	MoCatalogMoUserDefinedFunctionDDL = `create table mo_catalog.mo_user_defined_function (
    94  				function_id int auto_increment,
    95  				name     varchar(100) unique key,
    96  				owner  int unsigned,
    97  				args     json,
    98  				retType  varchar(20),
    99  				body     text,
   100  				language varchar(20),
   101  				db       varchar(100),
   102  				definer  varchar(50),
   103  				modified_time timestamp,
   104  				created_time  timestamp,
   105  				type    varchar(10),
   106  				security_type varchar(10),
   107  				comment  varchar(5000),
   108  				character_set_client varchar(64),
   109  				collation_connection varchar(64),
   110  				database_collation varchar(64),
   111  				primary key(function_id)
   112  			)`
   113  
   114  	MoCatalogMoMysqlCompatibilityModeDDL = `create table mo_catalog.mo_mysql_compatibility_mode (
   115  				configuration_id int auto_increment,
   116  				account_id int,
   117  				account_name varchar(300),
   118  				dat_name     varchar(5000) default NULL,
   119  				variable_name  varchar(300),
   120  				variable_value varchar(5000),
   121  				system_variables bool,
   122  				primary key(configuration_id)
   123  			)`
   124  
   125  	MoCatalogMoSnapshotsDDL = fmt.Sprintf(`CREATE TABLE %s.%s (
   126  			snapshot_id uuid unique key,
   127  			sname varchar(64) primary key,
   128  			ts bigint,
   129  			level enum('cluster','account','database','table'),
   130  	        account_name varchar(300),
   131  			database_name varchar(5000),
   132  			table_name  varchar(5000),
   133  			obj_id bigint unsigned
   134  			)`, catalog.MO_CATALOG, catalog.MO_SNAPSHOTS)
   135  
   136  	MoCatalogMoPubsDDL = `create table mo_catalog.mo_pubs (
   137      		pub_name varchar(64) primary key,
   138      		database_name varchar(5000),
   139      		database_id bigint unsigned,
   140      		all_table bool,
   141      		table_list text,
   142      		account_list text,
   143      		created_time timestamp,
   144      		update_time timestamp default NULL,
   145      		owner int unsigned,
   146      		creator int unsigned,
   147      		comment text
   148      		)`
   149  
   150  	MoCatalogMoStoredProcedureDDL = `create table mo_catalog.mo_stored_procedure (
   151  				proc_id int auto_increment,
   152  				name     varchar(100) unique key,
   153  				creator  int unsigned,
   154  				args     text,
   155  				body     text,
   156  				db       varchar(100),
   157  				definer  varchar(50),
   158  				modified_time timestamp,
   159  				created_time  timestamp,
   160  				type    varchar(10),
   161  				security_type varchar(10),
   162  				comment  varchar(5000),
   163  				character_set_client varchar(64),
   164  				collation_connection varchar(64),
   165  				database_collation varchar(64),
   166  				primary key(proc_id)
   167  			)`
   168  
   169  	MoCatalogMoStagesDDL = `create table mo_catalog.mo_stages (
   170  				stage_id int unsigned auto_increment,
   171  				stage_name varchar(64) unique key,
   172  				url text,
   173  				stage_credentials text,
   174  				stage_status varchar(64),
   175  				created_time timestamp,
   176  				comment text,
   177  				primary key(stage_id)
   178  			)`
   179  
   180  	MoCatalogMoSessionsDDL       = `CREATE VIEW mo_catalog.mo_sessions AS SELECT node_id, conn_id, session_id, account, user, host, db, session_start, command, info, txn_id, statement_id, statement_type, query_type, sql_source_type, query_start, client_host, role, proxy_host FROM mo_sessions() AS mo_sessions_tmp`
   181  	MoCatalogMoConfigurationsDDL = `CREATE VIEW mo_catalog.mo_configurations AS SELECT node_type, node_id, name, current_value, default_value, internal FROM mo_configurations() AS mo_configurations_tmp`
   182  	MoCatalogMoLocksDDL          = `CREATE VIEW mo_catalog.mo_locks AS SELECT cn_id, txn_id, table_id, lock_key, lock_content, lock_mode, lock_status, lock_wait FROM mo_locks() AS mo_locks_tmp`
   183  	MoCatalogMoVariablesDDL      = `CREATE VIEW mo_catalog.mo_variables AS SELECT configuration_id, account_id, account_name, dat_name, variable_name, variable_value, system_variables FROM mo_catalog.mo_mysql_compatibility_mode`
   184  	MoCatalogMoTransactionsDDL   = `CREATE VIEW mo_catalog.mo_transactions AS SELECT cn_id, txn_id, create_ts, snapshot_ts, prepared_ts, commit_ts, txn_mode, isolation, user_txn, txn_status, table_id, lock_key, lock_content, lock_mode FROM mo_transactions() AS mo_transactions_tmp`
   185  	MoCatalogMoCacheDDL          = `CREATE VIEW mo_catalog.mo_cache AS SELECT node_type, node_id, type, used, free, hit_ratio FROM mo_cache() AS mo_cache_tmp`
   186  )
   187  
   188  // `mo_catalog` database system tables
   189  // Note: The following tables belong to data dictionary table, and system tables's creation will depend on
   190  // the following system tables. Therefore, when creating tenants, they must be created first
   191  var (
   192  	MoCatalogMoAutoIncrTableDDL = fmt.Sprintf(`create table %s.%s (
   193  			table_id   bigint unsigned, 
   194  			col_name   varchar(770), 
   195  			col_index  int,
   196  			offset     bigint unsigned, 
   197  			step       bigint unsigned,  
   198  			primary key(table_id, col_name)
   199  		)`, catalog.MO_CATALOG, catalog.MOAutoIncrTable)
   200  
   201  	MoCatalogMoIndexesDDL = fmt.Sprintf(`create table %s.%s (
   202  			id 			bigint unsigned not null,
   203  			table_id 	bigint unsigned not null,
   204  			database_id bigint unsigned not null,
   205  			name 		varchar(64) not null,
   206  			type        varchar(11) not null,
   207      		algo	varchar(11),
   208      		algo_table_type varchar(11),
   209  			algo_params varchar(2048),
   210  			is_visible  tinyint not null,
   211  			hidden      tinyint not null,
   212  			comment 	varchar(2048) not null,
   213  			column_name    varchar(256) not null,
   214  			ordinal_position  int unsigned  not null,
   215  			options     text,
   216  			index_table_name varchar(5000),
   217  			primary key(id, column_name)
   218  		)`, catalog.MO_CATALOG, catalog.MO_INDEXES)
   219  
   220  	MoCatalogMoForeignKeysDDL = fmt.Sprintf(`create table %s.%s (
   221  			constraint_name varchar(5000) not null,
   222  			constraint_id BIGINT UNSIGNED not null default 0,
   223  			db_name varchar(5000) not null,
   224  			db_id BIGINT UNSIGNED not null default 0,
   225  			table_name varchar(5000) not null,
   226  			table_id BIGINT UNSIGNED not null default 0,
   227  			column_name varchar(256) not null,
   228  			column_id BIGINT UNSIGNED not null default 0,
   229  			refer_db_name varchar(5000) not null,
   230  			refer_db_id BIGINT UNSIGNED not null default 0,
   231  			refer_table_name varchar(5000) not null,
   232  			refer_table_id BIGINT UNSIGNED not null default 0,
   233  			refer_column_name varchar(256) not null,
   234  			refer_column_id BIGINT UNSIGNED not null default 0,
   235  			on_delete varchar(128) not null,
   236  			on_update varchar(128) not null,
   237  	
   238  			primary key(
   239  				constraint_name,
   240  				constraint_id,
   241  				db_name,
   242  				db_id,
   243  				table_name,
   244  				table_id,
   245  				column_name,
   246  				column_id,
   247  				refer_db_name,
   248  				refer_db_id,
   249  				refer_table_name,
   250  				refer_table_id,
   251  				refer_column_name,
   252  				refer_column_id)
   253  		)`, catalog.MO_CATALOG, catalog.MOForeignKeys)
   254  
   255  	MoCatalogMoTablePartitionsDDL = fmt.Sprintf(`CREATE TABLE %s.%s (
   256  			  table_id bigint unsigned NOT NULL,
   257  			  database_id bigint unsigned not null,
   258  			  number smallint unsigned NOT NULL,
   259  			  name varchar(64) NOT NULL,
   260      		  partition_type varchar(50) NOT NULL,
   261                partition_expression varchar(2048) NULL,
   262  			  description_utf8 text,
   263  			  comment varchar(2048) NOT NULL,
   264  			  options text,
   265  			  partition_table_name varchar(1024) NOT NULL,
   266      		  PRIMARY KEY table_id (table_id, name)
   267  			)`, catalog.MO_CATALOG, catalog.MO_TABLE_PARTITIONS)
   268  )
   269  
   270  // step3InitSQLs
   271  // `mo_catalog` database system tables
   272  // They are all Cluster level system tables for system upgrades
   273  var (
   274  	MoCatalogMoVersionDDL = fmt.Sprintf(`create table %s.%s (
   275  			version             varchar(50) not null,
   276  		    version_offset      int unsigned default 0,
   277  			state               int,
   278  			create_at           timestamp not null,
   279  			update_at           timestamp not null,
   280  			primary key(version, version_offset)
   281  		)`, catalog.MO_CATALOG, catalog.MOVersionTable)
   282  
   283  	MoCatalogMoUpgradeDDL = fmt.Sprintf(`create table %s.%s (
   284  			id                   bigint unsigned not null primary key auto_increment,
   285  			from_version         varchar(50) not null,
   286  			to_version           varchar(50) not null,
   287  			final_version        varchar(50) not null,
   288              final_version_offset int unsigned default 0,
   289  			state                int,
   290  			upgrade_cluster      int,
   291  			upgrade_tenant       int,
   292  			upgrade_order        int,
   293  			total_tenant         int,
   294  			ready_tenant         int,
   295  			create_at            timestamp not null,
   296  			update_at            timestamp not null
   297  		)`, catalog.MO_CATALOG, catalog.MOUpgradeTable)
   298  
   299  	MoCatalogMoUpgradeTenantDDL = fmt.Sprintf(`create table %s.%s (
   300  			id                  bigint unsigned not null primary key auto_increment,
   301  			upgrade_id		    bigint unsigned not null,
   302  			target_version      varchar(50) not null,
   303  			from_account_id     int not null,
   304  			to_account_id       int not null,
   305  			ready               int,
   306  			create_at           timestamp not null,
   307  			update_at           timestamp not null
   308  		)`, catalog.MO_CATALOG, catalog.MOUpgradeTenantTable)
   309  )
   310  
   311  // ----------------------------------------------------------------------------------------------------------------------
   312  // step2InitSQLs
   313  // `mo_task` database system tables
   314  // They are all Cluster level system tables
   315  var (
   316  	MoTaskSysAsyncTaskDDL = fmt.Sprintf(`create table %s.sys_async_task (
   317  			task_id                     bigint primary key auto_increment,
   318  			task_metadata_id            varchar(50) unique not null,
   319  			task_metadata_executor      int,
   320  			task_metadata_context       blob,
   321  			task_metadata_option        varchar(1000),
   322  			task_parent_id              varchar(50),
   323  			task_status                 int,
   324  			task_runner                 varchar(50),
   325  			task_epoch                  int,
   326  			last_heartbeat              bigint,
   327  			result_code                 int null,
   328  			error_msg                   varchar(1000) null,
   329  			create_at                   bigint,
   330  			end_at                      bigint)`,
   331  		catalog.MOTaskDB)
   332  
   333  	MoTaskSysCronTaskDDL = fmt.Sprintf(`create table %s.sys_cron_task (
   334  			cron_task_id				bigint primary key auto_increment,
   335      		task_metadata_id            varchar(50) unique not null,
   336  			task_metadata_executor      int,
   337  			task_metadata_context       blob,
   338  			task_metadata_option 		varchar(1000),
   339  			cron_expr					varchar(100) not null,
   340  			next_time					bigint,
   341  			trigger_times				int,
   342  			create_at					bigint,
   343  			update_at					bigint)`,
   344  		catalog.MOTaskDB)
   345  
   346  	MoTaskSysDaemonTaskDDL = fmt.Sprintf(`create table %s.sys_daemon_task (
   347  			task_id                     bigint primary key auto_increment,
   348  			task_metadata_id            varchar(50),
   349  			task_metadata_executor      int,
   350  			task_metadata_context       blob,
   351  			task_metadata_option        varchar(1000),
   352  			account_id                  int unsigned not null,
   353  			account                     varchar(128) not null,
   354  			task_type                   varchar(64) not null,
   355  			task_runner                 varchar(64),
   356  			task_status                 int not null,
   357  			last_heartbeat              timestamp,
   358  			create_at                   timestamp not null,
   359  			update_at                   timestamp not null,
   360  			end_at                      timestamp,
   361  			last_run                    timestamp,
   362  			details                     blob)`,
   363  		catalog.MOTaskDB)
   364  )