void Node::setLogicalType()

in lang/c++/impl/Node.cc [136:244]


void Node::setLogicalType(LogicalType logicalType) {
    checkLock();

    // Check that the logical type is applicable to the node type.
    switch (logicalType.type()) {
        case LogicalType::NONE: break;
        case LogicalType::BIG_DECIMAL: {
            if (type_ != AVRO_BYTES) {
                throw Exception("BIG_DECIMAL logical type can annotate "
                                "only BYTES type");
            }
            break;
        }
        case LogicalType::DECIMAL: {
            if (type_ != AVRO_BYTES && type_ != AVRO_FIXED) {
                throw Exception("DECIMAL logical type can annotate "
                                "only BYTES or FIXED type");
            }
            if (type_ == AVRO_FIXED) {
                // Max precision that can be supported by the current size of
                // the FIXED type.
                auto maxPrecision = static_cast<int32_t>(floor(log10(2.0) * (8.0 * static_cast<double>(fixedSize()) - 1)));
                if (logicalType.precision() > maxPrecision) {
                    throw Exception(
                        "DECIMAL precision {} is too large for the "
                        "FIXED type of size {}, precision cannot be "
                        "larger than {}",
                        logicalType.precision(), fixedSize(), maxPrecision);
                }
            }
            if (logicalType.scale() > logicalType.precision()) {
                throw Exception("DECIMAL scale cannot exceed precision");
            }
            break;
        }
        case LogicalType::DATE:
            if (type_ != AVRO_INT) {
                throw Exception("DATE logical type can only annotate INT type");
            }
            break;
        case LogicalType::TIME_MILLIS:
            if (type_ != AVRO_INT) {
                throw Exception("TIME-MILLIS logical type can only annotate "
                                "INT type");
            }
            break;
        case LogicalType::TIME_MICROS:
            if (type_ != AVRO_LONG) {
                throw Exception("TIME-MICROS logical type can only annotate "
                                "LONG type");
            }
            break;
        case LogicalType::TIMESTAMP_MILLIS:
            if (type_ != AVRO_LONG) {
                throw Exception("TIMESTAMP-MILLIS logical type can only annotate "
                                "LONG type");
            }
            break;
        case LogicalType::TIMESTAMP_MICROS:
            if (type_ != AVRO_LONG) {
                throw Exception("TIMESTAMP-MICROS logical type can only annotate "
                                "LONG type");
            }
            break;
        case LogicalType::TIMESTAMP_NANOS:
            if (type_ != AVRO_LONG) {
                throw Exception("TIMESTAMP-NANOS logical type can only annotate "
                                "LONG type");
            }
            break;
        case LogicalType::LOCAL_TIMESTAMP_MILLIS:
            if (type_ != AVRO_LONG) {
                throw Exception("LOCAL-TIMESTAMP-MILLIS logical type can only annotate "
                                "LONG type");
            }
            break;
        case LogicalType::LOCAL_TIMESTAMP_MICROS:
            if (type_ != AVRO_LONG) {
                throw Exception("LOCAL-TIMESTAMP-MICROS logical type can only annotate "
                                "LONG type");
            }
            break;
        case LogicalType::LOCAL_TIMESTAMP_NANOS:
            if (type_ != AVRO_LONG) {
                throw Exception("LOCAL-TIMESTAMP-NANOS logical type can only annotate "
                                "LONG type");
            }
            break;
        case LogicalType::DURATION:
            if (type_ != AVRO_FIXED || fixedSize() != 12) {
                throw Exception("DURATION logical type can only annotate "
                                "FIXED type of size 12");
            }
            break;
        case LogicalType::UUID:
            if (type_ != AVRO_STRING) {
                throw Exception("UUID logical type can only annotate "
                                "STRING type");
            }
            break;
        case LogicalType::CUSTOM:
            if (logicalType.customLogicalType() == nullptr) {
                throw Exception("CUSTOM logical type is not set");
            }
            break;
    }

    logicalType_ = logicalType;
}