Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Error occurs when convert bool to integer #579

Closed
dai-chen opened this issue Jul 15, 2020 · 2 comments
Closed

Error occurs when convert bool to integer #579

dai-chen opened this issue Jul 15, 2020 · 2 comments
Assignees
Labels
BI integration Issues for integration with BI tools bug Something isn't working SQL

Comments

@dai-chen
Copy link
Member

dai-chen commented Jul 15, 2020

Exception is thrown from plugin for a query with SELECT trying to convert a boolean field to integer.

POST _opendistro/_sql
{  
  "query": """
      SELECT CAST(Cancelled AS INT) 
      FROM kibana_sample_data_flights
  """
}
{
  "error": {
    "reason": "Error occurred in Elasticsearch engine: all shards failed",
    "details": """Shard[0]: ScriptException[runtime error]; nested: NumberFormatException[For input string: "false"];

For more details, please send request for Json format to see the raw response from elasticsearch engine.""",
    "type": "SearchPhaseExecutionException"
  },
  "status": 400
}
@dai-chen dai-chen added bug Something isn't working SQL labels Jul 15, 2020
@dai-chen
Copy link
Member Author

The root cause is the painless script generated is wrong, particularly in Double.parseDouble(doc['Cancelled'].value.toString()).

{
  "from" : 0,
  "size" : 200,
  "_source" : {
    "includes" : [
      "cast_Cancelled"
    ],
    "excludes" : [ ]
  },
  "script_fields" : {
    "cast_Cancelled" : {
      "script" : {
        "source" : "def cast_1 = Double.parseDouble(doc['Cancelled'].value.toString()).intValue();return cast_1;",
        "lang" : "painless"
      },
      "ignore_failure" : false
    }
  }
}

@dai-chen
Copy link
Member Author

dai-chen commented Jul 15, 2020

The code generating the script above is here:

return String.format("def %s = Double.parseDouble(%s.toString()).intValue()", name, castFieldName);

It seems our implementation always assumes the source type is double type when target is INT/LONG/FLOAT.

MySQL example:

SELECT CAST(TRUE AS SIGNED);

CAST(TRUE AS SIGNED)
-------------------------
1

SELECT CAST(FALSE AS SIGNED);

CAST(FALSE AS SIGNED)
-------------------------
0

@dai-chen dai-chen added the BI integration Issues for integration with BI tools label Jul 17, 2020
@dai-chen dai-chen self-assigned this Jul 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
BI integration Issues for integration with BI tools bug Something isn't working SQL
Projects
None yet
Development

No branches or pull requests

1 participant