ID | IEP-134 |
Author | |
Sponsor | |
Created |
|
Status | DRAFT |
SQL-schema is defined in SQL standard as a named collection of descriptors.
Simple name is the name of a schema object. Different schemas may contain different objects with the same simple names. The simple name always requires a schema name to resolve the named object.
Qualified name is a name, which contains an object name and a schema name the object belongs to. In contrast to a simple name, a qualified name allows one to resolve the named object unambiguously.
SQL-schema support is a mandatory feature for standard conformance. Now, the only hardcoded "PUBLIC" is available for storing user objects in catalog.
User schemas can be created via SQL-commands described below. But there are predefined names, which are reserved for system schemas and default user schema, which is created during grid initialization.
The reserved names for system schemas are:
Name PUBLIC - is not a system or reserved name, but used as default schema name for simple names resolution. In some cases, the default schema can be overridden (see below).
A schema with name PUBLIC - is a predefined schema for holding user objects, which is created automatically at cluster initialization, and can be renamed or dropped later.
Default schema name can be overridden, when running a query from thin clients, JDBC/ODBC clients, or using SQL Statement object. For these cases, we can pass a defaultSchema option to the SQL execution engine via connection property, or SQL statement property.
For ThinClient and embedded cases there are no such option to specify default schema, because there is no connection/session or any other context, where default schema could be defined/overridden. A hardcoded name PUBLIC shall be used in these cases.
If PUBLIC schema was dropped or renamed, and an API call (ThinClient or embedded), which can be SQL query execution or getting table, implies a default schema is required, then such call will fail with an error due to absence of hardcoded PUBLIC schema.
CREATE SCHEMA [IF NOT EXISTS] <schema_name_identifier>;
The <schema name identifier> is a simple name and must not clash with system schema name or any other existing schema name. Schemas can’t be nested.
Create-schema command syntax can be optionally extended as.
CREATE SCHEMA [IF NOT EXISTS] <schema_name_identifier> [<schema_element> .... ]; // Schema element as DDL definition of any of supported objects <schema_element> ::= <table_definition> | <index_definition> ...
The extended syntax looks useful for initial schema creation as a single `atomic` command.
DROP SCHEMA [IF EXISTS] <schema_name_identifier > [CASCADE | RESTRICT];
CASCADE - drop behavior that forces dropping all its objects together with the schema at once.
RESTRICT
- drop behavior that drops schema if empty or fails otherwise.
Default behavior: RESTRICT
.
Let’s introduce a QualifiedName class, which represents a named schema object, and provides factory methods for better UX and implements parsing rules for object’s names in a single place.
/** Class represents a table name and contains factory methods. */ final class QualifiedName { /** Parses simple or canonical name, and returns an object, which represents qualified name. */ static QualifiedName parseof(String name); /** Normalize schemaName and objectName and returns an object, which represents qualified name.*/ static QualifiedName of(String schemaName, String objectName); /** Normalizes given simple name and resolves it against the default schema. */ static QualifiedName fromSimple(String simpleName) { // Effectively the same as next. return of(DEFAULT_SCHEMA, simpleName); } /** Returns normalized schema name. **/ String schemaName(); /** Returns normalized schema's object name. **/ String objectName(); /** Returns object name in canonical form: {@code [<schema_name>.]<object_name>} **/ String toCanonicalForm(); }
The object contains and provides method for accessing normalized schema and normalized object names.
A method for converting back to string of canonical form should respect SQL syntax rules: quote name only when needed and escape double quotes correctly.
The class provides static factory methods for creating QualifiedName instance and respects SQL identifier syntax rules:
Table API consistency
Extend IgniteTable API methods to accept a schema name.
interface IgniteTables { /** Gets a list of all tables */ List<Table> tables(); /** Gets a table with specified name. */ @Nullable Table table(QualifiedName tableName); @Nullable Table table(String tableName) { return table(QualifiedName.parse(tableName)); } /* ... + async methods ... */ }
The methods, which accept String parameters, are shortcuts to the new ones.
Table names should follow the SQL-parser notation.
Extend JobTarget.colocate() methods to accept qualified table names.
interface JobTarget { static JobTarget tableColocated(String tableName, Tuple key) static JobTarget tableColocated(QualifiedName tableName, Tuple key) static <K> JobTarget tableColocated(String tableName, K key, Mapper<K> keyMapper) static <K> JobTarget tableColocated(QualifiedName tableName, K key, Mapper<K> keyMapper) }
The methods, which accept String parameters, are shortcuts to the new ones.
Table names should follow the SQL-parser notation.
Extend IgniteCatalog interface with methods for creating/dropping schemas, and fix table methods signatures, which require table name.
interface IgniteCatalog { void createSchema(SchemaDefinition definition); void dropSchema(String schema, bool cascade=false); SchemaDefinition schemaDefinition(String schemaName); TableDefinition tableDefinition(QualifiedName tableName); TableDefinition tableDefinition(String tableName); void dropTable(QualifiedName tableName); void dropTable(String tableName); } class SchemaDefinition { String schemaName; }
The methods, which accept String parameters, are shortcuts to the new ones.
Table names should follow the SQL-parser notation.
// Links to discussions on the devlist, if applicable.
// Links to various reference documents, if applicable.