1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parser/parser_extension.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12#include "duckdb/common/enums/statement_type.hpp"
13#include "duckdb/function/table_function.hpp"
14
15namespace duckdb {
16
17//! The ParserExtensionInfo holds static information relevant to the parser extension
18//! It is made available in the parse_function, and will be kept alive as long as the database system is kept alive
19struct ParserExtensionInfo {
20 virtual ~ParserExtensionInfo() {
21 }
22};
23
24//===--------------------------------------------------------------------===//
25// Parse
26//===--------------------------------------------------------------------===//
27enum class ParserExtensionResultType : uint8_t { PARSE_SUCCESSFUL, DISPLAY_ORIGINAL_ERROR, DISPLAY_EXTENSION_ERROR };
28
29//! The ParserExtensionParseData holds the result of a successful parse step
30//! It will be passed along to the subsequent plan function
31struct ParserExtensionParseData {
32 virtual ~ParserExtensionParseData() {
33 }
34
35 virtual unique_ptr<ParserExtensionParseData> Copy() const = 0;
36};
37
38struct ParserExtensionParseResult {
39 ParserExtensionParseResult() : type(ParserExtensionResultType::DISPLAY_ORIGINAL_ERROR) {
40 }
41 ParserExtensionParseResult(string error_p)
42 : type(ParserExtensionResultType::DISPLAY_EXTENSION_ERROR), error(std::move(error_p)) {
43 }
44 ParserExtensionParseResult(unique_ptr<ParserExtensionParseData> parse_data_p)
45 : type(ParserExtensionResultType::PARSE_SUCCESSFUL), parse_data(std::move(parse_data_p)) {
46 }
47
48 //! Whether or not parsing was successful
49 ParserExtensionResultType type;
50 //! The parse data (if successful)
51 unique_ptr<ParserExtensionParseData> parse_data;
52 //! The error message (if unsuccessful)
53 string error;
54};
55
56typedef ParserExtensionParseResult (*parse_function_t)(ParserExtensionInfo *info, const string &query);
57//===--------------------------------------------------------------------===//
58// Plan
59//===--------------------------------------------------------------------===//
60struct ParserExtensionPlanResult {
61 //! The table function to execute
62 TableFunction function;
63 //! Parameters to the function
64 vector<Value> parameters;
65 //! The set of databases that will be modified by this statement (empty for a read-only statement)
66 unordered_set<string> modified_databases;
67 //! Whether or not the statement requires a valid transaction to be executed
68 bool requires_valid_transaction = true;
69 //! What type of result set the statement returns
70 StatementReturnType return_type = StatementReturnType::NOTHING;
71};
72
73typedef ParserExtensionPlanResult (*plan_function_t)(ParserExtensionInfo *info, ClientContext &context,
74 unique_ptr<ParserExtensionParseData> parse_data);
75
76//===--------------------------------------------------------------------===//
77// ParserExtension
78//===--------------------------------------------------------------------===//
79class ParserExtension {
80public:
81 //! The parse function of the parser extension.
82 //! Takes a query string as input and returns ParserExtensionParseData (on success) or an error
83 parse_function_t parse_function;
84
85 //! The plan function of the parser extension
86 //! Takes as input the result of the parse_function, and outputs various properties of the resulting plan
87 plan_function_t plan_function;
88
89 //! Additional parser info passed to the parse function
90 shared_ptr<ParserExtensionInfo> parser_info;
91};
92
93} // namespace duckdb
94