Skip to content

[BUG] Subquery aliasing doesn't work with ` characters #550

Closed

Description

What is the bug?
Subquery aliasing doesn't work with ` characters.

How can one reproduce the bug?
Steps to reproduce the behavior:

  1. Make a query for
SELECT `a`.`LOD - Fixed - 1 (copy 2)` AS `LOD - Fixed - 1 (copy 2)`
FROM (
  SELECT MIN(`Staples`.`Order Date`) AS `LOD - Fixed - 1 (copy 2)`
  FROM `staples` `Staples`
  GROUP BY `Staples`.`Customer Name`
) `a`
GROUP BY `LOD - Fixed - 1 (copy 2)`
ORDER BY `LOD - Fixed - 1 (copy 2)` ASC
  1. See "ClassCastException"

What is the expected behavior?
Behaviour should match if the alias of the inner query is just a which is a valid result.

What is your host/environment?

  • OpenSearch 1.3.1

Activity

  1. ghost added
    bug
    on Apr 12, 2022
  2. added
    tdvt
    on Apr 12, 2022
  3. dai-chen commented on Dec 15, 2022

    @dai-chen
    Collaborator

    I can reproduce the issue by ODS sample data in playground:

    POST _plugins/_sql
    {
      "query": """
        SELECT
          `a`.`Origin` AS `Origin`,
          AVG(`AvgTicketPrice`) AS `avgPrice`
        FROM (
          SELECT `Origin`, `AvgTicketPrice`
          FROM `opensearch_dashboards_sample_data_flights`
          WHERE `FlightDelay` = True
        ) AS `a`
        GROUP BY `a`.`Origin`
      """
    }
    
    {
      "error": {
        "reason": "There was internal problem at backend",
        "details": "class com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource cannot be cast to class com.alibaba.druid.sql.ast.statement.SQLJoinTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource and com.alibaba.druid.sql.ast.statement.SQLJoinTableSource are in unnamed module of loader java.net.FactoryURLClassLoader @55e42684)",
        "type": "ClassCastException"
      },
      "status": 503
    }
    

    I suspect this issue is the root cause of other subquery issue:

    Because I tested complicated subquery in FROM clause without back quoted alias, I see no problem and all outer grouping and sorting can be done in memory. Probably we can update the limitation doc: https://github.com/opensearch-project/sql/blob/main/docs/user/limitations/limitations.rst#subqueries-in-the-from-clause

    POST _plugins/_sql
    {
      "query": """
        SELECT b.Origin, b.avgPrice
        FROM (
          SELECT
            a.Origin AS Origin,
            AVG(AvgTicketPrice) AS avgPrice
          FROM (
            SELECT Origin, AvgTicketPrice
            FROM opensearch_dashboards_sample_data_flights
            WHERE FlightDelay = True
          ) AS a
          GROUP BY a.Origin
        ) AS b
        ORDER BY b.avgPrice DESC
      """
    }
    
    {
      "schema": [
        {
          "name": "Origin",
          "type": "keyword"
        },
        {
          "name": "avgPrice",
          "type": "double"
        }
      ],
      "datarows": [
        [
          "Charles de Gaulle International Airport",
          999.1396484375
        ],
        [
          "Huntsville International Carl T Jones Field",
          993.1741333007812
        ],
        [
          "Minneapolis-St Paul International/Wold-Chamberlain Airport",
          952.474853515625
        ],
    ...
    
    Explain output:
    {
      "root": {
        "name": "ProjectOperator",
        "description": {
          "fields": "[Origin, avgPrice]"
        },
        "children": [
          {
            "name": "SortOperator",
            "description": {
              "sortList": {
                "avgPrice": {
                  "sortOrder": "DESC",
                  "nullOrder": "NULL_LAST"
                }
              }
            },
            "children": [
              {
                "name": "ProjectOperator",
                "description": {
                  "fields": "[Origin, avgPrice]"
                },
                "children": [
                  {
                    "name": "AggregationOperator",
                    "description": {
                      "aggregators": "[AVG(AvgTicketPrice)]",
                      "groupBy": "[Origin]"
                    },
                    "children": [
                      {
                        "name": "ProjectOperator",
                        "description": {
                          "fields": "[Origin, AvgTicketPrice]"
                        },
                        "children": [
                          {
                            "name": "OpenSearchIndexScan",
                            "description": {
                              "request": """OpenSearchQueryRequest(indexName=opensearch_dashboards_sample_data_flights, sourceBuilder={"from":0,"size":200,"timeout":"1m","query":{"term":{"FlightDelay":{"value":true,"boost":1.0}}},"_source":{"includes":["Origin","AvgTicketPrice"],"excludes":[]},"sort":[{"_doc":{"order":"asc"}}]}, searchDone=false)"""
                            },
                            "children": []
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    }
    
  4. added
    v2.5.0
    on Dec 15, 2022
  5. self-assigned this
    on Dec 15, 2022
  6. dai-chen commented on Dec 15, 2022

    @dai-chen
    Collaborator

    Analyzing failure caused fall back to legacy which throws ClassCastException due to unsupported FROM subquery with aggregation.

    The detailed error is: "The qualifier [a] of qualified name [a.Origin] must be an field name, index name or its alias". The reason may be visitSubqueryRelation doesn't push alias name to symbol table.

  7. dai-chen commented on Dec 15, 2022

    @dai-chen
    Collaborator

    It seems the back quoted alias name is pushed to symbol table rather than unquoted:

    Screen Shot 2022-12-15 at 11 27 28 AM

  8. linked a pull request that will close this issueFix back quoted alias of FROM subquery #1189on Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugtdvtv2.5.0

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions