Skip to content

fix(ast): Add database name to object_reference #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2195,12 +2195,18 @@ module.exports = grammar({
$.identifier,
),

object_reference: $ => seq(
optional(
seq(
field('schema', $.identifier),
'.',
),
object_reference: $ => choice(
seq(
field('database', $.identifier),
'.',
field('schema', $.identifier),
'.',
field('name', $.identifier),
),
seq(
field('schema', $.identifier),
'.',
field('name', $.identifier),
),
field('name', $.identifier),
),
Expand Down
29 changes: 29 additions & 0 deletions test/corpus/alter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,35 @@ ALTER TABLE my_table
(keyword_not)
(keyword_null))))))

================================================================================
Alter table and add column with FQN
================================================================================

ALTER TABLE my_database.my_table.my_table
ADD COLUMN val3 VARCHAR(100) NOT NULL;

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

(program
(statement
(alter_table
(keyword_alter)
(keyword_table)
(object_reference
database: (identifier)
schema: (identifier)
name: (identifier))
(add_column
(keyword_add)
(keyword_column)
(column_definition
name: (identifier)
type: (varchar
(keyword_varchar)
size: (literal))
(keyword_not)
(keyword_null))))))

================================================================================
Alter table and add column, eliding column keyword
================================================================================
Expand Down
44 changes: 44 additions & 0 deletions test/corpus/comment_stat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ COMMENT ON TABLE my_schema.my_table IS "this table is a test";
(keyword_is)
(literal))))

================================================================================
Comment on table with FQN
================================================================================

COMMENT ON TABLE my_database.my_schema.my_table IS "this table is a test";

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

(program
(statement
(comment_statement
(keyword_comment)
(keyword_on)
(keyword_table)
(object_reference
database: (identifier)
schema: (identifier)
name: (identifier))
(keyword_is)
(literal))))

================================================================================
Comment on column is null
================================================================================
Expand All @@ -40,6 +61,29 @@ COMMENT ON COLUMN my_schema.my_table.my_column IS NULL;
(keyword_is)
(keyword_null))))

================================================================================
Comment on column is null with FQN
================================================================================

COMMENT ON COLUMN my_database.my_schema.my_table.my_column IS NULL;

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

(program
(statement
(comment_statement
(keyword_comment)
(keyword_on)
(keyword_column)
(object_reference
(object_reference
(identifier)
(identifier)
(identifier))
(identifier))
(keyword_is)
(keyword_null))))

================================================================================
Comment on cast
================================================================================
Expand Down
27 changes: 27 additions & 0 deletions test/corpus/create.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ CREATE TABLE my_schema.my_table (id BIGINT NOT NULL PRIMARY KEY);
(keyword_primary)
(keyword_key))))))

================================================================================
Create table with FQN
================================================================================

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

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

(program
(statement
(create_table
(keyword_create)
(keyword_table)
(object_reference
database: (identifier)
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
================================================================================
Expand Down
19 changes: 19 additions & 0 deletions test/corpus/delete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ DELETE FROM my_table;
(object_reference
name: (identifier)))))

================================================================================
Delete whole table with FQN
================================================================================

DELETE FROM my_database.my_schema.my_table;

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

(program
(statement
(delete
(keyword_delete))
(from
(keyword_from)
(object_reference
database: (identifier)
schema: (identifier)
name: (identifier)))))

================================================================================
Delete whole table and only the whole table
================================================================================
Expand Down
36 changes: 36 additions & 0 deletions test/corpus/drop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ DROP TABLE my_table;
(object_reference
name: (identifier)))))

================================================================================
Drop table with FQN
================================================================================

DROP TABLE my_table.my_schema.my_table;

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

(program
(statement
(drop_table
(keyword_drop)
(keyword_table)
(object_reference
database: (identifier)
schema: (identifier)
name: (identifier)))))

================================================================================
Drop table and cascade through
================================================================================
Expand Down Expand Up @@ -47,6 +65,24 @@ DROP VIEW my_view;
(object_reference
name: (identifier)))))

================================================================================
Drop view with FQN
================================================================================

DROP VIEW my_database.my_schema.my_view;

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

(program
(statement
(drop_view
(keyword_drop)
(keyword_view)
(object_reference
database: (identifier)
schema: (identifier)
name: (identifier)))))

================================================================================
Drop index
================================================================================
Expand Down
35 changes: 31 additions & 4 deletions test/corpus/select.txt
Original file line number Diff line number Diff line change
Expand Up @@ -749,14 +749,37 @@ SELECT * FROM my_schema.my_table;
schema: (identifier)
name: (identifier))))))

================================================================================
Simple select with FQN
================================================================================

SELECT * FROM my_database.my_schema.my_table;

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

(program
(statement
(select
(keyword_select)
(select_expression
(term
value: (all_fields))))
(from
(keyword_from)
(relation
(object_reference
database: (identifier)
schema: (identifier)
name: (identifier))))))

================================================================================
Simple select with schema and fully-qualified *
================================================================================

SELECT
my_schema.my_table.*
my_database.my_schema.my_table.*
FROM
my_schema.my_table;
my_database.my_schema.my_table;

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

Expand All @@ -768,12 +791,14 @@ FROM
(term
value: (all_fields
(object_reference
database: (identifier)
schema: (identifier)
name: (identifier))))))
(from
(keyword_from)
(relation
(object_reference
database: (identifier)
schema: (identifier)
name: (identifier))))))

Expand All @@ -782,9 +807,9 @@ Simple select with schema and fully-pathed fields
================================================================================

SELECT
my_schema.my_table.my_field
my_database.my_schema.my_table.my_field
FROM
my_schema.my_table;
my_database.my_schema.my_table;

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

Expand All @@ -796,13 +821,15 @@ FROM
(term
value: (field
(object_reference
database: (identifier)
schema: (identifier)
name: (identifier))
name: (identifier)))))
(from
(keyword_from)
(relation
(object_reference
database: (identifier)
schema: (identifier)
name: (identifier))))))

Expand Down
Loading