Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEMO ONLY DO NOT CHECK IN] How to get ZetaSQL AST Node from string #812

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ build --host_cxxopt='-Wno-deprecated-declarations'
build --cxxopt='-Wno-thread-safety-precise'
build --host_cxxopt='-Wno-thread-safety-precise'

# These warnings must be disabled to run ZetaSQL on either linux or Mac off of google3.
build --cxxopt='-Wno-address'
build --host_cxxopt='-Wno-address'
build --cxxopt='-Wno-deprecated-copy'
build --host_cxxopt='-Wno-deprecated-copy'
build --cxxopt='-Wno-array-compare'
build --host_cxxopt='-Wno-array-compare'
build --cxxopt='-Wno-cpp'
build --host_cxxopt='-Wno-cpp'
build --cxxopt='-Wno-format-overflow'
build --host_cxxopt='-Wno-format-overflow'
build --cxxopt='-Wno-return-type'
build --host_cxxopt='-Wno-return-type'
build --cxxopt='-Wno-class-memaccess'
build --host_cxxopt='-Wno-class-memaccess'

# Need this for ZetaSQL's icu dependency as long as it remains at version 65.
# Should remove when it gets upgraded.
build --cxxopt='-Wno-implicit-int-float-conversion'
Expand Down
20 changes: 20 additions & 0 deletions src/parser/sql/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,23 @@ cc_test(
"@com_google_zetasql//zetasql/public:parse_helpers",
],
)

cc_library(
name = "zeta_to_raksha",
srcs = ["zeta_to_raksha.h"],
deps = [
"@com_google_absl//absl/strings",
"@com_google_zetasql//zetasql/public:parse_helpers",
"@com_google_zetasql//zetasql/public:type",
"@com_google_zetasql//zetasql/public:analyzer"
],
)

cc_test(
name = "zeta_to_raksha_test",
srcs = ["zeta_to_raksha_test.cc"],
deps = [
":zeta_to_raksha",
"//src/common/testing:gtest",
]
)
51 changes: 51 additions & 0 deletions src/parser/sql/zeta_to_raksha.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//-------------------------------------------------------------------------------
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-------------------------------------------------------------------------------

#include <iostream>
#include <sstream>

#include "absl/strings/string_view.h"
#include "zetasql/public/analyzer.h"
#include "zetasql/public/parse_helpers.h"
#include "zetasql/public/type.h"

namespace raksha::parser::sql {

std::string PrintParseResult(absl::string_view sql_text) {
std::stringstream ss;
std::unique_ptr<const zetasql::AnalyzerOutput> output;
zetasql::AnalyzerOptions default_options; // language options for changing
// behavior of analysis/output
// Catalog has to do with global namespaces. It seems like this
// should be populated by parsing the input somehow?
zetasql::Catalog* catalog = nullptr;
// TypeFactory creates and owns type objects. I am not sure why
// this is not encapsulated and is a part of the public
// APIs... hmmm...
zetasql::TypeFactory type_factory;
absl::Status result = AnalyzeStatement(sql_text, default_options, catalog,
&type_factory, &output);

if (result != absl::OkStatus()) {
ss << "AnalyzeStatement result failed: " << result << std::endl;
} else {
ss << output->resolved_statement()->DebugString();
}
return ss.str();
}

} // namespace raksha::parser::sql
40 changes: 40 additions & 0 deletions src/parser/sql/zeta_to_raksha_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//-------------------------------------------------------------------------------
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-------------------------------------------------------------------------------

#include "src/parser/sql/zeta_to_raksha.h"

#include "absl/strings/string_view.h"
#include "src/common/testing/gtest.h"

namespace raksha::parser::sql {
namespace {
TEST(AnalysisResultTest, SimpleTest) {
EXPECT_EQ(PrintParseResult("SELECT 1;"),
R"""(QueryStmt
+-output_column_list=
| +-$query.$col1#1 AS `$col1` [INT64]
+-query=
+-ProjectScan
+-column_list=[$query.$col1#1]
+-expr_list=
| +-$col1#1 := Literal(type=INT64, value=1)
+-input_scan=
+-SingleRowScan
)""");
}
} // namespace
} // namespace raksha::parser::sql