Skip to content

Commit ee28168

Browse files
committed
Support TVF
1 parent accdff2 commit ee28168

File tree

7 files changed

+55
-9
lines changed

7 files changed

+55
-9
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ jobs:
7979
with:
8080
commit-message: Update Snapshot
8181
reviewers: Matts966
82-
branch: snapshot-testing-${{ steps.extract_branch.outputs.branch }}
82+
branch: snapshot-${{ steps.extract_branch.outputs.branch }}
8383
delete-branch: true
8484
title: 'Updated Snapshot :tada:'
8585
body: |
8686
Updated Snapshot :tada:
87-
Auto-generated by [create-pull-request][https://github.com/peter-evans/create-pull-request]
87+
Auto-generated by [create-pull-request](https://github.com/peter-evans/create-pull-request)

alphasql/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ cc_binary(
9999
"@com_google_zetasql//zetasql/public:evaluator_table_iterator",
100100
"@com_google_zetasql//zetasql/public:language_options",
101101
"@com_google_zetasql//zetasql/public:simple_catalog",
102+
"@com_google_zetasql//zetasql/public:templated_sql_tvf",
102103
"@com_google_zetasql//zetasql/public:type",
103104
"@com_google_zetasql//zetasql/public:type_cc_proto",
104105
"@com_google_zetasql//zetasql/public:value",

alphasql/alphacheck.cc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "zetasql/public/type.h"
4444
#include "zetasql/public/value.h"
4545
#include "zetasql/public/templated_sql_function.h"
46+
#include "zetasql/public/templated_sql_tvf.h"
4647
#include "zetasql/resolved_ast/resolved_ast.h"
4748

4849
#include "alphasql/common_lib.h"
@@ -216,6 +217,19 @@ absl::Status check(const std::string &sql, const ASTStatement *statement,
216217
}
217218
break;
218219
}
220+
case RESOLVED_CREATE_TABLE_FUNCTION_STMT: {
221+
auto *create_table_function_stmt =
222+
resolved_statement->GetAs<ResolvedCreateTableFunctionStmt>();
223+
std::cout
224+
<< "Create Table Function Statement analyzed, adding function to catalog..."
225+
<< std::endl;
226+
catalog->AddOwnedTableValuedFunction(new TemplatedSQLTVF(
227+
create_table_function_stmt->name_path(),
228+
create_table_function_stmt->signature(),
229+
create_table_function_stmt->argument_name_list(),
230+
ParseResumeLocation::FromString(create_table_function_stmt->code())));
231+
break;
232+
}
219233
// TODO: DROP PROCEDURE Support?
220234
case RESOLVED_CREATE_PROCEDURE_STMT: {
221235
auto *create_procedure_stmt =
@@ -484,13 +498,25 @@ int main(int argc, char *argv[]) {
484498
status = zetasql::UpdateErrorLocationPayloadWithFilenameIfNotPresent(
485499
status, sql_file_path);
486500
std::cerr << "ERROR: " << status << std::endl;
487-
std::cout << "catalog:" << std::endl;
501+
std::cout << "tables:" << std::endl;
488502
// For deterministic output
489503
auto table_names = catalog->table_names();
490504
std::sort(table_names.begin(), table_names.end());
491505
for (const std::string &table_name : table_names) {
492506
std::cout << "\t" << table_name << std::endl;
493507
}
508+
// Too many outputs
509+
/* auto function_names = catalog->function_names(); */
510+
/* std::sort(function_names.begin(), function_names.end()); */
511+
/* for (const std::string &function_name : function_names) { */
512+
/* std::cout << "\t" << function_name << std::endl; */
513+
/* } */
514+
std::cout << "tvfs:" << std::endl;
515+
auto table_function_names = catalog->table_valued_function_names();
516+
std::sort(table_function_names.begin(), table_function_names.end());
517+
for (const std::string &table_function_name : table_function_names) {
518+
std::cout << "\t" << table_function_name << std::endl;
519+
}
494520
return 1;
495521
}
496522
}

alphasql/identifier_resolver.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,6 @@ void IdentifierResolver::visitASTCreateTableStatement(
138138
visitASTChildren(node, data);
139139
}
140140

141-
// TODO:(Matts966) Check if this node is callee or caller and implement
142-
// correctly void IdentifierResolver::visitASTTVF(const ASTTVF* node, void*
143-
// data) {
144-
// function_information.called.insert(node->name()->ToIdentifierVector());
145-
// }
146-
147141
// Check INSERT and UPDATE statement to emit warnings for side effects.
148142
void IdentifierResolver::visitASTInsertStatement(const ASTInsertStatement *node,
149143
void *data) {
@@ -231,6 +225,12 @@ void IdentifierResolver::visitASTDropFunctionStatement(
231225
visitASTChildren(node, data);
232226
}
233227

228+
void IdentifierResolver::visitASTTVF(const ASTTVF* node, void* data) {
229+
identifier_information.function_information.called.insert(
230+
node->name()->ToIdentifierVector());
231+
visitASTChildren(node, data);
232+
}
233+
234234
void IdentifierResolver::visitASTFunctionCall(const ASTFunctionCall *node,
235235
void *data) {
236236
identifier_information.function_information.called.insert(

alphasql/identifier_resolver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class IdentifierResolver : public DefaultParseTreeVisitor {
101101
// Functions
102102
void visitASTDropFunctionStatement(const ASTDropFunctionStatement *node,
103103
void *data) override;
104+
void visitASTTVF(const ASTTVF* node, void* data) override;
104105
void visitASTFunctionCall(const ASTFunctionCall *node, void *data) override;
105106
void visitASTFunctionDeclaration(const ASTFunctionDeclaration *node,
106107
void *data) override;

samples/tvf/call_tvf.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE OR REPLACE TABLE `dataset.tvf_table` AS
2+
SELECT
3+
*
4+
FROM
5+
`dataset.tvf`(0);

samples/tvf/define_tvf.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE OR REPLACE TABLE FUNCTION `dataset.tvf`(input int64)
2+
AS
3+
SELECT
4+
*
5+
EXCEPT (
6+
test_code
7+
)
8+
FROM
9+
UNNEST(ARRAY[STRUCT("1" AS id, "apple" AS product_name, 120 AS price, date("2022-01-11") AS purchase_date,
10+
1 AS test_code), ("2", "banana", 100, date("2022-01-11"), 1), ("3", "orange", 80, date("2022-01-12"),
11+
1)])
12+
WHERE
13+
test_code = input;

0 commit comments

Comments
 (0)