Skip to content

Commit 0835117

Browse files
ovrmcheshkov
authored andcommitted
feat: Parse special keywors as functions (current_user, user, etc)
Can drop this after rebase on commit 54a29e8 " feat: Parse special keywords as functions (current_user, user, etc) (apache#561)", first released in 0.21.0
1 parent 896d448 commit 0835117

File tree

6 files changed

+109
-16
lines changed

6 files changed

+109
-16
lines changed

src/ast/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,20 +2264,27 @@ pub struct Function {
22642264
pub over: Option<WindowSpec>,
22652265
// aggregate functions may specify eg `COUNT(DISTINCT x)`
22662266
pub distinct: bool,
2267+
pub special: bool,
22672268
}
22682269

22692270
impl fmt::Display for Function {
22702271
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2271-
write!(
2272-
f,
2273-
"{}({}{})",
2274-
self.name,
2275-
if self.distinct { "DISTINCT " } else { "" },
2276-
display_comma_separated(&self.args),
2277-
)?;
2278-
if let Some(o) = &self.over {
2279-
write!(f, " OVER ({})", o)?;
2272+
if self.special {
2273+
write!(f, "{}", self.name)?;
2274+
} else {
2275+
write!(
2276+
f,
2277+
"{}({}{})",
2278+
self.name,
2279+
if self.distinct { "DISTINCT " } else { "" },
2280+
display_comma_separated(&self.args),
2281+
)?;
2282+
2283+
if let Some(o) = &self.over {
2284+
write!(f, " OVER ({})", o)?;
2285+
}
22802286
}
2287+
22812288
Ok(())
22822289
}
22832290
}

src/parser.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,20 @@ impl<'a> Parser<'a> {
421421
self.prev_token();
422422
Ok(Expr::Value(self.parse_value()?))
423423
}
424+
Keyword::CURRENT_CATALOG
425+
| Keyword::CURRENT_USER
426+
| Keyword::SESSION_USER
427+
| Keyword::USER
428+
if dialect_of!(self is PostgreSqlDialect | GenericDialect) =>
429+
{
430+
Ok(Expr::Function(Function {
431+
name: ObjectName(vec![w.to_ident()]),
432+
args: vec![],
433+
over: None,
434+
distinct: false,
435+
special: true,
436+
}))
437+
}
424438
Keyword::CURRENT_TIMESTAMP | Keyword::CURRENT_TIME | Keyword::CURRENT_DATE => {
425439
self.parse_time_functions(ObjectName(vec![w.to_ident()]))
426440
}
@@ -598,6 +612,7 @@ impl<'a> Parser<'a> {
598612
args,
599613
over,
600614
distinct,
615+
special: false,
601616
}))
602617
}
603618

@@ -612,6 +627,7 @@ impl<'a> Parser<'a> {
612627
args,
613628
over: None,
614629
distinct: false,
630+
special: false,
615631
}))
616632
}
617633

tests/sqlparser_common.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ fn parse_select_count_wildcard() {
503503
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Wildcard)],
504504
over: None,
505505
distinct: false,
506+
special: false,
506507
}),
507508
expr_from_projection(only(&select.projection))
508509
);
@@ -521,6 +522,7 @@ fn parse_select_count_distinct() {
521522
}))],
522523
over: None,
523524
distinct: true,
525+
special: false,
524526
}),
525527
expr_from_projection(only(&select.projection))
526528
);
@@ -1336,6 +1338,7 @@ fn parse_select_having() {
13361338
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Wildcard)],
13371339
over: None,
13381340
distinct: false,
1341+
special: false,
13391342
})),
13401343
op: BinaryOperator::Gt,
13411344
right: Box::new(Expr::Value(number("1")))
@@ -1367,7 +1370,8 @@ fn parse_select_qualify() {
13671370
}],
13681371
window_frame: None
13691372
}),
1370-
distinct: false
1373+
distinct: false,
1374+
special: false,
13711375
})),
13721376
op: BinaryOperator::Eq,
13731377
right: Box::new(Expr::Value(number("1")))
@@ -2374,6 +2378,7 @@ fn parse_scalar_function_in_projection() {
23742378
))],
23752379
over: None,
23762380
distinct: false,
2381+
special: false,
23772382
}),
23782383
expr_from_projection(only(&select.projection))
23792384
);
@@ -2452,6 +2457,7 @@ fn parse_named_argument_function() {
24522457
],
24532458
over: None,
24542459
distinct: false,
2460+
special: false,
24552461
}),
24562462
expr_from_projection(only(&select.projection))
24572463
);
@@ -2485,6 +2491,7 @@ fn parse_window_functions() {
24852491
window_frame: None,
24862492
}),
24872493
distinct: false,
2494+
special: false,
24882495
}),
24892496
expr_from_projection(&select.projection[0])
24902497
);
@@ -2592,6 +2599,7 @@ fn parse_expr_interval() {
25922599
))],
25932600
over: None,
25942601
distinct: false,
2602+
special: false,
25952603
});
25962604

