Support SELECT DISTINCT

This commit is contained in:
Nickolay Ponomarev 2019-04-20 14:15:05 +03:00
parent 31b6076d05
commit bbf1805729
3 changed files with 17 additions and 1 deletions

View file

@ -110,6 +110,7 @@ impl ToString for SQLSetOperator {
/// to a set operation like `UNION`.
#[derive(Debug, Clone, PartialEq)]
pub struct SQLSelect {
pub distinct: bool,
/// projection expressions
pub projection: Vec<SQLSelectItem>,
/// FROM
@ -127,7 +128,8 @@ pub struct SQLSelect {
impl ToString for SQLSelect {
fn to_string(&self) -> String {
let mut s = format!(
"SELECT {}",
"SELECT{} {}",
if self.distinct { " DISTINCT" } else { "" },
self.projection
.iter()
.map(|p| p.to_string())

View file

@ -1352,6 +1352,7 @@ impl Parser {
/// Parse a restricted `SELECT` statement (no CTEs / `UNION` / `ORDER BY`),
/// assuming the initial `SELECT` was already consumed
pub fn parse_select(&mut self) -> Result<SQLSelect, ParserError> {
let distinct = self.parse_keyword("DISTINCT");
let projection = self.parse_select_list()?;
let (relation, joins) = if self.parse_keyword("FROM") {
@ -1381,6 +1382,7 @@ impl Parser {
};
Ok(SQLSelect {
distinct,
projection,
selection,
relation,