Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] SqlTable use empty string instead of None for StringVariable #5120

Merged
merged 4 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions Orange/data/sql/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def _fetch_row(self, row_index):
if not values:
raise IndexError('Could not retrieve row {} from table {}'.format(
row_index, self.name))
return SqlRowInstance(self.domain, values[0])
return Instance(self.domain, values[0])

def __iter__(self):
""" Iterating through the rows executes the query using a cursor and
Expand All @@ -187,7 +187,7 @@ def __iter__(self):
attributes = self.domain.variables + self.domain.metas

for row in self._query(attributes):
yield SqlRowInstance(self.domain, row)
yield Instance(self.domain, row)

def _query(self, attributes=None, filters=(), rows=None):
if attributes is not None:
Expand Down Expand Up @@ -653,16 +653,3 @@ def _execute_sql_query(self, query, param=None):

def checksum(self, include_metas=True):
return np.nan


class SqlRowInstance(Instance):
"""
Extends :obj:`Orange.data.Instance` to correctly handle values of meta
attributes.
"""

def __init__(self, domain, data=None):
nvar = len(domain.variables)
super().__init__(domain, data[:nvar])
if len(data) > nvar:
self._metas = np.asarray(data[nvar:], dtype=object)
72 changes: 36 additions & 36 deletions Orange/tests/sql/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import unittest

from Orange.data.sql.table import SqlTable, SqlRowInstance
from Orange.data import filter, domain
from Orange.data.sql.table import SqlTable
from Orange.data import filter, domain, Instance
from Orange.tests.sql.base import DataBaseTest as dbt


Expand Down Expand Up @@ -368,7 +368,7 @@ def test_filter_string_is_defined(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.IsDefined)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data if row[0] is not None]

self.assertEqual(len(filtered_data), len(correct_data))
Expand All @@ -379,7 +379,7 @@ def test_filter_string_equal(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.Equal, 'in')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data if row[0] == 'in']

self.assertEqual(len(filtered_data), len(correct_data))
Expand All @@ -391,7 +391,7 @@ def test_filter_string_equal_case_insensitive_value(self):
filter.FilterString(-1, filter.FilterString.Equal, 'In',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data if row[0] == 'in']

self.assertEqual(len(filtered_data), len(correct_data))
Expand All @@ -403,7 +403,7 @@ def test_filter_string_equal_case_insensitive_data(self):
filter.FilterString(-1, filter.FilterString.Equal, 'donec',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data if row[0] == 'Donec']

self.assertEqual(len(filtered_data), len(correct_data))
Expand All @@ -414,7 +414,7 @@ def test_filter_string_not_equal(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.NotEqual, 'in')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data if row[0] != 'in']

self.assertEqual(len(filtered_data), len(correct_data))
Expand All @@ -426,7 +426,7 @@ def test_filter_string_not_equal_case_insensitive_value(self):
filter.FilterString(-1, filter.FilterString.NotEqual, 'In',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data if row[0] != 'in']

self.assertEqual(len(filtered_data), len(correct_data))
Expand All @@ -438,7 +438,7 @@ def test_filter_string_not_equal_case_insensitive_data(self):
filter.FilterString(-1, filter.FilterString.NotEqual, 'donec',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data if row[0] != 'Donec']

self.assertEqual(len(filtered_data), len(correct_data))
Expand All @@ -449,7 +449,7 @@ def test_filter_string_less(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.Less, 'A')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0] < 'A']

Expand All @@ -462,7 +462,7 @@ def test_filter_string_less_case_insensitive_value(self):
filter.FilterString(-1, filter.FilterString.Less, 'In',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0].lower() < 'in']

Expand All @@ -475,7 +475,7 @@ def test_filter_string_less_case_insensitive_data(self):
filter.FilterString(-1, filter.FilterString.Less, 'donec',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0].lower() < 'donec']

Expand All @@ -487,7 +487,7 @@ def test_filter_string_less_equal(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.LessEqual, 'A')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0] <= 'A']

Expand All @@ -500,7 +500,7 @@ def test_filter_string_less_equal_case_insensitive_value(self):
filter.FilterString(-1, filter.FilterString.LessEqual, 'In',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0].lower() <= 'in']

Expand All @@ -513,7 +513,7 @@ def test_filter_string_less_equal_case_insensitive_data(self):
filter.FilterString(-1, filter.FilterString.LessEqual, 'donec',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0].lower() <= 'donec']

Expand All @@ -525,7 +525,7 @@ def test_filter_string_greater(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.Greater, 'volutpat')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0] > 'volutpat']

Expand All @@ -538,7 +538,7 @@ def test_filter_string_greater_case_insensitive_value(self):
filter.FilterString(-1, filter.FilterString.Greater, 'In',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0].lower() > 'in']

Expand All @@ -551,7 +551,7 @@ def test_filter_string_greater_case_insensitive_data(self):
filter.FilterString(-1, filter.FilterString.Greater, 'donec',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0].lower() > 'donec']

Expand All @@ -563,7 +563,7 @@ def test_filter_string_greater_equal(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.GreaterEqual, 'volutpat')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0] >= 'volutpat']

Expand All @@ -576,7 +576,7 @@ def test_filter_string_greater_equal_case_insensitive_value(self):
filter.FilterString(-1, filter.FilterString.GreaterEqual, 'In',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0].lower() >= 'in']

Expand All @@ -589,7 +589,7 @@ def test_filter_string_greater_equal_case_insensitive_data(self):
filter.FilterString(-1, filter.FilterString.GreaterEqual, 'donec',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0].lower() >= 'donec']

Expand All @@ -601,7 +601,7 @@ def test_filter_string_between(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.Between, 'a', 'c')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and 'a' <= row[0] <= 'c']

Expand All @@ -614,7 +614,7 @@ def test_filter_string_between_case_insensitive_value(self):
filter.FilterString(-1, filter.FilterString.Between, 'I', 'O',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and 'i' < row[0].lower() <= 'o']

Expand All @@ -627,7 +627,7 @@ def test_filter_string_between_case_insensitive_data(self):
filter.FilterString(-1, filter.FilterString.Between, 'i', 'O',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and 'i' <= row[0].lower() <= 'o']

Expand All @@ -639,7 +639,7 @@ def test_filter_string_contains(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.Contains, 'et')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and 'et' in row[0]]

Expand All @@ -652,7 +652,7 @@ def test_filter_string_contains_case_insensitive_value(self):
filter.FilterString(-1, filter.FilterString.Contains, 'eT',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and 'et' in row[0].lower()]

Expand All @@ -665,7 +665,7 @@ def test_filter_string_contains_case_insensitive_data(self):
filter.FilterString(-1, filter.FilterString.Contains, 'do',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and 'do' in row[0].lower()]

Expand All @@ -677,7 +677,7 @@ def test_filter_string_outside(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.Outside, 'am', 'di')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and not 'am' < row[0] < 'di']

Expand All @@ -690,7 +690,7 @@ def test_filter_string_outside_case_insensitive(self):
filter.FilterString(-1, filter.FilterString.Outside, 'd', 'k',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and not 'd' < row[0].lower() < 'k']

Expand All @@ -702,7 +702,7 @@ def test_filter_string_starts_with(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.StartsWith, 'D')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0].startswith('D')]

Expand All @@ -715,7 +715,7 @@ def test_filter_string_starts_with_case_insensitive(self):
filter.FilterString(-1, filter.FilterString.StartsWith, 'D',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None
and row[0].lower().startswith('d')]
Expand All @@ -728,7 +728,7 @@ def test_filter_string_ends_with(self):
filtered_data = filter.Values(conditions=[
filter.FilterString(-1, filter.FilterString.EndsWith, 's')
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None and row[0].endswith('s')]

Expand All @@ -741,7 +741,7 @@ def test_filter_string_ends_with_case_insensitive(self):
filter.FilterString(-1, filter.FilterString.EndsWith, 'S',
case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data
if row[0] is not None
and row[0].lower().endswith('s')]
Expand All @@ -754,7 +754,7 @@ def test_filter_string_list(self):
filtered_data = filter.Values(conditions=[
filter.FilterStringList(-1, ['et', 'in'])
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data if row[0] in ['et', 'in']]

self.assertEqual(len(filtered_data), len(correct_data))
Expand All @@ -765,7 +765,7 @@ def test_filter_string_list_case_insensitive_value(self):
filtered_data = filter.Values(conditions=[
filter.FilterStringList(-1, ['Et', 'In'], case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data if row[0] in ['et', 'in']]

self.assertEqual(len(filtered_data), len(correct_data))
Expand All @@ -776,7 +776,7 @@ def test_filter_string_list_case_insensitive_data(self):
filtered_data = filter.Values(conditions=[
filter.FilterStringList(-1, ['donec'], case_sensitive=False)
])(self.table)
correct_data = [SqlRowInstance(filtered_data.domain, row)
correct_data = [Instance(filtered_data.domain, row)
for row in self.data if row[0] in ['Donec']]

self.assertEqual(len(filtered_data), len(correct_data))
Expand Down
Loading