absl::StatusOr DDLColumnTypeToGoogleSqlType()

in backend/schema/updater/ddl_type_conversion.cc [150:242]


absl::StatusOr<const zetasql::Type*> DDLColumnTypeToGoogleSqlType(
    const ddl::ColumnDefinition& ddl_column_def,
    zetasql::TypeFactory* type_factory, const ProtoBundle* proto_bundle) {
  ZETASQL_RET_CHECK(ddl_column_def.has_type())
      << "No type field specification in "
      << "ddl::ColumnDefinition input: " << ddl_column_def.ShortDebugString();

  switch (ddl_column_def.type()) {
    case ddl::ColumnDefinition::FLOAT:
      return type_factory->get_float();
    case ddl::ColumnDefinition::DOUBLE:
      return type_factory->get_double();
    case ddl::ColumnDefinition::INT64:
      return type_factory->get_int64();
    case ddl::ColumnDefinition::BOOL:
      return type_factory->get_bool();
    case ddl::ColumnDefinition::STRING:
      return type_factory->get_string();
    case ddl::ColumnDefinition::BYTES:
      return type_factory->get_bytes();
    case ddl::ColumnDefinition::TIMESTAMP:
      return type_factory->get_timestamp();
    case ddl::ColumnDefinition::DATE:
      return type_factory->get_date();
    case ddl::ColumnDefinition::NUMERIC:
      return type_factory->get_numeric();
    case ddl::ColumnDefinition::PG_NUMERIC:
      return postgres_translator::spangres::types::PgNumericMapping()
          ->mapped_type();
    case ddl::ColumnDefinition::JSON:
      return type_factory->get_json();
    case ddl::ColumnDefinition::PG_JSONB:
      return postgres_translator::spangres::types::PgJsonbMapping()
          ->mapped_type();
    case ddl::ColumnDefinition::INTERVAL:
      return type_factory->get_interval();
    case ddl::ColumnDefinition::ARRAY: {
      ZETASQL_RET_CHECK(ddl_column_def.has_array_subtype())
          << "Missing array_subtype field for ddl::ColumnDefinition input: "
          << ddl_column_def.ShortDebugString();
      if (ddl_column_def.array_subtype().type() ==
          ddl::ColumnDefinition::ARRAY) {
        // TODO : Update when we have a proper way to
        // construct user-facing error messages in the error catalog.
        return absl::Status(
            absl::StatusCode::kInvalidArgument,
            "Array of arrays type is not supported by the schema.");
      }
      ZETASQL_ASSIGN_OR_RETURN(
          auto array_element_type,
          DDLColumnTypeToGoogleSqlType(ddl_column_def.array_subtype(),
                                       type_factory, proto_bundle));
      ZETASQL_RET_CHECK_NE(array_element_type, nullptr);
      const zetasql::Type* array_type;
      ZETASQL_RETURN_IF_ERROR(
          type_factory->MakeArrayType(array_element_type, &array_type));
      return array_type;
    }
    case ddl::ColumnDefinition::STRUCT: {
      return DDLColumnTypeToGoogleSqlType(ddl_column_def.type_definition(),
                                          type_factory, proto_bundle);
    }
    case ddl::ColumnDefinition::TOKENLIST: {
      return type_factory->get_tokenlist();
    }
    default:
      if (ddl_column_def.type() == ddl::ColumnDefinition::NONE &&
          ddl_column_def.has_proto_type_name()) {
        ZETASQL_RET_CHECK_NE(proto_bundle, nullptr);
        auto proto_descriptor =
            proto_bundle->GetTypeDescriptor(ddl_column_def.proto_type_name());
        if (proto_descriptor.ok()) {
          // PROTO
          const zetasql::Type* proto_type;
          ZETASQL_RETURN_IF_ERROR(type_factory->MakeProtoType(proto_descriptor.value(),
                                                      &proto_type));
          return proto_type;
        }
        auto enum_descriptor = proto_bundle->GetEnumTypeDescriptor(
            ddl_column_def.proto_type_name());
        if (enum_descriptor.ok()) {
          // ENUM
          const zetasql::Type* enum_type;
          ZETASQL_RETURN_IF_ERROR(
              type_factory->MakeEnumType(enum_descriptor.value(), &enum_type));
          return enum_type;
        }
      }
      ZETASQL_RET_CHECK(false).SetErrorCode(absl::StatusCode::kInternal)
          << "Unrecognized ddl::ColumnDefinition: "
          << (ddl_column_def).ShortDebugString();
  }
}