Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/sql_parser/expression_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ class ExpressionParser {
case TokenType::TK_TRUE:
case TokenType::TK_FALSE: {
tok_.skip();
return make_node(arena_, NodeType::NODE_LITERAL_INT, t.text);
return make_node_from_token(arena_, NodeType::NODE_LITERAL_INT, t);
}
case TokenType::TK_DEFAULT: {
tok_.skip();
Expand Down
5 changes: 4 additions & 1 deletion include/sql_parser/insert_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ class InsertParser {
if (query_source_start()) {
if (!subquery_cb_) return error();
source = subquery_cb_(tok_, arena_);
} else if (take(TokenType::TK_VALUES) || take_word("VALUE")) {
} else if (tok_.peek().type == TokenType::TK_VALUES ||
ExpressionParser<D>::keyword(tok_.peek(), "VALUE")) {
const Token keyword = tok_.next_token();
source = parse_values_clause();
if (source) source->set_source(keyword.source);
alias_allowed = !is_replace_;
} else if (!columns && take(TokenType::TK_SET)) {
source = parse_assignments(NodeType::NODE_INSERT_SET_CLAUSE);
Expand Down
1 change: 1 addition & 0 deletions include/sql_parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct ParserConfig {
size_t arena_block_size = 65536; // 64KB
size_t arena_max_size = 1048576; // 1MB
size_t stmt_cache_capacity = 128;
bool mysql_backslash_escapes = true; // Set false for NO_BACKSLASH_ESCAPES.
};

template <Dialect D>
Expand Down
8 changes: 6 additions & 2 deletions include/sql_parser/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Tokenizer {
bool has_user_variables() const { return has_user_variables_; }
StringRef error_source() const { return error_source_; }

void set_mysql_backslash_escapes(bool enabled) { mysql_backslash_escapes_ = enabled; }
bool mysql_backslash_escapes() const { return mysql_backslash_escapes_; }

// Hook for parser-level (non-tokenizer) errors -- when the parser
// detects clearly invalid input (e.g. `SET = X`, `SET x = ;`,
// `SET x = ,foo`) it can flag the error so the eventual ParseResult
Expand Down Expand Up @@ -89,6 +92,7 @@ class Tokenizer {
bool has_error_ = false;
bool has_fatal_error_ = false;
bool has_user_variables_ = false;
bool mysql_backslash_escapes_ = true;
uint32_t paren_depth_ = 0;
const char* first_open_paren_ = nullptr;
StringRef error_source_;
Expand Down Expand Up @@ -356,7 +360,7 @@ class Tokenizer {
}
break; // end of string
}
if (*cursor_ == '\\' && (D == Dialect::MySQL || escape_prefix)) {
if (*cursor_ == '\\' && ((D == Dialect::MySQL && mysql_backslash_escapes_) || escape_prefix)) {
++cursor_; // skip escaped char
if (cursor_ < end_) ++cursor_;
} else {
Expand Down Expand Up @@ -385,7 +389,7 @@ class Tokenizer {
}
break;
}
if (*cursor_ == '\\') {
if (*cursor_ == '\\' && mysql_backslash_escapes_) {
++cursor_;
if (cursor_ < end_) ++cursor_;
} else {
Expand Down
8 changes: 7 additions & 1 deletion src/sql_parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ namespace sql_parser {
template <Dialect D>
Parser<D>::Parser(const ParserConfig& config)
: arena_(config.arena_block_size, config.arena_max_size),
stmt_cache_(config.stmt_cache_capacity) {}
stmt_cache_(config.stmt_cache_capacity) {
if constexpr (D == Dialect::MySQL)
tokenizer_.set_mysql_backslash_escapes(config.mysql_backslash_escapes);
}

template <Dialect D>
void Parser<D>::reset() {
Expand All @@ -38,6 +41,7 @@ ParseResult Parser<D>::parse(const char* sql, size_t len) {
bool has_user_variables = false;
if constexpr (D == Dialect::MySQL) {
Tokenizer<D> detector;
detector.set_mysql_backslash_escapes(tokenizer_.mysql_backslash_escapes());
detector.reset(sql, len);
while (detector.next_token().type != TokenType::TK_EOF) {}
has_user_variables = detector.has_user_variables();
Expand All @@ -61,6 +65,8 @@ BatchParseResult Parser<D>::parse_all(const char* sql, size_t len) {
size_t cursor = 0;
while (cursor < len) {
Tokenizer<D> scanner;
if constexpr (D == Dialect::MySQL)
scanner.set_mysql_backslash_escapes(tokenizer_.mysql_backslash_escapes());
scanner.reset(sql + cursor, len - cursor);
Token first = scanner.next_token();
if (first.type == TokenType::TK_SEMICOLON) {
Expand Down
22 changes: 22 additions & 0 deletions tests/test_insert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ TEST_F(MySQLInsertTest, InsertMultiRow) {
EXPECT_EQ(child_count(values), 2); // two rows
}

TEST_F(MySQLInsertTest, PreservesValuesAndBooleanSourceSpans) {
for (const std::string keyword : {"VALUE", "VaLuEs"}) {
const std::string sql = "INSERT INTO t (enabled) " + keyword + " (TrUe), (FaLsE)";
auto result = parser.parse(sql.data(), sql.size());
ASSERT_TRUE(result.ok());
ASSERT_NE(result.ast, nullptr);
const auto* values = find_child(result.ast, NodeType::NODE_VALUES_CLAUSE);
ASSERT_NE(values, nullptr);
EXPECT_EQ(values->source_ptr, sql.data() + sql.find(keyword));
EXPECT_EQ(values->source_len, keyword.size());
ASSERT_EQ(child_count(values), 2);
auto* literal = values->first_child->first_child;
ASSERT_NE(literal, nullptr);
EXPECT_EQ(literal->source_ptr, sql.data() + sql.find("TrUe"));
EXPECT_EQ(literal->source_len, 4u);
literal = values->first_child->next_sibling->first_child;
ASSERT_NE(literal, nullptr);
EXPECT_EQ(literal->source_ptr, sql.data() + sql.find("FaLsE"));
EXPECT_EQ(literal->source_len, 5u);
}
}

TEST_F(MySQLInsertTest, InsertTableRef) {
auto r = parser.parse("INSERT INTO mydb.users (id) VALUES (1)", 39);
EXPECT_EQ(r.status, ParseResult::OK);
Expand Down
23 changes: 23 additions & 0 deletions tests/test_parse_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ TEST(ParseAll, MySqlEscapesAndVariables) {
EXPECT_FALSE(batch.statements[1].result.has_user_variables);
}

TEST(ParseAll, MySqlNoBackslashEscapesAppliesToAllScans) {
ParserConfig config;
config.mysql_backslash_escapes = false;
Parser<Dialect::MySQL> parser(config);
Parser<Dialect::MySQL> default_parser;
const std::string first_statements[] = {R"(SELECT 'a\', @x)", R"(SELECT "a\", @x)"};
for (const auto& first : first_statements) {
const auto result = parser.parse(first.data(), first.size());
ASSERT_TRUE(result.ok());
EXPECT_TRUE(result.full_input);
EXPECT_TRUE(result.has_user_variables);
EXPECT_FALSE(default_parser.parse(first.data(), first.size()).full_input);

const auto sql = first + "; SELECT 2";
const auto batch = parser.parse_all(sql.data(), sql.size());
ASSERT_TRUE(batch.ok());
ASSERT_EQ(batch.statements.size(), 2u);
EXPECT_EQ(batch.statements[0].source.len, first.size() + 1);
EXPECT_TRUE(batch.statements[0].result.has_user_variables);
EXPECT_FALSE(batch.statements[1].result.has_user_variables);
}
}

TEST(ParseAll, UnknownStatementsDoNotMakeSuccessfulBatch) {
Parser<Dialect::PostgreSQL> parser;
std::string sql = "SELECT 1; gibberish; SELECT 2";
Expand Down
Loading