MCPcopy Create free account
hub / github.com/apache/arrow / gdv_boolean castBIT_utf8

Function gdv_boolean castBIT_utf8

cpp/src/gandiva/precompiled/string_ops.cc:546–587  ·  view source on GitHub ↗

Try to cast the received string ('0', '1', 'true', 'false'), ignoring leading and trailing spaces, also ignoring lower and upper case.

Source from the content-addressed store, hash-verified

544// Try to cast the received string ('0', '1', 'true', 'false'), ignoring leading
545// and trailing spaces, also ignoring lower and upper case.
546FORCE_INLINE
547gdv_boolean castBIT_utf8(gdv_int64 context, const char* data, gdv_int32 data_len) {
548 if (data_len <= 0) {
549 gdv_fn_context_set_error_msg(context,
550 "CAST_BIT: Invalid value for boolean: empty string "
551 "(expected 0, 1, true, false; case-insensitive)");
552 return false;
553 }
554
555 // trim leading and trailing spaces
556 int32_t trimmed_len;
557 int32_t start = 0, end = data_len - 1;
558 while (start <= end && data[start] == ' ') {
559 ++start;
560 }
561 while (end >= start && data[end] == ' ') {
562 --end;
563 }
564 trimmed_len = end - start + 1;
565 const char* trimmed_data = data + start;
566
567 // compare received string with the valid bool string values '1', '0', 'true', 'false'
568 if (trimmed_len == 1) {
569 // case for '0' and '1' value
570 if (trimmed_data[0] == '1') return true;
571 if (trimmed_data[0] == '0') return false;
572 } else if (trimmed_len == 4) {
573 // case for matching 'true'
574 if (compare_lower_strings("true", 4, trimmed_data, trimmed_len)) return true;
575 } else if (trimmed_len == 5) {
576 // case for matching 'false'
577 if (compare_lower_strings("false", 5, trimmed_data, trimmed_len)) return false;
578 }
579 // if no 'true', 'false', '0' or '1' value is found, set an error
580 char err_msg[128];
581 snprintf(err_msg, sizeof(err_msg),
582 "CAST_BIT: Invalid value for boolean: '%.*s'"
583 " (expected 0, 1, true, false; case-insensitive)",
584 data_len, data);
585 gdv_fn_context_set_error_msg(context, err_msg);
586 return false;
587}
588
589FORCE_INLINE
590const char* castVARCHAR_bool_int64(gdv_int64 context, gdv_boolean value,

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected