Skip to content

Commit 6876853

Browse files
authored
feat: Add WITH OFFSET Alias (#528)
* feat: Add WITH OFFSET Alias * update: fix with_offset_alias type
1 parent 17c238b commit 6876853

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/ast/query.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ pub enum TableFactor {
367367
alias: Option<TableAlias>,
368368
array_expr: Box<Expr>,
369369
with_offset: bool,
370+
with_offset_alias: Option<Ident>,
370371
},
371372
/// Represents a parenthesized table factor. The SQL spec only allows a
372373
/// join expression (`(foo <JOIN> bar [ <JOIN> baz ... ])`) to be nested,
@@ -423,6 +424,7 @@ impl fmt::Display for TableFactor {
423424
alias,
424425
array_expr,
425426
with_offset,
427+
with_offset_alias,
426428
} => {
427429
write!(f, "UNNEST({})", array_expr)?;
428430
if let Some(alias) = alias {
@@ -431,6 +433,9 @@ impl fmt::Display for TableFactor {
431433
if *with_offset {
432434
write!(f, " WITH OFFSET")?;
433435
}
436+
if let Some(alias) = with_offset_alias {
437+
write!(f, " AS {}", alias)?;
438+
}
434439
Ok(())
435440
}
436441
TableFactor::NestedJoin(table_reference) => write!(f, "({})", table_reference),

src/parser.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3842,10 +3842,18 @@ impl<'a> Parser<'a> {
38423842
Err(_) => false,
38433843
};
38443844

3845+
let with_offset_alias =
3846+
match self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS) {
3847+
Ok(Some(alias)) => Some(alias),
3848+
Ok(None) => None,
3849+
Err(e) => return Err(e),
3850+
};
3851+
38453852
Ok(TableFactor::UNNEST {
38463853
alias,
38473854
array_expr: Box::new(expr),
38483855
with_offset,
3856+
with_offset_alias,
38493857
})
38503858
} else {
38513859
let name = self.parse_object_name()?;

tests/sqlparser_common.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,11 +2837,22 @@ fn parse_table_function() {
28372837

28382838
#[test]
28392839
fn parse_unnest() {
2840-
fn chk(alias: bool, with_offset: bool, dialects: &TestedDialects, want: Vec<TableWithJoins>) {
2840+
fn chk(
2841+
alias: bool,
2842+
with_offset: bool,
2843+
with_offset_alias: bool,
2844+
dialects: &TestedDialects,
2845+
want: Vec<TableWithJoins>,
2846+
) {
28412847
let sql = &format!(
2842-
"SELECT * FROM UNNEST(expr){}{}",
2848+
"SELECT * FROM UNNEST(expr){}{}{}",
28432849
if alias { " AS numbers" } else { "" },
28442850
if with_offset { " WITH OFFSET" } else { "" },
2851+
if with_offset_alias {
2852+
" AS with_offset_alias"
2853+
} else {
2854+
""
2855+
},
28452856
);
28462857
let select = dialects.verified_only_select(sql);
28472858
assert_eq!(select.from, want);
@@ -2853,6 +2864,7 @@ fn parse_unnest() {
28532864
chk(
28542865
true,
28552866
true,
2867+
false,
28562868
&dialects,
28572869
vec![TableWithJoins {
28582870
relation: TableFactor::UNNEST {
@@ -2862,12 +2874,14 @@ fn parse_unnest() {
28622874
}),
28632875
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
28642876
with_offset: true,
2877+
with_offset_alias: None,
28652878
},
28662879
joins: vec![],
28672880
}],
28682881
);
28692882
// 2. neither Alias nor WITH OFFSET clause.
28702883
chk(
2884+
false,
28712885
false,
28722886
false,
28732887
&dialects,
@@ -2876,6 +2890,7 @@ fn parse_unnest() {
28762890
alias: None,
28772891
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
28782892
with_offset: false,
2893+
with_offset_alias: None,
28792894
},
28802895
joins: vec![],
28812896
}],
@@ -2884,12 +2899,14 @@ fn parse_unnest() {
28842899
chk(
28852900
false,
28862901
true,
2902+
false,
28872903
&dialects,
28882904
vec![TableWithJoins {
28892905
relation: TableFactor::UNNEST {
28902906
alias: None,
28912907
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
28922908
with_offset: true,
2909+
with_offset_alias: None,
28932910
},
28942911
joins: vec![],
28952912
}],
@@ -2898,6 +2915,26 @@ fn parse_unnest() {
28982915
chk(
28992916
true,
29002917
false,
2918+
false,
2919+
&dialects,
2920+
vec![TableWithJoins {
2921+
relation: TableFactor::UNNEST {
2922+
alias: Some(TableAlias {
2923+
name: Ident::new("numbers"),
2924+
columns: vec![],
2925+
}),
2926+
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
2927+
with_offset: false,
2928+
with_offset_alias: None,
2929+
},
2930+
joins: vec![],
2931+
}],
2932+
);
2933+
// 5. WITH OFFSET and WITH OFFSET Alias
2934+
chk(
2935+
true,
2936+
false,
2937+
true,
29012938
&dialects,
29022939
vec![TableWithJoins {
29032940
relation: TableFactor::UNNEST {
@@ -2907,6 +2944,7 @@ fn parse_unnest() {
29072944
}),
29082945
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
29092946
with_offset: false,
2947+
with_offset_alias: Some(Ident::new("with_offset_alias")),
29102948
},
29112949
joins: vec![],
29122950
}],

0 commit comments

Comments
 (0)