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

Support relation visitor to visit the Option field #1556

Merged
merged 3 commits into from
Nov 29, 2024

Conversation

goldmedal
Copy link
Contributor

@goldmedal goldmedal commented Nov 26, 2024

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))]
pub struct ShowStatementIn {
    pub clause: ShowStatementInClause,
    pub parent_type: Option<ShowStatementInParentType>,
    #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
    pub parent_name: Option<ObjectName>,
}

This will generate

impl sqlparser::ast::Visit for ShowStatementIn {
    fn visit<V: sqlparser::ast::Visitor>(
        &self,
        visitor: &mut V,
    ) -> ::std::ops::ControlFlow<V::Break> {
        sqlparser::ast::Visit::visit(&self.clause, visitor)?;
        sqlparser::ast::Visit::visit(&self.parent_type, visitor)?;
        if let Some(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::VisitMut for ShowStatementIn {
    fn visit<V: sqlparser::ast::VisitorMut>(
        &mut self,
        visitor: &mut V,
    ) -> ::std::ops::ControlFlow<V::Break> {
        sqlparser::ast::VisitMut::visit(&mut self.clause, visitor)?;
        sqlparser::ast::VisitMut::visit(&mut self.parent_type, visitor)?;
        if let Some(value) = &mut self.parent_name {
            visitor.pre_visit_relation(value)?;
            sqlparser::ast::VisitMut::visit(value, visitor)?;
            visitor.post_visit_relation(value)?;
        }
        ::std::ops::ControlFlow::Continue(())
    }
}

@goldmedal goldmedal changed the title Support to visit the option field Support to visit the Option field Nov 26, 2024
@alamb alamb changed the title Support to visit the Option field Support relation visitor to visit the Option field Nov 28, 2024
Copy link
Contributor

@alamb alamb left a comment

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

@@ -7653,6 +7653,7 @@ impl fmt::Display for ShowStatementInParentType {
pub struct ShowStatementIn {
pub clause: ShowStatementInClause,
pub parent_type: Option<ShowStatementInParentType>,
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

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.

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
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
Contributor Author

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.

Copy link
Contributor

@iffyio iffyio left a comment

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
Contributor

alamb commented Nov 29, 2024

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

@alamb alamb merged commit 92c6e7f into apache:main Nov 29, 2024
8 checks passed
Sign up for free to 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