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

Function inferMapOrStruct

src/function/cast_string_non_nested_functions.cpp:108–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

106}
107
108static LogicalType inferMapOrStruct(std::string_view str) {
109 auto split = StringUtils::smartSplit(str.substr(1, str.size() - 2), ',');
110 bool isMap = true, isStruct = true; // Default match to map if both are true
111 for (auto& ele : split) {
112 if (StringUtils::smartSplit(ele, '=', 2).size() != 2) {
113 isMap = false;
114 }
115 if (StringUtils::smartSplit(ele, ':', 2).size() != 2) {
116 isStruct = false;
117 }
118 }
119 if (isMap) {
120 auto childKeyType = LogicalType::ANY();
121 auto childValueType = LogicalType::ANY();
122 for (auto& ele : split) {
123 auto split = StringUtils::smartSplit(ele, '=', 2);
124 auto& key = split[0];
125 auto& value = split[1];
126 childKeyType =
127 LogicalTypeUtils::combineTypes(childKeyType, inferMinimalTypeFromString(key));
128 childValueType =
129 LogicalTypeUtils::combineTypes(childValueType, inferMinimalTypeFromString(value));
130 }
131 return LogicalType::MAP(std::move(childKeyType), std::move(childValueType));
132 } else if (isStruct) {
133 std::vector<StructField> fields;
134 for (auto& ele : split) {
135 auto split = StringUtils::smartSplit(ele, ':', 2);
136 auto fieldKey = StringUtils::ltrim(StringUtils::rtrim(split[0]));
137 if (fieldKey.size() > 0 && fieldKey.front() == '\'') {
138 fieldKey = fieldKey.substr(1);
139 }
140 if (fieldKey.size() > 0 && fieldKey.back() == '\'') {
141 fieldKey = fieldKey.substr(0, fieldKey.size() - 1);
142 }
143 auto fieldType = inferMinimalTypeFromString(split[1]);
144 fields.emplace_back(std::string(fieldKey), std::move(fieldType));
145 }
146 return LogicalType::STRUCT(std::move(fields));
147 } else {
148 return LogicalType::STRING();
149 }
150}
151
152LogicalType inferMinimalTypeFromString(const std::string& str) {
153 return inferMinimalTypeFromString(std::string_view(str));

Callers 1

Calls 5

ltrimFunction · 0.85
rtrimFunction · 0.85
emplace_backMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected