Skip to content

Support relation visitor to visit the Option field - #1556

Merged
alamb merged 3 commits into
apache:mainfrom
goldmedal:fix/1554-support-visit-option
Nov 29, 2024
Merged

Support relation visitor to visit the Option field#1556
alamb merged 3 commits into
apache:mainfrom
goldmedal:fix/1554-support-visit-option

Conversation

@goldmedal

@goldmedalgoldmedal commented Nov 26, 2024

Copy link
Copy Markdown
Contributor

close#1554

If the field is a Option and add #[with = "visit_xxx"] to the field, the generated code
will try to access the field only if it is Some:

#[cfg_attr(feature = "visitor", derive(Visit,VisitMut))]pubstructShowStatementIn{pubclause:ShowStatementInClause,pubparent_type:Option<ShowStatementInParentType>,#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]pubparent_name:Option<ObjectName>,}

This will generate

impl sqlparser::ast::VisitforShowStatementIn{fnvisit<V: sqlparser::ast::Visitor>(&self,visitor:&mutV,) -> ::std::ops::ControlFlow<V::Break>{
sqlparser::ast::Visit::visit(&self.clause, visitor)?;
sqlparser::ast::Visit::visit(&self.parent_type, visitor)?;ifletSome(value) = &self.parent_name{
visitor.pre_visit_relation(value)?;
sqlparser::ast::Visit::visit(value, visitor)?;
visitor.post_visit_relation(value)?;}::std::ops::ControlFlow::Continue(())}}impl sqlparser::ast::VisitMutforShowStatementIn{fnvisit<V: sqlparser::ast::VisitorMut>(&mutself,visitor:&mutV,) -> ::std::ops::ControlFlow<V::Break>{
sqlparser::ast::VisitMut::visit(&mutself.clause, visitor)?;
sqlparser::ast::VisitMut::visit(&mutself.parent_type, visitor)?;ifletSome(value) = &mutself.parent_name{
visitor.pre_visit_relation(value)?;
sqlparser::ast::VisitMut::visit(value, visitor)?;
visitor.post_visit_relation(value)?;}::std::ops::ControlFlow::Continue(())}}

@goldmedalgoldmedal changed the title Support to visit the option fieldSupport to visit the Option fieldNov 26, 2024
@alambalamb changed the title Support to visit the Option fieldSupport relation visitor to visit the Option fieldNov 28, 2024

@alambalamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @goldmedal . I think there were some other fields that have some wrapping structure simply because this feature was missing

I think we will also have to release a new version of sqlparser derive (which we don't always do) as part of this release given the change to the proc macro; I left a note to remind us here

I will also test this with DataFusion as well and report back

Comment threadsrc/ast/mod.rs
pub struct ShowStatementIn {
pub clause: ShowStatementInClause,
pub parent_type: Option<ShowStatementInParentType>,
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #1568 to track it.

Comment threadderive/src/lib.rs
fn is_option(ty: &Type) -> bool {
if let Type::Path(TypePath { path: Path { segments, .. }, .. }) = ty {
if let Some(segment) = segments.last() {
if segment.ident == "Option" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems in theory this would match anything called Option (even if it wasn't std::Option) but I think that seems ok to me (I don't think we'll have anything else realistically)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I think we won't have another struct called Option in this project 🤔. Or we can match the full path with std::option::Option or core::option::Option if required.

@iffyioiffyio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @goldmedal!

@alamb

Copy link
Copy Markdown
Contributor

I double checked with apache/datafusion#13546 and this works great. Thank you again @goldmedal and @iffyio

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Relation visitor fails to visit the SHOW COLUMNS statement in the latest commit of the main branch

3 participants

@goldmedal@alamb@iffyio