Git Product home page Git Product logo

antlr4-snowflake's Introduction

Antlr4 Snowflake Python

This repo is a work in progress, looking to explore how some tools fit together, and do some hello-world-y stuff w/ Antlr4.

Resources

pip Dependencies

pip install -r requirements.txt

Fetch snowflake parser grammar

wget https://raw.githubusercontent.com/antlr/grammars-v4/master/sql/snowflake/SnowflakeLexer.g4
wget https://raw.githubusercontent.com/antlr/grammars-v4/master/sql/snowflake/SnowflakeParser.g4

Codegen and Parsing

Use antlr4 from the antlr4-tools pip pacakge to compile the grammars to python code:

antlr4 -Dlanguage=Python3 SnowflakeLexer.g4 
antlr4 -Dlanguage=Python3 SnowflakeParser.g4

Next, we can parse the small example query, shown below from query_small.sql:

SELECT COUNT(*) as weekly_active_users
FROM users
WHERE DATEDIFF('day', last_active, CURRENT_TIMESTAMP) < 7
cat query_small.sql | python print_query_tree.py

This will print the parsed tree for the query

  (query_statement
    (select_statement
      (select_clause SELECT
        (select_list_no_top
          (select_list
            (select_list_elem
              (expression_elem
                (expr
                  (function_call
                    (aggregate_function
                      (id_
                        (builtin_function COUNT))
                      ( * ))))
                (as_alias AS
                  (alias
                    (id_ weekly_active_users))))))))
      (select_optional_clauses
        (from_clause FROM
          (table_sources
            (table_source
              (table_source_item_joined
                (object_ref
                  (object_name
                    (id_ active_users)))))))
        (where_clause WHERE
          (search_condition
            (predicate
              (expr
                (ternary_builtin_function DATEDIFF)
                (
                  (expr
                    (primitive_expression
                      (literal 'day'))),
                  (expr
                    (primitive_expression
                      (id_ last_active))),
                  (expr
                    (primitive_expression
                      (id_
                        (builtin_function CURRENT_TIMESTAMP)))) ))
              (comparison_operator <)
              (expr
                (primitive_expression
                  (literal 7)))))))))

A Bigger Query

Try it yourself with the larger, more complex query.

cat query_big.sql | python print_query_tree.py

Acknowledgements / References

antlr4-snowflake's People

Contributors

dexhorthy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.