apache/cassandra · error · InvalidRequestException

Could not compile function '%s' from Java source: %s

Error message

Could not compile function '%s' from Java source: %s

What it means

The Java UDF source was compiled but invoking the generated class (e.g. its constructor or a method via reflection) threw an InvocationTargetException. Cassandra unwraps the underlying cause, logs the full Java source, and rethrows as an InvalidRequestException so the user sees the root problem from their code.

Source

Thrown at src/java/org/apache/cassandra/cql3/functions/JavaBasedUDFunction.java:368

                }

                if (nonSyntheticMethodCount != 3 || cls.getDeclaredConstructors().length != 1)
                    throw new InvalidRequestException("Check your source to not define additional Java methods or constructors");
                MethodType methodType = MethodType.methodType(void.class)
                                                  .appendParameterTypes(UDFDataType.class, UDFContext.class);
                MethodHandle ctor = MethodHandles.lookup().findConstructor(cls, methodType);
                this.javaUDF = (JavaUDF) ctor.invokeWithArguments(resultType, udfContext);
            }
            finally
            {
                thread.setContextClassLoader(orig);
            }
        }
        catch (InvocationTargetException e)
        {
            // in case of an ITE, use the cause
            logger.error(String.format("Could not compile function '%s' from Java source:%n%s", name, javaSource), e);
            throw new InvalidRequestException(String.format("Could not compile function '%s' from Java source: %s", name, e.getCause()));
        }
        catch (InvalidRequestException | VirtualMachineError e)
        {
            throw e;
        }
        catch (Throwable e)
        {
            logger.error(String.format("Could not compile function '%s' from Java source:%n%s", name, javaSource), e);
            throw new InvalidRequestException(String.format("Could not compile function '%s' from Java source: %s", name, e));
        }
    }

    @Override
    protected ExecutorService executor()
    {
        return executor;
    }

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Read the nested cause (%s in the message) to identify the real exception from the UDF source
  2. Fix the failing code in the UDF Java source (imports, class references, types)
  3. Verify any classes referenced by the UDF exist in lib/ or the classpath on every node
  4. Re-create the function with corrected source after testing the logic standalone in plain Java

Example fix

// before (UDF body throws during class init)
static final int LIMIT = Integer.parseInt(System.getProperty("udf.limit"));
return x < LIMIT;
// after
return x < 100; // avoid env-dependent static init inside UDFs
Defensive patterns

Strategy: try-catch

Try / catch

try { session.execute(createStmt); }
catch (InvalidRequestException e) {
  if (e.getMessage().startsWith("Could not compile function")) {
    Throwable cause = e.getCause(); // root cause from UDF source
    log.warn("UDF init failed: {}", cause, cause);
  } else throw e;
}

Prevention

When it happens

Trigger: CREATE FUNCTION with Java source whose generated class throws during construction or first invocation — e.g. static initializer failure, null/invalid argument to the generated constructor, or an exception thrown eagerly inside the UDF wrapper.

Common situations: UDF body references a class not on the classpath causing NoClassDefFoundError; static field initialization throwing; wrong return type causing a MethodHandle invocation failure.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/77bce69ac4e71019. Report an issue: GitHub.