25972605
assert_eq!(
@@ -2736,6 +2744,7 @@ fn parse_at_timezone() {
27362744
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(zero.clone()))],
27372745
over: None,
27382746
distinct: false,
2747+
special: false,
27392748
})),
27402749
time_zone: "UTC-06:00".to_string()
27412750
},
@@ -2761,6 +2770,7 @@ fn parse_at_timezone() {
27612770
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(zero,),),],
27622771
over: None,
27632772
distinct: false,
2773+
special: false,
27642774
},)),
27652775
time_zone: "UTC-06:00".to_string(),
27662776
},),),
@@ -2770,6 +2780,7 @@ fn parse_at_timezone() {
27702780
],
27712781
over: None,
27722782
distinct: false,
2783+
special: false,
27732784
},),
27742785
alias: Ident {
27752786
value: "hour".to_string(),
@@ -2805,6 +2816,7 @@ fn parse_table_function() {
28052816
)))],
28062817
over: None,
28072818
distinct: false,
2819+
special: false,
28082820
});
28092821
assert_eq!(expr, expected_expr);
28102822
assert_eq!(alias, table_alias("a"))
@@ -2861,6 +2873,7 @@ fn parse_delimited_identifiers() {
28612873
args: vec![],
28622874
over: None,
28632875
distinct: false,
2876+
special: false,
28642877
}),
28652878
expr_from_projection(&select.projection[1]),
28662879
);
@@ -4766,6 +4779,7 @@ fn parse_time_functions() {
47664779
args: vec![],
47674780
over: None,
47684781
distinct: false,
4782+
special: false,
47694783
}),
47704784
expr_from_projection(&select.projection[0])
47714785
);
@@ -4781,6 +4795,7 @@ fn parse_time_functions() {
47814795
args: vec![],
47824796
over: None,
47834797
distinct: false,
4798+
special: false,
47844799
}),
47854800
expr_from_projection(&select.projection[0])
47864801
);
@@ -4796,6 +4811,7 @@ fn parse_time_functions() {
47964811
args: vec![],
47974812
over: None,
47984813
distinct: false,
4814+
special: false,
47994815
}),
48004816
expr_from_projection(&select.projection[0])
48014817
);

tests/sqlparser_mysql.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,8 @@ fn parse_insert_with_on_duplicate_update() {
622622
Expr::Identifier(Ident::new("description"))
623623
))],
624624
over: None,
625-
distinct: false
625+
distinct: false,
626+
special: false,
626627
})
627628
},
628629
Assignment {
@@ -633,7 +634,8 @@ fn parse_insert_with_on_duplicate_update() {
633634
Expr::Identifier(Ident::new("perm_create"))
634635
))],
635636
over: None,
636-
distinct: false
637+
distinct: false,
638+
special: false,
637639
})
638640
},
639641
Assignment {
@@ -644,7 +646,8 @@ fn parse_insert_with_on_duplicate_update() {
644646
Expr::Identifier(Ident::new("perm_read"))
645647
))],
646648
over: None,
647-
distinct: false
649+
distinct: false,
650+
special: false,
648651
})
649652
},
650653
Assignment {
@@ -655,7 +658,8 @@ fn parse_insert_with_on_duplicate_update() {
655658
Expr::Identifier(Ident::new("perm_update"))
656659
))],
657660
over: None,
658-
distinct: false
661+
distinct: false,
662+
special: false,
659663
})
660664
},
661665
Assignment {
@@ -666,7 +670,8 @@ fn parse_insert_with_on_duplicate_update() {
666670
Expr::Identifier(Ident::new("perm_delete"))
667671
))],
668672
over: None,
669-
distinct: false
673+
distinct: false,
674+
special: false,
670675
})
671676
},
672677
])),

tests/sqlparser_postgres.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,7 @@ fn test_composite_value() {
14001400
)))],
14011401
over: None,
14021402
distinct: false,
1403+
special: false
14031404
}))))
14041405
}),
14051406
select.projection[0]
@@ -1517,6 +1518,52 @@ fn parse_escaped_literal_string() {
15171518
);
15181519
}
15191520

1521+
#[test]
1522+
fn parse_current_functions() {
1523+
let sql = "SELECT CURRENT_CATALOG, CURRENT_USER, SESSION_USER, USER";
1524+
let select = pg_and_generic().verified_only_select(sql);
1525+
assert_eq!(
1526+
&Expr::Function(Function {
1527+
name: ObjectName(vec![Ident::new("CURRENT_CATALOG")]),
1528+
args: vec![],
1529+
over: None,
1530+
distinct: false,
1531+
special: true,
1532+
}),
1533+
expr_from_projection(&select.projection[0])
1534+
);
1535+
assert_eq!(
1536+
&Expr::Function(Function {
1537+
name: ObjectName(vec![Ident::new("CURRENT_USER")]),
1538+
args: vec![],
1539+
over: None,
1540+
distinct: false,
1541+
special: true,
1542+
}),
1543+
expr_from_projection(&select.projection[1])
1544+
);
1545+
assert_eq!(
1546+
&Expr::Function(Function {
1547+
name: ObjectName(vec![Ident::new("SESSION_USER")]),
1548+
args: vec![],
1549+
over: None,
1550+
distinct: false,
1551+
special: true,
1552+
}),
1553+
expr_from_projection(&select.projection[2])
1554+
);
1555+
assert_eq!(
1556+
&Expr::Function(Function {
1557+
name: ObjectName(vec![Ident::new("USER")]),
1558+
args: vec![],
1559+
over: None,
1560+
distinct: false,
1561+
special: true,
1562+
}),
1563+
expr_from_projection(&select.projection[3])
1564+
);
1565+
}
1566+
15201567
#[test]
15211568
fn parse_declare() {
15221569
pg_and_generic()

tests/sqpparser_clickhouse.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ fn parse_map_access_expr() {
5151
],
5252
over: None,
5353
distinct: false,
54+
special: false,
5455
})],
5556
})],
5657
into: None,
@@ -85,7 +86,8 @@ fn parse_map_access_expr() {
8586
))),
8687
],
8788
over: None,
88-
distinct: false
89+
distinct: false,
90+
special: false,
8991
})]
9092
}),
9193
op: BinaryOperator::NotEq,

0 commit comments

Comments
 (0)