-
Notifications
You must be signed in to change notification settings - Fork 81
feat(clp-s): Add boilerplate for SQL parsing. #504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b41eed1
Add boilerplate for new sql parser
gibber9809 86acbf5
Fix lint
gibber9809 93cb37c
Apply suggestions from code review
gibber9809 b5f55d3
Merge branch 'main' into basic-sql
gibber9809 d96505e
Move antlr visitor derived classes into private namespace and dedupli…
gibber9809 436c396
Apply suggestions from code review
gibber9809 4037d5b
Remove duplicated code
gibber9809 2b5db82
Merge remote-tracking branch 'upstream/main' into basic-sql
gibber9809 f8d67fb
Fix clang-tidy warnings
gibber9809 ce70d88
Undo incorrect clang-tidy suggestion
gibber9809 0f768a5
More clang-tidy fixes
gibber9809 a1d8fe7
Fix compilation error
gibber9809 1805505
Apply suggestions from code review
gibber9809 4af7f86
Lint fix
gibber9809 d09e6c4
Fix macOS build
gibber9809 991ca73
Apply suggestions from code review
gibber9809 ce13759
Update components/core/src/clp_s/search/sql/sql.cpp
gibber9809 5696ac3
Address more review comments
gibber9809 712afbe
Add comment indicating sql grammar is incomplete boilerplate
gibber9809 c2432bb
Minor fix
gibber9809 cd7f164
Update components/core/src/clp_s/search/sql/sql.cpp
gibber9809 d687fe4
Merge remote-tracking branch 'upstream/main' into basic-sql
gibber9809 8f00de2
Merge remote-tracking branch 'upstream/main' into basic-sql
gibber9809 29f8744
Remove spdlog dependency to fix build issue on macOS
gibber9809 f0118f1
Revert "Remove spdlog dependency to fix build issue on macOS"
gibber9809 866dcfc
Attempt to fix macOS build issues
gibber9809 e17ebd0
Merge remote-tracking branch 'upstream/main' into basic-sql
gibber9809 012915f
Merge remote-tracking branch 'upstream/main' into basic-sql
gibber9809 fbb28eb
Fix strange macos build issue
gibber9809 b0e1a7c
lint fix
gibber9809 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
add_subdirectory(search/kql) | ||
add_subdirectory(search/sql) | ||
|
||
set( | ||
CLP_SOURCES | ||
|
36 changes: 36 additions & 0 deletions
36
components/core/src/clp_s/search/antlr_common/ErrorListener.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef CLP_S_SEARCH_ANTLRCOMMON_ERRORLISTENER_HPP | ||
#define CLP_S_SEARCH_ANTLRCOMMON_ERRORLISTENER_HPP | ||
|
||
#include <cstddef> | ||
#include <exception> | ||
#include <string> | ||
#include <string_view> | ||
|
||
#include <antlr4-runtime.h> | ||
|
||
namespace clp_s::search::antlr_common { | ||
class ErrorListener : public antlr4::BaseErrorListener { | ||
public: | ||
auto syntaxError( | ||
[[maybe_unused]] antlr4::Recognizer* recognizer, | ||
[[maybe_unused]] antlr4::Token* offending_symbol, | ||
[[maybe_unused]] size_t line, | ||
[[maybe_unused]] size_t char_position_in_line, | ||
std::string const& msg, | ||
[[maybe_unused]] std::exception_ptr e | ||
) -> void override { | ||
m_error = true; | ||
m_error_message = msg; | ||
} | ||
|
||
[[nodiscard]] auto error() const -> bool { return m_error; } | ||
|
||
[[nodiscard]] auto message() const -> std::string_view { return m_error_message; } | ||
|
||
private: | ||
bool m_error{false}; | ||
std::string m_error_message; | ||
}; | ||
} // namespace clp_s::search::antlr_common | ||
|
||
#endif // CLP_S_SEARCH_ANTLRCOMMON_ERRORLISTENER_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
ANTLR_TARGET( | ||
SqlParser | ||
Sql.g4 | ||
LEXER PARSER VISITOR | ||
PACKAGE sql | ||
) | ||
|
||
add_library( | ||
sql | ||
../../Utils.hpp | ||
../antlr_common/ErrorListener.hpp | ||
../AndExpr.hpp | ||
../BooleanLiteral.hpp | ||
../ColumnDescriptor.hpp | ||
../DateLiteral.hpp | ||
../EmptyExpr.hpp | ||
../Expression.hpp | ||
../FilterExpr.hpp | ||
../Integral.hpp | ||
../NullLiteral.hpp | ||
../OrExpr.hpp | ||
../StringLiteral.hpp | ||
${ANTLR_SqlParser_CXX_OUTPUTS} | ||
sql.cpp | ||
sql.hpp | ||
) | ||
gibber9809 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
target_compile_features(sql PRIVATE cxx_std_20) | ||
target_include_directories(sql PRIVATE ${ANTLR_SqlParser_OUTPUT_DIR}) | ||
target_link_libraries(sql PRIVATE antlr4_static Boost::filesystem) | ||
gibber9809 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Boilerplate for work in progress SQL grammar. | ||
grammar Sql; | ||
|
||
start: EOF ; | ||
|
||
SPACE: [ \t\r\n] -> skip ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <any> | ||
#include <iostream> | ||
gibber9809 marked this conversation as resolved.
Show resolved
Hide resolved
gibber9809 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <memory> | ||
|
||
#include <antlr4-runtime.h> | ||
#include <spdlog/spdlog.h> | ||
|
||
#include "../antlr_common/ErrorListener.hpp" | ||
#include "../EmptyExpr.hpp" | ||
#include "../Expression.hpp" | ||
#include "SqlBaseVisitor.h" | ||
#include "SqlLexer.h" | ||
#include "SqlParser.h" | ||
|
||
using antlr4::ANTLRInputStream; | ||
using antlr4::CommonTokenStream; | ||
using clp_s::search::antlr_common::ErrorListener; | ||
using sql::SqlBaseVisitor; | ||
using sql::SqlLexer; | ||
using sql::SqlParser; | ||
|
||
namespace clp_s::search::sql { | ||
namespace { | ||
class ParseTreeVisitor : public SqlBaseVisitor { | ||
public: | ||
[[nodiscard]] auto visitStart([[maybe_unused]] SqlParser::StartContext* ctx) | ||
-> std::any override { | ||
return EmptyExpr::create(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
auto parse_sql_expression(std::istream& in) -> std::shared_ptr<Expression> { | ||
ErrorListener lexer_error_listener; | ||
ErrorListener parser_error_listener; | ||
|
||
ANTLRInputStream input{in}; | ||
SqlLexer lexer{&input}; | ||
lexer.removeErrorListeners(); | ||
lexer.addErrorListener(&lexer_error_listener); | ||
CommonTokenStream tokens{&lexer}; | ||
SqlParser parser(&tokens); | ||
parser.removeErrorListeners(); | ||
parser.addErrorListener(&parser_error_listener); | ||
SqlParser::StartContext* tree{parser.start()}; | ||
|
||
if (lexer_error_listener.error()) { | ||
SPDLOG_ERROR("Lexer error: {}", lexer_error_listener.message()); | ||
return nullptr; | ||
} | ||
if (parser_error_listener.error()) { | ||
SPDLOG_ERROR("Parser error: {}", parser_error_listener.message()); | ||
return nullptr; | ||
} | ||
|
||
ParseTreeVisitor visitor; | ||
try { | ||
return std::any_cast<std::shared_ptr<Expression>>(visitor.visitStart(tree)); | ||
} catch (std::exception const& e) { | ||
return nullptr; | ||
} | ||
} | ||
} // namespace clp_s::search::sql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef CLP_S_SEARCH_SQL_SQL_HPP | ||
#define CLP_S_SEARCH_SQL_SQL_HPP | ||
|
||
#include <istream> | ||
#include <memory> | ||
|
||
#include "../Expression.hpp" | ||
|
||
namespace clp_s::search::sql { | ||
/** | ||
* Parses an SQL expression from the given stream to generate a search AST. | ||
* @param in Input stream containing an SQL expression followed by EOF | ||
* @return a search AST on success, nullptr otherwise | ||
*/ | ||
[[nodiscard]] auto parse_sql_expression(std::istream& in) -> std::shared_ptr<Expression>; | ||
} // namespace clp_s::search::sql | ||
|
||
#endif // CLP_S_SEARCH_SQL_SQL_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <memory> | ||
#include <sstream> | ||
gibber9809 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#include <Catch2/single_include/catch2/catch.hpp> | ||
|
||
#include "../src/clp_s/search/EmptyExpr.hpp" | ||
#include "../src/clp_s/search/sql/sql.hpp" | ||
#include "LogSuppressor.hpp" | ||
|
||
using clp_s::search::EmptyExpr; | ||
using clp_s::search::sql::parse_sql_expression; | ||
using std::stringstream; | ||
|
||
TEST_CASE("Test parsing SQL", "[SQL]") { | ||
// Suppress logging | ||
LogSuppressor const suppressor; | ||
|
||
SECTION("Stub accepts empty string") { | ||
stringstream empty_string{""}; | ||
auto filter = std::dynamic_pointer_cast<EmptyExpr>(parse_sql_expression(empty_string)); | ||
REQUIRE((nullptr != filter)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.