347816 {
347817 static constexpr idx_t MAX_LINE_RENDER_WIDTH = 120;
347818 if (!error_loc.IsValid()) {
347819
347820 return error_message;
347821 }
347822 idx_t error_location = error_loc.GetIndex();
347823 if (error_location >= query.size()) {
347824
347825 return error_message;
347826 }
347827
347828
347829 idx_t start_pos = 0;
347830 idx_t line_number = 1;
347831 for (
idx_t i = 0; i < error_location; i++) {
347832 bool is_newline = false;
347833 switch (query[i]) {
347834 case '\r':
347835 if (i + 1 >= error_location || query[i + 1] != '\n') {
347836
347837 is_newline = true;
347838 }
347839 break;
347840 case '\n':
347841 is_newline = true;
347842 break;
347843 default:
347844 break;
347845 }
347846 if (is_newline) {
347847 line_number++;
347848 start_pos = i + 1;
347849 }
347850 }
347851
347852
347853 idx_t end_pos = query.size();
347854 for (
idx_t i = error_location; i < query.size(); i++) {
347855 if (StringUtil::CharacterIsNewline(query[i])) {
347856 end_pos = i;
347857 break;
347858 }
347859 }
347860
347861
347862
347863 const char *buf = query.c_str() + start_pos;
347864 idx_t len = end_pos - start_pos;
347865 vector<idx_t> render_widths;
347866 vector<idx_t> positions;
347867 vector<idx_t> natural_break;
347869
347870
347871 for (
idx_t cpos = 0; cpos < len;) {
347873 positions.push_back(cpos);
347874 render_widths.push_back(char_render_width);
347875 natural_break.push_back(StringUtil::CharacterIsOperator(buf[cpos]) ||
347876 StringUtil::CharacterIsSpace(buf[cpos]));
347878 }
347879 } else {
347880
347881
347882 for (
idx_t cpos = 0; cpos < len; cpos++) {
347883 positions.push_back(cpos);
347884 render_widths.push_back(1);
347885 natural_break.push_back(StringUtil::CharacterIsOperator(buf[cpos]) ||
347886 StringUtil::CharacterIsSpace(buf[cpos]));
347887 }
347888 }
347889
347891
347892 for (
idx_t i = 0; i < positions.size(); i++) {
347893 if (positions[i] >= (error_location - start_pos)) {
347894 epos = i;
347895 break;
347896 }
347897 }
347898 bool truncate_beginning = false;
347899 bool truncate_end = false;
347901
347902
347903 idx_t error_line_start = start_pos;
347904 idx_t current_render_width = 0;
347905 for (
idx_t i = epos; i > 0; i--) {
347906 current_render_width += render_widths[i];
347907 if (current_render_width >= MAX_LINE_RENDER_WIDTH / 2) {
347908
347909
347910
347911 idx_t start_scan = i > 4 ? i - 4 : 0;
347912 idx_t end_scan = MinValue<idx_t>(i + 4, epos);
347913 for (
idx_t k = start_scan; k < end_scan; k++) {
347914 if (natural_break[k]) {
347915 i = k;
347916 break;
347917 }
347918 }
347919 truncate_beginning = true;
347920 start_pos += positions[i];
347921 spos = i;
347922 break;
347923 }
347924 }
347925
347926 current_render_width = 0;
347927 for (
idx_t i = epos; i < positions.size(); i++) {
347928 current_render_width += render_widths[i];
347929 if (current_render_width >= MAX_LINE_RENDER_WIDTH / 2) {
347930
347931
347932
347933 idx_t start_scan = i > epos + 4 ? i - 4 : epos;
347934 idx_t end_scan = MinValue<idx_t>(i + 4, positions.size());
347935 for (
idx_t k = start_scan; k < end_scan; k++) {
347936 if (natural_break[k]) {
347937 i = k;
347938 break;
347939 }
347940 }
347941 truncate_end = true;
347942 end_pos = error_line_start + positions[i];
347943 break;
347944 }
347945 }
347946 string line_indicator;
347947 if (add_line_indicator) {
347948 line_indicator =
"LINE " +
to_string(line_number) +
": ";
347949 }
347950 string begin_trunc = truncate_beginning ? "..." : "";
347951 string end_trunc = truncate_end ? "..." : "";
347952
347953
347954 idx_t error_render_width = 0;
347955 for (
idx_t i = spos; i < epos; i++) {
347956 error_render_width += render_widths[i];
347957 }
347958 error_render_width += line_indicator.size() + begin_trunc.size();
347959
347960
347961 string result = error_message;
347962 result += "\n\n" + line_indicator + begin_trunc + query.substr(start_pos, end_pos - start_pos) + end_trunc;
347963
347964 result += "\n" + string(error_render_width, ' ') + "^";
347965 return result;
347966}
static size_t RenderWidth(const char *s, size_t len, size_t pos)
Returns the render width of a single character in a string.
Definition duckdb.cpp:507613
static bool IsValid(const char *s, size_t len)
Returns whether or not the UTF8 string is valid.
Definition duckdb.cpp:507383
static size_t NextGraphemeCluster(const char *s, size_t len, size_t pos)
Returns the position (in bytes) of the next grapheme cluster.
Definition duckdb.cpp:507427
std::string to_string(const T &value)
Definition duckdb.cpp:19425
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237