15 February 2012

I’ve spent the last 2 weeks on a super-secret project to slice and dice (and search, aggregate, and transform) XML in a Oracle database using the Hibernate ORM tool. Okay, maybe it’s not super-secret, but it is super-fun, and the kind of project that might stir up the world’s tiniest dust cloud of enviousness in perhaps one or two of my peers. Seamlessly querying XML and relational data is definitely useful at times. But I digress…

I started writing this post because I seem to have found a bug in Oracle’s XQuery support. As I so often do, I tried something just to see what would happen. I was not disappointed, because what happened was interesting. I ran a query like this:

select
    XMLQuery('fn:distinct-values(/document/parent/child[@name])'
            passing x.OBJECT_VALUE returning content).getClobVal()
from
    xmltype_table x
where
    XmlExists('/document/author/name[text() = $name]'
            passing x.OBJECT_VALUE, 'Frederick' as "name")
;

This query was supposed to return the distinct values found in the attribute name on all elements named child that descend from parent which descends from document. As you can see, I did not get the result I expected:

ORA-00600: internal error code, arguments: [qctcte1], [0], [], [], [], [], [], [], [], [], [], []
00600. 00000 -  "internal error code, arguments: [%s], [%s], [%s], [%s], [%s], [%s], [%s], [%s]"
*Cause:    This is the generic internal error number for Oracle program
        exceptions.             This indicates that a process has encountered an
        exceptional condition.
*Action:   Report as a bug - the first argument is the internal error number

I honestly expected that the query would return a result. I ought to be able to get a set of distinct attribute values, just like I can get a set of distinct element values. At the same time, I also thought that in the worst case I would get a syntax error, or perhaps some other indication that I was attempting to use the query system in an unsupported manner.

I’ve been working with Oracle databases for 11 years now, and I believe this is the first time I’ve found a real bug. Considering the complexity of the full XQuery specification combined with a relational database engine, bugs are pretty much inevitable. That said, this is a great example of a case for automated regression testing. It’s probably also an example of a use case that may have been thought of but never got enough priority to be implemented.

One thing I do know: an ORA-00600 is a great thing to discover before you promise to deliver some piece of functionality. For now, my code will have to sort through attribute values on the client side.