Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::QueryErrorContext Class Reference
Collaboration diagram for duckdb::QueryErrorContext:

Public Member Functions

 QueryErrorContext (const ParsedExpression &expr)
 QueryErrorContext (optional_idx query_location_p=optional_idx())

Static Public Member Functions

static string Format (const string &query, const string &error_message, optional_idx error_loc, bool add_line_indicator=true)

Public Attributes

optional_idx query_location
 The location in which the error should be thrown.

Constructor & Destructor Documentation

◆ QueryErrorContext() [1/2]

duckdb::QueryErrorContext::QueryErrorContext ( const ParsedExpression & expr)
347812 : query_location(expr.query_location) {
347813}
optional_idx query_location
The location in which the error should be thrown.
Definition duckdb.hpp:2622

◆ QueryErrorContext() [2/2]

duckdb::QueryErrorContext::QueryErrorContext ( optional_idx query_location_p = optional_idx())
inlineexplicit
2618 : query_location(query_location_p) {
2619 }

Member Function Documentation

◆ Format()

string duckdb::QueryErrorContext::Format ( const string & query,
const string & error_message,
optional_idx error_loc,
bool add_line_indicator = true )
static
347816 {
347817 static constexpr idx_t MAX_LINE_RENDER_WIDTH = 120;
347818 if (!error_loc.IsValid()) {
347819 // no location in query provided
347820 return error_message;
347821 }
347822 idx_t error_location = error_loc.GetIndex();
347823 if (error_location >= query.size()) {
347824 // out of bounds
347825 return error_message;
347826 }
347827 // count the line numbers until the error location
347828 // and set the start position as the first character of that line
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 // not \r\n
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 // now find either the next newline token after the query, or find the end of string
347852 // this is the initial end position
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 // now start scanning from the start pos
347861 // we want to figure out the start and end pos of what we are going to render
347862 // we want to render at most 80 characters in total, with the error_location located in the middle
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;
347868 if (Utf8Proc::IsValid(buf, len)) {
347869 // for unicode awareness, we traverse the graphemes of the current line and keep track of their render widths
347870 // and of their position in the string
347871 for (idx_t cpos = 0; cpos < len;) {
347872 auto char_render_width = Utf8Proc::RenderWidth(buf, len, cpos);
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]));
347877 cpos = Utf8Proc::NextGraphemeCluster(buf, len, cpos);
347878 }
347879 } else { // LCOV_EXCL_START
347880 // invalid utf-8, we can't do much at this point
347881 // we just assume every character is a character, and every character has a render width of 1
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 } // LCOV_EXCL_STOP
347889 // now we want to find the (unicode aware) start and end position
347890 idx_t epos = 0;
347891 // start by finding the error location inside the array
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;
347900 idx_t spos = 0;
347901 // now we iterate backwards from the error location
347902 // we show max 60 render width before the error location
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 // we're exceeding the render width - truncate the beginning
347909 // try to break at a "nice" point (i.e. a space, bracket, etc)
347910 // try to find a natural break that is within 4 bytes of here
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 // now do the same, but going forward
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 // we're exceeding the render width - truncate the end
347931 // try to break at a "nice" point (i.e. a space, bracket, etc)
347932 // try to find a natural break that is within 4 bytes of here
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 // get the render width of the error indicator (i.e. how many spaces we need to insert before the ^)
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 // now first print the error message plus the current line (or a subset of the line)
347961 string result = error_message;
347962 result += "\n\n" + line_indicator + begin_trunc + query.substr(start_pos, end_pos - start_pos) + end_trunc;
347963 // print an arrow pointing at the error location
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

The documentation for this class was generated from the following files: