1

With the Teradata database, it is possible to load values of NaN, -Inf, and +Inf into FLOAT columns through Java. Unfortunately, once those values get into the tables, they make life difficult when writing SQL that needs to filter them out. There is no IsNaN() function, nor can you "CAST ('NaN' as FLOAT)" and use an equality comparison.

What I would like to do is,

SELECT
  SUM(VAL**2)
FROM
  DTM
WHERE
  NOT ABS(VAL) > 1e+21 AND
  NOT VAL = CAST ('NaN' AS FLOAT)

but that fails with error 2620, "The format or data contains a bad character.", specifically on the CAST. I've tried simply "... AND NOT VAL = 'NaN'", which also fails for a similar reason (3535, "A character string failed conversion to a numeric value."). I cannot seem to figure out how to represent NaN within the SQL statement. Even if I could represent NaN successfully in an SQL statement, I would be concerned that the comparison would fail. According to the IEEE 754 spec, NaN = NaN should evaluate to false. What I really seem to need is an IsNaN() function. Yet that function does not seem to exist.

4

1 に答える 1

0

I have figured out a work-around, and I'll share that with those of you searching for a solution. But first, it's obvious to me that Teradata's handling of NaN floating point values is incomplete and any behavior I've stumbled into figuring out is likely unintentional and inconsistent across different versions. So I offer the following free advice, with no guarantees, promises, or liabilities of any kind. Caveat emptor.

After drilling down into the data, I find that if I CAST the FLOAT value to a VARCHAR(50), the NaN values come out as a string of 22 asterisks (**********************). I can cast to a VARCHAR(1) instead, and the NaN comes out as a single asterisk (*). That comparison isn't that bad.

SELECT
  SUM(VAL**2)
FROM
  DTM
WHERE
  NOT CAST (VAL AS VARCHAR(1)) = '*' AND
  ABS(VAL) < 1.0e+21

I'll note two things.

First, my original desire of "NOT ABS(VAL) > 1.0e+21" on my version of Teradata seems to get converted to "ABS(VAL) <= 1.0e+21" under the covers. This (sometimes) fails because of the equality potion of the comparison -- error [2651], "Operation Error computing expression involving VAL." I'm assuming that the conversion from "NOT >" to "<=" is happening, because "NOT ABS(VAL) >= 1.0e+21" works fine (but looks ugly). Using "ABS(VAL) < 1.0e+21" works just fine and captures the need.

Second, while I can't reproduce it consistently, I have examples in my history where "ABS(VAL) <= 1.0e+21" does effectively screen out NaN, some where it doesn't, and some where it fails with [2651] (see above), yet the SQL and the data in the tables were identical. The only conclusion I can come to is that Teradata is inconsistent in how it evaluates the comparison when NaN is involved (or it thinks it might be). It's possible that different amps are handling it differently, but I don't know for sure. That said, the above two comparisons in the where clause consistently and effectively screen out the Inf and NaN values.

于 2012-11-20T22:57:11.420 に答える