{"record":{"id":"c7ce21fa86cc185e","repo":"apache/beam","slug":"does-not-support-s-distinct","errorCode":null,"errorMessage":"Does not support %s DISTINCT","messagePattern":"Does not support (.+?) DISTINCT","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/transform/agg/AggregationCombineFnAdapter.java","lineNumber":145,"sourceCode":"    }\n\n    @Override\n    public Coder<Row> getAccumulatorCoder(CoderRegistry registry, Coder<Row> inputCoder)\n        throws CannotProvideCoderException {\n      return SchemaCoder.of(EMPTY_SCHEMA);\n    }\n\n    @Override\n    public Coder<Row> getDefaultOutputCoder(CoderRegistry registry, Coder<Row> inputCoder) {\n      return SchemaCoder.of(EMPTY_SCHEMA);\n    }\n  }\n\n  /** Creates either a UDAF or a built-in {@link CombineFn}. */\n  public static CombineFn<?, ?, ?> createCombineFn(\n      AggregateCall call, Schema.Field field, String functionName) {\n    if (call.isDistinct()) {\n      throw new UnsupportedOperationException(\n          \"Does not support \" + call.getAggregation().getName() + \" DISTINCT\");\n    }\n\n    CombineFn combineFn;\n    if (call.getAggregation() instanceof SqlUserDefinedAggFunction) {\n      combineFn = getUdafCombineFn(call);\n    } else {\n      combineFn = BeamBuiltinAggregations.create(functionName, field.getType());\n    }\n    if (call.getArgList().isEmpty()) {\n      return new SingleInputCombiner(combineFn);\n    } else if (call.getArgList().size() == 1) {\n      return new SingleInputCombiner(combineFn);\n    } else {\n      return new MultiInputCombiner(combineFn);\n    }\n  }\n","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/transform/agg/AggregationCombineFnAdapter.java#L127-L163","documentation":"AggregationCombineFnAdapter.createCombineFn rejects AggregateCalls marked DISTINCT because the built-in CombineFn implementations cannot deduplicate inputs before aggregating. Beam SQL throws UnsupportedOperationException at query translation time.","triggerScenarios":"Running a SQL query like SELECT COUNT(DISTINCT x), SUM(DISTINCT x) FROM ... that translates into an AggregateCall with isDistinct()=true handled by a built-in CombineFn.","commonSituations":"Porting SQL from other engines (BigQuery/Calcite) that support DISTINCT aggregates; analytics queries counting unique users.","solutions":["Rewrite the query using a subquery with GROUP BY on the distinct column before aggregating (e.g. SELECT COUNT(*) FROM (SELECT user GROUP BY user))","Use COUNT(DISTINCT x) only on versions/configurations that support it (HLL/UXTS variants)","Enable Beam SQL DISTINCT support flags or newer Beam version if available","Implement a custom UDAF with deduplication built in"],"exampleFix":"// before\nSELECT COUNT(DISTINCT user_id) FROM events;\n// after\nSELECT COUNT(user_id) FROM (SELECT user_id FROM events GROUP BY user_id);","handlingStrategy":"validation","validationCode":"// Reject DISTINCT aggregates before submitting the query\nif (sql.toUpperCase().matches(\".*\\\\b(COUNT|SUM|AVG)\\\\s*\\\\(\\\\s*DISTINCT\\\\b.*\")) {\n  throw new IllegalArgumentException(\"Beam SQL does not support DISTINCT aggregates; rewrite query\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  return AggregationCombineFnAdapter.createCombineFn(call, field, name);\n} catch (UnsupportedOperationException e) {\n  if (e.getMessage().contains(\"DISTINCT\")) {\n    throw new IllegalArgumentException(\"Rewrite query to deduplicate via GROUP BY subquery\", e);\n  }\n  throw e;\n}","preventionTips":["Lint SQL for DISTINCT aggregates before execution","Use GROUP BY subqueries to express distinct semantics","Track Beam releases for DISTINCT aggregate support","Educate SQL authors porting from engines with DISTINCT support"],"tags":["java","apache-beam","sql","aggregation","distinct"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}