================================================================================
Create table
================================================================================

CREATE TABLE my_schema.my_table (id BIGINT NOT NULL PRIMARY KEY);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (table_reference
        schema: (identifier)
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint))
          (keyword_not)
          (keyword_null)
          (keyword_primary)
          (keyword_key))))))

================================================================================
Create table multiple columns
================================================================================

CREATE TABLE my_table (
  id BIGINT NOT NULL PRIMARY KEY,
  date DATE DEFAULT NULL ASC
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint))
          (keyword_not)
          (keyword_null)
          (keyword_primary)
          (keyword_key))
        (column_definition
          name: (identifier)
          type: (keyword_date)
          (keyword_default)
          (literal
            (keyword_null))
          (direction
            (keyword_asc)))))))

================================================================================
Create temp table
================================================================================

CREATE TEMP TABLE my_table (id BIGINT NOT NULL PRIMARY KEY);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_temp)
      (keyword_table)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint))
          (keyword_not)
          (keyword_null)
          (keyword_primary)
          (keyword_key))))))

================================================================================
Create table with constraint
================================================================================

CREATE TABLE my_table (
  host CHAR(50) NOT NULL,
  created_date DATE NOT NULL,
  CONSTRAINT pk PRIMARY KEY (host ASC, created_date DESC)
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (char
            (keyword_char)
            size: (literal))
          (keyword_not)
          (keyword_null))
        (column_definition
          name: (identifier)
          type: (keyword_date)
          (keyword_not)
          (keyword_null))
        (constraints
          (constraint
            (keyword_constraint)
            name: (identifier)
            (keyword_primary)
            (keyword_key)
            (ordered_columns
              (column
                name: (identifier)
                (direction
                  (keyword_asc)))
              (column
                name: (identifier)
                (direction
                  (keyword_desc))))))))))

================================================================================
Create table with constraint on multiple columns
================================================================================

CREATE TABLE my_table (
  host CHAR(50) NOT NULL,
  created_date DATE NOT NULL,
  random FLOAT DEFAULT RAND(),
  random TEXT DEFAULT 'test',
  KEY `idx` (`host`, `created_date`)
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (char
            (keyword_char)
            size: (literal))
          (keyword_not)
          (keyword_null))
        (column_definition
          name: (identifier)
          type: (keyword_date)
          (keyword_not)
          (keyword_null))
        (column_definition
          name: (identifier)
          type: (float
            (keyword_float))
          (keyword_default)
          (invocation
            name: (identifier)))
        (column_definition
          name: (identifier)
          type: (keyword_text)
          (keyword_default)
          (literal))
        (constraints
          (constraint
            (keyword_key)
            name: (identifier)
            (ordered_columns
              (column
                name: (identifier))
              (column
                name: (identifier)))))))))

================================================================================
Create table if not exists
================================================================================

CREATE TABLE IF NOT EXISTS `addresses` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (keyword_if)
      (keyword_not)
      (keyword_exists)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)
            size: (literal))
          (keyword_not)
          (keyword_null)
          (keyword_auto_increment))))))

================================================================================
Create table with unsigned zerofill types
================================================================================

CREATE TABLE `addresses` (
  `id` bigint(20) UNSIGNED ZEROFILL,
  `id2` int4(20) ZEROFILL,
  `id3` UNSIGNED tinyint(20),
  `id4` mediumint(20) UNSIGNED,
  `id5` float(20, 3) UNSIGNED,
  `id6` double(20,3) UNSIGNED
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)
            size: (literal)
            (keyword_unsigned)
            (keyword_zerofill)))
        (column_definition
          name: (identifier)
          type: (int
            (keyword_int)
            size: (literal)
            (keyword_zerofill)))
        (column_definition
          name: (identifier)
          type: (tinyint
            (keyword_unsigned)
            (keyword_tinyint)
            size: (literal)))
        (column_definition
          name: (identifier)
          type: (mediumint
            (keyword_mediumint)
            size: (literal)
            (keyword_unsigned)))
        (column_definition
          name: (identifier)
          type: (float
            (keyword_float)
            precision: (literal)
            scale: (literal)
            (keyword_unsigned)))
        (column_definition
          name: (identifier)
          type: (double
            (keyword_double)
            precision: (literal)
            scale: (literal)
            (keyword_unsigned)))))))

================================================================================
Create table with auto increment
================================================================================

