Skip to content

Commit d88724e

Browse files
committed
Add schema to functions
Some functions may require schema before the name. E.g.: MonetDB json.isarray function: json.isarray('[1, 2, 3]'). PostgreSQL also allows it: "Schemas also contain other kinds of named objects, including data types, functions, and operators." (https://www.postgresql.org/docs/current/ddl-schemas.html). Let's support it.
1 parent 3456f97 commit d88724e

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/parser/bison_parser.y

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,9 @@ comp_expr : operand '=' operand { $$ = Expr::makeOpBinary($1, kOpEquals, $3); }
11711171
// `function_expr is used for window functions, aggregate expressions, and functions calls because we run into shift/
11721172
// reduce conflicts when splitting them.
11731173
function_expr : IDENTIFIER '(' ')' opt_window { $$ = Expr::makeFunctionRef($1, new std::vector<Expr*>(), false, $4); }
1174-
| IDENTIFIER '(' opt_distinct expr_list ')' opt_window { $$ = Expr::makeFunctionRef($1, $4, $3, $6); };
1174+
| IDENTIFIER '(' opt_distinct expr_list ')' opt_window { $$ = Expr::makeFunctionRef($1, $4, $3, $6); }
1175+
| IDENTIFIER '.' IDENTIFIER '(' ')' opt_window { $$ = Expr::makeFunctionRef($3, $1, new std::vector<Expr*>(), false, $6); }
1176+
| IDENTIFIER '.' IDENTIFIER '(' opt_distinct expr_list ')' opt_window { $$ = Expr::makeFunctionRef($3, $1, $6, $5, $8); };
11751177

11761178
// Window function expressions, based on https://www.postgresql.org/docs/15/sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS
11771179
// We do not support named windows, collations and exclusions (for simplicity) and filters (not part of the SQL standard).

src/sql/Expr.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Expr::Expr(ExprType type)
4949
select(nullptr),
5050
name(nullptr),
5151
table(nullptr),
52+
schema(nullptr),
5253
alias(nullptr),
5354
fval(0),
5455
ival(0),
@@ -68,6 +69,7 @@ Expr::~Expr() {
6869

6970
free(name);
7071
free(table);
72+
free(schema);
7173
free(alias);
7274

7375
if (exprList) {
@@ -219,6 +221,16 @@ Expr* Expr::makeFunctionRef(char* func_name, std::vector<Expr*>* exprList, bool
219221
return e;
220222
}
221223

224+
Expr* Expr::makeFunctionRef(char* func_name, char* schema, std::vector<Expr*>* exprList, bool distinct, WindowDescription* window) {
225+
Expr* e = new Expr(kExprFunctionRef);
226+
e->name = func_name;
227+
e->schema = schema;
228+
e->exprList = exprList;
229+
e->distinct = distinct;
230+
e->windowDescription = window;
231+
return e;
232+
}
233+
222234
Expr* Expr::makeArray(std::vector<Expr*>* exprList) {
223235
Expr* e = new Expr(kExprArray);
224236
e->exprList = exprList;

src/sql/Expr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ struct Expr {
133133
SelectStatement* select;
134134
char* name;
135135
char* table;
136+
char* schema;
136137
char* alias;
137138
double fval;
138139
int64_t ival;
@@ -200,6 +201,8 @@ struct Expr {
200201

201202
static Expr* makeFunctionRef(char* func_name, std::vector<Expr*>* exprList, bool distinct, WindowDescription* window);
202203

204+
static Expr* makeFunctionRef(char* func_name, char* schema, std::vector<Expr*>* exprList, bool distinct, WindowDescription* window);
205+
203206
static Expr* makeArray(std::vector<Expr*>* exprList);
204207

205208
static Expr* makeArrayIndex(Expr* expr, int64_t index);

test/select_tests.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,4 +1255,26 @@ TEST(WindowFunctions) {
12551255
}
12561256
}
12571257

1258+
TEST(FunctionSchema) {
1259+
TEST_PARSE_SQL_QUERY(
1260+
"SELECT sys.uuid();"
1261+
"SELECT json.isarray('[1, 2, 3]');"
1262+
result, 1);
1263+
1264+
auto stmt = (SelectStatement*)result.getStatement(0);
1265+
ASSERT_TRUE(stmt->selectList);
1266+
ASSERT_EQ(stmt->selectList->size(), 1);
1267+
ASSERT_STREQ(stmt->selectList->at(0)->schema, "sys");
1268+
ASSERT_STREQ(stmt->selectList->at(0)->name, "uuid");
1269+
ASSERT_EQ(stmt->selectList->at(0)->exprList->size, 0);
1270+
1271+
auto stmt = (SelectStatement*)result.getStatement(1);
1272+
ASSERT_TRUE(stmt->selectList);
1273+
ASSERT_EQ(stmt->selectList->size(), 1);
1274+
ASSERT_STREQ(stmt->selectList->at(0)->schema, "json");
1275+
ASSERT_STREQ(stmt->selectList->at(0)->name, "isarray");
1276+
ASSERT_EQ(stmt->selectList->at(0)->exprList->size, 1);
1277+
ASSERT_STREQ(stmt->selectList->at(0)->exprList->at(0)->name, "'[1, 2, 3]'");
1278+
}
1279+
12581280
} // namespace hsql

0 commit comments

Comments
 (0)