Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bigquery: IGNORE NULLS ORDER BY support #22

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2055,7 +2055,6 @@ pub enum Statement {
/// Table flag
table_flag: Option<ObjectName>,
/// Table name

#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
table_name: ObjectName,
has_as: bool,
Expand Down Expand Up @@ -3602,13 +3601,15 @@ pub struct OnConflict {
pub conflict_target: Option<ConflictTarget>,
pub action: OnConflictAction,
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum ConflictTarget {
Columns(Vec<WithSpan<Ident>>),
OnConstraint(ObjectName),
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
Expand Down Expand Up @@ -3639,6 +3640,7 @@ impl fmt::Display for OnInsert {
}
}
}

impl fmt::Display for OnConflict {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, " ON CONFLICT")?;
Expand All @@ -3648,6 +3650,7 @@ impl fmt::Display for OnConflict {
write!(f, " {}", self.action)
}
}

impl fmt::Display for ConflictTarget {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand All @@ -3656,6 +3659,7 @@ impl fmt::Display for ConflictTarget {
}
}
}

impl fmt::Display for OnConflictAction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand Down Expand Up @@ -4009,10 +4013,10 @@ impl fmt::Display for Function {
if self.special {
write!(f, "{}", self.name)?;
} else {
let order_by = if !self.order_by.is_empty() {
" ORDER BY "
let order_by: String = if !self.order_by.is_empty() {
format!(" ORDER BY {}", display_comma_separated(&self.order_by))
} else {
""
"".to_string()
};

let null_treatment = if let Some(null_treatment) = self.null_treatment {
Expand All @@ -4038,11 +4042,10 @@ impl fmt::Display for Function {

write!(
f,
"{}({}{}{order_by}{}{limit}{on_overflow}){null_treatment}",
"{}({}{}{order_by}{limit}{on_overflow}){null_treatment}",
self.name,
if self.distinct { "DISTINCT " } else { "" },
display_comma_separated(&self.args),
display_comma_separated(&self.order_by),
)?;

if let Some(within_group) = &self.within_group {
Expand Down
34 changes: 18 additions & 16 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ mod recursion {
Self { remaining_depth }
}
}

impl Drop for DepthGuard {
fn drop(&mut self) {
self.remaining_depth.fetch_add(1, Ordering::SeqCst);
Expand Down Expand Up @@ -959,7 +960,7 @@ impl<'a> Parser<'a> {
return parser_err!(
format!("Expected identifier, found: {tok}"),
tok.span.start
)
);
}
};
Ok(Expr::CompositeAccess {
Expand Down Expand Up @@ -2466,7 +2467,7 @@ impl<'a> Parser<'a> {
.get(max(2, self.index) - 2)
.map(|t| t.span.end.span_to(t.span.end));
TokenWithLocation::new(Token::EOF, eof_span.unwrap_or_default())
})
});
}
}
}
Expand Down Expand Up @@ -5724,12 +5725,12 @@ impl<'a> Parser<'a> {
Token::EOF => {
return Err(ParserError::ParserError(
"Empty input when parsing identifier".to_string(),
))?
))?;
}
token => {
return Err(ParserError::ParserError(format!(
"Unexpected token in identifier: {token}"
)))?
)))?;
}
};

Expand All @@ -5742,19 +5743,19 @@ impl<'a> Parser<'a> {
Token::EOF => {
return Err(ParserError::ParserError(
"Trailing period in identifier".to_string(),
))?
))?;
}
token => {
return Err(ParserError::ParserError(format!(
"Unexpected token following period in identifier: {token}"
)))?
)))?;
}
},
Token::EOF => break,
token => {
return Err(ParserError::ParserError(format!(
"Unexpected token in identifier: {token}"
)))?
)))?;
}
}
}
Expand Down Expand Up @@ -5941,7 +5942,7 @@ impl<'a> Parser<'a> {
return parser_err!(
format!("unbalanced parens: {}", modifier),
next_token.span.start
)
);
}
Token::LParen => {
modifier.push_str(&next_token.to_string());
Expand Down Expand Up @@ -6831,7 +6832,7 @@ impl<'a> Parser<'a> {
_ => {
return Err(ParserError::ParserError(format!(
"expected OUTER, SEMI, ANTI or JOIN after {kw:?}"
)))
)));
}
}
}
Expand Down Expand Up @@ -7699,6 +7700,14 @@ impl<'a> Parser<'a> {
None
};

let null_treatment = if self.parse_keywords(&[Keyword::IGNORE, Keyword::NULLS]) {
Some(NullTreatment::IGNORE)
} else if self.parse_keywords(&[Keyword::RESPECT, Keyword::NULLS]) {
Some(NullTreatment::RESPECT)
} else {
None
};

let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
self.parse_comma_separated(Parser::parse_order_by_expr)?
} else {
Expand All @@ -7710,13 +7719,6 @@ impl<'a> Parser<'a> {
None
};

let null_treatment = if self.parse_keywords(&[Keyword::IGNORE, Keyword::NULLS]) {
Some(NullTreatment::IGNORE)
} else if self.parse_keywords(&[Keyword::RESPECT, Keyword::NULLS]) {
Some(NullTreatment::RESPECT)
} else {
None
};
self.expect_token(&Token::RParen)?;
Ok((args, on_overflow, order_by, limit, null_treatment))
}
Expand Down
Loading
Loading