Skip to content

Commit 2e16dd0

Browse files
justing-bqalinaliBQ
authored andcommitted
Allow spaces while parsing Table Type
1 parent 8d35fd1 commit 2e16dd0

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

cpp/src/arrow/flight/sql/odbc/flight_sql/flight_sql_statement_get_tables.cc

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "arrow/flight/sql/odbc/flight_sql/utils.h"
2323
#include "arrow/flight/sql/odbc/odbcabstraction/include/odbcabstraction/platform.h"
2424
#include "arrow/flight/types.h"
25+
#include "arrow/util/string.h"
2526

2627
namespace driver {
2728
namespace flight_sql {
@@ -31,6 +32,15 @@ using arrow::flight::FlightClientOptions;
3132
using arrow::flight::FlightInfo;
3233
using arrow::flight::sql::FlightSqlClient;
3334

35+
static void AddTableType(std::string& table_type, std::vector<std::string>& table_types) {
36+
std::string trimmed_type = arrow::internal::TrimString(table_type);
37+
38+
// Only put the string if the trimmed result is non-empty
39+
if (trimmed_type.length() > 0) {
40+
table_types.emplace_back(trimmed_type);
41+
}
42+
}
43+
3444
void ParseTableTypes(const std::string& table_type,
3545
std::vector<std::string>& table_types) {
3646
bool encountered = false; // for checking if there is a single quote
@@ -39,36 +49,23 @@ void ParseTableTypes(const std::string& table_type,
3949
for (char temp : table_type) { // while still in the string
4050
switch (temp) { // switch depending on the character
4151
case '\'': // if the character is a single quote
42-
if (encountered) {
43-
encountered = false; // if we already found a single quote, reset encountered
44-
} else {
45-
encountered =
46-
true; // if we haven't found a single quote, set encountered to true
47-
}
52+
// track when we've encountered a single opening quote
53+
// and are still looking for the closing quote
54+
encountered = !encountered;
4855
break;
49-
case ',': // if it is a comma
50-
if (!encountered) { // if we have not found a single quote
51-
table_types.push_back(curr_parse); // put our current string into our vector
52-
curr_parse = ""; // reset the current string
56+
case ',': // if it is a comma
57+
if (!encountered) { // if we have not found a single quote
58+
AddTableType(curr_parse, table_types); // put current string into vector
59+
curr_parse = ""; // reset the current string
5360
break;
5461
}
55-
default: // if it is a normal character
56-
if (encountered && isspace(temp)) {
57-
curr_parse.push_back(temp); // if we have found a single quote put the
58-
// whitespace, we don't care
59-
} else if (temp == '\'' || temp == ' ') {
60-
break; // if the current character is a single quote, trash it and go to
61-
// the next character.
62-
} else {
63-
curr_parse.push_back(temp); // if all of the above failed, put the
64-
// character into the current string
65-
}
66-
break; // go to the next character
62+
[[fallthrough]];
63+
default: // if it is a normal character
64+
curr_parse.push_back(temp); // put the character into the current string
65+
break; // go to the next character
6766
}
6867
}
69-
table_types.emplace_back(
70-
curr_parse); // if we have found a single quote put the whitespace,
71-
// we don't care
68+
AddTableType(curr_parse, table_types);
7269
}
7370

7471
std::shared_ptr<ResultSet> GetTablesForSQLAllCatalogs(

cpp/src/arrow/flight/sql/odbc/flight_sql/parse_table_types_test.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,18 @@ TEST(TableTypeParser, ParsingWithSingleQuotesWithoutLeadingWhiteSpace) {
4949
TEST(TableTypeParser, ParsingWithCommaInsideSingleQuotes) {
5050
AssertParseTest("'TABLE, TEST', 'VIEW, TEMPORARY'", {"TABLE, TEST", "VIEW, TEMPORARY"});
5151
}
52+
53+
TEST(TableTypeParser, ParsingWithManyLeadingAndTrailingWhiteSpaces) {
54+
AssertParseTest(" TABLE , VIEW ", {"TABLE", "VIEW"});
55+
}
56+
57+
TEST(TableTypeParser, ParsingWithOnlyWhiteSpaceBetweenCommas) {
58+
AssertParseTest("TABLE, ,VIEW", {"TABLE", "VIEW"});
59+
}
60+
61+
TEST(TableTypeParser, ParsingWithWhiteSpaceInsideValue) {
62+
AssertParseTest("BASE TABLE", {"BASE TABLE"});
63+
}
64+
5265
} // namespace flight_sql
5366
} // namespace driver

0 commit comments

Comments
 (0)