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

Function tryCastStringToStruct

src/function/cast_from_string_functions.cpp:590–649  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

588}
589
590static bool tryCastStringToStruct(const char* input, uint64_t len, ValueVector* vector,
591 uint64_t rowToAdd, const CSVOption* option) {
592 // default values to NULL
593 auto fieldVectors = StructVector::getFieldVectors(vector);
594 for (auto& fieldVector : fieldVectors) {
595 fieldVector->setNull(rowToAdd, true);
596 }
597
598 // check if start with {
599 auto end = input + len;
600 const auto& type = vector->dataType;
601 skipWhitespace(input, end);
602 if (input == end || *input != '{') {
603 return false;
604 }
605 skipWhitespace(++input, end);
606
607 if (input == end) { // no closing bracket
608 return false;
609 }
610 if (*input == '}') {
611 skipWhitespace(++input, end);
612 return input == end;
613 }
614
615 bool closeBracket = false;
616 while (input < end) {
617 auto keyStart = input;
618 if (!parseStructFieldName(input, end)) { // find key
619 return false;
620 }
621 auto keyEnd = input;
622 trimRightWhitespace(keyStart, keyEnd);
623 trimQuotes(keyStart, keyEnd);
624 auto fieldIdx = StructType::getFieldIdx(type, std::string{keyStart, keyEnd});
625 if (fieldIdx == INVALID_STRUCT_FIELD_IDX) {
626 throw ParserException{"Invalid struct field name: " + std::string{keyStart, keyEnd}};
627 }
628
629 skipWhitespace(++input, end);
630 auto valStart = input;
631 if (!parseStructFieldValue(input, end, option, closeBracket)) { // find value
632 return false;
633 }
634 auto valEnd = input;
635 trimRightWhitespace(valStart, valEnd);
636 trimQuotes(valStart, valEnd);
637 skipWhitespace(++input, end);
638
639 auto fieldVector = StructVector::getFieldVector(vector, fieldIdx).get();
640 fieldVector->setNull(rowToAdd, false);
641 CastString::copyStringToVector(fieldVector, rowToAdd,
642 std::string_view{valStart, (uint32_t)(valEnd - valStart)}, option);
643
644 if (closeBracket) {
645 return (input == end);
646 }
647 }

Callers 1

castMethod · 0.85

Calls 8

getFieldVectorsFunction · 0.85
skipWhitespaceFunction · 0.85
parseStructFieldNameFunction · 0.85
trimRightWhitespaceFunction · 0.85
trimQuotesFunction · 0.85
parseStructFieldValueFunction · 0.85
setNullMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected