apache/druid · error · IllegalArgumentException

Only one of fieldName or expression should be non-null

Error message

Only one of fieldName or expression should be non-null

What it means

This validation in AggregatorUtil enforces that exactly one of fieldName (a column) or fieldExpression (an expression) is supplied when building a float-defaulting column value selector. Supplying both or neither is ambiguous and rejected with IllegalArgumentException.

Source

Thrown at processing/src/main/java/org/apache/druid/query/aggregation/AggregatorUtil.java:256

      if (dependencySet.contains(aggregatorSpec.getName())) {
        condensedAggs.add(aggregatorSpec);
      }
    }
    return new Pair<>(condensedAggs, condensedPostAggs);
  }

  /**
   * Only one of fieldName and fieldExpression should be non-null
   */
  static ColumnValueSelector makeColumnValueSelectorWithFloatDefault(
      final ColumnSelectorFactory columnSelectorFactory,
      @Nullable final String fieldName,
      @Nullable final Expr fieldExpression,
      final float nullValue
  )
  {
    if ((fieldName == null) == (fieldExpression == null)) {
      throw new IllegalArgumentException("Only one of fieldName or expression should be non-null");
    }
    if (fieldName != null) {
      return columnSelectorFactory.makeColumnValueSelector(fieldName);
    } else {
      final ColumnValueSelector<ExprEval> baseSelector = ExpressionSelectors.makeExprEvalSelector(
          columnSelectorFactory,
          fieldExpression
      );
      class ExpressionFloatColumnSelector implements FloatColumnSelector
      {
        @Override
        public float getFloat()
        {
          // Although baseSelector.getObject is nullable
          // exprEval returned from Expression selectors is never null.
          final ExprEval<?> exprEval = baseSelector.getObject();
          return exprEval.isNumericNull() ? nullValue : (float) exprEval.asDouble();
        }

View on GitHub (pinned to 9b90983fd2)

Solutions

  1. Provide exactly one of fieldName or fieldExpression in the spec/constructor
  2. Remove the redundant field if both were set
  3. Add a default field if neither was set

Example fix

// before
new DoubleMeanAggregatorFactory("name", "field", "x + 1") // both set
// after
new DoubleMeanAggregatorFactory("name", "field", null) // or null field with expression
Defensive patterns

Strategy: validation

Validate before calling

// before construction
if ((fieldName == null) == (fieldExpression == null)) {
  throw new IllegalArgumentException("Exactly one of fieldName or fieldExpression must be set");
}

Try / catch

try {
  selector = AggregatorUtil.makeColumnValueSelectorWithFloatDefault(sf, fieldName, expr, 0.0f);
} catch (IllegalArgumentException e) {
  log.error("Bad aggregator spec: set exactly one of fieldName/expression", e);
  throw e;
}

Prevention

When it happens

Trigger: Calling makeColumnValueSelectorWithFloatDefault with both fieldName and fieldExpression non-null, or with both null.

Common situations: Config errors in aggregator/extractor specs where a JSON spec sets both 'fieldName' and 'expression', or omits both; programmatic construction passing nulls accidentally.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/96d0bd7b6f14f011. Report an issue: GitHub.