{"record":{"id":"f0db2a00d48291f8","repo":"apache/druid","slug":"incompatible-types-s-and-s","errorCode":null,"errorMessage":"Incompatible types: %s and %s","messagePattern":"Incompatible types: (.+?) and (.+?)","errorType":"exception","errorClass":"IncompatibleTypeException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/segment/column/ColumnType.java","lineNumber":178,"sourceCode":"   */\n  @Nullable\n  public static ColumnType leastRestrictiveType(@Nullable ColumnType type, @Nullable ColumnType other) throws Types.IncompatibleTypeException\n  {\n    if (type == null) {\n      return other;\n    }\n    if (other == null) {\n      return type;\n    }\n    if (type.is(ValueType.COMPLEX) && other.is(ValueType.COMPLEX)) {\n      if (type.getComplexTypeName() == null) {\n        return other;\n      }\n      if (other.getComplexTypeName() == null) {\n        return type;\n      }\n      if (!Objects.equals(type, other)) {\n        throw new Types.IncompatibleTypeException(type, other);\n      }\n      return type;\n    }\n    // if either is nested data, use nested data, otherwise error\n    if (type.is(ValueType.COMPLEX) || other.is(ValueType.COMPLEX)) {\n      if (ColumnType.NESTED_DATA.equals(type) || ColumnType.NESTED_DATA.equals(other)) {\n        return ColumnType.NESTED_DATA;\n      }\n      throw new Types.IncompatibleTypeException(type, other);\n    }\n\n    // arrays convert based on least restrictive element type\n    if (type.isArray()) {\n      if (other.equals(type.getElementType())) {\n        return type;\n      }\n      final ColumnType commonElementType;\n      // commonElementType cannot be null if we got this far, we always return a value unless both args are null","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/segment/column/ColumnType.java#L160-L196","documentation":"ColumnType.leastRestrictiveType computes the common supertype of two column types; when neither is null and neither is nested-data-compatible complex, incompatible types (e.g. STRING vs LONG) raise Types.IncompatibleTypeException. It is thrown while reconciling types across operators, functions, or auto-detected inputs.","triggerScenarios":"leastRestrictiveType(string, long) or similar, invoked from autoDetect, operator/function type resolution, or recursive calls, when no common supertype exists.","commonSituations":"SQL queries joining/comparing columns of incompatible types (string vs number, different complex types); COALESCE/CASE branches with mismatched types; schema drift between tables in a join.","solutions":["Add explicit CASTs so both sides share a type: CAST(col AS BIGINT) or CAST(col AS VARCHAR)","Fix the schema/type of one input column (re-index or correct ingestion type)","If a complex (COMPLEX<name>) column is involved, ensure both sides use the same complex type name or cast to a primitive"],"exampleFix":"// before\n// WHERE stringCol = longCol  -> Incompatible types: STRING and LONG\n// after\n// WHERE stringCol = CAST(longCol AS VARCHAR)","handlingStrategy":"validation","validationCode":"ColumnType a = sqlTypeNameOf(left), b = sqlTypeNameOf(right);\ntry { ColumnType.common = ColumnType.leastRestrictiveType(a, b); } catch (IncompatibleTypeException e) { /* require explicit CAST */ }","typeGuard":null,"tryCatchPattern":"try { common = ColumnType.leastRestrictiveType(t1, t2); } catch (IncompatibleTypeException e) { throw new ValidationException(\"add a CAST between \" + t1 + \" and \" + t2); }","preventionTips":["Add explicit CASTs whenever comparing/joining across types","Check column types via INFORMATION_SCHEMA/segment metadata before writing queries","Keep join key types identical across datasources"],"tags":["druid","sql","type-mismatch"],"backgroundTag":"type-mismatch","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"}