I’m running the following query in Hypersonic DB (HSQLDB):
SELECT (CASE foo WHEN 'a' THEN 'bar' WHEN 'b' THEN 'biz' .... ELSE 'fin' END ) FROM MyTable LIMIT 1
When the number of ‘WHEN’ clauses exceeds about 1000, I get a Java StackOverflowError thrown by the JDBC driver in org.hsqldb.jdbc.Util.sqlException().
Here’s the really weird part: I tried breaking up my CASE statement into pieces with e.g. 100 WHEN clauses followed by ELSE ( CASE foo WHEN ... ) END. But even with this rewrite I get exactly the same behavior!
I don’t see any reference to a limit of 1000 or anything else in the HSQLDB manual. Help!
You should never get anywhere near 1000 terms in a
CASEstatement. Long before that, you should put the other values into a separate table and pick them by joining.Java API says about StackOverflowError:
So I would guess that when HSQLDB parses a
CASEexpression, eachWHENterm adds another layer to the runtime stack (actually probably several layers perWHEN).You’d probably get a similar StackOverflowError if you had an arithmetic expression with 1,000 levels of nested parentheses.
The limit of 1,000 is probably variable, depending on the implementation of the Java VM, the version of Java, the platform you’re running on, the amount of memory available, etc. They may not document it in the HSQLDB documentation because it’s a platform-specific limit, not something built into HSQLDB.