mirror of
https://github.com/Infinidat/infi.clickhouse_orm.git
synced 2025-02-19 18:00:32 +03:00
add support for Filtering null values
This commit is contained in:
parent
dc890dde5e
commit
a32453394a
|
@ -28,12 +28,15 @@ class SimpleOperator(Operator):
|
||||||
A simple binary operator such as a=b, a<b, a>b etc.
|
A simple binary operator such as a=b, a<b, a>b etc.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, sql_operator):
|
def __init__(self, sql_operator, sql_for_null=None):
|
||||||
self._sql_operator = sql_operator
|
self._sql_operator = sql_operator
|
||||||
|
self._sql_for_null = sql_for_null
|
||||||
|
|
||||||
def to_sql(self, model_cls, field_name, value):
|
def to_sql(self, model_cls, field_name, value):
|
||||||
field = getattr(model_cls, field_name)
|
field = getattr(model_cls, field_name)
|
||||||
value = field.to_db_string(field.to_python(value, pytz.utc))
|
value = field.to_db_string(field.to_python(value, pytz.utc))
|
||||||
|
if value == '\\N' and self._sql_for_null is not None:
|
||||||
|
return ' '.join([field_name, self._sql_for_null])
|
||||||
return ' '.join([field_name, self._sql_operator, value])
|
return ' '.join([field_name, self._sql_operator, value])
|
||||||
|
|
||||||
|
|
||||||
|
@ -132,8 +135,8 @@ _operators = {}
|
||||||
def register_operator(name, sql):
|
def register_operator(name, sql):
|
||||||
_operators[name] = sql
|
_operators[name] = sql
|
||||||
|
|
||||||
register_operator('eq', SimpleOperator('='))
|
register_operator('eq', SimpleOperator('=', 'IS NULL'))
|
||||||
register_operator('ne', SimpleOperator('!='))
|
register_operator('ne', SimpleOperator('!=', 'IS NOT NULL'))
|
||||||
register_operator('gt', SimpleOperator('>'))
|
register_operator('gt', SimpleOperator('>'))
|
||||||
register_operator('gte', SimpleOperator('>='))
|
register_operator('gte', SimpleOperator('>='))
|
||||||
register_operator('lt', SimpleOperator('<'))
|
register_operator('lt', SimpleOperator('<'))
|
||||||
|
|
|
@ -38,13 +38,18 @@ class Person(Model):
|
||||||
last_name = StringField()
|
last_name = StringField()
|
||||||
birthday = DateField()
|
birthday = DateField()
|
||||||
height = Float32Field()
|
height = Float32Field()
|
||||||
|
passport = NullableField(UInt32Field())
|
||||||
|
|
||||||
engine = MergeTree('birthday', ('first_name', 'last_name', 'birthday'))
|
engine = MergeTree('birthday', ('first_name', 'last_name', 'birthday'))
|
||||||
|
|
||||||
|
|
||||||
data = [
|
data = [
|
||||||
{"first_name": "Abdul", "last_name": "Hester", "birthday": "1970-12-02", "height": "1.63"},
|
{"first_name": "Abdul", "last_name": "Hester", "birthday": "1970-12-02", "height": "1.63",
|
||||||
{"first_name": "Adam", "last_name": "Goodman", "birthday": "1986-01-07", "height": "1.74"},
|
"passport": 35052255},
|
||||||
|
|
||||||
|
{"first_name": "Adam", "last_name": "Goodman", "birthday": "1986-01-07", "height": "1.74",
|
||||||
|
"passport": 36052255},
|
||||||
|
|
||||||
{"first_name": "Adena", "last_name": "Norman", "birthday": "1979-05-14", "height": "1.66"},
|
{"first_name": "Adena", "last_name": "Norman", "birthday": "1979-05-14", "height": "1.66"},
|
||||||
{"first_name": "Aline", "last_name": "Crane", "birthday": "1988-05-01", "height": "1.62"},
|
{"first_name": "Aline", "last_name": "Crane", "birthday": "1988-05-01", "height": "1.62"},
|
||||||
{"first_name": "Althea", "last_name": "Barrett", "birthday": "2004-07-28", "height": "1.71"},
|
{"first_name": "Althea", "last_name": "Barrett", "birthday": "2004-07-28", "height": "1.71"},
|
||||||
|
|
|
@ -134,7 +134,7 @@ class DatabaseTestCase(TestCaseWithData):
|
||||||
self._insert_and_check(self._sample_data(), len(data))
|
self._insert_and_check(self._sample_data(), len(data))
|
||||||
query = "SELECT * FROM `test-db`.person WHERE first_name = 'Whitney' ORDER BY last_name"
|
query = "SELECT * FROM `test-db`.person WHERE first_name = 'Whitney' ORDER BY last_name"
|
||||||
results = self.database.raw(query)
|
results = self.database.raw(query)
|
||||||
self.assertEqual(results, "Whitney\tDurham\t1977-09-15\t1.72\nWhitney\tScott\t1971-07-04\t1.7\n")
|
self.assertEqual(results, "Whitney\tDurham\t1977-09-15\t1.72\t\\N\nWhitney\tScott\t1971-07-04\t1.7\t\\N\n")
|
||||||
|
|
||||||
def test_invalid_user(self):
|
def test_invalid_user(self):
|
||||||
with self.assertRaises(ServerError) as cm:
|
with self.assertRaises(ServerError) as cm:
|
||||||
|
|
|
@ -38,6 +38,13 @@ class QuerySetTestCase(TestCaseWithData):
|
||||||
self.assertTrue(qs.filter(first_name='Connor'))
|
self.assertTrue(qs.filter(first_name='Connor'))
|
||||||
self.assertFalse(qs.filter(first_name='Willy'))
|
self.assertFalse(qs.filter(first_name='Willy'))
|
||||||
|
|
||||||
|
def test_filter_null_value(self):
|
||||||
|
qs = Person.objects_in(self.database)
|
||||||
|
self._test_qs(qs.filter(passport=None), 98)
|
||||||
|
self._test_qs(qs.exclude(passport=None), 2)
|
||||||
|
self._test_qs(qs.filter(passport__ne=None), 2)
|
||||||
|
self._test_qs(qs.exclude(passport__ne=None), 98)
|
||||||
|
|
||||||
def test_filter_string_field(self):
|
def test_filter_string_field(self):
|
||||||
qs = Person.objects_in(self.database)
|
qs = Person.objects_in(self.database)
|
||||||
self._test_qs(qs.filter(first_name='Ciaran'), 2)
|
self._test_qs(qs.filter(first_name='Ciaran'), 2)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user