databasetime-seriesquestdb

Exception in function factory with a CASE statement


I am finding an error when trying to do a CASE over a byte column. To check this out, you can create a simple table like this:

CREATE TABLE 'byteTest' ( 
    ts TIMESTAMP,
    id SYMBOL CAPACITY 256 CACHE,
    status BYTE
) timestamp(ts) PARTITION BY DAY;

And then execute this query, even without any data inside:

SELECT
  id,
  CASE
      WHEN status = 1 THEN 'ACTIVE'
  END as status
FROM
  byteTest;

The web console returns exception in function factory, and if I check the logs I can see the following output:

2025-03-27T12:21:40.032872Z E i.q.g.FunctionParser exception in function factory:
java.lang.UnsupportedOperationException
    at io.questdb.griffin.engine.functions.IntFunction.getByte(IntFunction.java:55)
    at io.questdb.griffin.engine.functions.conditional.SwitchFunctionFactory.getByte(SwitchFunctionFactory.java:174)
    at io.questdb.griffin.engine.functions.conditional.SwitchFunctionFactory.getIntKeyedFunction(SwitchFunctionFactory.java:420)
    at io.questdb.griffin.engine.functions.conditional.SwitchFunctionFactory.newInstance(SwitchFunctionFactory.java:136)
    at io.questdb.griffin.FunctionParser.checkAndCreateFunction(FunctionParser.java:566)
    at io.questdb.griffin.FunctionParser.createFunction(FunctionParser.java:1001)
    at io.questdb.griffin.FunctionParser.visit(FunctionParser.java:391)
    at io.questdb.griffin.PostOrderTreeTraversalAlgo.traverse(PostOrderTreeTraversalAlgo.java:96)
    at io.questdb.griffin.FunctionParser.parseFunction(FunctionParser.java:319)
    at io.questdb.griffin.SqlCodeGenerator.generateSelectVirtualWithSubQuery(SqlCodeGenerator.java:4449)
    at io.questdb.griffin.SqlCodeGenerator.generateSelectVirtual(SqlCodeGenerator.java:4422)
    at io.questdb.griffin.SqlCodeGenerator.generateSelect(SqlCodeGenerator.java:3733)
    at io.questdb.griffin.SqlCodeGenerator.generateQuery0(SqlCodeGenerator.java:3282)
    at io.questdb.griffin.SqlCodeGenerator.generateQuery(SqlCodeGenerator.java:3270)
    at io.questdb.griffin.SqlCodeGenerator.generate(SqlCodeGenerator.java:529)
    at io.questdb.griffin.SqlCompilerImpl.generateSelectOneShot(SqlCompilerImpl.java:3714)
    at io.questdb.griffin.SqlCompilerImpl.generateSelectWithRetries(SqlCompilerImpl.java:422)
    at io.questdb.griffin.SqlCompilerImpl.compileUsingModel(SqlCompilerImpl.java:2244)
    at io.questdb.griffin.SqlCompilerImpl.compileInner(SqlCompilerImpl.java:1928)
    at io.questdb.griffin.SqlCompilerImpl.compile(SqlCompilerImpl.java:305)
    at io.questdb.cairo.pool.SqlCompilerPool$C.compile(SqlCompilerPool.java:138)
    at io.questdb.cutlass.http.processors.JsonQueryProcessor.compileAndExecuteQuery(JsonQueryProcessor.java:542)
    at io.questdb.cutlass.http.processors.JsonQueryProcessor.execute0(JsonQueryProcessor.java:224)
    at io.questdb.cutlass.http.processors.JsonQueryProcessor.onRequestComplete(JsonQueryProcessor.java:296)
    at io.questdb.cutlass.http.HttpConnectionContext.handleClientRecv(HttpConnectionContext.java:991)
    at io.questdb.cutlass.http.HttpConnectionContext.handleClientOperation(HttpConnectionContext.java:307)
    at io.questdb.cutlass.http.HttpServer.handleClientOperation(HttpServer.java:345)
    at io.questdb.cutlass.http.HttpServer$1.lambda$$0(HttpServer.java:129)
    at io.questdb.network.AbstractIODispatcher.processIOQueue(AbstractIODispatcher.java:216)
    at io.questdb.cutlass.http.HttpServer$1.run(HttpServer.java:133)
    at io.questdb.mp.Worker.run(Worker.java:152)

2025-03-27T12:21:40.033831Z E i.q.g.e.QueryProgress err [id=-1, sql=`SELECT
  id,
  CASE
      WHEN status = 1 THEN 'ACTIVE'
  END as status
FROM
  byteTest`, principal=admin, cache=false, jit=true, time=10207500, msg=exception in function factory: , errno=0, pos=15]
2025-03-27T12:21:44.866629Z I http-server disconnected [ip=127.0.0.1, fd=154619049490, src=queue]

Not sure what I am doing wrong here.


Solution

  • In this case, this is a QuestDB bug. It has been already reported on GitHub, and there has been some work on the issue, which according to one of the engineers should be fixed soon.

    In the meantime, the simple workaround is casting the byte to an integer instead

    SELECT
      id,
      CASE
          WHEN status::int = 1 THEN 'ACTIVE'
      END as status
    FROM
      byteTest;