Skip to content

Commit 8d8c236

Browse files
committed
fix(ast): Add database name to object_reference
Add optional database name to the object_reference rule and add some tests to add the new functionality
1 parent b0e6359 commit 8d8c236

File tree

7 files changed

+198
-10
lines changed

7 files changed

+198
-10
lines changed

grammar.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -2195,12 +2195,18 @@ module.exports = grammar({
21952195
$.identifier,
21962196
),
21972197

2198-
object_reference: $ => seq(
2199-
optional(
2200-
seq(
2201-
field('schema', $.identifier),
2202-
'.',
2203-
),
2198+
object_reference: $ => choice(
2199+
seq(
2200+
field('database', $.identifier),
2201+
'.',
2202+
field('schema', $.identifier),
2203+
'.',
2204+
field('name', $.identifier),
2205+
),
2206+
seq(
2207+
field('schema', $.identifier),
2208+
'.',
2209+
field('name', $.identifier),
22042210
),
22052211
field('name', $.identifier),
22062212
),

test/corpus/alter.txt

+29
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,35 @@ ALTER TABLE my_table
2525
(keyword_not)
2626
(keyword_null))))))
2727

28+
================================================================================
29+
Alter table and add column with FQN
30+
================================================================================
31+
32+
ALTER TABLE my_database.my_table.my_table
33+
ADD COLUMN val3 VARCHAR(100) NOT NULL;
34+
35+
--------------------------------------------------------------------------------
36+
37+
(program
38+
(statement
39+
(alter_table
40+
(keyword_alter)
41+
(keyword_table)
42+
(object_reference
43+
database: (identifier)
44+
schema: (identifier)
45+
name: (identifier))
46+
(add_column
47+
(keyword_add)
48+
(keyword_column)
49+
(column_definition
50+
name: (identifier)
51+
type: (varchar
52+
(keyword_varchar)
53+
size: (literal))
54+
(keyword_not)
55+
(keyword_null))))))
56+
2857
================================================================================
2958
Alter table and add column, eliding column keyword
3059
================================================================================

test/corpus/comment_stat.txt

+44
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@ COMMENT ON TABLE my_schema.my_table IS "this table is a test";
1818
(keyword_is)
1919
(literal))))
2020

21+
================================================================================
22+
Comment on table with FQN
23+
================================================================================
24+
25+
COMMENT ON TABLE my_database.my_schema.my_table IS "this table is a test";
26+
27+
--------------------------------------------------------------------------------
28+
29+
(program
30+
(statement
31+
(comment_statement
32+
(keyword_comment)
33+
(keyword_on)
34+
(keyword_table)
35+
(object_reference
36+
database: (identifier)
37+
schema: (identifier)
38+
name: (identifier))
39+
(keyword_is)
40+
(literal))))
41+
2142
================================================================================
2243
Comment on column is null
2344
================================================================================
@@ -40,6 +61,29 @@ COMMENT ON COLUMN my_schema.my_table.my_column IS NULL;
4061
(keyword_is)
4162
(keyword_null))))
4263

64+
================================================================================
65+
Comment on column is null with FQN
66+
================================================================================
67+
68+
COMMENT ON COLUMN my_database.my_schema.my_table.my_column IS NULL;
69+
70+
--------------------------------------------------------------------------------
71+
72+
(program
73+
(statement
74+
(comment_statement
75+
(keyword_comment)
76+
(keyword_on)
77+
(keyword_column)
78+
(object_reference
79+
(object_reference
80+
(identifier)
81+
(identifier)
82+
(identifier))
83+
(identifier))
84+
(keyword_is)
85+
(keyword_null))))
86+
4387
================================================================================
4488
Comment on cast
4589
================================================================================

test/corpus/create.txt

+27
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,33 @@ CREATE TABLE my_schema.my_table (id BIGINT NOT NULL PRIMARY KEY);
2424
(keyword_primary)
2525
(keyword_key))))))
2626

27+
================================================================================
28+
Create table with FQN
29+
================================================================================
30+
31+
CREATE TABLE my_database.my_schema.my_table (id BIGINT NOT NULL PRIMARY KEY);
32+
33+
--------------------------------------------------------------------------------
34+
35+
(program
36+
(statement
37+
(create_table
38+
(keyword_create)
39+
(keyword_table)
40+
(object_reference
41+
database: (identifier)
42+
schema: (identifier)
43+
name: (identifier))
44+
(column_definitions
45+
(column_definition
46+
name: (identifier)
47+
type: (bigint
48+
(keyword_bigint))
49+
(keyword_not)
50+
(keyword_null)
51+
(keyword_primary)
52+
(keyword_key))))))
53+
2754
================================================================================
2855
Create table multiple columns
2956
================================================================================

