mirror of
https://github.com/valitydev/redash.git
synced 2024-11-07 01:25:16 +00:00
Add support for int/float values in guess_type (#3913)
This commit is contained in:
parent
9ee393ec75
commit
27259b5abe
@ -271,7 +271,18 @@ def import_query_runners(query_runner_imports):
|
|||||||
__import__(runner_import)
|
__import__(runner_import)
|
||||||
|
|
||||||
|
|
||||||
def guess_type(string_value):
|
def guess_type(value):
|
||||||
|
if isinstance(value, bool):
|
||||||
|
return TYPE_BOOLEAN
|
||||||
|
elif isinstance(value, int):
|
||||||
|
return TYPE_INTEGER
|
||||||
|
elif isinstance(value, float):
|
||||||
|
return TYPE_FLOAT
|
||||||
|
|
||||||
|
return guess_type_from_string(value)
|
||||||
|
|
||||||
|
|
||||||
|
def guess_type_from_string(string_value):
|
||||||
if string_value == '' or string_value is None:
|
if string_value == '' or string_value is None:
|
||||||
return TYPE_STRING
|
return TYPE_STRING
|
||||||
|
|
||||||
@ -296,4 +307,4 @@ def guess_type(string_value):
|
|||||||
except (ValueError, OverflowError):
|
except (ValueError, OverflowError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return TYPE_STRING
|
return TYPE_STRING
|
@ -1,8 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from redash.query_runner import TYPE_DATETIME, TYPE_FLOAT, TYPE_INTEGER, TYPE_BOOLEAN, TYPE_STRING
|
from redash.query_runner import TYPE_DATETIME, TYPE_FLOAT, TYPE_INTEGER, TYPE_BOOLEAN, TYPE_STRING, guess_type
|
||||||
from redash.query_runner.drill import guess_type
|
|
||||||
|
|
||||||
|
|
||||||
class TestGuessType(TestCase):
|
class TestGuessType(TestCase):
|
||||||
@ -16,6 +15,7 @@ class TestGuessType(TestCase):
|
|||||||
self.assertEqual(guess_type('false'), TYPE_BOOLEAN)
|
self.assertEqual(guess_type('false'), TYPE_BOOLEAN)
|
||||||
self.assertEqual(guess_type('False'), TYPE_BOOLEAN)
|
self.assertEqual(guess_type('False'), TYPE_BOOLEAN)
|
||||||
self.assertEqual(guess_type('FALSE'), TYPE_BOOLEAN)
|
self.assertEqual(guess_type('FALSE'), TYPE_BOOLEAN)
|
||||||
|
self.assertEqual(guess_type(False), TYPE_BOOLEAN)
|
||||||
|
|
||||||
def test_detects_strings(self):
|
def test_detects_strings(self):
|
||||||
self.assertEqual(guess_type(None), TYPE_STRING)
|
self.assertEqual(guess_type(None), TYPE_STRING)
|
||||||
@ -24,9 +24,11 @@ class TestGuessType(TestCase):
|
|||||||
|
|
||||||
def test_detects_integer(self):
|
def test_detects_integer(self):
|
||||||
self.assertEqual(guess_type('42'), TYPE_INTEGER)
|
self.assertEqual(guess_type('42'), TYPE_INTEGER)
|
||||||
|
self.assertEqual(guess_type(42), TYPE_INTEGER)
|
||||||
|
|
||||||
def test_detects_float(self):
|
def test_detects_float(self):
|
||||||
self.assertEqual(guess_type('3.14'), TYPE_FLOAT)
|
self.assertEqual(guess_type('3.14'), TYPE_FLOAT)
|
||||||
|
self.assertEqual(guess_type(3.14), TYPE_FLOAT)
|
||||||
|
|
||||||
def test_detects_date(self):
|
def test_detects_date(self):
|
||||||
self.assertEqual(guess_type('2018-10-31'), TYPE_DATETIME)
|
self.assertEqual(guess_type('2018-10-31'), TYPE_DATETIME)
|
||||||
|
Loading…
Reference in New Issue
Block a user