Skip to content

Commit dc14ac3

Browse files
authored
fix: function contents spacing rules (#1805)
* fix: function contents spacing rules - fixes comment on function * temp fix cv04 * fix st08 * fixing cv11 * fix rule tests * temp * fix tests in infer * fix lineage
1 parent 9067db5 commit dc14ac3

File tree

258 files changed

+11778
-10446
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

258 files changed

+11778
-10446
lines changed

crates/lib-core/src/dialects/syntax.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub enum SyntaxKind {
2222
Expression,
2323
WildcardIdentifier,
2424
Function,
25+
FunctionContents,
2526
HavingClause,
2627
PathSegment,
2728
LimitClause,

crates/lib-dialects/src/ansi.rs

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,28 +1303,52 @@ pub fn raw_dialect() -> Dialect {
13031303
.to_matchable()
13041304
.into(),
13051305
),
1306+
(
1307+
// DateTimeFunctionContentsSegment(BaseSegment):
1308+
// """Datetime function contents."""
1309+
//
1310+
// type = "function_contents"
1311+
//
1312+
// match_grammar = Sequence(
1313+
// Bracketed(
1314+
// Delimited(
1315+
// Ref("DatetimeUnitSegment"),
1316+
// Ref(
1317+
// "FunctionContentsGrammar",
1318+
// # The brackets might be empty for some functions...
1319+
// optional=True,
1320+
// ),
1321+
// ),
1322+
// ),
1323+
// )
1324+
"DateTimeFunctionContentsSegment".into(),
1325+
NodeMatcher::new(SyntaxKind::FunctionContents, |_| {
1326+
Sequence::new(vec_of_erased![Bracketed::new(vec_of_erased![
1327+
Delimited::new(vec_of_erased![
1328+
Ref::new("DatetimeUnitSegment"),
1329+
Ref::new("FunctionContentsGrammar").optional()
1330+
])
1331+
])])
1332+
.to_matchable()
1333+
})
1334+
.to_matchable()
1335+
.into(),
1336+
),
13061337
(
13071338
"FunctionSegment".into(),
13081339
NodeMatcher::new(SyntaxKind::Function, |_| {
13091340
one_of(vec_of_erased![
1310-
Sequence::new(vec_of_erased![Sequence::new(vec_of_erased![
1341+
Sequence::new(vec_of_erased![
13111342
Ref::new("DatePartFunctionNameSegment"),
1312-
Bracketed::new(vec_of_erased![Delimited::new(vec_of_erased![
1313-
Ref::new("DatetimeUnitSegment"),
1314-
Ref::new("FunctionContentsGrammar").optional()
1315-
])])
1316-
.config(|this| this.parse_mode(ParseMode::Greedy))
1317-
])]),
1343+
Ref::new("DateTimeFunctionContentsSegment"),
1344+
]),
13181345
Sequence::new(vec_of_erased![
13191346
Sequence::new(vec_of_erased![
13201347
Ref::new("FunctionNameSegment").exclude(one_of(vec_of_erased![
13211348
Ref::new("DatePartFunctionNameSegment"),
13221349
Ref::new("ValuesClauseSegment")
13231350
])),
1324-
Bracketed::new(vec_of_erased![
1325-
Ref::new("FunctionContentsGrammar").optional()
1326-
])
1327-
.config(|this| this.parse_mode(ParseMode::Greedy))
1351+
Ref::new("FunctionContentsSegment"),
13281352
]),
13291353
Ref::new("PostFunctionGrammar").optional()
13301354
])
@@ -2386,12 +2410,7 @@ pub fn raw_dialect() -> Dialect {
23862410
Ref::keyword("EXECUTE").to_matchable(),
23872411
Ref::keyword("PROCEDURE").to_matchable(),
23882412
Ref::new("FunctionNameIdentifierSegment").to_matchable(),
2389-
Bracketed::new(vec![
2390-
Ref::new("FunctionContentsGrammar")
2391-
.optional()
2392-
.to_matchable(),
2393-
])
2394-
.to_matchable(),
2413+
Ref::new("FunctionContentsSegment").to_matchable(),
23952414
])
23962415
.config(|this| this.optional())
23972416
.to_matchable(),
@@ -4331,6 +4350,19 @@ pub fn raw_dialect() -> Dialect {
43314350
)]);
43324351

43334352
ansi_dialect.add([
4353+
(
4354+
"FunctionContentsSegment".into(),
4355+
NodeMatcher::new(SyntaxKind::FunctionContents, |_| {
4356+
Sequence::new(vec![
4357+
Bracketed::new(vec![Ref::new("FunctionContentsGrammar").to_matchable()])
4358+
.config(|this| this.optional())
4359+
.to_matchable(),
4360+
])
4361+
.to_matchable()
4362+
})
4363+
.to_matchable()
4364+
.into(),
4365+
),
43344366
// FunctionContentsExpressionGrammar intended as a hook to override in other dialects.
43354367
(
43364368
"FunctionContentsExpressionGrammar".into(),

crates/lib-dialects/src/bigquery.rs

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,57 @@ pub fn dialect() -> Dialect {
942942
);
943943

944944
dialect.add([
945+
(
946+
"DateTimeFunctionContentsSegment".into(),
947+
NodeMatcher::new(SyntaxKind::FunctionContents, |_| {
948+
Bracketed::new(vec_of_erased![Delimited::new(vec_of_erased![
949+
Ref::new("DatetimeUnitSegment"),
950+
Ref::new("DatePartWeekSegment"),
951+
Ref::new("FunctionContentsGrammar")
952+
])])
953+
.to_matchable()
954+
})
955+
.to_matchable()
956+
.into(),
957+
),
958+
(
959+
"ExtractFunctionContentsSegment".into(),
960+
NodeMatcher::new(SyntaxKind::FunctionContents, |_| {
961+
Bracketed::new(vec_of_erased![
962+
one_of(vec_of_erased![
963+
Ref::new("DatetimeUnitSegment"),
964+
Ref::new("DatePartWeekSegment"),
965+
Ref::new("ExtendedDatetimeUnitSegment")
966+
]),
967+
Ref::keyword("FROM"),
968+
Ref::new("ExpressionSegment")
969+
])
970+
.to_matchable()
971+
})
972+
.to_matchable()
973+
.into(),
974+
),
975+
(
976+
"NormalizeFunctionContentsSegment".into(),
977+
NodeMatcher::new(SyntaxKind::FunctionContents, |_| {
978+
Bracketed::new(vec_of_erased![
979+
Ref::new("ExpressionSegment"),
980+
Sequence::new(vec_of_erased![
981+
Ref::new("CommaSegment"),
982+
one_of(vec_of_erased![
983+
Ref::keyword("NFC"),
984+
Ref::keyword("NFKC"),
985+
Ref::keyword("NFD"),
986+
Ref::keyword("NFKD")
987+
])
988+
])
989+
.config(|this| this.optional())
990+
])
991+
.to_matchable()
992+
})
993+
.to_matchable()
994+
.into(),
995+
),
945996
(
946997
"ExtractFunctionNameSegment".into(),
947998
NodeMatcher::new(SyntaxKind::FunctionName, |_| {
@@ -1023,41 +1074,18 @@ pub fn dialect() -> Dialect {
10231074
Sequence::new(vec_of_erased![
10241075
// BigQuery EXTRACT allows optional TimeZone
10251076
Ref::new("ExtractFunctionNameSegment"),
1026-
Bracketed::new(vec_of_erased![
1027-
one_of(vec_of_erased![
1028-
Ref::new("DatetimeUnitSegment"),
1029-
Ref::new("DatePartWeekSegment"),
1030-
Ref::new("ExtendedDatetimeUnitSegment")
1031-
]),
1032-
Ref::keyword("FROM"),
1033-
Ref::new("ExpressionSegment")
1034-
])
1077+
Ref::new("ExtractFunctionContentsSegment")
10351078
]),
10361079
Sequence::new(vec_of_erased![
1080+
// BigQuery NORMALIZE allows optional normalization_mode
10371081
Ref::new("NormalizeFunctionNameSegment"),
1038-
Bracketed::new(vec_of_erased![
1039-
Ref::new("ExpressionSegment"),
1040-
Sequence::new(vec_of_erased![
1041-
Ref::new("CommaSegment"),
1042-
one_of(vec_of_erased![
1043-
Ref::keyword("NFC"),
1044-
Ref::keyword("NFKC"),
1045-
Ref::keyword("NFD"),
1046-
Ref::keyword("NFKD")
1047-
])
1048-
])
1049-
.config(|this| this.optional())
1050-
])
1082+
Ref::new("NormalizeFunctionContentsSegment")
10511083
]),
10521084
Sequence::new(vec_of_erased![
1085+
// Treat functions which take date parts separately
10531086
Ref::new("DatePartFunctionNameSegment")
10541087
.exclude(Ref::new("ExtractFunctionNameSegment")),
1055-
Bracketed::new(vec_of_erased![Delimited::new(vec_of_erased![
1056-
Ref::new("DatetimeUnitSegment"),
1057-
Ref::new("DatePartWeekSegment"),
1058-
Ref::new("FunctionContentsGrammar")
1059-
])])
1060-
.config(|this| this.parse_mode(ParseMode::Greedy))
1088+
Ref::new("DateTimeFunctionContentsSegment")
10611089
]),
10621090
Sequence::new(vec_of_erased![
10631091
Sequence::new(vec_of_erased![
@@ -1066,10 +1094,7 @@ pub fn dialect() -> Dialect {
10661094
Ref::new("NormalizeFunctionNameSegment"),
10671095
Ref::new("ValuesClauseSegment"),
10681096
])),
1069-
Bracketed::new(vec_of_erased![
1070-
Ref::new("FunctionContentsGrammar").optional()
1071-
])
1072-
.config(|this| this.parse_mode(ParseMode::Greedy))
1097+
Ref::new("FunctionContentsSegment")
10731098
]),
10741099
Ref::new("ArrayAccessorSegment").optional(),
10751100
Ref::new("SemiStructuredAccessorSegment").optional(),
@@ -1216,11 +1241,20 @@ pub fn dialect() -> Dialect {
12161241
.into(),
12171242
)]);
12181243

1244+
dialect.add([(
1245+
"ArrayFunctionContentsSegment".into(),
1246+
NodeMatcher::new(SyntaxKind::FunctionContents, |_| {
1247+
Bracketed::new(vec_of_erased![Ref::new("SelectableGrammar")]).to_matchable()
1248+
})
1249+
.to_matchable()
1250+
.into(),
1251+
)]);
1252+
12191253
dialect.replace_grammar(
12201254
"ArrayExpressionSegment",
12211255
Sequence::new(vec_of_erased![
12221256
Ref::new("ArrayFunctionNameSegment"),
1223-
Bracketed::new(vec_of_erased![Ref::new("SelectableGrammar")]),
1257+
Ref::new("ArrayFunctionContentsSegment"),
12241258
])
12251259
.to_matchable(),
12261260
);

crates/lib-dialects/test/fixtures/dialects/ansi/arithmetic_a.yml

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,12 @@ file:
231231
- function:
232232
- function_name:
233233
- function_name_identifier: func
234-
- bracketed:
235-
- start_bracket: (
236-
- expression:
237-
- numeric_literal: '5'
238-
- end_bracket: )
234+
- function_contents:
235+
- bracketed:
236+
- start_bracket: (
237+
- expression:
238+
- numeric_literal: '5'
239+
- end_bracket: )
239240
- statement_terminator: ;
240241
- statement:
241242
- select_statement:
@@ -251,11 +252,12 @@ file:
251252
- function:
252253
- function_name:
253254
- function_name_identifier: func
254-
- bracketed:
255-
- start_bracket: (
256-
- expression:
257-
- numeric_literal: '5'
258-
- end_bracket: )
255+
- function_contents:
256+
- bracketed:
257+
- start_bracket: (
258+
- expression:
259+
- numeric_literal: '5'
260+
- end_bracket: )
259261
- statement_terminator: ;
260262
- statement:
261263
- select_statement:
@@ -356,11 +358,12 @@ file:
356358
- function:
357359
- function_name:
358360
- function_name_identifier: func
359-
- bracketed:
360-
- start_bracket: (
361-
- expression:
362-
- numeric_literal: '5'
363-
- end_bracket: )
361+
- function_contents:
362+
- bracketed:
363+
- start_bracket: (
364+
- expression:
365+
- numeric_literal: '5'
366+
- end_bracket: )
364367
- statement_terminator: ;
365368
- statement:
366369
- select_statement:

crates/lib-dialects/test/fixtures/dialects/ansi/create_table_default_function.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ file:
1616
- function:
1717
- function_name:
1818
- function_name_identifier: GETDATE
19-
- bracketed:
20-
- start_bracket: (
21-
- end_bracket: )
19+
- function_contents:
20+
- bracketed:
21+
- start_bracket: (
22+
- end_bracket: )
2223
- end_bracket: )

0 commit comments

Comments
 (0)