test/corpus/delete.txt

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ DELETE FROM my_table;
1515
(object_reference
1616
name: (identifier)))))
1717

18+
================================================================================
19+
Delete whole table with FQN
20+
================================================================================
21+
22+
DELETE FROM my_database.my_schema.my_table;
23+
24+
--------------------------------------------------------------------------------
25+
26+
(program
27+
(statement
28+
(delete
29+
(keyword_delete))
30+
(from
31+
(keyword_from)
32+
(object_reference
33+
database: (identifier)
34+
schema: (identifier)
35+
name: (identifier)))))
36+
1837
================================================================================
1938
Delete whole table and only the whole table
2039
================================================================================

test/corpus/drop.txt

+36
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ DROP TABLE my_table;
1414
(object_reference
1515
name: (identifier)))))
1616

17+
================================================================================
18+
Drop table with FQN
19+
================================================================================
20+
21+
DROP TABLE my_table.my_schema.my_table;
22+
23+
--------------------------------------------------------------------------------
24+
25+
(program
26+
(statement
27+
(drop_table
28+
(keyword_drop)
29+
(keyword_table)
30+
(object_reference
31+
database: (identifier)
32+
schema: (identifier)
33+
name: (identifier)))))
34+
1735
================================================================================
1836
Drop table and cascade through
1937
================================================================================
@@ -47,6 +65,24 @@ DROP VIEW my_view;
4765
(object_reference
4866
name: (identifier)))))
4967

68+
================================================================================
69+
Drop view with FQN
70+
================================================================================
71+
72+
DROP VIEW my_database.my_schema.my_view;
73+
74+
--------------------------------------------------------------------------------
75+
76+
(program
77+
(statement
78+
(drop_view
79+
(keyword_drop)
80+
(keyword_view)
81+
(object_reference
82+
database: (identifier)
83+
schema: (identifier)
84+
name: (identifier)))))
85+
5086
================================================================================
5187
Drop index
5288
================================================================================

test/corpus/select.txt

+31-4
Original file line numberDiff line numberDiff line change
@@ -749,14 +749,37 @@ SELECT * FROM my_schema.my_table;
749749
schema: (identifier)
750750
name: (identifier))))))
751751

752+
================================================================================
753+
Simple select with FQN
754+
================================================================================
755+
756+
SELECT * FROM my_database.my_schema.my_table;
757+
758+
--------------------------------------------------------------------------------
759+
760+
(program
761+
(statement
762+
(select
763+
(keyword_select)
764+
(select_expression
765+
(term
766+
value: (all_fields))))
767+
(from
768+
(keyword_from)
769+
(relation
770+
(object_reference
771+
database: (identifier)
772+
schema: (identifier)
773+
name: (identifier))))))
774+
752775
================================================================================
753776
Simple select with schema and fully-qualified *
754777
================================================================================
755778

756779
SELECT
757-
my_schema.my_table.*
780+
my_database.my_schema.my_table.*
758781
FROM
759-
my_schema.my_table;
782+
my_database.my_schema.my_table;
760783

761784
--------------------------------------------------------------------------------
762785

@@ -768,12 +791,14 @@ FROM
768791
(term
769792
value: (all_fields
770793
(object_reference
794+
database: (identifier)
771795
schema: (identifier)
772796
name: (identifier))))))
773797
(from
774798
(keyword_from)
775799
(relation
776800
(object_reference
801+
database: (identifier)
777802
schema: (identifier)
778803
name: (identifier))))))
779804

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

784809
SELECT
785-
my_schema.my_table.my_field
810+
my_database.my_schema.my_table.my_field
786811
FROM
787-
my_schema.my_table;
812+
my_database.my_schema.my_table;
788813

789814
--------------------------------------------------------------------------------
790815

@@ -796,13 +821,15 @@ FROM
796821
(term
797822
value: (field
798823
(object_reference
824+
database: (identifier)
799825
schema: (identifier)
800826
name: (identifier))
801827
name: (identifier)))))
802828
(from
803829
(keyword_from)
804830
(relation
805831
(object_reference
832+
database: (identifier)
806833
schema: (identifier)
807834
name: (identifier))))))
808835

0 commit comments

Comments
 (0)