Skip to content

Commit 087b628

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 087b628

File tree

6 files changed

+107
-15
lines changed

6 files changed

+107
-15
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: 15 additions & 0 deletions
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")))
@@ -2374,6 +2377,7 @@ fn parse_scalar_function_in_projection() {
23742377
))],
23752378
over: None,
23762379
distinct: false,
2380+
special: false,
23772381
}),
23782382
expr_from_projection(only(&select.projection))
23792383
);
@@ -2452,6 +2456,7 @@ fn parse_named_argument_function() {
24522456
],
24532457
over: None,
24542458
distinct: false,
2459+
special: false,
24552460
}),
24562461
expr_from_projection(only(&select.projection))
24572462
);
@@ -2485,6 +2490,7 @@ fn parse_window_functions() {
24852490
window_frame: None,
24862491
}),
24872492
distinct: false,
2493+
special: false,
24882494
}),
24892495
expr_from_projection(&select.projection[0])
24902496
);
@@ -2592,6 +2598,7 @@ fn parse_expr_interval() {
25922598
))],
25932599
over: None,
25942600
distinct: false,
2601+
special: false,
25952602
});
25962603

25972604
assert_eq!(
@@ -2736,6 +2743,7 @@ fn parse_at_timezone() {
27362743
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(zero.clone()))],
27372744
over: None,
27382745
distinct: false,
2746+
special: false,
27392747
})),
27402748
time_zone: "UTC-06:00".to_string()
27412749
},
@@ -2761,6 +2769,7 @@ fn parse_at_timezone() {
27612769
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(zero,),),],
27622770
over: None,
27632771
distinct: false,
2772+
special: false,
27642773
},)),
27652774
time_zone: "UTC-06:00".to_string(),
27662775
},),),
@@ -2770,6 +2779,7 @@ fn parse_at_timezone() {
27702779
],
27712780
over: None,
27722781
distinct: false,
2782+
special: false,
27732783
},),
27742784
alias: Ident {
27752785
value: "hour".to_string(),
@@ -2805,6 +2815,7 @@ fn parse_table_function() {
28052815
)))],
28062816
over: None,
28072817
distinct: false,
2818+
special: false,
28082819
});
28092820
assert_eq!(expr, expected_expr);
28102821
assert_eq!(alias, table_alias("a"))
@@ -2861,6 +2872,7 @@ fn parse_delimited_identifiers() {
28612872
args: vec![],
28622873
over: None,
28632874
distinct: false,
2875+
special: false,
28642876
}),
28652877
expr_from_projection(&select.projection[1]),
28662878
);
@@ -4766,6 +4778,7 @@ fn parse_time_functions() {
47664778
args: vec![],
47674779
over: None,
47684780
distinct: false,
4781+
special: false,
47694782
}),
47704783
expr_from_projection(&select.projection[0])
47714784
);
@@ -4781,6 +4794,7 @@ fn parse_time_functions() {
47814794
args: vec![],
47824795
over: None,
47834796
distinct: false,
4797+
special: false,
47844798
}),
47854799
expr_from_projection(&select.projection[0])
47864800
);
@@ -4796,6 +4810,7 @@ fn parse_time_functions() {
47964810
args: vec![],
47974811
over: None,
47984812
distinct: false,
4813+
special: false,
47994814
}),
48004815
expr_from_projection(&select.projection[0])
48014816
);

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)