MCPcopy Create free account
hub / github.com/documentdb/documentdb / AddInt32ToValue

Function AddInt32ToValue

pg_documentdb_core/src/io/pgbson_utils.c:765–808  ·  view source on GitHub ↗

* Adds an Int32 to the current value. If the current value is an int64/double * applies the sum as an int64/double. If the value overflows, applies * the sum as an int64 * @overflowedFromInt64: Used for AddInt64ToValue(); Set if overflow from Int64 occurs, unset otherwise. */

Source from the content-addressed store, hash-verified

763 * @overflowedFromInt64: Used for AddInt64ToValue(); Set if overflow from Int64 occurs, unset otherwise.
764 */
765static void
766AddInt32ToValue(bson_value_t *current, int32_t value, bool *overflowedFromInt64)
767{
768 if (current->value_type == BSON_TYPE_DOUBLE)
769 {
770 /* current value is already double - just do double add. */
771 AddDoubleToValue(current, (double) value);
772 return;
773 }
774 else if (current->value_type == BSON_TYPE_INT64)
775 {
776 /* current value is int64 - push to int64 handling and let it do */
777 /* overflow checks. */
778 AddInt64ToValue(current, (int64_t) value, overflowedFromInt64);
779 return;
780 }
781 else if (current->value_type == BSON_TYPE_DECIMAL128)
782 {
783 bson_value_t valueToAdd;
784 valueToAdd.value.v_int32 = value;
785 valueToAdd.value_type = BSON_TYPE_INT32;
786
787 valueToAdd.value.v_decimal128 = GetBsonValueAsDecimal128Quantized(&valueToAdd);
788 valueToAdd.value_type = BSON_TYPE_DECIMAL128;
789 AddDecimal128Numbers(current, &valueToAdd, current);
790 return;
791 }
792
793 /* current sum is int32. */
794 int64_t currentValue = BsonValueAsInt64(current);
795 currentValue += value;
796
797 /* Handle overflow. */
798 if (currentValue > INT32_MAX || currentValue < INT32_MIN)
799 {
800 current->value.v_int64 = currentValue;
801 current->value_type = BSON_TYPE_INT64;
802 }
803 else
804 {
805 current->value.v_int32 = (int32_t) currentValue;
806 current->value_type = BSON_TYPE_INT32;
807 }
808}
809
810
811/*

Callers 2

AddNumberToBsonValueFunction · 0.85

Calls 5

AddDoubleToValueFunction · 0.85
AddInt64ToValueFunction · 0.85
AddDecimal128NumbersFunction · 0.85
BsonValueAsInt64Function · 0.85

Tested by

no test coverage detected