Skip to content

Commit

Permalink
Merge pull request #185 from GoogleCloudPlatform/5789B3FF9E750C15D6B7…
Browse files Browse the repository at this point in the history
…4524C048BD98

Project import generated by Copybara.
  • Loading branch information
olavloite authored Sep 20, 2024
2 parents c25ad94 + 7f91aff commit c756a43
Show file tree
Hide file tree
Showing 84 changed files with 1,579 additions and 372 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,11 @@ load("@com_google_zetasql//bazel:zetasql_deps_step_4.bzl", "zetasql_deps_step_4"

zetasql_deps_step_4()

google_cloud_cpp_version = "2.12.0"
google_cloud_cpp_version = "2.19.0"

http_archive(
name = "com_github_googleapis_google_cloud_cpp",
sha256 = "8cda870803925c62de8716a765e03eb9d34249977e5cdb7d0d20367e997a55e2",
sha256 = "63f009092afd900cb812050bcecf607e37d762ac911e0bcbf4af9a432da91890",
strip_prefix = "google-cloud-cpp-{0}".format(google_cloud_cpp_version),
url = "https://github.com/googleapis/google-cloud-cpp/archive/v{0}.tar.gz".format(google_cloud_cpp_version),
)
Expand Down
3 changes: 1 addition & 2 deletions backend/actions/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ class ActionRegistry {
table_effectors_;

// List of effectors for primary key columns per table.
absl::node_hash_map<const std::string,
std::unique_ptr<GeneratedColumnEffector>>
absl::node_hash_map<std::string, std::unique_ptr<GeneratedColumnEffector>>
table_generated_key_effectors_;

// List of modifiers per table.
Expand Down
2 changes: 1 addition & 1 deletion backend/common/rows.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace emulator {
namespace backend {

// Common row abstraction used by various parts of the backend.
using Row = absl::flat_hash_map<const Column* const, zetasql::Value>;
using Row = absl::flat_hash_map<const Column*, zetasql::Value>;

// Returns the value from the given Row for the specified column. Returns Null
// if the specified column was not found.
Expand Down
14 changes: 14 additions & 0 deletions backend/query/catalog_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ TEST_F(AnalyzeStatementTest, SelectNestedCatalogPGFunctions) {
"pg.map_double_to_int(2.0)) as col"));
}

TEST_F(AnalyzeStatementTest, SelectIdentityColumnFieldsInformationSchema) {
ZETASQL_EXPECT_OK(AnalyzeStatement(
"SELECT is_identity, identity_generation, identity_kind, "
"identity_start_with_counter, identity_skip_range_min, "
"identity_skip_range_max FROM information_schema.columns"));
}

TEST_F(AnalyzeStatementTest, SelectIdentityColumnFieldsPGInformationSchema) {
ZETASQL_EXPECT_OK(AnalyzeStatement(
"SELECT is_identity, identity_generation, identity_kind, "
"identity_start_with_counter, identity_skip_range_min, "
"identity_skip_range_max FROM pg_information_schema.columns"));
}

TEST_F(AnalyzeStatementTest, SelectFromPGInformationSchema) {
ZETASQL_EXPECT_OK(AnalyzeStatement(
"SELECT column_name FROM pg_information_schema.columns"));
Expand Down
6 changes: 6 additions & 0 deletions backend/query/function_catalog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "backend/query/function_catalog.h"

#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

Expand All @@ -35,12 +37,16 @@
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "absl/types/span.h"
#include "backend/query/analyzer_options.h"
#include "backend/query/ml/ml_predict_row_function.h"
#include "backend/query/ml/ml_predict_table_valued_function.h"
#include "backend/schema/catalog/column.h"
#include "backend/schema/catalog/table.h"
#include "third_party/spanner_pg/datatypes/extended/pg_jsonb_type.h"
#include "backend/schema/catalog/schema.h"
#include "backend/schema/catalog/sequence.h"
Expand Down
6 changes: 6 additions & 0 deletions backend/query/info_schema_columns_metadata.csv
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ COLUMNS,IS_GENERATED,NO,STRING(MAX)
COLUMNS,GENERATION_EXPRESSION,YES,STRING(MAX)
COLUMNS,IS_STORED,YES,STRING(MAX)
COLUMNS,SPANNER_STATE,YES,STRING(MAX)
COLUMNS,IS_IDENTITY,YES,STRING(MAX)
COLUMNS,IDENTITY_GENERATION,YES,STRING(MAX)
COLUMNS,IDENTITY_KIND,YES,STRING(MAX)
COLUMNS,IDENTITY_START_WITH_COUNTER,YES,STRING(MAX)
COLUMNS,IDENTITY_SKIP_RANGE_MIN,YES,STRING(MAX)
COLUMNS,IDENTITY_SKIP_RANGE_MAX,YES,STRING(MAX)
COLUMN_COLUMN_USAGE,TABLE_CATALOG,NO,STRING(MAX)
COLUMN_COLUMN_USAGE,TABLE_SCHEMA,NO,STRING(MAX)
COLUMN_COLUMN_USAGE,TABLE_NAME,NO,STRING(MAX)
Expand Down
38 changes: 36 additions & 2 deletions backend/query/information_schema_catalog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "absl/log/check.h"
#include "absl/status/statusor.h"
#include "absl/strings/ascii.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
Expand Down Expand Up @@ -87,6 +88,15 @@ static constexpr char kSpannerType[] = "SPANNER_TYPE";
static constexpr char kIsGenerated[] = "IS_GENERATED";
static constexpr char kIsStored[] = "IS_STORED";
static constexpr char kGenerationExpression[] = "GENERATION_EXPRESSION";
static constexpr char kIsIdentity[] = "IS_IDENTITY";
static constexpr char kIdentityGeneration[] = "IDENTITY_GENERATION";
static constexpr char kIdentityKind[] = "IDENTITY_KIND";
static constexpr char kBitReversedPositiveSequence[] =
"BIT_REVERSED_POSITIVE_SEQUENCE";
static constexpr char kIdentityStartWithCounter[] =
"IDENTITY_START_WITH_COUNTER";
static constexpr char kIdentitySkipRangeMin[] = "IDENTITY_SKIP_RANGE_MIN";
static constexpr char kIdentitySkipRangeMax[] = "IDENTITY_SKIP_RANGE_MAX";
static constexpr char kSpannerState[] = "SPANNER_STATE";
static constexpr char kColumns[] = "COLUMNS";
static constexpr char kSchemaName[] = "SCHEMA_NAME";
Expand Down Expand Up @@ -118,6 +128,7 @@ static constexpr char kYes[] = "YES";
static constexpr char kNo[] = "NO";
static constexpr char kNone[] = "NONE";
static constexpr char kAlways[] = "ALWAYS";
static constexpr char kByDefault[] = "BY DEFAULT";
static constexpr char kNever[] = "NEVER";
static constexpr char kPrimary_Key[] = "PRIMARY_KEY";
static constexpr char kPrimaryKey[] = "PRIMARY KEY";
Expand Down Expand Up @@ -791,8 +802,9 @@ void InformationSchemaCatalog::FillColumnsTable() {
}

specific_kvs[kColumnDefault] =
column->has_default_value() ? String(column->expression().value())
: NullString();
column->has_default_value()
? String(column->expression().value())
: NullString();

specific_kvs[kDataType] = NullString();
specific_kvs[kSpannerType] = GetSpannerType(column);
Expand All @@ -812,6 +824,12 @@ void InformationSchemaCatalog::FillColumnsTable() {
specific_kvs[kIsStored] = NullString();
}
specific_kvs[kSpannerState] = String(kCommitted);
specific_kvs[kIsIdentity] = String(kNo);
specific_kvs[kIdentityGeneration] = NullString();
specific_kvs[kIdentityKind] = NullString();
specific_kvs[kIdentityStartWithCounter] = NullString();
specific_kvs[kIdentitySkipRangeMin] = NullString();
specific_kvs[kIdentitySkipRangeMax] = NullString();

rows.push_back(GetRowFromRowKVs(columns, specific_kvs));
specific_kvs.clear();
Expand Down Expand Up @@ -854,6 +872,12 @@ void InformationSchemaCatalog::FillColumnsTable() {
specific_kvs[kGenerationExpression] = NullString();
specific_kvs[kIsStored] = NullString();
specific_kvs[kSpannerState] = String(kCommitted);
specific_kvs[kIsIdentity] = String(kNo);
specific_kvs[kIdentityGeneration] = NullString();
specific_kvs[kIdentityKind] = NullString();
specific_kvs[kIdentityStartWithCounter] = NullString();
specific_kvs[kIdentitySkipRangeMin] = NullString();
specific_kvs[kIdentitySkipRangeMax] = NullString();

rows.push_back(GetRowFromRowKVs(columns, specific_kvs));
specific_kvs.clear();
Expand Down Expand Up @@ -895,6 +919,12 @@ void InformationSchemaCatalog::FillColumnsTable() {
specific_kvs[kGenerationExpression] = NullString();
specific_kvs[kIsStored] = NullString();
specific_kvs[kSpannerState] = NullString();
specific_kvs[kIsIdentity] = String(kNo);
specific_kvs[kIdentityGeneration] = NullString();
specific_kvs[kIdentityKind] = NullString();
specific_kvs[kIdentityStartWithCounter] = NullString();
specific_kvs[kIdentitySkipRangeMin] = NullString();
specific_kvs[kIdentitySkipRangeMax] = NullString();

rows.push_back(GetRowFromRowKVs(columns, specific_kvs));
specific_kvs.clear();
Expand Down Expand Up @@ -2322,6 +2352,10 @@ void InformationSchemaCatalog::FillSequencesTable() {
auto sequences = tables_by_name_.at(GetNameForDialect(kSequences)).get();
std::vector<std::vector<zetasql::Value>> rows;
for (const Sequence* sequence : default_schema_->sequences()) {
if (absl::StartsWith(sequence->Name(), "_")) {
// Skip internal sequences.
continue;
}
if (dialect_ == DatabaseDialect::POSTGRESQL) {
rows.push_back({// sequence_catalog
String(""),
Expand Down
4 changes: 2 additions & 2 deletions backend/query/information_schema_catalog_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TEST(InformationSchemaCatalogTest, ColumnsMetadataCount) {
SpannerSysCatalog spanner_sys_catalog;
InformationSchemaCatalog catalog(InformationSchemaCatalog::kName, &schema,
&spanner_sys_catalog);
EXPECT_EQ(ColumnsMetadata().size(), 248);
EXPECT_EQ(ColumnsMetadata().size(), 254);
}

TEST(InformationSchemaCatalogTest, IndexColumnsMetadataCount) {
Expand All @@ -56,7 +56,7 @@ TEST(InformationSchemaCatalogTest, PGColumnsMetadataCount) {
SpannerSysCatalog spanner_sys_catalog;
InformationSchemaCatalog catalog(InformationSchemaCatalog::kPGName, &schema,
&spanner_sys_catalog);
EXPECT_EQ(PGColumnsMetadata().size(), 404);
EXPECT_EQ(PGColumnsMetadata().size(), 408);
}

TEST(InformationSchemaCatalogTest, PGIndexColumnsMetadataCount) {
Expand Down
4 changes: 4 additions & 0 deletions backend/query/pg_info_schema_columns_metadata.csv
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ columns,identity_increment,YES,character varying
columns,identity_maximum,YES,character varying
columns,identity_minimum,YES,character varying
columns,identity_cycle,YES,character varying
columns,identity_kind,YES,character varying
columns,identity_start_with_counter,YES,character varying
columns,identity_skip_range_min,YES,character varying
columns,identity_skip_range_max,YES,character varying
columns,is_generated,YES,character varying
columns,generation_expression,YES,character varying
columns,is_updatable,YES,character varying
Expand Down
4 changes: 2 additions & 2 deletions backend/query/query_validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ absl::Status QueryValidator::ValidateHints(
absl::Status QueryValidator::CheckSpannerHintName(
absl::string_view name, const zetasql::ResolvedNodeKind node_kind) const {
static const auto* supported_hints = new const absl::flat_hash_map<
const zetasql::ResolvedNodeKind,
zetasql::ResolvedNodeKind,
absl::flat_hash_set<absl::string_view, zetasql_base::StringViewCaseHash,
zetasql_base::StringViewCaseEqual>>{
{zetasql::RESOLVED_TABLE_SCAN,
Expand Down Expand Up @@ -220,7 +220,7 @@ absl::Status QueryValidator::CheckSpannerHintName(
absl::Status QueryValidator::CheckEmulatorHintName(
absl::string_view name, const zetasql::ResolvedNodeKind node_kind) const {
static const auto* supported_hints = new const absl::flat_hash_map<
const zetasql::ResolvedNodeKind,
zetasql::ResolvedNodeKind,
absl::flat_hash_set<absl::string_view, zetasql_base::StringViewCaseHash,
zetasql_base::StringViewCaseEqual>>{
{zetasql::RESOLVED_TABLE_SCAN,
Expand Down
3 changes: 0 additions & 3 deletions backend/query/queryable_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ QueryableTable::AnalyzeColumnExpression(
<< " for generated column : " << column->FullName();
}
}
absl::Status s = zetasql::AnalyzeExpressionForAssignmentToType(
column->expression().value(), options, catalog, type_factory,
column->GetType(), &output);
std::string expression_type = "default";
if (is_generated_column) {
expression_type = "generated";
Expand Down
12 changes: 12 additions & 0 deletions backend/schema/builders/database_options_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
#define THIRD_PARTY_CLOUD_SPANNER_EMULATOR_BACKEND_SCHEMA_BUILDERS_DATABASE_OPTIONS_BUILDER_H_

#include <memory>
#include <optional>
#include <string>
#include <utility>

#include "absl/memory/memory.h"
#include "backend/schema/catalog/database_options.h"
#include "backend/schema/validators/database_options_validator.h"
#include "google/protobuf/repeated_ptr_field.h"

namespace google {
namespace spanner {
Expand All @@ -47,6 +49,10 @@ class DatabaseOptions::Builder {
return *this;
}
const DatabaseOptions* get() const { return instance_.get(); }
Builder& set_options(::google::protobuf::RepeatedPtrField<ddl::SetOption> options) {
instance_->options_ = options;
return *this;
}

private:
std::unique_ptr<DatabaseOptions> instance_;
Expand All @@ -58,6 +64,12 @@ class DatabaseOptions::Editor {

const DatabaseOptions* get() const { return instance_; }

Editor& set_options(
const ::google::protobuf::RepeatedPtrField<ddl::SetOption> options) {
instance_->options_ = options;
return *this;
}

private:
// Not owned.
DatabaseOptions* instance_;
Expand Down
1 change: 1 addition & 0 deletions backend/schema/catalog/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ cc_test(
"//tests/common:scoped_feature_flags_setter",
"//tests/common:test_schema_constructor",
"@com_github_grpc_grpc//:grpc++",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/time",
"@com_google_googleapis//google/spanner/admin/database/v1:database_cc_proto",
"@com_google_googletest//:gtest_main",
Expand Down
8 changes: 5 additions & 3 deletions backend/schema/catalog/database_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "backend/schema/ddl/operations.pb.h"
#include "backend/schema/graph/schema_node.h"
#include "backend/schema/updater/schema_validation_context.h"
#include "google/protobuf/repeated_ptr_field.h"

namespace google {
namespace spanner {
Expand All @@ -38,7 +39,9 @@ class DatabaseOptions : public SchemaNode {
// Returns the name of this database.
std::string Name() const { return database_name_; }

const ddl::SetOption* options() const { return options_; }
::google::protobuf::RepeatedPtrField<ddl::SetOption> options() const {
return options_;
}

// SchemaNode interface implementation.
// ------------------------------------
Expand Down Expand Up @@ -81,8 +84,7 @@ class DatabaseOptions : public SchemaNode {

// Name of this database.
std::string database_name_;
// TODO: Add functionality to update schema.
const ddl::SetOption* options_ = nullptr;
::google::protobuf::RepeatedPtrField<ddl::SetOption> options_;
};
} // namespace backend
} // namespace emulator
Expand Down
6 changes: 3 additions & 3 deletions backend/schema/catalog/schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,11 @@ void DumpModel(const Model* model, ddl::CreateModel& create_model) {
void DumpDatabaseOptions(const DatabaseOptions* database_option,
ddl::AlterDatabase& alter_database) {
alter_database.set_db_name(database_option->Name());
if (database_option->options()->has_option_name()) {
for (const auto& option : database_option->options()) {
ddl::SetOption* set_option =
alter_database.mutable_set_options()->add_options();
set_option->set_option_name(database_option->options()->option_name());
set_option->set_string_value(database_option->options()->string_value());
set_option->set_option_name(option.option_name());
set_option->set_string_value(option.string_value());
}
}

Expand Down
7 changes: 4 additions & 3 deletions backend/schema/catalog/schema_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "gtest/gtest.h"
#include "zetasql/base/testing/status_matchers.h"
#include "tests/common/proto_matchers.h"
#include "absl/status/statusor.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "backend/schema/builders/change_stream_builder.h"
Expand Down Expand Up @@ -1534,11 +1535,12 @@ TEST_F(SchemaTest, PrintDDLStatementsTestSequences) {
R"(CREATE SEQUENCE myseq OPTIONS (
sequence_kind = 'bit_reversed_positive',
skip_range_min = 1,
skip_range_max = 1000 ))",
skip_range_max = 1000))",
R"(CREATE TABLE test_table (
int64_col INT64 NOT NULL DEFAULT (GET_NEXT_SEQUENCE_VALUE(SEQUENCE myseq)),
string_col STRING(MAX),
) PRIMARY KEY(int64_col))")));
) PRIMARY KEY(int64_col))"
)));
}

TEST_F(SchemaTest, PostgreSQLPrintDDLStatementsTestSequences) {
Expand Down Expand Up @@ -1618,7 +1620,6 @@ ALTER TABLE vanishing_data ALTER TTL INTERVAL '3 WEEKS 2 DAYS' ON shadow_date
PRIMARY KEY(id)
) TTL INTERVAL '3 WEEKS 2 DAYS' ON shadow_date)")));
}

} // namespace
} // namespace backend
} // namespace emulator
Expand Down
Loading

0 comments on commit c756a43

Please sign in to comment.