mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-21 22:44:08 +00:00
feat: Add WITH OFFSET Alias (#528)
* feat: Add WITH OFFSET Alias * update: fix with_offset_alias type
This commit is contained in:
parent
17c238bda7
commit
68768530cd
3 changed files with 53 additions and 2 deletions
|
@ -367,6 +367,7 @@ pub enum TableFactor {
|
||||||
alias: Option<TableAlias>,
|
alias: Option<TableAlias>,
|
||||||
array_expr: Box<Expr>,
|
array_expr: Box<Expr>,
|
||||||
with_offset: bool,
|
with_offset: bool,
|
||||||
|
with_offset_alias: Option<Ident>,
|
||||||
},
|
},
|
||||||
/// Represents a parenthesized table factor. The SQL spec only allows a
|
/// Represents a parenthesized table factor. The SQL spec only allows a
|
||||||
/// join expression (`(foo <JOIN> bar [ <JOIN> baz ... ])`) to be nested,
|
/// join expression (`(foo <JOIN> bar [ <JOIN> baz ... ])`) to be nested,
|
||||||
|
@ -423,6 +424,7 @@ impl fmt::Display for TableFactor {
|
||||||
alias,
|
alias,
|
||||||
array_expr,
|
array_expr,
|
||||||
with_offset,
|
with_offset,
|
||||||
|
with_offset_alias,
|
||||||
} => {
|
} => {
|
||||||
write!(f, "UNNEST({})", array_expr)?;
|
write!(f, "UNNEST({})", array_expr)?;
|
||||||
if let Some(alias) = alias {
|
if let Some(alias) = alias {
|
||||||
|
@ -431,6 +433,9 @@ impl fmt::Display for TableFactor {
|
||||||
if *with_offset {
|
if *with_offset {
|
||||||
write!(f, " WITH OFFSET")?;
|
write!(f, " WITH OFFSET")?;
|
||||||
}
|
}
|
||||||
|
if let Some(alias) = with_offset_alias {
|
||||||
|
write!(f, " AS {}", alias)?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
TableFactor::NestedJoin(table_reference) => write!(f, "({})", table_reference),
|
TableFactor::NestedJoin(table_reference) => write!(f, "({})", table_reference),
|
||||||
|
|
|
@ -3842,10 +3842,18 @@ impl<'a> Parser<'a> {
|
||||||
Err(_) => false,
|
Err(_) => false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let with_offset_alias =
|
||||||
|
match self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS) {
|
||||||
|
Ok(Some(alias)) => Some(alias),
|
||||||
|
Ok(None) => None,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
};
|
||||||
|
|
||||||
Ok(TableFactor::UNNEST {
|
Ok(TableFactor::UNNEST {
|
||||||
alias,
|
alias,
|
||||||
array_expr: Box::new(expr),
|
array_expr: Box::new(expr),
|
||||||
with_offset,
|
with_offset,
|
||||||
|
with_offset_alias,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let name = self.parse_object_name()?;
|
let name = self.parse_object_name()?;
|
||||||
|
|
|
@ -2837,11 +2837,22 @@ fn parse_table_function() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_unnest() {
|
fn parse_unnest() {
|
||||||
fn chk(alias: bool, with_offset: bool, dialects: &TestedDialects, want: Vec<TableWithJoins>) {
|
fn chk(
|
||||||
|
alias: bool,
|
||||||
|
with_offset: bool,
|
||||||
|
with_offset_alias: bool,
|
||||||
|
dialects: &TestedDialects,
|
||||||
|
want: Vec<TableWithJoins>,
|
||||||
|
) {
|
||||||
let sql = &format!(
|
let sql = &format!(
|
||||||
"SELECT * FROM UNNEST(expr){}{}",
|
"SELECT * FROM UNNEST(expr){}{}{}",
|
||||||
if alias { " AS numbers" } else { "" },
|
if alias { " AS numbers" } else { "" },
|
||||||
if with_offset { " WITH OFFSET" } else { "" },
|
if with_offset { " WITH OFFSET" } else { "" },
|
||||||
|
if with_offset_alias {
|
||||||
|
" AS with_offset_alias"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
);
|
);
|
||||||
let select = dialects.verified_only_select(sql);
|
let select = dialects.verified_only_select(sql);
|
||||||
assert_eq!(select.from, want);
|
assert_eq!(select.from, want);
|
||||||
|
@ -2853,6 +2864,7 @@ fn parse_unnest() {
|
||||||
chk(
|
chk(
|
||||||
true,
|
true,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
&dialects,
|
&dialects,
|
||||||
vec![TableWithJoins {
|
vec![TableWithJoins {
|
||||||
relation: TableFactor::UNNEST {
|
relation: TableFactor::UNNEST {
|
||||||
|
@ -2862,12 +2874,14 @@ fn parse_unnest() {
|
||||||
}),
|
}),
|
||||||
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
|
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
|
||||||
with_offset: true,
|
with_offset: true,
|
||||||
|
with_offset_alias: None,
|
||||||
},
|
},
|
||||||
joins: vec![],
|
joins: vec![],
|
||||||
}],
|
}],
|
||||||
);
|
);
|
||||||
// 2. neither Alias nor WITH OFFSET clause.
|
// 2. neither Alias nor WITH OFFSET clause.
|
||||||
chk(
|
chk(
|
||||||
|
false,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
&dialects,
|
&dialects,
|
||||||
|
@ -2876,6 +2890,7 @@ fn parse_unnest() {
|
||||||
alias: None,
|
alias: None,
|
||||||
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
|
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
|
||||||
with_offset: false,
|
with_offset: false,
|
||||||
|
with_offset_alias: None,
|
||||||
},
|
},
|
||||||
joins: vec![],
|
joins: vec![],
|
||||||
}],
|
}],
|
||||||
|
@ -2884,12 +2899,14 @@ fn parse_unnest() {
|
||||||
chk(
|
chk(
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
&dialects,
|
&dialects,
|
||||||
vec![TableWithJoins {
|
vec![TableWithJoins {
|
||||||
relation: TableFactor::UNNEST {
|
relation: TableFactor::UNNEST {
|
||||||
alias: None,
|
alias: None,
|
||||||
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
|
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
|
||||||
with_offset: true,
|
with_offset: true,
|
||||||
|
with_offset_alias: None,
|
||||||
},
|
},
|
||||||
joins: vec![],
|
joins: vec![],
|
||||||
}],
|
}],
|
||||||
|
@ -2898,6 +2915,7 @@ fn parse_unnest() {
|
||||||
chk(
|
chk(
|
||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
|
false,
|
||||||
&dialects,
|
&dialects,
|
||||||
vec![TableWithJoins {
|
vec![TableWithJoins {
|
||||||
relation: TableFactor::UNNEST {
|
relation: TableFactor::UNNEST {
|
||||||
|
@ -2907,6 +2925,26 @@ fn parse_unnest() {
|
||||||
}),
|
}),
|
||||||
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
|
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
|
||||||
with_offset: false,
|
with_offset: false,
|
||||||
|
with_offset_alias: None,
|
||||||
|
},
|
||||||
|
joins: vec![],
|
||||||
|
}],
|
||||||
|
);
|
||||||
|
// 5. WITH OFFSET and WITH OFFSET Alias
|
||||||
|
chk(
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
&dialects,
|
||||||
|
vec![TableWithJoins {
|
||||||
|
relation: TableFactor::UNNEST {
|
||||||
|
alias: Some(TableAlias {
|
||||||
|
name: Ident::new("numbers"),
|
||||||
|
columns: vec![],
|
||||||
|
}),
|
||||||
|
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
|
||||||
|
with_offset: false,
|
||||||
|
with_offset_alias: Some(Ident::new("with_offset_alias")),
|
||||||
},
|
},
|
||||||
joins: vec![],
|
joins: vec![],
|
||||||
}],
|
}],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue