{"record":{"id":"822bc2f913835238","repo":"apache/druid","slug":"cannot-translate-sqltypename-s-to-druid-type-for-822bc2","errorCode":null,"errorMessage":"Cannot translate sqlTypeName[%s] to Druid type for field[%s]","messagePattern":"Cannot translate sqlTypeName\\[(.+?)\\] to Druid type for field\\[(.+?)\\]","errorType":"exception","errorClass":"org.apache.druid.java.util.common.ISE","httpStatus":null,"severity":"error","filePath":"extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/sql/ThetaSketchBaseSqlAggregator.java","lineNumber":123,"sourceCode":"                        .map(type -> (\n                            SketchModule.THETA_SKETCH_TYPE.equals(type) ||\n                            SketchModule.MERGE_TYPE.equals(type) ||\n                            SketchModule.BUILD_TYPE.equals(type)\n                        ))\n                        .orElse(false)) {\n      aggregatorFactory = new SketchMergeAggregatorFactory(\n          aggregatorName,\n          columnArg.getDirectColumn(),\n          sketchSize,\n          finalizeSketch || SketchQueryContext.isFinalizeOuterSketches(plannerContext),\n          null,\n          null\n      );\n    } else {\n      final RelDataType dataType = columnRexNode.getType();\n      final ColumnType inputType = Calcites.getColumnTypeForRelDataType(dataType);\n      if (inputType == null) {\n        throw new ISE(\n            \"Cannot translate sqlTypeName[%s] to Druid type for field[%s]\",\n            dataType.getSqlTypeName(),\n            aggregatorName\n        );\n      }\n\n      if (inputType.is(ValueType.COMPLEX)) {\n        if (!isValidComplexInputType(inputType)) {\n          plannerContext.setPlanningError(\n              \"Using APPROX_COUNT_DISTINCT() or enabling approximation with COUNT(DISTINCT) is not supported for\"\n              + \" column type [%s]. You can disable approximation by setting [%s: false] in the query context.\",\n              columnArg.getDruidType(),\n              PlannerConfig.CTX_KEY_USE_APPROXIMATE_COUNT_DISTINCT\n          );\n          return null;\n        }\n      }\n","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/sql/ThetaSketchBaseSqlAggregator.java#L105-L141","documentation":"When building a theta sketch SQL aggregation, the input RexNode is a column reference whose RelDataType must map to a known Druid ColumnType. Calcites.getColumnTypeForRelDataType() returns null for unmapped SQL types, and the planner then throws this ISE because the sketch aggregator cannot be constructed over an unknown input type.","triggerScenarios":"Issuing a SQL query using APPROX_COUNT_DISTINCT_DS_THETA (or other theta aggregators) over a column/expression whose SQL type has no Druid ColumnType counterpart — e.g. exotic types from extensions, multi-valued/complex types outside the supported set, or ANY-typed expressions.","commonSituations":"Aggregating over a nested/complex expression the planner cannot type; using a UDF or extension-injected type; version mismatches where Calcite produces a SqlTypeName the Druid type mapping doesn't cover.","solutions":["Aggregate over a base column with a standard type (string/long/double) rather than a complex expression","Cast the expression to a supported type in SQL, e.g. CAST(col AS VARCHAR)","Check the Druid and Calcite versions for type-mapping coverage; upgrade if the type mapping was added later","Inspect dataType.getSqlTypeName() (already in the message) and consult Calcites.getColumnTypeForRelDataType to see supported types"],"exampleFix":"// before\nSELECT APPROX_COUNT_DISTINCT_DS_THETA(NESTED_FIELD(col, 'x')) FROM t\n// after\nSELECT APPROX_COUNT_DISTINCT_DS_THETA(CAST(NESTED_FIELD(col, 'x') AS VARCHAR)) FROM t","handlingStrategy":"validation","validationCode":"RelDataType t = expr.getType();\nColumnType inputType = Calcites.getColumnTypeForRelDataType(t);\nif (inputType == null) {\n  throw new IllegalArgumentException(\"Cannot aggregate over SQL type \" + t.getSqlTypeName());\n}","typeGuard":"boolean hasMappableType(RelDataType t) {\n  return Calcites.getColumnTypeForRelDataType(t) != null;\n}","tryCatchPattern":"try {\n  aggregation = sqlAggregator.toDruidAggregation(name, inputRowSignature, relNode);\n} catch (IllegalStateException e) {\n  // rewrite query with a CAST of the argument to VARCHAR/DOUBLE before aggregating\n}","preventionTips":["Aggregate over base columns of standard Druid types","CAST complex expressions to VARCHAR before passing to theta sketch SQL functions","Keep Druid upgraded so Calcite type mappings cover your types"],"tags":["java","datasketches","sql-planning"],"backgroundTag":"type-mismatch","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}