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

Include (Python) listener #79

Merged
merged 2 commits into from
Apr 5, 2023
Merged

Include (Python) listener #79

merged 2 commits into from
Apr 5, 2023

Conversation

svengiegerich
Copy link
Contributor

Motivation: I'd like to use our grammar's (Python) listener to facilitate the parser's utility beyond SQL execution.
More concretely, I'm currently playing around with extracting a dependency tree for my SQL-based pipeline based on the listener.
👉 I propose to ship the listener within the package

Changes: Removed arg no-listener for Python target & re-generated grammar

@ivergara
Copy link
Collaborator

@svengiegerich just a small suggestion, keep the changes in the generating code from the changes in the generated code in different commits. In this case is not critical, but when there are multiple manual changes, particularly big ones, it's hard to distinguish from the autogenerated code.

@svengiegerich
Copy link
Contributor Author

@svengiegerich just a small suggestion, keep the changes in the generating code from the changes in the generated code in different commits. In this case is not critical, but when there are multiple manual changes, particularly big ones, it's hard to distinguish from the autogenerated code.

Thanks! You're right, I was lazy this time 🙈

@svengiegerich
Copy link
Contributor Author

(In light of PR #73, I'll need to update this PR before the review can continue)

@svengiegerich svengiegerich marked this pull request as draft April 3, 2023 06:16
@svengiegerich svengiegerich marked this pull request as ready for review April 3, 2023 07:50
Copy link
Member

@SimeonStoykovQC SimeonStoykovQC left a comment

Choose a reason for hiding this comment

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

Could consider adding a test that asserts one of the "listener" callbacks is called when parsing a simple SQL statement.

@svengiegerich
Copy link
Contributor Author

Thanks, @SimeonStoykovQC.

Could consider adding a test that asserts one of the "listener" callbacks is called when parsing a simple SQL statement.

I do like the idea! But how would I do this? Do I need to mock the class TSqlParserListener?

@SimeonStoykovQC
Copy link
Member

Do I need to mock the class TSqlParserListener?

You can try, yes. Do you have an idea how you will use it -- from what I see it's just method stubs with pass? Can you provide your implementation of the listener interface upon parsing?

@svengiegerich
Copy link
Contributor Author

Do you have an idea how you will use it -- from what I see it's just method stubs with pass?
Can you provide your implementation of the listener interface upon parsing?

In the end - outside of the package - I'd like to do smth like this:

class DependencyFinder(TSqlParserListener.ParseTreeListener):
    def __init__(self):
        self.table = ""
        self.visit_source = 0
        self.table_dependencies = []

    def enterTable_sources(self, ctx):
        self.visit_source+=1
        
    def exitTable_sources(self, ctx):
        self.visit_source-=1

    def enterFull_table_name(self, ctx):
        if self.visit_source > 0:
            self.table_dependencies.append(ctx.getText())
        else:
            self.table = ctx.getText()

    def enterTable_name(self, ctx):
        if self.visit_source > 0:
            self.table_dependencies.extend(ctx.getText())
        else:
            self.table = ctx.getText()

    def enterSimple_name(self, ctx):
        self.table = ctx.getText()

    def print_results(self):
        print(f"Table: {self.table}")
        print(f"Dependencies: {self.table_dependencies}")

tree = parse(InputStream(data=sql), "tsql_file")
walker = antlr4.ParseTreeWalker()
dependency_finder = DependencyFinder()
walker.walk(dependency_finder, tree)
dependency_finder.print_results()

@svengiegerich
Copy link
Contributor Author

Merging now; I'll add the test separately.

@svengiegerich svengiegerich merged commit 7640a43 into main Apr 5, 2023
@svengiegerich svengiegerich deleted the sg_add_python_listener branch April 5, 2023 12:13
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.

3 participants