{"record":{"id":"04fe7380ed91d226","repo":"apache/druid","slug":"cannot-return-long-for-null-value-04fe73","errorCode":null,"errorMessage":"Cannot return long for Null Value","messagePattern":"Cannot return long for Null Value","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/aggregation/NullableNumericAggregator.java","lineNumber":92,"sourceCode":"      return null;\n    }\n    return delegate.get();\n  }\n\n  @Override\n  public float getFloat()\n  {\n    if (isNullResult) {\n      throw new IllegalStateException(\"Cannot return float for Null Value\");\n    }\n    return delegate.getFloat();\n  }\n\n  @Override\n  public long getLong()\n  {\n    if (isNullResult) {\n      throw new IllegalStateException(\"Cannot return long for Null Value\");\n    }\n    return delegate.getLong();\n  }\n\n  @Override\n  public double getDouble()\n  {\n    if (isNullResult) {\n      throw new IllegalStateException(\"Cannot return double for Null Value\");\n    }\n    return delegate.getDouble();\n  }\n\n  @Override\n  public boolean isNull()\n  {\n    return isNullResult || delegate.isNull();\n  }","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/aggregation/NullableNumericAggregator.java#L74-L110","documentation":"Same contract as getFloat(): NullableNumericAggregator tracks a null result state and refuses to return a primitive long when the aggregated value is null. Returning 0L would mask the null, so an IllegalStateException is thrown from getLong().","triggerScenarios":"Calling getLong() while isNullResult is true — i.e., the wrapped aggregator aggregated only null values and no valid long result exists.","commonSituations":"Long-typed aggregations (sum, longMin/longMax, longFirst/longLast) over columns that are entirely null or missing; result-extraction code that ignores isNull(); mixed-schema segments where a column is absent in some segments.","solutions":["Check agg.isNull() before calling getLong() and handle the null branch (e.g., return null or a default)","Verify the input column actually contains non-null long values in the queried segments","Ensure aggregator init/reset is called correctly so isNullResult reflects only the current aggregation","If the null is unexpected, inspect the selector/factory wiring to confirm the base aggregator is reading the intended column"],"exampleFix":"// before\nlong v = agg.getLong();\n// after\nLong v = agg.isNull() ? null : agg.getLong();","handlingStrategy":"type-guard","validationCode":"if (agg.isNull()) {\n  return null;\n}","typeGuard":"Long value = agg.isNull() ? null : agg.getLong();","tryCatchPattern":"try {\n  return agg.getLong();\n} catch (IllegalStateException e) {\n  if (e.getMessage().contains(\"Cannot return long for Null Value\")) {\n    return null;\n  }\n  throw e;\n}","preventionTips":["Check isNull() before any primitive getter on NullableNumericAggregator","Handle all-null columns explicitly in query/extract code","Verify segment schemas for the queried interval (column may be absent/null in some segments)","Keep aggregator reset/init logic symmetric so null state never leaks between queries"],"tags":["java","aggregation","null-handling"],"backgroundTag":"internal-invariant-violation","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}