{"record":{"id":"96d0bd7b6f14f011","repo":"apache/druid","slug":"only-one-of-fieldname-or-expression-should-be-non","errorCode":null,"errorMessage":"Only one of fieldName or expression should be non-null","messagePattern":"Only one of fieldName or expression should be non-null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":400,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/aggregation/AggregatorUtil.java","lineNumber":256,"sourceCode":"      if (dependencySet.contains(aggregatorSpec.getName())) {\n        condensedAggs.add(aggregatorSpec);\n      }\n    }\n    return new Pair<>(condensedAggs, condensedPostAggs);\n  }\n\n  /**\n   * Only one of fieldName and fieldExpression should be non-null\n   */\n  static ColumnValueSelector makeColumnValueSelectorWithFloatDefault(\n      final ColumnSelectorFactory columnSelectorFactory,\n      @Nullable final String fieldName,\n      @Nullable final Expr fieldExpression,\n      final float nullValue\n  )\n  {\n    if ((fieldName == null) == (fieldExpression == null)) {\n      throw new IllegalArgumentException(\"Only one of fieldName or expression should be non-null\");\n    }\n    if (fieldName != null) {\n      return columnSelectorFactory.makeColumnValueSelector(fieldName);\n    } else {\n      final ColumnValueSelector<ExprEval> baseSelector = ExpressionSelectors.makeExprEvalSelector(\n          columnSelectorFactory,\n          fieldExpression\n      );\n      class ExpressionFloatColumnSelector implements FloatColumnSelector\n      {\n        @Override\n        public float getFloat()\n        {\n          // Although baseSelector.getObject is nullable\n          // exprEval returned from Expression selectors is never null.\n          final ExprEval<?> exprEval = baseSelector.getObject();\n          return exprEval.isNumericNull() ? nullValue : (float) exprEval.asDouble();\n        }","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/aggregation/AggregatorUtil.java#L238-L274","documentation":"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.","triggerScenarios":"Calling makeColumnValueSelectorWithFloatDefault with both fieldName and fieldExpression non-null, or with both null.","commonSituations":"Config errors in aggregator/extractor specs where a JSON spec sets both 'fieldName' and 'expression', or omits both; programmatic construction passing nulls accidentally.","solutions":["Provide exactly one of fieldName or fieldExpression in the spec/constructor","Remove the redundant field if both were set","Add a default field if neither was set"],"exampleFix":"// before\nnew DoubleMeanAggregatorFactory(\"name\", \"field\", \"x + 1\") // both set\n// after\nnew DoubleMeanAggregatorFactory(\"name\", \"field\", null) // or null field with expression","handlingStrategy":"validation","validationCode":"// before construction\nif ((fieldName == null) == (fieldExpression == null)) {\n  throw new IllegalArgumentException(\"Exactly one of fieldName or fieldExpression must be set\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  selector = AggregatorUtil.makeColumnValueSelectorWithFloatDefault(sf, fieldName, expr, 0.0f);\n} catch (IllegalArgumentException e) {\n  log.error(\"Bad aggregator spec: set exactly one of fieldName/expression\", e);\n  throw e;\n}","preventionTips":["In JSON specs, include either 'fieldName' or 'expression', never both or neither","Validate ingestion/query specs before submission","Watch for copy-paste leftovers of 'expression' keys"],"tags":["aggregation","validation","druid"],"backgroundTag":"mutually-exclusive-options","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"}