Skip to content

Commit

Permalink
Support empty Array of Struct / Do not support null-valued Array of S…
Browse files Browse the repository at this point in the history
…truct

according to the behaviour in realworld Spanner

fix: GoogleCloudPlatform#165
  • Loading branch information
MITSUBOSHI committed Dec 5, 2024
1 parent 0084b2c commit 205bd8a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions backend/query/query_validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ absl::Status QueryValidator::VisitResolvedLiteral(
const zetasql::ResolvedLiteral* node) {
if (node->type()->IsArray() &&
node->type()->AsArray()->element_type()->IsStruct() &&
node->value().is_empty_array()) {
return error::UnsupportedArrayConstructorSyntaxForEmptyStructArray();
node->value().is_null()) {
return error::UnsupportedArrayConstructorSyntaxForNullValuedStructArray();
}

return DefaultVisit(node);
Expand Down
4 changes: 2 additions & 2 deletions common/errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2884,11 +2884,11 @@ absl::Status UnsupportedReturnStructAsColumn() {
"value. Rewrite the query to flatten the struct fields in the result.");
}

absl::Status UnsupportedArrayConstructorSyntaxForEmptyStructArray() {
absl::Status UnsupportedArrayConstructorSyntaxForNullValuedStructArray() {
return absl::Status(
absl::StatusCode::kUnimplemented,
"Unsupported query shape: Spanner does not support array constructor "
"syntax for an empty array where array elements are Structs.");
"syntax for a null-valued array of struct.");
}

absl::Status UnsupportedFeatureSafe(absl::string_view feature_type,
Expand Down
2 changes: 1 addition & 1 deletion common/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ absl::Status ReadOnlyTransactionDoesNotSupportReadWriteOnlyFunctions(
absl::Status CannotInsertDuplicateKeyInsertOrUpdateDml(absl::string_view key);
// Unsupported query shape errors.
absl::Status UnsupportedReturnStructAsColumn();
absl::Status UnsupportedArrayConstructorSyntaxForEmptyStructArray();
absl::Status UnsupportedArrayConstructorSyntaxForNullValuedStructArray();
absl::Status UnsupportedFeatureSafe(absl::string_view feature_type,
absl::string_view info_message);
absl::Status UnsupportedFunction(absl::string_view function_name);
Expand Down
26 changes: 19 additions & 7 deletions tests/conformance/cases/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -555,16 +555,13 @@ TEST_P(QueryTest, CanReturnArrayOfStructTypedColumns) {
IsOkAndHoldsRow({Value(std::vector<SimpleStruct>{SimpleStruct{1}})}));
}

TEST_P(QueryTest, CannotQueryArrayOfEmptyStruct) {
TEST_P(QueryTest, CanQueryArrayOfEmptyStruct) {
// Spanner PG dialect doesn't support STRUCT.
if (GetParam() != database_api::DatabaseDialect::POSTGRESQL) {
using SimpleStruct = std::tuple<int64_t>;
EXPECT_THAT(
Query("SELECT ARRAY<STRUCT<int64_val INT64>>[]"),
StatusIs(
absl::StatusCode::kUnimplemented,
"Unsupported query shape: Spanner does not support array "
"constructor "
"syntax for an empty array where array elements are Structs."));
Query("SELECT ARRAY<STRUCT<INT64>>[]"),
IsOkAndHoldsRow({Value(std::vector<SimpleStruct>{SimpleStruct{}})}));
}

std::string query = "SELECT ARRAY<INT64>[]";
Expand All @@ -574,6 +571,21 @@ TEST_P(QueryTest, CannotQueryArrayOfEmptyStruct) {
EXPECT_THAT(Query(query), IsOkAndHoldsRow({Value(std::vector<int64_t>{})}));
}

TEST_P(QueryTest, CannotQueryNullValuedArrayOfStruct) {
// Spanner PG dialect doesn't support STRUCT and array subquery expressions.
if (GetParam() == database_api::DatabaseDialect::POSTGRESQL) {
GTEST_SKIP();
}

EXPECT_THAT(
Query("SELECT IF(false, ARRAY<STRUCT<INT64>>[], null)"),
StatusIs(
absl::StatusCode::kUnimplemented,
"Unsupported query shape: Spanner does not support array "
"constructor "
"syntax for a null-valued array of struct."));
}

TEST_P(QueryTest, QueryColumnCannotBeStruct) {
// Spanner PG dialect doesn't support STRUCT.
if (GetParam() == database_api::DatabaseDialect::POSTGRESQL) {
Expand Down

0 comments on commit 205bd8a

Please sign in to comment.