CREATE TABLE IF NOT EXISTS `addresses` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`),
  KEY `addresses_s_id_index` (`s_id`)
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (keyword_if)
      (keyword_not)
      (keyword_exists)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)
            size: (literal))
          (keyword_not)
          (keyword_null)
          (keyword_auto_increment))
        (constraints
          (constraint
            (keyword_primary)
            (keyword_key)
            (ordered_columns
              (column
                name: (identifier))))
          (constraint
            (keyword_key)
            name: (identifier)
            (ordered_columns
              (column
                name: (identifier)))))))))

================================================================================
Create table with multiple constraints
================================================================================

CREATE TABLE IF NOT EXISTS `addresses` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `s_id` bigint(20) DEFAULT NULL,
  `first_name` varchar(255) DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `addresses_s_id_index` (`s_id`),
  KEY `index_addresses_on_updated_at` (`updated_at`),
  KEY `index_addresses_on_s_id_and_id` (`s_id`, `id`)
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (keyword_if)
      (keyword_not)
      (keyword_exists)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)
            size: (literal))
          (keyword_not)
          (keyword_null)
          (keyword_auto_increment))
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)
            size: (literal))
          (keyword_default)
          (literal
            (keyword_null)))
        (column_definition
          name: (identifier)
          type: (varchar
            (keyword_varchar)
            size: (literal))
          (keyword_default)
          (literal
            (keyword_null)))
        (column_definition
          name: (identifier)
          type: (keyword_datetime)
          (keyword_default)
          (literal
            (keyword_null)))
        (constraints
          (constraint
            (keyword_primary)
            (keyword_key)
            (ordered_columns
              (column
                name: (identifier))))
          (constraint
            (keyword_key)
            name: (identifier)
            (ordered_columns
              (column
                name: (identifier))))
          (constraint
            (keyword_key)
            name: (identifier)
            (ordered_columns
              (column
                name: (identifier))))
          (constraint
            (keyword_key)
            name: (identifier)
            (ordered_columns
              (column
                name: (identifier))
              (column
                name: (identifier)))))))))

================================================================================
Debugging
================================================================================

CREATE TABLE IF NOT EXISTS `addresses` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `s_id` bigint(20) DEFAULT NULL,
  `first_name` varchar(255) DEFAULT NULL
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (keyword_if)
      (keyword_not)
      (keyword_exists)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)
            size: (literal))
          (keyword_not)
          (keyword_null)
          (keyword_auto_increment))
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)
            size: (literal))
          (keyword_default)
          (literal
            (keyword_null)))
        (column_definition
          name: (identifier)
          type: (varchar
            (keyword_varchar)
            size: (literal))
          (keyword_default)
          (literal
            (keyword_null)))))))

================================================================================
Create table with table options
================================================================================

CREATE TABLE my_table (
  id BIGINT(20) NOT NULL,
  date DATE DEFAULT NULL ASC
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)
            size: (literal))
          (keyword_not)
          (keyword_null))
        (column_definition
          name: (identifier)
          type: (keyword_date)
          (keyword_default)
          (literal
            (keyword_null))
          (direction
            (keyword_asc))))
      (table_options
        (table_option
          name: (keyword_engine)
          value: (identifier))
        (table_option
          name: (keyword_default))
        (table_option
          name: (identifier)
          value: (identifier))
        (table_option
          name: (identifier)
          value: (identifier))))))

================================================================================
Create view
================================================================================

CREATE VIEW my_view AS
SELECT * FROM my_table;

--------------------------------------------------------------------------------

(program
  (statement
    (create_view
      (keyword_create)
      (keyword_view)
      (table_reference
        name: (identifier))
      (keyword_as)
      (select
        (keyword_select)
        (select_expression
          (all_fields)))
      (from
        (keyword_from)
        (relation
          (table_reference
            name: (identifier)))))))

================================================================================
Create or replace view
================================================================================

CREATE OR REPLACE VIEW my_view AS
SELECT * FROM my_table;

--------------------------------------------------------------------------------

(program
  (statement
    (create_view
      (keyword_create)
      (keyword_or)
      (keyword_replace)
      (keyword_view)
      (table_reference
        name: (identifier))
      (keyword_as)
      (select
        (keyword_select)
        (select_expression
          (all_fields)))
      (from
        (keyword_from)
        (relation
          (table_reference
            name: (identifier)))))))

================================================================================
Create matview
================================================================================

CREATE MATERIALIZED VIEW my_view AS
SELECT * FROM my_table
WITH NO DATA;

--------------------------------------------------------------------------------

(program
  (statement
    (create_materialized_view
      (keyword_create)
      (keyword_materialized)
      (keyword_view)
      (table_reference
        name: (identifier))
      (keyword_as)
      (select
        (keyword_select)
        (select_expression
          (all_fields)))
      (from
        (keyword_from)
        (relation
          (table_reference
            name: (identifier))))
      (keyword_with)
      (keyword_no)
      (keyword_data))))

================================================================================
Create matview with ambiguous grammar
================================================================================

CREATE MATERIALIZED VIEW my_view AS
SELECT * FROM my_table
WITH NO DATA

--------------------------------------------------------------------------------

(program
  (statement
    (create_materialized_view
      (keyword_create)
      (keyword_materialized)
      (keyword_view)
      (table_reference
        name: (identifier))
      (keyword_as)
      (select
        (keyword_select)
        (select_expression
          (all_fields)))
      (from
        (keyword_from)
        (relation
          (table_reference
            name: (identifier))))
      (keyword_with)
      (keyword_no)
      (keyword_data))))

================================================================================
Create table with all types
================================================================================

CREATE TABLE type_test (
  a_bool BOOLEAN,
  a_smallser SMALLSERIAL,
  a_serial SERIAL,
  a_bigser BIGSERIAL,
  an_int INT,
  an_integer INTEGER,
  a_bigint BIGINT,
  a_decimal DECIMAL,
  a_sized_decimal DECIMAL (8),
  a_sized_decimal_with_scale DECIMAL (8, 4),
  a_numeric NUMERIC,
  a_sized_numeric NUMERIC (8),
  a_sized_numeric_with_scale NUMERIC (8, 4),
  a_real REAL,
  a_double_precision DOUBLE PRECISION,
  a_money MONEY,
  a_char CHAR,
  a_character CHARACTER,
  a_sized_char CHAR (10),
  a_varchar VARCHAR (10),
  a_character_varying CHARACTER VARYING (10),
  a_text TEXT,
  a_json JSON,
  a_jsonb JSONB,
  an_xml XML,
  a_bytea BYTEA,
  a_date DATE,
  a_datetime DATETIME,
  a_timestamp TIMESTAMP,
  a_verbose_timestamp TIMESTAMP WITHOUT TIME ZONE,
  a_tstz TIMESTAMPTZ,
  a_verbose_tstz TIMESTAMP WITH TIME ZONE,
  a_geometry GEOMETRY,
  a_geography GEOGRAPHY,
  a_box2d BOX2D,
  a_box3d BOX3D,
  a_uuid UUID
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (keyword_boolean))
        (column_definition
          name: (identifier)
          type: (keyword_smallserial))
        (column_definition
          name: (identifier)
          type: (keyword_serial))
        (column_definition
          name: (identifier)
          type: (keyword_bigserial))
        (column_definition
          name: (identifier)
          type: (int (keyword_int)))
        (column_definition
          name: (identifier)
          type: (int (keyword_int)))
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)))
        (column_definition
          name: (identifier)
          type: (decimal
            (keyword_decimal)))
        (column_definition
          name: (identifier)
          type: (decimal
            (keyword_decimal)
            precision: (literal)))
        (column_definition
          name: (identifier)
          type: (decimal
            (keyword_decimal)
            precision: (literal)
            scale: (literal)))
        (column_definition
          name: (identifier)
          type: (numeric
            (keyword_numeric)))
        (column_definition
          name: (identifier)
          type: (numeric
            (keyword_numeric)
            precision: (literal)))
        (column_definition
          name: (identifier)
          type: (numeric
            (keyword_numeric)
            precision: (literal)
            scale: (literal)))
        (column_definition
          name: (identifier)
          type: (double
            (keyword_real)))
        (column_definition
          name: (identifier)
          type: (double
            (keyword_double)
            (keyword_precision)))
        (column_definition
          name: (identifier)
          type: (keyword_money))
        (column_definition
          name: (identifier)
          type: (char
            (keyword_char)))
        (column_definition
          name: (identifier)
          type: (char
            (keyword_char)))
        (column_definition
          name: (identifier)
          type: (char
            (keyword_char)
            size: (literal)))
        (column_definition
          name: (identifier)
          type: (varchar
            (keyword_varchar)
            size: (literal)))
        (column_definition
          name: (identifier)
          type: (varchar
            (keyword_varchar)
            size: (literal)))
        (column_definition
          name: (identifier)
          type: (keyword_text))
        (column_definition
          name: (identifier)
          type: (keyword_json))
        (column_definition
          name: (identifier)
          type: (keyword_jsonb))
        (column_definition
          name: (identifier)
          type: (keyword_xml))
        (column_definition
          name: (identifier)
          type: (keyword_bytea))
        (column_definition
          name: (identifier)
          type: (keyword_date))
        (column_definition
          name: (identifier)
          type: (keyword_datetime))
        (column_definition
          name: (identifier)
          type: (keyword_timestamp))
        (column_definition
          name: (identifier)
          type: (keyword_timestamp))
        (column_definition
          name: (identifier)
          type: (keyword_timestamptz))
        (column_definition
          name: (identifier)
          type: (keyword_timestamptz))
        (column_definition
          name: (identifier)
          type: (keyword_geometry))
        (column_definition
          name: (identifier)
          type: (keyword_geography))
        (column_definition
          name: (identifier)
          type: (keyword_box2d))
        (column_definition
          name: (identifier)
          type: (keyword_box3d))
        (column_definition
          name: (identifier)
          type: (keyword_uuid))))))

================================================================================
Create table with nullable columns
================================================================================

CREATE TABLE tableName (
    id NUMERIC NULL,
    name VARCHAR NULL
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (table_reference
        (identifier))
      (column_definitions
        (column_definition
          (identifier)
          (numeric
            (keyword_numeric))
          (keyword_null))
        (column_definition
          (identifier)
          (varchar
            (keyword_varchar))
          (keyword_null))))))

================================================================================
Create unlogged table
================================================================================

CREATE UNLOGGED TABLE tableName (
    id NUMERIC
);

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_unlogged)
      (keyword_table)
      (table_reference
        (identifier))
      (column_definitions
        (column_definition
          (identifier)
          (numeric
            (keyword_numeric)))))))

================================================================================
Create table with index
================================================================================

CREATE TABLE some_table(
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `s_id` bigint(20) DEFAULT NULL,
    INDEX `parent`(pid),
    INDEX `range`(publication, published_at)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)
            size: (literal))
          (keyword_not)
          (keyword_null)
          (keyword_auto_increment))
        (column_definition
          name: (identifier)
          type: (bigint
            (keyword_bigint)
            size: (literal))
          (keyword_default)
          (literal
            (keyword_null)))
        (constraints
          (constraint
            (keyword_index)
            name: (identifier)
            (ordered_columns
              (column
                name: (identifier))))
          (constraint
            (keyword_index)
            name: (identifier)
            (ordered_columns
              (column
                name: (identifier))
              (column
                name: (identifier))))))
      (table_options
        (table_option
          (keyword_default)
          (keyword_character)
          (keyword_set)
          (identifier))
        (table_option
          (keyword_collate)
          (identifier))
        (table_option
          name: (keyword_engine)
          value: (identifier))))))

================================================================================
Create table as select
================================================================================

CREATE TABLE tableName AS SELECT * FROM otherTable;

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_table)
      (table_reference
        name: (identifier))
      (keyword_as)
      (select
        (keyword_select)
        (select_expression
          (all_fields)))
      (from
        (keyword_from)
        (relation
          (table_reference
            name: (identifier)))))))

================================================================================
Create hive table
================================================================================

CREATE EXTERNAL TABLE tab (col int)
PARTITIONED BY (col int)
SORT BY (col)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ';' ESCAPED BY '"'
LINES TERMINATED BY '\n'
STORED AS PARQUET
LOCATION '/path/data'
CACHED IN 'pool1' WITH REPLICATION = 2

--------------------------------------------------------------------------------

(program
  (statement
    (create_table
      (keyword_create)
      (keyword_external)
      (keyword_table)
      (table_reference
        name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (int (keyword_int))))
      (table_partition
        (keyword_partitioned)
        (keyword_by)
        (column_definitions
          (column_definition
            name: (identifier)
            type: (int (keyword_int)))))
      (table_sort
        (keyword_sort)
        (keyword_by)
        (identifier))
      (row_format
        (keyword_row)
        (keyword_format)
        (keyword_delimited)
        (keyword_fields)
        (keyword_terminated)
        (keyword_by)
        fields_terminated_char: (literal)
        (keyword_escaped)
        (keyword_by)
        escaped_char: (literal)
        (keyword_lines)
        (keyword_terminated)
        (keyword_by)
        row_terminated_char: (literal))
      (stored_as
        (keyword_stored)
        (keyword_as)
        (keyword_parquet))
      (storage_location
        (keyword_location)
        path: (literal)
        (keyword_cached)
        (keyword_in)
        pool: (literal)
        (keyword_with)
        (keyword_replication)
        value: (literal)))))
