Line data Source code
1 : #ifndef BIGQUERY_EMULATOR_BACKEND_ENGINE_SEMANTIC_EVAL_EXPR_TEST_FIXTURE_H_
2 : #define BIGQUERY_EMULATOR_BACKEND_ENGINE_SEMANTIC_EVAL_EXPR_TEST_FIXTURE_H_
3 :
4 : #include <memory>
5 : #include <optional>
6 : #include <string>
7 :
8 : #include "absl/status/status.h"
9 : #include "absl/strings/str_cat.h"
10 : #include "backend/engine/semantic/eval_expr.h"
11 : #include "googlesql/public/analyzer.h"
12 : #include "googlesql/public/analyzer_options.h"
13 : #include "googlesql/public/analyzer_output.h"
14 : #include "googlesql/public/builtin_function_options.h"
15 : #include "googlesql/public/catalog.h"
16 : #include "googlesql/public/language_options.h"
17 : #include "googlesql/public/options.pb.h"
18 : #include "googlesql/public/simple_catalog.h"
19 : #include "googlesql/public/types/type_factory.h"
20 : #include "googlesql/resolved_ast/resolved_ast.h"
21 : #include "gtest/gtest.h"
22 :
23 : namespace bigquery_emulator {
24 : namespace backend {
25 : namespace engine {
26 : namespace semantic {
27 :
28 40 : inline ::googlesql::AnalyzerOptions MakeAnalyzerOptions() {
29 40 : ::googlesql::LanguageOptions language;
30 40 : language.EnableMaximumLanguageFeatures();
31 40 : language.set_product_mode(::googlesql::PRODUCT_EXTERNAL);
32 40 : ::googlesql::AnalyzerOptions options(language);
33 40 : options.CreateDefaultArenasIfNotSet();
34 40 : return options;
35 40 : }
36 :
37 : class EvalExprTest : public ::testing::Test {
38 : protected:
39 47 : void SetUp() override {
40 47 : type_factory_ = std::make_unique<::googlesql::TypeFactory>();
41 47 : catalog_ = std::make_unique<::googlesql::SimpleCatalog>(
42 47 : "eval_catalog", type_factory_.get());
43 47 : catalog_->AddBuiltinFunctions(
44 47 : ::googlesql::BuiltinFunctionOptions::AllReleasedFunctions());
45 47 : }
46 :
47 : const ::googlesql::ResolvedExpr* AnalyzeExpr(
48 : absl::string_view expr,
49 39 : std::optional<::googlesql::AnalyzerOptions> options_in = std::nullopt) {
50 39 : ::googlesql::AnalyzerOptions options =
51 39 : options_in.has_value() ? *std::move(options_in) : MakeAnalyzerOptions();
52 39 : last_output_.reset();
53 39 : const std::string sql = absl::StrCat("SELECT ", expr);
54 39 : absl::Status s = ::googlesql::AnalyzeStatement(
55 39 : sql, options, catalog_.get(), type_factory_.get(), &last_output_);
56 78 : EXPECT_TRUE(s.ok()) << s;
57 39 : if (!s.ok() || last_output_ == nullptr) return nullptr;
58 39 : const auto* stmt = last_output_->resolved_statement()
59 39 : ->GetAs<::googlesql::ResolvedQueryStmt>();
60 39 : if (stmt == nullptr) return nullptr;
61 39 : const auto* project =
62 39 : stmt->query()->GetAs<::googlesql::ResolvedProjectScan>();
63 39 : if (project == nullptr || project->expr_list_size() == 0) return nullptr;
64 39 : return project->expr_list(0)->expr();
65 39 : }
66 :
67 : std::unique_ptr<::googlesql::TypeFactory> type_factory_{};
68 : std::unique_ptr<::googlesql::SimpleCatalog> catalog_{};
69 : std::unique_ptr<const ::googlesql::AnalyzerOutput> last_output_{};
70 : };
71 :
72 : } // namespace semantic
73 : } // namespace engine
74 : } // namespace backend
75 : } // namespace bigquery_emulator
76 :
77 : #endif // BIGQUERY_EMULATOR_BACKEND_ENGINE_SEMANTIC_EVAL_EXPR_TEST_FIXTURE_H_
|