I’m using Oracle object data types to represent a timespan or period. And I’ve got to do a bunch of operations that involve working with collections of periods. Iterating over collections in SQL is significantly faster than in PL/SQL.
CREATE TYPE PERIOD AS OBJECT (
beginning DATE,
ending DATE,
... some member functions...);
CREATE TYPE PERIOD_TABLE AS TABLE OF PERIOD;
-- what I would like to do: where t.column_value is still a period type
SELECT (t.column_value).range_intersect(period2)
FROM TABLE(period_table1) t
WHERE pa_contains(period_table1, (t.column_value).prev()) = 0
AND pa_contains(period_table1, (t.column_value).next()) = 1
The problem is that the TABLE() function explodes the objects into scalar values, and I really need the objects instead. I could use the scalar values to recreate the objects but this would incur the overhead of re-instantiating the objects. And the period is designed to be subclassed so there would be additional difficulty trying to figure out what to initialize it as.
Is there another way to do this in SQL that doesn’t destroy my objects?
Sorry this was a really tough question. But I finally found a way to do this using some tools I had created earlier. The trick ended up being to iterate over a nested table of number to get each element.
So the first piece was a series generator that I had shamelessly borrowed from Postgres. There are other ways to generate numbers but this is pretty efficient.
And the second piece is the ability to subscript a collection item in SQL.
And finally putting it all together:
Not easy, but doable.