apache/seatunnel · error · MongodbConnectorException

ILLEGAL_ARGUMENT

ILLEGAL_ARGUMENT

Error message

Unable to convert to LocalDateTime from unexpected value '${bsonValue}' of type ${bsonValue.getBsonType()}

What it means

convertToLocalDateTime converts a BsonValue into LocalDateTime, accepting only BSON Timestamp and BSON Date. Any other BsonType (string, int, null-like BsonNull, object, etc.) cannot become a timestamp, so it throws MongodbConnectorException(ILLEGAL_ARGUMENT) describing the offending value and its BSON type.

Source

Thrown at seatunnel-connectors-v2/connector-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/serde/BsonToRowDataConverters.java:232

                return createMapConverter(
                        mapType.toString(), mapType.getKeyType(), mapType.getValueType());

            case ROW:
                return createRowConverter((SeaTunnelRowType) type);
            default:
                throw new MongodbConnectorException(
                        UNSUPPORTED_DATA_TYPE, "Not support to parse type: " + type);
        }
    }

    private static LocalDateTime convertToLocalDateTime(BsonValue bsonValue) {
        Instant instant;
        if (bsonValue.isTimestamp()) {
            instant = Instant.ofEpochMilli(bsonValue.asTimestamp().getValue());
        } else if (bsonValue.isDateTime()) {
            instant = Instant.ofEpochMilli(bsonValue.asDateTime().getValue());
        } else {
            throw new MongodbConnectorException(
                    ILLEGAL_ARGUMENT,
                    "Unable to convert to LocalDateTime from unexpected value '"
                            + bsonValue
                            + "' of type "
                            + bsonValue.getBsonType());
        }
        return Timestamp.from(instant).toLocalDateTime();
    }

    private static SerializableFunction<BsonValue, Object> createRowConverter(
            SeaTunnelRowType type) {
        SeaTunnelDataType<?>[] fieldTypes = type.getFieldTypes();
        final SerializableFunction<BsonValue, Object>[] fieldConverters =
                Arrays.stream(fieldTypes)
                        .map(BsonToRowDataConverters::createNullSafeInternalConverter)
                        .toArray(SerializableFunction[]::new);
        int fieldCount = type.getTotalFields();

View on GitHub (pinned to cf67b549a7)

Solutions

  1. Make the documents store the field as a real BSON Date (or Timestamp) type.
  2. Re-declare the column as STRING in the schema and parse the date downstream.
  3. Clean/normalize the collection so all documents use a consistent BSON Date type for the field.
  4. Filter out or null-safe documents whose field is BsonNull before conversion.

Example fix

// before (mongo doc)
{ "created_at": "2024-01-01T00:00:00Z" }
// after
{ "created_at": ISODate("2024-01-01T00:00:00Z") }
Defensive patterns

Strategy: validation

Validate before calling

// Run this against the collection before reading with a TIMESTAMP column
db.coll.find({ created_at: { $not: { $type: { $in: ["date", "timestamp"] } } } }).limit(1)
// Returns documents that would trigger the error; result should be empty

Type guard

static boolean isBsonTemporal(org.bson.BsonValue v) {
    return v != null && (v.isDateTime() || v.isTimestamp());
}

Try / catch

try {
    convertToLocalDateTime(bsonValue);
} catch (MongodbConnectorException e) {
    if (e.getMessage().startsWith("Unable to convert to LocalDateTime")) {
        // treat as null / fallback to string parsing of the raw value
    } else { throw e; }
}

Prevention

When it happens

Trigger: A column is declared TIMESTAMP (or DATE) in the SeaTunnel schema but the actual MongoDB document stores the field as a string (e.g. "2024-01-01T00:00:00Z"), a number, or BsonNull; apply() then calls convertToLocalDateTime which hits the else branch.

Common situations: Schema defined as TIMESTAMP while documents hold ISO-8601 strings written by another tool; mixed-type collections where some documents have the field missing (BsonNull) or of a different BSON type.

Related errors


AI-assisted analysis of apache/seatunnel@cf67b549a7 (2026-09-10). Data as JSON: /api/errors/486a55b95c393cdf. Report an issue: GitHub.