Skip to content

Conversation

@watarukura
Copy link
Contributor

https://github.com/sqlparser-rs/sqlparser-rs/pull/425/files#r816162124

  • add DataType for UNSIGNED
  • select cast(x as unsigned) is no longer panic
  • create table foo (bar int unsigned) is no longer panic
❯ cat select_cast.sql 
select cast(x as unsigned) from bar;
select cast(y as int unsigned) from baz;
❯ cargo run --features json_example --example cli select_cast.sql --mysql
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/examples/cli select_cast.sql --mysql`
Parsing from file 'select_cast.sql' using MySqlDialect
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] Parsing sql 'select cast(x as unsigned) from bar;
select cast(y as int unsigned) from baz;'...
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] parsing expr
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] parsing expr
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] prefix: Identifier(Ident { value: "x", quote_style: None })
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] get_next_precedence() Word(Word { value: "as", quote_style: None, keyword: AS })
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] next precedence: 0
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] prefix: Cast { expr: Identifier(Ident { value: "x", quote_style: None }), data_type: Custom(ObjectName([Ident { value: "unsigned", quote_style: None }])) }
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] get_next_precedence() Word(Word { value: "from", quote_style: None, keyword: FROM })
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] next precedence: 0
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] parsing expr
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] parsing expr
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] prefix: Identifier(Ident { value: "y", quote_style: None })
2022-03-06T05:45:40.058Z DEBUG [sqlparser::parser] get_next_precedence() Word(Word { value: "as", quote_style: None, keyword: AS })
2022-03-06T05:45:40.059Z DEBUG [sqlparser::parser] next precedence: 0
2022-03-06T05:45:40.059Z DEBUG [sqlparser::parser] prefix: Cast { expr: Identifier(Ident { value: "y", quote_style: None }), data_type: UnsignedInt(None) }
2022-03-06T05:45:40.059Z DEBUG [sqlparser::parser] get_next_precedence() Word(Word { value: "from", quote_style: None, keyword: FROM })
2022-03-06T05:45:40.059Z DEBUG [sqlparser::parser] next precedence: 0
Round-trip:
'SELECT CAST(x AS unsigned) FROM bar
SELECT CAST(y AS INT UNSIGNED) FROM baz'
Serialized as JSON:
[
  {
    "Query": {
      "with": null,
      "body": {
        "Select": {
          "distinct": false,
          "top": null,
          "projection": [
            {
              "UnnamedExpr": {
                "Cast": {
                  "expr": {
                    "Identifier": {
                      "value": "x",
                      "quote_style": null
                    }
                  },
                  "data_type": {
                    "Custom": [
                      {
                        "value": "unsigned",
                        "quote_style": null
                      }
                    ]
                  }
                }
              }
            }
          ],
          "from": [
            {
              "relation": {
                "Table": {
                  "name": [
                    {
                      "value": "bar",
                      "quote_style": null
                    }
                  ],
                  "alias": null,
                  "args": [],
                  "with_hints": []
                }
              },
              "joins": []
            }
          ],
          "lateral_views": [],
          "selection": null,
          "group_by": [],
          "cluster_by": [],
          "distribute_by": [],
          "sort_by": [],
          "having": null
        }
      },
      "order_by": [],
      "limit": null,
      "offset": null,
      "fetch": null,
      "lock": null
    }
  },
  {
    "Query": {
      "with": null,
      "body": {
        "Select": {
          "distinct": false,
          "top": null,
          "projection": [
            {
              "UnnamedExpr": {
                "Cast": {
                  "expr": {
                    "Identifier": {
                      "value": "y",
                      "quote_style": null
                    }
                  },
                  "data_type": {
                    "UnsignedInt": null
                  }
                }
              }
            }
          ],
          "from": [
            {
              "relation": {
                "Table": {
                  "name": [
                    {
                      "value": "baz",
                      "quote_style": null
                    }
                  ],
                  "alias": null,
                  "args": [],
                  "with_hints": []
                }
              },
              "joins": []
            }
          ],
          "lateral_views": [],
          "selection": null,
          "group_by": [],
          "cluster_by": [],
          "distribute_by": [],
          "sort_by": [],
          "having": null
        }
      },
      "order_by": [],
      "limit": null,
      "offset": null,
      "fetch": null,
      "lock": null
    }
  }
]

@coveralls
Copy link

Pull Request Test Coverage Report for Build 1940313760

  • 61 of 66 (92.42%) changed or added relevant lines in 3 files are covered.
  • 3 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.03%) to 90.574%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/ast/data_type.rs 18 20 90.0%
src/parser.rs 16 19 84.21%
Files with Coverage Reduction New Missed Lines %
src/ast/data_type.rs 1 85.94%
src/parser.rs 2 84.24%
Totals Coverage Status
Change from base Build 1916566413: 0.03%
Covered Lines: 7226
Relevant Lines: 7978

💛 - Coveralls

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.

Thanks @watarukura -- looks good to me.

Thanks @houqp for the review

@alamb alamb merged commit 994b86a into apache:main Mar 7, 2022
@alamb alamb changed the title mysql unsigned datatype Support for mysql unsigned tinyint, unsigned int and unsigned bigint datatypes Mar 7, 2022
@alamb alamb changed the title Support for mysql unsigned tinyint, unsigned int and unsigned bigint datatypes Support for mysql unsigned tinyint, unsigned int, unsigned smallint and unsigned bigint datatypes Mar 7, 2022
@alamb
Copy link
Contributor

alamb commented Mar 7, 2022

see also #425

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.

4 participants