I have a table like this:
create table t (
t0 datetime year to fraction,
t1 datetime year to fraction(1),
t2 datetime year to fraction(2),
t3 datetime year to fraction(3),
t4 datetime year to fraction(4)
);
Now I'd like to reverse engineer this table's data type information. I'm mostly interested in the fractional seconds part, but if I can find the other qualifier information, even better. The following query doesn't work:
select
c.colname::varchar(10) colname,
informix.schema_coltypename(c.coltype, c.extended_id)::varchar(10) coltypename,
c.collength,
informix.schema_precision(c.coltype, c.extended_id, c.collength) precision,
informix.schema_numscale(c.coltype, c.collength) numscale,
informix.schema_datetype(c.coltype, c.collength) datetype,
c.coltype
from syscolumns c
join systables t on c.tabid = t.tabid
where t.tabname = 't'
It yields
|colname |coltypename|collength|precision |numscale |datetype |coltype|
|----------|-----------|---------|-----------|-----------|-----------|-------|
|t0 |DATETIME |4365 |4365 | |60 |10 |
|t1 |DATETIME |3851 |3851 | |60 |10 |
|t2 |DATETIME |4108 |4108 | |60 |10 |
|t3 |DATETIME |4365 |4365 | |60 |10 |
|t4 |DATETIME |4622 |4622 | |60 |10 |
The collength seems to contain the relevant information, but I cannot extract it with schema_precision
or schema_numscale
as is otherwise possible for numeric precisions. Also, schema_datetype
yields no interesting results.
How can I reverse engineer the coltype
information back to datetime year to fraction(N)
?
Based on documentation Time data types:
For columns of type DATETIME or INTERVAL, collength is determined using the following formula:
(length * 256) + (first_qualifier * 16) + last_qualifier
The length is the physical length of the DATETIME or INTERVAL field, and first_qualifier and last_qualifier have values that the following table shows.
+------------------+--------+------------------+-------+ | Field qualifier | Value | Field qualifier | Value | +------------------+--------+------------------+-------+ | YEAR | 0 | FRACTION(1) | 11 | | MONTH | 2 | FRACTION(2) | 12 | | DAY | 4 | FRACTION(3) | 13 | | HOUR | 6 | FRACTION(4) | 14 | | MINUTE | 8 | FRACTION(5) | 15 | | SECOND | 10 | | | +------------------+--------+------------------+-------+
Calculation(hex value to easier spot the pattern):
t1 datetime year to fraction(1), 15*256 + 0*16+11 = 3851 0x0F0B
t2 datetime year to fraction(2), 16*256 + 0*16+12 = 4108 0x100C
t3 datetime year to fraction(3), 17*256 + 0*16+13 = 4365 0x110D
t4 datetime year to fraction(4), 18*256 + 0*16+14 = 4622 0x120E
If the length is known then it is possible to reverse engineer it even using "brute force".
Lookup:
WITH l(v) AS (
VALUES (12),(13),(14),(15),(16),(17),(18)
), first_q(v, first_qualifier) AS (
VALUES (0,'YEAR'),(2,'MONTH'),(4,'DAY'),(6,'HOUR'),(8,'MINUTE'),(10, 'SECOND')
), last_q(v, last_qualifier) AS (
VALUES (11, 'FRACTION(0)'),(12, 'FRACTION(1)'),(13, 'FRACTION(2)'),
(14, 'FRACTION(3)'),(15, 'FRACTION(4)')
), result AS (
SELECT l.v * 256 + (first_q.v * 256) + last_q.v AS collen, *
FROM l CROSS JOIN first_q CROSS JOIN last_q
)
SELECT *
FROM result
--WHERE collen = 3851