GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - systemxlabs/sqlparser-nom: A SQL query parser written using nom.

This repository was archived by the owner on Apr 18, 2025. It is now read-only.
BranchesTags
Open more actions menu

Latest commit

 

History

56 Commits
56 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
Cargo.lock
Cargo.lock
 
 
Cargo.toml
Cargo.toml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 

Repository files navigation

sqlparser-nom

A SQL query parser written using nom.

  • Query
    • Select
    • From
    • Where
    • Order by
    • Limit
    • CTE
    • Group by
    • Having
    • Aggregate
    • Window
  • Pratt Parsing
  • Friendly error info

Example

More examples can be found in tests.

select a, count(*)
from (select * from t1) as t2
join t3 on t2.a = t3.a
left join t4 on t3.b = t4.b
where a > ((1 + 2) * 3) and b < c 
group by a, c 
having count(*) > 5
order by a, b desc 
limit 1, 2

output ast:

SelectStatement {
    body: Select {
        projection: [
            UnnamedExpr(
                ColumnRef {
                    database: None,
                    table: None,
                    column: Ident {
                        value: "a",
                    },
                },
            ),
            UnnamedExpr(
                Function {
                    name: Ident {
                        value: "count",
                    },
                    distinct: false,
                    args: [
                        Wildcard,
                    ],
                },
            ),
        ],
        from: Some(
            Join {
                op: LeftOuter,
                condition: On(
                    BinaryOp {
                        left: ColumnRef {
                            database: None,
                            table: Some(
                                Ident {
                                    value: "t3",
                                },
                            ),
                            column: Ident {
                                value: "b",
                            },
                        },
                        op: Eq,
                        right: ColumnRef {
                            database: None,
                            table: Some(
                                Ident {
                                    value: "t4",
                                },
                            ),
                            column: Ident {
                                value: "b",
                            },
                        },
                    },
                ),
                left: Join {
                    op: Inner,
                    condition: On(
                        BinaryOp {
                            left: ColumnRef {
                                database: None,
                                table: Some(
                                    Ident {
                                        value: "t2",
                                    },
                                ),
                                column: Ident {
                                    value: "a",
                                },
                            },
                            op: Eq,
                            right: ColumnRef {
                                database: None,
                                table: Some(
                                    Ident {
                                        value: "t3",
                                    },
                                ),
                                column: Ident {
                                    value: "a",
                                },
                            },
                        },
                    ),
                    left: Subquery {
                        subquery: SelectStatement {
                            body: Select {
                                projection: [
                                    Wildcard,
                                ],
                                from: Some(
                                    BaseTable {
                                        name: TableName {
                                            database: None,
                                            table: Ident {
                                                value: "t1",
                                            },
                                        },
                                        alias: None,
                                    },
                                ),
                                selection: None,
                                group_by: [],
                                having: None,
                            },
                            order_by: [],
                            limit: None,
                            offset: None,
                        },
                        alias: Some(
                            Ident {
                                value: "t2",
                            },
                        ),
                    },
                    right: BaseTable {
                        name: TableName {
                            database: None,
                            table: Ident {
                                value: "t3",
                            },
                        },
                        alias: None,
                    },
                },
                right: BaseTable {
                    name: TableName {
                        database: None,
                        table: Ident {
                            value: "t4",
                        },
                    },
                    alias: None,
                },
            },
        ),
        selection: Some(
            BinaryOp {
                left: BinaryOp {
                    left: ColumnRef {
                        database: None,
                        table: None,
                        column: Ident {
                            value: "a",
                        },
                    },
                    op: Gt,
                    right: BinaryOp {
                        left: BinaryOp {
                            left: Literal(
                                UnsignedInteger(
                                    1,
                                ),
                            ),
                            op: Add,
                            right: Literal(
                                UnsignedInteger(
                                    2,
                                ),
                            ),
                        },
                        op: Mul,
                        right: Literal(
                            UnsignedInteger(
                                3,
                            ),
                        ),
                    },
                },
                op: And,
                right: BinaryOp {
                    left: ColumnRef {
                        database: None,
                        table: None,
                        column: Ident {
                            value: "b",
                        },
                    },
                    op: Lt,
                    right: ColumnRef {
                        database: None,
                        table: None,
                        column: Ident {
                            value: "c",
                        },
                    },
                },
            },
        ),
        group_by: [
            ColumnRef {
                database: None,
                table: None,
                column: Ident {
                    value: "a",
                },
            },
            ColumnRef {
                database: None,
                table: None,
                column: Ident {
                    value: "c",
                },
            },
        ],
        having: Some(
            BinaryOp {
                left: Function {
                    name: Ident {
                        value: "count",
                    },
                    distinct: false,
                    args: [
                        Wildcard,
                    ],
                },
                op: Gt,
                right: Literal(
                    UnsignedInteger(
                        5,
                    ),
                ),
            },
        ),
    },
    order_by: [
        OrderByExpr {
            expr: ColumnRef {
                database: None,
                table: None,
                column: Ident {
                    value: "a",
                },
            },
            asc: None,
        },
        OrderByExpr {
            expr: ColumnRef {
                database: None,
                table: None,
                column: Ident {
                    value: "b",
                },
            },
            asc: Some(
                false,
            ),
        },
    ],
    limit: Some(
        Literal(
            UnsignedInteger(
                1,
            ),
        ),
    ),
    offset: Some(
        Literal(
            UnsignedInteger(
                2,
            ),
        ),
    ),
}

References

  • SQL92 Standard
  • Antlr Grammars for SQL
  • PostgreSQL Doc
  • BNF Grammars for SQL-92, SQL-99 and SQL-2003
  • Queries - Microsoft SQL Doc
  • Simple but Powerful Pratt Parsing
  • 手写一个Parser - 代码简单而功能强大的Pratt Parsing
  • sqlparser-rs
  • databend

About

A SQL query parser written using nom.

Topics

databaselogosnomparserrustsql

Resources

Readme
MIT license
Activity
Custom properties

Stars

77 stars

Watchers

1 watching

Forks

8 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.