opensearch-project / opensearch-project/sql

Enhance support for CAST and/or CONVERT functions for more data types

Open
#256 3 comments 0 reactions 1 assignee View on GitHub

@Yury-Fridlyand is already working on this.

Since Dec 14, 2022.

enhancement tdvt
Dominant language
Java
Stars
176
Forks
229
Avg merge
2d 21h
Merged PRs (30d)
43

Description

Is your feature request related to a problem? Please describe.
Functions CAST and CONVERT are not fully supported. A user should be able to convert data between the following types: integer, double, [var]char, date, datetime, time.

Describe the solution you'd like
Support them according to the standard: cast, convert.

Describe alternatives you've considered
STR function could be used to convert data into strings, but it also not supported.

Additional context
String to float CAST works:

curl -XPOST http://localhost:9200/_plugins/_sql -H 'Content-Type: application/json' -
d '{"query": "SELECT cast(\"25.5\" as float)"}'
{
  "schema": [
    {
      "name": "cast(\"25.5\" as float)",
      "type": "float"
    }
  ],
  "datarows": [
    [
      25.0
    ]
  ],
  "total": 1,
  "size": 1,
  "status": 200
}

But not in vice-versa:

curl -XPOST http://localhost:9200/_plugins/_sql -H 'Content-Type: application/json' -d '{"query": "SELECT cast(25.5 as varchar)"}'
{
  "error": {
    "reason": "There was internal problem at backend",
    "details": "class com.alibaba.druid.sql.ast.expr.SQLNumberExpr cannot be cast to class com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr and com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr are in unnamed module of loader java.net.FactoryURLClassLoader @1a01d7f0)",
    "type": "ClassCastException"
  },
  "status": 503
}

CONVERT:

curl -XPOST http://localhost:9200/_plugins/_sql -H 'Content-Type: application/json' -d '{"query": "SELECT convert(varchar, 25.5)"}'
{
  "error": {
    "reason": "Invalid SQL query",
    "details": "Cannot invoke \"com.alibaba.druid.sql.ast.statement.SQLTableSource.getAlias()\" because the return value of \"com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.getFrom()\" is null",
    "type": "NullPointerException"
  },
  "status": 400
}
curl -XPOST http://localhost:9200/_plugins/_sql -H 'Content-Type: application/json' -d '{"query": "SELECT convert(double, \"25.5\")"}'
{
  "error": {
    "reason": "Invalid SQL query",
    "details": "Cannot invoke \"com.alibaba.druid.sql.ast.statement.SQLTableSource.getAlias()\" because the return value of \"com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.getFrom()\" is null",
    "type": "NullPointerException"
  },
  "status": 400
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.