1#include "duckdb/function/table/system_functions.hpp"
2
3#include "duckdb/common/exception.hpp"
4#include "duckdb/main/client_context.hpp"
5#include "duckdb/parser/parser.hpp"
6
7namespace duckdb {
8
9struct DuckDBKeywordsData : public GlobalTableFunctionState {
10 DuckDBKeywordsData() : offset(0) {
11 }
12
13 vector<ParserKeyword> entries;
14 idx_t offset;
15};
16
17static unique_ptr<FunctionData> DuckDBKeywordsBind(ClientContext &context, TableFunctionBindInput &input,
18 vector<LogicalType> &return_types, vector<string> &names) {
19 names.emplace_back(args: "keyword_name");
20 return_types.emplace_back(args: LogicalType::VARCHAR);
21
22 names.emplace_back(args: "keyword_category");
23 return_types.emplace_back(args: LogicalType::VARCHAR);
24
25 return nullptr;
26}
27
28unique_ptr<GlobalTableFunctionState> DuckDBKeywordsInit(ClientContext &context, TableFunctionInitInput &input) {
29 auto result = make_uniq<DuckDBKeywordsData>();
30 result->entries = Parser::KeywordList();
31 return std::move(result);
32}
33
34void DuckDBKeywordsFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
35 auto &data = data_p.global_state->Cast<DuckDBKeywordsData>();
36 if (data.offset >= data.entries.size()) {
37 // finished returning values
38 return;
39 }
40 // start returning values
41 // either fill up the chunk or return all the remaining columns
42 idx_t count = 0;
43 while (data.offset < data.entries.size() && count < STANDARD_VECTOR_SIZE) {
44 auto &entry = data.entries[data.offset++];
45
46 // keyword_name, VARCHAR
47 output.SetValue(col_idx: 0, index: count, val: Value(entry.name));
48 // keyword_category, VARCHAR
49 string category_name;
50 switch (entry.category) {
51 case KeywordCategory::KEYWORD_RESERVED:
52 category_name = "reserved";
53 break;
54 case KeywordCategory::KEYWORD_UNRESERVED:
55 category_name = "unreserved";
56 break;
57 case KeywordCategory::KEYWORD_TYPE_FUNC:
58 category_name = "type_function";
59 break;
60 case KeywordCategory::KEYWORD_COL_NAME:
61 category_name = "column_name";
62 break;
63 default:
64 throw InternalException("Unrecognized keyword category");
65 }
66 output.SetValue(col_idx: 1, index: count, val: Value(std::move(category_name)));
67
68 count++;
69 }
70 output.SetCardinality(count);
71}
72
73void DuckDBKeywordsFun::RegisterFunction(BuiltinFunctions &set) {
74 set.AddFunction(
75 function: TableFunction("duckdb_keywords", {}, DuckDBKeywordsFunction, DuckDBKeywordsBind, DuckDBKeywordsInit));
76}
77
78} // namespace duckdb
79