Skip to content

Commit

Permalink
SqlTable: Automatically recognize date/time fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lanzagar committed Jul 7, 2016
1 parent 6ffe353 commit 2242c2b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Orange/data/sql/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
psycopg2.pool = Orange.misc.import_late_warning("psycopg2.pool")

from .. import domain, variable, table, instance, filter,\
DiscreteVariable, ContinuousVariable, StringVariable
DiscreteVariable, ContinuousVariable, StringVariable, TimeVariable
from Orange.data.sql import filter as sql_filter


Expand Down Expand Up @@ -104,8 +104,12 @@ def get_domain(self, type_hints=None, guess_values=False):

def add_to_sql(var, field_name):
if var.is_continuous:
var.to_sql = ToSql("({})::double precision".format(
self.quote_identifier(field_name)))
if isinstance(var, TimeVariable):
var.to_sql = ToSql("extract(epoch from {})".format(
self.quote_identifier(field_name)))
else:
var.to_sql = ToSql("({})::double precision".format(
self.quote_identifier(field_name)))
elif var.is_discrete:
var.to_sql = ToSql("({})::text".format(
self.quote_identifier(field_name)))
Expand Down Expand Up @@ -137,10 +141,15 @@ def get_variable(self, field_name, type_code, inspect_values=False):
INT_TYPES = (20, 21, 23) # bigint, int, smallint
CHAR_TYPES = (25, 1042, 1043,) # text, char, varchar
BOOLEAN_TYPES = (16,) # bool
# date, time, timestamp, timestamptz, timetz
TIME_TYPES = (1082, 1083, 1114, 1184, 1266, )

if type_code in FLOATISH_TYPES:
return ContinuousVariable(field_name)

if type_code in TIME_TYPES:
return TimeVariable(field_name)

if type_code in INT_TYPES: # bigint, int, smallint
if inspect_values:
values = self.get_distinct_values(field_name)
Expand Down
9 changes: 9 additions & 0 deletions Orange/data/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,3 +986,12 @@ def parse(self, datestr):
try: return dt.timestamp()
except OverflowError:
return -(self.UNIX_EPOCH - dt).total_seconds()

def to_val(self, s):
"""
Convert a value, given as an instance of an arbitrary type, to a float.
"""
if isinstance(s, str):
return self.parse(s)
else:
return super().to_val(s)

0 comments on commit 2242c2b

Please sign in to comment.