Skip to content

Commit

Permalink
Merge pull request #91 from Matts966/feature/tvf
Browse files Browse the repository at this point in the history
Support TVF
  • Loading branch information
Matts966 authored Apr 13, 2022
2 parents accdff2 + 371950b commit 9afef98
Show file tree
Hide file tree
Showing 61 changed files with 185 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ jobs:
with:
commit-message: Update Snapshot
reviewers: Matts966
branch: snapshot-testing-${{ steps.extract_branch.outputs.branch }}
branch: snapshot-${{ steps.extract_branch.outputs.branch }}
delete-branch: true
title: 'Updated Snapshot :tada:'
body: |
Updated Snapshot :tada:
Auto-generated by [create-pull-request][https://github.com/peter-evans/create-pull-request]
Auto-generated by [create-pull-request](https://github.com/peter-evans/create-pull-request)
1 change: 1 addition & 0 deletions alphasql/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ cc_binary(
"@com_google_zetasql//zetasql/public:evaluator_table_iterator",
"@com_google_zetasql//zetasql/public:language_options",
"@com_google_zetasql//zetasql/public:simple_catalog",
"@com_google_zetasql//zetasql/public:templated_sql_tvf",
"@com_google_zetasql//zetasql/public:type",
"@com_google_zetasql//zetasql/public:type_cc_proto",
"@com_google_zetasql//zetasql/public:value",
Expand Down
28 changes: 27 additions & 1 deletion alphasql/alphacheck.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "zetasql/public/type.h"
#include "zetasql/public/value.h"
#include "zetasql/public/templated_sql_function.h"
#include "zetasql/public/templated_sql_tvf.h"
#include "zetasql/resolved_ast/resolved_ast.h"

#include "alphasql/common_lib.h"
Expand Down Expand Up @@ -216,6 +217,19 @@ absl::Status check(const std::string &sql, const ASTStatement *statement,
}
break;
}
case RESOLVED_CREATE_TABLE_FUNCTION_STMT: {
auto *create_table_function_stmt =
resolved_statement->GetAs<ResolvedCreateTableFunctionStmt>();
std::cout
<< "Create Table Function Statement analyzed, adding function to catalog..."
<< std::endl;
catalog->AddOwnedTableValuedFunction(new TemplatedSQLTVF(
create_table_function_stmt->name_path(),
create_table_function_stmt->signature(),
create_table_function_stmt->argument_name_list(),
ParseResumeLocation::FromString(create_table_function_stmt->code())));
break;
}
// TODO: DROP PROCEDURE Support?
case RESOLVED_CREATE_PROCEDURE_STMT: {
auto *create_procedure_stmt =
Expand Down Expand Up @@ -484,13 +498,25 @@ int main(int argc, char *argv[]) {
status = zetasql::UpdateErrorLocationPayloadWithFilenameIfNotPresent(
status, sql_file_path);
std::cerr << "ERROR: " << status << std::endl;
std::cout << "catalog:" << std::endl;
std::cout << "tables:" << std::endl;
// For deterministic output
auto table_names = catalog->table_names();
std::sort(table_names.begin(), table_names.end());
for (const std::string &table_name : table_names) {
std::cout << "\t" << table_name << std::endl;
}
// Too many outputs
/* auto function_names = catalog->function_names(); */
/* std::sort(function_names.begin(), function_names.end()); */
/* for (const std::string &function_name : function_names) { */
/* std::cout << "\t" << function_name << std::endl; */
/* } */
std::cout << "tvfs:" << std::endl;
auto table_function_names = catalog->table_valued_function_names();
std::sort(table_function_names.begin(), table_function_names.end());
for (const std::string &table_function_name : table_function_names) {
std::cout << "\t" << table_function_name << std::endl;
}
return 1;
}
}
Expand Down
12 changes: 6 additions & 6 deletions alphasql/identifier_resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ void IdentifierResolver::visitASTCreateTableStatement(
visitASTChildren(node, data);
}

// TODO:(Matts966) Check if this node is callee or caller and implement
// correctly void IdentifierResolver::visitASTTVF(const ASTTVF* node, void*
// data) {
// function_information.called.insert(node->name()->ToIdentifierVector());
// }

// Check INSERT and UPDATE statement to emit warnings for side effects.
void IdentifierResolver::visitASTInsertStatement(const ASTInsertStatement *node,
void *data) {
Expand Down Expand Up @@ -231,6 +225,12 @@ void IdentifierResolver::visitASTDropFunctionStatement(
visitASTChildren(node, data);
}

void IdentifierResolver::visitASTTVF(const ASTTVF* node, void* data) {
identifier_information.function_information.called.insert(
node->name()->ToIdentifierVector());
visitASTChildren(node, data);
}

void IdentifierResolver::visitASTFunctionCall(const ASTFunctionCall *node,
void *data) {
identifier_information.function_information.called.insert(
Expand Down
1 change: 1 addition & 0 deletions alphasql/identifier_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class IdentifierResolver : public DefaultParseTreeVisitor {
// Functions
void visitASTDropFunctionStatement(const ASTDropFunctionStatement *node,
void *data) override;
void visitASTTVF(const ASTTVF* node, void* data) override;
void visitASTFunctionCall(const ASTFunctionCall *node, void *data) override;
void visitASTFunctionDeclaration(const ASTFunctionDeclaration *node,
void *data) override;
Expand Down
3 changes: 2 additions & 1 deletion samples/ml/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/ml/create_input.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
3 changes: 2 additions & 1 deletion samples/ml/side_effect_first/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/ml/create_input.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/ml/create_input.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
3 changes: 2 additions & 1 deletion samples/ml/with_all/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/ml/create_input.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
3 changes: 2 additions & 1 deletion samples/ml/with_functions/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/ml/create_input.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
3 changes: 2 additions & 1 deletion samples/ml/with_tables/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/ml/create_input.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
3 changes: 2 additions & 1 deletion samples/sample-ci/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/sample-ci/sample/create_datawarehouse3.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
3 changes: 2 additions & 1 deletion samples/sample-ci/side_effect_first/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/sample-ci/sample/create_datawarehouse3.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/sample-ci/sample/create_datawarehouse3.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
3 changes: 2 additions & 1 deletion samples/sample-ci/with_all/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/sample-ci/sample/create_datawarehouse3.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
3 changes: 2 additions & 1 deletion samples/sample-ci/with_functions/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/sample-ci/sample/create_datawarehouse3.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
3 changes: 2 additions & 1 deletion samples/sample-ci/with_tables/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Analyzing "samples/sample-ci/sample/create_datawarehouse3.sql"
catalog:
tables:
dataset.main
tablename1
tablename2
tvfs:
Empty file.
7 changes: 7 additions & 0 deletions samples/tvf/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Analyzing "samples/tvf/define_tvf.sql"
Create Table Function Statement analyzed, adding function to catalog...
SUCCESS: analysis finished!
Analyzing "samples/tvf/call_tvf.sql"
DDL analyzed, adding table to catalog...
SUCCESS: analysis finished!
Successfully finished type check!
Empty file added samples/tvf/alphadag_stderr.txt
Empty file.
4 changes: 4 additions & 0 deletions samples/tvf/alphadag_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Reading paths passed as a command line arguments...
Only files that end with .sql or .bq are analyzed.
Reading "samples/tvf/call_tvf.sql"
Reading "samples/tvf/define_tvf.sql"
5 changes: 5 additions & 0 deletions samples/tvf/call_tvf.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE OR REPLACE TABLE `dataset.tvf_table` AS
SELECT
*
FROM
`dataset.tvf`(0);
5 changes: 5 additions & 0 deletions samples/tvf/dag.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
digraph G {
0 [label="samples/tvf/call_tvf.sql", shape="", type=query];
1 [label="samples/tvf/define_tvf.sql", shape="", type=query];
1->0 ;
}
Binary file added samples/tvf/dag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions samples/tvf/define_tvf.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE OR REPLACE TABLE FUNCTION `dataset.tvf`(input int64)
AS
SELECT
*
EXCEPT (
test_code
)
FROM
UNNEST(ARRAY[STRUCT("1" AS id, "apple" AS product_name, 120 AS price, date("2022-01-11") AS purchase_date,
1 AS test_code), ("2", "banana", 100, date("2022-01-11"), 1), ("3", "orange", 80, date("2022-01-12"),
1)])
WHERE
test_code = input;
Empty file added samples/tvf/external_tables.txt
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions samples/tvf/side_effect_first/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Analyzing "samples/tvf/define_tvf.sql"
Create Table Function Statement analyzed, adding function to catalog...
SUCCESS: analysis finished!
Analyzing "samples/tvf/call_tvf.sql"
DDL analyzed, adding table to catalog...
SUCCESS: analysis finished!
Successfully finished type check!
Empty file.
4 changes: 4 additions & 0 deletions samples/tvf/side_effect_first/alphadag_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Reading paths passed as a command line arguments...
Only files that end with .sql or .bq are analyzed.
Reading "samples/tvf/call_tvf.sql"
Reading "samples/tvf/define_tvf.sql"
5 changes: 5 additions & 0 deletions samples/tvf/side_effect_first/dag.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
digraph G {
0 [label="samples/tvf/call_tvf.sql", shape="", type=query];
1 [label="samples/tvf/define_tvf.sql", shape="", type=query];
1->0 ;
}
Binary file added samples/tvf/side_effect_first/dag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Analyzing "samples/tvf/define_tvf.sql"
Create Table Function Statement analyzed, adding function to catalog...
SUCCESS: analysis finished!
Analyzing "samples/tvf/call_tvf.sql"
DDL analyzed, adding table to catalog...
SUCCESS: analysis finished!
Successfully finished type check!
Empty file.
4 changes: 4 additions & 0 deletions samples/tvf/side_effect_first_with_tables/alphadag_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Reading paths passed as a command line arguments...
Only files that end with .sql or .bq are analyzed.
Reading "samples/tvf/call_tvf.sql"
Reading "samples/tvf/define_tvf.sql"
7 changes: 7 additions & 0 deletions samples/tvf/side_effect_first_with_tables/dag.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
digraph G {
0 [label="samples/tvf/call_tvf.sql", shape="", type=query];
1 [label="samples/tvf/define_tvf.sql", shape="", type=query];
2 [label="dataset.tvf_table", shape=box, type=table];
0->2 ;
1->0 ;
}
Binary file added samples/tvf/side_effect_first_with_tables/dag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions samples/tvf/with_all/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Analyzing "samples/tvf/define_tvf.sql"
Create Table Function Statement analyzed, adding function to catalog...
SUCCESS: analysis finished!
Analyzing "samples/tvf/call_tvf.sql"
DDL analyzed, adding table to catalog...
SUCCESS: analysis finished!
Successfully finished type check!
Empty file.
4 changes: 4 additions & 0 deletions samples/tvf/with_all/alphadag_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Reading paths passed as a command line arguments...
Only files that end with .sql or .bq are analyzed.
Reading "samples/tvf/call_tvf.sql"
Reading "samples/tvf/define_tvf.sql"
9 changes: 9 additions & 0 deletions samples/tvf/with_all/dag.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
digraph G {
0 [label="samples/tvf/call_tvf.sql", shape="", type=query];
1 [label="samples/tvf/define_tvf.sql", shape="", type=query];
2 [label="dataset.tvf_table", shape=box, type=table];
3 [label="dataset.tvf", shape=cds, type=function];
0->2 ;
1->3 ;
3->0 ;
}
Binary file added samples/tvf/with_all/dag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions samples/tvf/with_functions/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Analyzing "samples/tvf/define_tvf.sql"
Create Table Function Statement analyzed, adding function to catalog...
SUCCESS: analysis finished!
Analyzing "samples/tvf/call_tvf.sql"
DDL analyzed, adding table to catalog...
SUCCESS: analysis finished!
Successfully finished type check!
Empty file.
4 changes: 4 additions & 0 deletions samples/tvf/with_functions/alphadag_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Reading paths passed as a command line arguments...
Only files that end with .sql or .bq are analyzed.
Reading "samples/tvf/call_tvf.sql"
Reading "samples/tvf/define_tvf.sql"
7 changes: 7 additions & 0 deletions samples/tvf/with_functions/dag.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
digraph G {
0 [label="samples/tvf/call_tvf.sql", shape="", type=query];
1 [label="samples/tvf/define_tvf.sql", shape="", type=query];
2 [label="dataset.tvf", shape=cds, type=function];
1->2 ;
2->0 ;
}
Binary file added samples/tvf/with_functions/dag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions samples/tvf/with_tables/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Analyzing "samples/tvf/define_tvf.sql"
Create Table Function Statement analyzed, adding function to catalog...
SUCCESS: analysis finished!
Analyzing "samples/tvf/call_tvf.sql"
DDL analyzed, adding table to catalog...
SUCCESS: analysis finished!
Successfully finished type check!
Empty file.
4 changes: 4 additions & 0 deletions samples/tvf/with_tables/alphadag_stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Reading paths passed as a command line arguments...
Only files that end with .sql or .bq are analyzed.
Reading "samples/tvf/call_tvf.sql"
Reading "samples/tvf/define_tvf.sql"
7 changes: 7 additions & 0 deletions samples/tvf/with_tables/dag.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
digraph G {
0 [label="samples/tvf/call_tvf.sql", shape="", type=query];
1 [label="samples/tvf/define_tvf.sql", shape="", type=query];
2 [label="dataset.tvf_table", shape=box, type=table];
0->2 ;
1->0 ;
}
Binary file added samples/tvf/with_tables/dag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.

0 comments on commit 9afef98

Please sign in to comment.