MCPcopy Create free account
hub / github.com/LadybugDB/ladybug / copyValueForNested

Function copyValueForNested

src/c_api/value.cpp:72–125  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

70}
71
72static std::unique_ptr<Value> copyValueForNested(const Value& value,
73 const LogicalType& targetType) {
74 if (value.isNull() && value.getDataType().getLogicalTypeID() == LogicalTypeID::ANY) {
75 return std::make_unique<Value>(Value::createNullValue(targetType));
76 }
77 if (value.getDataType() != targetType && isEmptyNestedWithAny(value)) {
78 return std::make_unique<Value>(Value::createDefaultValue(targetType));
79 }
80 if (value.getDataType() != targetType &&
81 targetType.getLogicalTypeID() == LogicalTypeID::STRING) {
82 return std::make_unique<Value>(Value::createValue<std::string>(value.toString()));
83 }
84 if (value.getDataType() != targetType &&
85 targetType.getLogicalTypeID() == LogicalTypeID::DOUBLE) {
86 return std::make_unique<Value>(std::stod(value.toString()));
87 }
88 if (value.getDataType() != targetType &&
89 targetType.getLogicalTypeID() == LogicalTypeID::FLOAT) {
90 return std::make_unique<Value>(static_cast<float>(std::stof(value.toString())));
91 }
92 if (value.getDataType() != targetType &&
93 value.getDataType().getLogicalTypeID() == LogicalTypeID::LIST &&
94 targetType.getLogicalTypeID() == LogicalTypeID::LIST) {
95 std::vector<std::unique_ptr<Value>> children;
96 const auto& childType = ListType::getChildType(targetType);
97 for (auto i = 0u; i < NestedVal::getChildrenSize(const_cast<Value*>(&value)); ++i) {
98 children.push_back(copyValueForNested(
99 *NestedVal::getChildVal(const_cast<Value*>(&value), i), childType));
100 }
101 return std::make_unique<Value>(targetType.copy(), std::move(children));
102 }
103 if (value.getDataType() != targetType &&
104 value.getDataType().getLogicalTypeID() == LogicalTypeID::MAP &&
105 targetType.getLogicalTypeID() == LogicalTypeID::MAP) {
106 std::vector<std::unique_ptr<Value>> children;
107 const auto& keyType = MapType::getKeyType(targetType);
108 const auto& valueType = MapType::getValueType(targetType);
109 for (auto i = 0u; i < NestedVal::getChildrenSize(const_cast<Value*>(&value)); ++i) {
110 auto* mapEntry = NestedVal::getChildVal(const_cast<Value*>(&value), i);
111 std::vector<StructField> fields;
112 fields.emplace_back(InternalKeyword::MAP_KEY, keyType.copy());
113 fields.emplace_back(InternalKeyword::MAP_VALUE, valueType.copy());
114 std::vector<std::unique_ptr<Value>> structValues;
115 structValues.push_back(
116 copyValueForNested(*NestedVal::getChildVal(mapEntry, 0), keyType));
117 structValues.push_back(
118 copyValueForNested(*NestedVal::getChildVal(mapEntry, 1), valueType));
119 children.push_back(std::make_unique<Value>(LogicalType::STRUCT(std::move(fields)),
120 std::move(structValues)));
121 }
122 return std::make_unique<Value>(targetType.copy(), std::move(children));
123 }
124 return value.copy();
125}
126
127lbug_value* lbug_value_create_null() {
128 auto* c_value = (lbug_value*)calloc(1, sizeof(lbug_value));

Callers 2

lbug_value_create_listFunction · 0.85
lbug_value_create_mapFunction · 0.85

Calls 10

isEmptyNestedWithAnyFunction · 0.85
getChildTypeFunction · 0.85
getKeyTypeFunction · 0.85
getLogicalTypeIDMethod · 0.80
getDataTypeMethod · 0.80
emplace_backMethod · 0.80
isNullMethod · 0.45
toStringMethod · 0.45
push_backMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected