Skip to content
Open
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
15 changes: 15 additions & 0 deletions teradata/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ def __init__(self, code, msg):
DateFromTicks = datetime.date.fromtimestamp


class Interval(str):
"""String representation of the Interval dataType."""
pass


class JSON(str):
"""String representation of the JSON dataType."""
pass


class Period(str):
"""String representation of the Period dataType."""
pass


def TimeFromTicks(x):
return datetime.datetime.fromtimestamp(x).time()

Expand Down
26 changes: 17 additions & 9 deletions teradata/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
periodRegEx1 = re.compile("\('(.*)',\s*'(.*)'\)")
periodRegEx2 = re.compile("ResultStruct:PERIOD\(.*\)\[(.*),\s*(.*)\]")

NUMBER_TYPES = ("BYTEINT", "BIGINT", "DECIMAL", "DOUBLE", "DOUBLE PRECISION",
NUMBER_TYPES = {"BYTEINT", "BIGINT", "DECIMAL", "DOUBLE", "DOUBLE PRECISION",
"INTEGER", "NUMBER", "SMALLINT", "FLOAT", "INT", "NUMERIC",
"REAL")
"REAL"}

INT_TYPES = ("BYTEINT", "BIGINT", "INTEGER", "SMALLINT", "INT")
INT_TYPES = {"BYTEINT", "BIGINT", "INTEGER", "SMALLINT", "INT"}

FLOAT_TYPES = ("FLOAT", "DOUBLE", "DOUBLE PRECISION", "REAL")
FLOAT_TYPES = {"FLOAT", "DOUBLE", "DOUBLE PRECISION", "REAL"}

BINARY_TYPES = ("BLOB", "BYTE", "VARBYTE")
BINARY_TYPES = {"BLOB", "BYTE", "VARBYTE"}


def _getMs(m, num):
Expand Down Expand Up @@ -230,7 +230,9 @@ def convertValue(self, dbType, dataType, typeCode, value):
logger.trace(
"Converting \"%s\" to (%s, %s).", value, dataType, typeCode)
if value is not None:
if typeCode == NUMBER:
if typeCode == STRING:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running the unit tests, this actually breaks conversion for JSON, INTERVAL, and PERIOD data types as these types don't have an equivalent in Python Database API specification and thus were mapped to STRING typecode.

If we want to short circuit String, then I think we have to introduce new type codes for JSON, INTERVAL, and PERIOD. Let me know if you want to submit or new pull request or otherwise I can make the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes sense. I updated this PR to include new type codes for these three types. Hopefully this helps fix the tests.

Caveat: I'm not super familiar with the Python DB API specs.

return value
elif typeCode == NUMBER:
try:
return NUMBER(value)
except:
Expand Down Expand Up @@ -270,12 +272,12 @@ def convertValue(self, dbType, dataType, typeCode, value):
elif typeCode == BINARY:
if util.isString(value):
return bytearray.fromhex(value)
elif dataType.startswith("INTERVAL"):
elif typeCode == Interval:
return convertInterval(dataType, value)
elif dataType.startswith("JSON") and util.isString(value):
elif typeCode == JSON and util.isString(value):
return json.loads(value, parse_int=decimal.Decimal,
parse_float=decimal.Decimal)
elif dataType.startswith("PERIOD"):
elif typeCode == Period:
return convertPeriod(dataType, value)
return value

Expand All @@ -294,6 +296,12 @@ def convertType(self, dbType, dataType):
typeCode = Timestamp
elif dataType.startswith("TIME"):
typeCode = Time
elif dataType.startswith("INTERVAL"):
typeCode = Interval
elif dataType.startswith("JSON"):
typeCode = JSON
elif dataType.startswith("PERIOD"):
typeCode = Period
return typeCode


Expand Down