sqlparser-rs / sqlparser-rs

Extensible SQL Lexer and Parser for Rust
Apache License 2.0
2.69k stars 506 forks source link

How to interprete 'B = T1.A' and 'T1.A AS B' as equivalent? (convert "UnnamedExpr" with "op": "Eq" into "ExprWithAlias") #272

Open aisbergde opened 4 years ago

aisbergde commented 4 years ago

in views mssqlserver allows 2 ways to define a columns alias. I would like to "normalize" the representation in json. I would like to have the same style in JSON for the same logic. There could be a property to indicate how this was in the original expression to not lost this information.

The best would be, if I could convert the presentation of B = T1.A into the presentation of T1.A AS B
but it would also be OK to convert T1.C AS D into D = T1.C

CREATE VIEW aaa.test01 AS SELECT B = T1.A, T1.C AS D FROM aaa.bbb AS T1

Serialized as JSON:

[
  {
    "CreateView": {
      "or_replace": false,
      "materialized": false,
      "name": [
        {
          "value": "aaa",
          "quote_style": null
        },
        {
          "value": "test01",
          "quote_style": null
        }
      ],
      "columns": [],
      "query": {
        "ctes": [],
        "body": {
          "Select": {
            "distinct": false,
            "top": null,
            "projection": [
              {
                "UnnamedExpr": {
                  "BinaryOp": {
                    "left": {
                      "Identifier": {
                        "value": "B",
                        "quote_style": null
                      }
                    },
                    "op": "Eq",
                    "right": {
                      "CompoundIdentifier": [
                        {
                          "value": "T1",
                          "quote_style": null
                        },
                        {
                          "value": "A",
                          "quote_style": null
                        }
                      ]
                    }
                  }
                }
              },
              {
                "ExprWithAlias": {
                  "expr": {
                    "CompoundIdentifier": [
                      {
                        "value": "T1",
                        "quote_style": null
                      },
                      {
                        "value": "C",
                        "quote_style": null
                      }
                    ]
                  },
                  "alias": {
                    "value": "D",
                    "quote_style": null
                  }
                }
              }
            ],
            "from": [
              {
                "relation": {
                  "Table": {
                    "name": [
                      {
                        "value": "aaa",
                        "quote_style": null
                      },
                      {
                        "value": "bbb",
                        "quote_style": null
                      }
                    ],
                    "alias": {
                      "name": {
                        "value": "T1",
                        "quote_style": null
                      },
                      "columns": []
                    },
                    "args": [],
                    "with_hints": []
                  }
                },
                "joins": []
              }
            ],
            "selection": null,
            "group_by": [],
            "having": null
          }
        },
        "order_by": [],
        "limit": null,
        "offset": null,
        "fetch": null
      },
      "with_options": []
    }
  }
]
nickolay commented 4 years ago

I'm not sure what's the question here. It's not the job of the parser to figure out the semantics of the query. I'm not aware of an existing library that does this kind of normalization. Implementing this manually should be straightforward, right?

aisbergde commented 4 years ago

OK, I got this and you are right, it is not part of the parser to manipulate the result but this should be done outside and after parsing.