# Cloud Spanner API - Class Google::Cloud::Spanner::BatchSnapshot (v2.36.0)

2.36.0 (latest) 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.0 2.24.0 2.23.0 2.22.0 2.21.0 2.20.0 2.19.1 2.18.1 2.17.0 2.16.1 2.15.0 2.14.0 2.13.0 2.12.1 2.11.0 Reference documentation and code samples for the Cloud Spanner API class Google::Cloud::Spanner::BatchSnapshot.

## BatchSnapshot

Represents a read-only transaction that can be configured to read at
timestamps in the past and allows for exporting arbitrarily large
amounts of data from Cloud Spanner databases. This is a snapshot which
additionally allows to partition a read or query request. The read/query
request can then be executed independently over each partition while
observing the same snapshot of the database. A BatchSnapshot can also be
shared across multiple processes/machines by passing around its
serialized value and then recreating the transaction using [#dump](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_dump_instance_ "Google::Cloud::Spanner::BatchSnapshot#dump (method)").

Unlike locking read-write transactions, BatchSnapshot will never abort.
They can fail if the chosen read timestamp is garbage collected; however
any read or query activity within an hour on the transaction avoids
garbage collection and most applications do not need to worry about this
in practice.

See [Google::Cloud::Spanner::BatchClient#batch_snapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchClient#Google__Cloud__Spanner__BatchClient_batch_snapshot_instance_ "Google::Cloud::Spanner::BatchClient#batch_snapshot (method)") and [Google::Cloud::Spanner::BatchClient#load_batch_snapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchClient#Google__Cloud__Spanner__BatchClient_load_batch_snapshot_instance_ "Google::Cloud::Spanner::BatchClient#load_batch_snapshot (method)").

## Inherits

- Object

## Example

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first
results = batch_snapshot.execute_partition partition

batch_snapshot.close
```

## Methods

### #close

    def close()

Closes the batch snapshot and releases the underlying resources.


<br />

This should only be called once the batch snapshot is no longer needed
anywhere. In particular if this batch snapshot is being used across
multiple machines, calling this method on any of the machines will
render the batch snapshot invalid everywhere.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first
results = batch_snapshot.execute_partition partition

batch_snapshot.close
```

### #dump

    def dump() -> String

**Aliases**

- [#serialize](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_serialize_instance_)
Serializes the batch snapshot object so it can be recreated on another
process. See [Google::Cloud::Spanner::BatchClient#load_batch_snapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchClient#Google__Cloud__Spanner__BatchClient_load_batch_snapshot_instance_ "Google::Cloud::Spanner::BatchClient#load_batch_snapshot (method)").
**Returns**

- (String) --- The serialized representation of the batch snapshot.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first

serialized_snapshot = batch_snapshot.dump
serialized_partition = partition.dump

# In a separate process
new_batch_snapshot = batch_client.load_batch_snapshot \
  serialized_snapshot

new_partition = batch_client.load_partition \
  serialized_partition

results = new_batch_snapshot.execute_partition \
  new_partition
```

### #execute

    def execute(sql, params: nil, types: nil, query_options: nil, call_options: nil, directed_read_options: nil) -> Google::Cloud::Spanner::Results

**Alias Of** : [#execute_query](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_execute_query_instance_) Executes a SQL query.

The following settings can be provided:

- `:exclude_replicas` (Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.
- `:include_replicas` (Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
**Parameters**

- **sql** (String) --- The SQL query string. See [Query
  syntax](https://cloud.google.com/spanner/docs/query-syntax).


  The SQL query string can contain parameter placeholders. A parameter
  placeholder consists of "@" followed by the parameter name.
  Parameter names consist of any combination of letters, numbers, and
  underscores.
- **params** (Hash) *(defaults to: nil)* --- SQL parameters for the query string. The parameter placeholders, minus the "@", are the the hash keys, and the literal values are the hash values. If the query string contains something like "WHERE id \> @msg_id", then the params must contain something like `:msg_id => 1`.


  Ruby types are mapped to Spanner types as follows:

  \| Spanner \| Ruby \| Notes \|
  \|---\|---\|---\|
  \| `BOOL` \| `true`/`false` \| \|
  \| `INT64` \| `Integer` \| \|
  \| `FLOAT64` \| `Float` \| \|
  \| `FLOAT32` \| `Float` \| \|
  \| `STRING` \| `String` \| \|
  \| `UUID` \| `String` \| \|
  \| `DATE` \| `Date` \| \|
  \| `TIMESTAMP` \| `Time`, `DateTime` \| \|
  \| `BYTES` \| `File`, `IO`, `StringIO`, or similar \| \|
  \| `ARRAY` \| `Array` \| Nested arrays are not supported. \|
  \| `STRUCT` \| `Hash`, [Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") \| \|

  See [Data
  types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).

  See [Data Types - Constructing a
  STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
- **types** (Hash) *(defaults to: nil)* --- Types of the SQL parameters in `params`. It is not always possible for Cloud Spanner to infer the right SQL type from a value in `params`. In these cases, the `types` hash must be used to specify the SQL type for these values.


  The keys of the hash should be query string parameter placeholders,
  minus the "@". The values of the hash should be Cloud Spanner type
  codes from the following list:
  - `:BOOL`
  - `:BYTES`
  - `:DATE`
  - `:FLOAT64`
  - `:FLOAT32`
  - `:INT64`
  - `:STRING`
  - `:TIMESTAMP`
  - `:UUID`
  - `Array` - Lists are specified by providing the type code in an array. For example, an array of integers are specified as `[:INT64]`.
  - [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") - Types for STRUCT values (`Hash`/[Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") objects) are specified using a [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") object.

  Types are optional.
- **query_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  query options for executing SQL query. Query options are optional.
  The following settings can be provided:
  - `:optimizer_version` (String) The version of optimizer to use. Empty to use database default. "latest" to use the latest available optimizer version.
  - `:optimizer_statistics_package` (String) Statistics package to use. Empty to use the database default.
- **call_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  call options, e.g., timeout, retries, etc. Call options are
  optional. The following settings can be provided:
  - `:timeout` (Numeric) A numeric value of custom timeout in seconds that overrides the default setting.
  - `:retry_policy` (Hash) A hash of values that overrides the default setting of retry policy with the following keys:
    - `:initial_delay` (`Numeric`) - The initial delay in seconds.
    - `:max_delay` (`Numeric`) - The max delay in seconds.
    - `:multiplier` (`Numeric`) - The incremental backoff multiplier.
    - `:retry_codes` (`Array<String>`) - The error codes that should trigger a retry.
- **directed_read_options** (Hash) *(defaults to: nil)* --- Client options used to set the directed_read_options for all ReadRequests and ExecuteSqlRequests that indicates which replicas or regions should be used for non-transactional reads or queries. Will represent [`Google::Cloud::Spanner::V1::DirectedReadOptions`](https://cloud.google.com/ruby/docs/reference/google-cloud-spanner-v1/latest/Google-Cloud-Spanner-V1-DirectedReadOptions)
**Returns**

- ([Google::Cloud::Spanner::Results](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Results)) --- The results of the query execution.
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query "SELECT * FROM users"

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using query parameters:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query(
   "SELECT * FROM users " \
   "WHERE active = @active",
   params: { active: true }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query with a SQL STRUCT query parameter as a Hash:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_hash = { id: 1, name: "Charlie", active: false }

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_hash }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Specify the SQL STRUCT type using Fields object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_type = batch_client.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
user_hash = { id: 1, name: nil, active: false }

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_hash },
  types: { user_struct: user_type }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Or, query with a SQL STRUCT as a typed Data object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_type = batch_client.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
user_data = user_type.struct id: 1, name: nil, active: false

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_data }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using query options:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query \
  "SELECT * FROM users",
  query_options: {
    optimizer_version: "1",
    optimizer_statistics_package: "auto_20191128_14_47_22UTC"
  }

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using custom timeout and retry policy:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

timeout = 30.0
retry_policy = {
  initial_delay: 0.25,
  max_delay:     32.0,
  multiplier:    1.3,
  retry_codes:   ["UNAVAILABLE"]
}
call_options = { timeout: timeout, retry_policy: retry_policy }

results = batch_snapshot.execute_query \
   "SELECT * FROM users",
   call_options: call_options

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

### #execute_partition

    def execute_partition(partition, call_options: nil)

Execute the partition to return a [Results](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Results "Google::Cloud::Spanner::Results (class)"). The result returned
could be zero or more rows. The row metadata may be absent if no rows
are returned.
**Parameters**

- **partition** ([Google::Cloud::Spanner::Partition](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Partition)) --- The partition to be executed.
- **call_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  call options, e.g., timeout, retries, etc. Call options are
  optional. The following settings can be provided:
  - `:timeout` (Numeric) A numeric value of custom timeout in seconds that overrides the default setting.
  - `:retry_policy` (Hash) A hash of values that overrides the default setting of retry policy with the following keys:
    - `:initial_delay` (`Numeric`) - The initial delay in seconds.
    - `:max_delay` (`Numeric`) - The max delay in seconds.
    - `:multiplier` (`Numeric`) - The incremental backoff multiplier.
    - `:retry_codes` (`Array<String>`) - The error codes that should trigger a retry.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first
results = batch_snapshot.execute_partition partition

batch_snapshot.close
```

### #execute_query

    def execute_query(sql, params: nil, types: nil, query_options: nil, call_options: nil, directed_read_options: nil) -> Google::Cloud::Spanner::Results

**Aliases**

- [#execute](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_execute_instance_)
- [#query](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_query_instance_)
- [#execute_sql](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_execute_sql_instance_)
Executes a SQL query.

The following settings can be provided:

- `:exclude_replicas` (Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.
- `:include_replicas` (Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
**Parameters**

- **sql** (String) --- The SQL query string. See [Query
  syntax](https://cloud.google.com/spanner/docs/query-syntax).


  The SQL query string can contain parameter placeholders. A parameter
  placeholder consists of "@" followed by the parameter name.
  Parameter names consist of any combination of letters, numbers, and
  underscores.
- **params** (Hash) *(defaults to: nil)* --- SQL parameters for the query string. The parameter placeholders, minus the "@", are the the hash keys, and the literal values are the hash values. If the query string contains something like "WHERE id \> @msg_id", then the params must contain something like `:msg_id => 1`.


  Ruby types are mapped to Spanner types as follows:

  \| Spanner \| Ruby \| Notes \|
  \|---\|---\|---\|
  \| `BOOL` \| `true`/`false` \| \|
  \| `INT64` \| `Integer` \| \|
  \| `FLOAT64` \| `Float` \| \|
  \| `FLOAT32` \| `Float` \| \|
  \| `STRING` \| `String` \| \|
  \| `UUID` \| `String` \| \|
  \| `DATE` \| `Date` \| \|
  \| `TIMESTAMP` \| `Time`, `DateTime` \| \|
  \| `BYTES` \| `File`, `IO`, `StringIO`, or similar \| \|
  \| `ARRAY` \| `Array` \| Nested arrays are not supported. \|
  \| `STRUCT` \| `Hash`, [Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") \| \|

  See [Data
  types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).

  See [Data Types - Constructing a
  STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
- **types** (Hash) *(defaults to: nil)* --- Types of the SQL parameters in `params`. It is not always possible for Cloud Spanner to infer the right SQL type from a value in `params`. In these cases, the `types` hash must be used to specify the SQL type for these values.


  The keys of the hash should be query string parameter placeholders,
  minus the "@". The values of the hash should be Cloud Spanner type
  codes from the following list:
  - `:BOOL`
  - `:BYTES`
  - `:DATE`
  - `:FLOAT64`
  - `:FLOAT32`
  - `:INT64`
  - `:STRING`
  - `:TIMESTAMP`
  - `:UUID`
  - `Array` - Lists are specified by providing the type code in an array. For example, an array of integers are specified as `[:INT64]`.
  - [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") - Types for STRUCT values (`Hash`/[Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") objects) are specified using a [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") object.

  Types are optional.
- **query_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  query options for executing SQL query. Query options are optional.
  The following settings can be provided:
  - `:optimizer_version` (String) The version of optimizer to use. Empty to use database default. "latest" to use the latest available optimizer version.
  - `:optimizer_statistics_package` (String) Statistics package to use. Empty to use the database default.
- **call_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  call options, e.g., timeout, retries, etc. Call options are
  optional. The following settings can be provided:
  - `:timeout` (Numeric) A numeric value of custom timeout in seconds that overrides the default setting.
  - `:retry_policy` (Hash) A hash of values that overrides the default setting of retry policy with the following keys:
    - `:initial_delay` (`Numeric`) - The initial delay in seconds.
    - `:max_delay` (`Numeric`) - The max delay in seconds.
    - `:multiplier` (`Numeric`) - The incremental backoff multiplier.
    - `:retry_codes` (`Array<String>`) - The error codes that should trigger a retry.
- **directed_read_options** (Hash) *(defaults to: nil)* --- Client options used to set the directed_read_options for all ReadRequests and ExecuteSqlRequests that indicates which replicas or regions should be used for non-transactional reads or queries. Will represent [`Google::Cloud::Spanner::V1::DirectedReadOptions`](https://cloud.google.com/ruby/docs/reference/google-cloud-spanner-v1/latest/Google-Cloud-Spanner-V1-DirectedReadOptions)
**Returns**

- ([Google::Cloud::Spanner::Results](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Results)) --- The results of the query execution.
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query "SELECT * FROM users"

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using query parameters:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query(
   "SELECT * FROM users " \
   "WHERE active = @active",
   params: { active: true }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query with a SQL STRUCT query parameter as a Hash:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_hash = { id: 1, name: "Charlie", active: false }

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_hash }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Specify the SQL STRUCT type using Fields object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_type = batch_client.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
user_hash = { id: 1, name: nil, active: false }

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_hash },
  types: { user_struct: user_type }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Or, query with a SQL STRUCT as a typed Data object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_type = batch_client.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
user_data = user_type.struct id: 1, name: nil, active: false

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_data }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using query options:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query \
  "SELECT * FROM users",
  query_options: {
    optimizer_version: "1",
    optimizer_statistics_package: "auto_20191128_14_47_22UTC"
  }

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using custom timeout and retry policy:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

timeout = 30.0
retry_policy = {
  initial_delay: 0.25,
  max_delay:     32.0,
  multiplier:    1.3,
  retry_codes:   ["UNAVAILABLE"]
}
call_options = { timeout: timeout, retry_policy: retry_policy }

results = batch_snapshot.execute_query \
   "SELECT * FROM users",
   call_options: call_options

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

### #execute_sql

    def execute_sql(sql, params: nil, types: nil, query_options: nil, call_options: nil, directed_read_options: nil) -> Google::Cloud::Spanner::Results

**Alias Of** : [#execute_query](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_execute_query_instance_) Executes a SQL query.

The following settings can be provided:

- `:exclude_replicas` (Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.
- `:include_replicas` (Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
**Parameters**

- **sql** (String) --- The SQL query string. See [Query
  syntax](https://cloud.google.com/spanner/docs/query-syntax).


  The SQL query string can contain parameter placeholders. A parameter
  placeholder consists of "@" followed by the parameter name.
  Parameter names consist of any combination of letters, numbers, and
  underscores.
- **params** (Hash) *(defaults to: nil)* --- SQL parameters for the query string. The parameter placeholders, minus the "@", are the the hash keys, and the literal values are the hash values. If the query string contains something like "WHERE id \> @msg_id", then the params must contain something like `:msg_id => 1`.


  Ruby types are mapped to Spanner types as follows:

  \| Spanner \| Ruby \| Notes \|
  \|---\|---\|---\|
  \| `BOOL` \| `true`/`false` \| \|
  \| `INT64` \| `Integer` \| \|
  \| `FLOAT64` \| `Float` \| \|
  \| `FLOAT32` \| `Float` \| \|
  \| `STRING` \| `String` \| \|
  \| `UUID` \| `String` \| \|
  \| `DATE` \| `Date` \| \|
  \| `TIMESTAMP` \| `Time`, `DateTime` \| \|
  \| `BYTES` \| `File`, `IO`, `StringIO`, or similar \| \|
  \| `ARRAY` \| `Array` \| Nested arrays are not supported. \|
  \| `STRUCT` \| `Hash`, [Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") \| \|

  See [Data
  types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).

  See [Data Types - Constructing a
  STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
- **types** (Hash) *(defaults to: nil)* --- Types of the SQL parameters in `params`. It is not always possible for Cloud Spanner to infer the right SQL type from a value in `params`. In these cases, the `types` hash must be used to specify the SQL type for these values.


  The keys of the hash should be query string parameter placeholders,
  minus the "@". The values of the hash should be Cloud Spanner type
  codes from the following list:
  - `:BOOL`
  - `:BYTES`
  - `:DATE`
  - `:FLOAT64`
  - `:FLOAT32`
  - `:INT64`
  - `:STRING`
  - `:TIMESTAMP`
  - `:UUID`
  - `Array` - Lists are specified by providing the type code in an array. For example, an array of integers are specified as `[:INT64]`.
  - [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") - Types for STRUCT values (`Hash`/[Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") objects) are specified using a [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") object.

  Types are optional.
- **query_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  query options for executing SQL query. Query options are optional.
  The following settings can be provided:
  - `:optimizer_version` (String) The version of optimizer to use. Empty to use database default. "latest" to use the latest available optimizer version.
  - `:optimizer_statistics_package` (String) Statistics package to use. Empty to use the database default.
- **call_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  call options, e.g., timeout, retries, etc. Call options are
  optional. The following settings can be provided:
  - `:timeout` (Numeric) A numeric value of custom timeout in seconds that overrides the default setting.
  - `:retry_policy` (Hash) A hash of values that overrides the default setting of retry policy with the following keys:
    - `:initial_delay` (`Numeric`) - The initial delay in seconds.
    - `:max_delay` (`Numeric`) - The max delay in seconds.
    - `:multiplier` (`Numeric`) - The incremental backoff multiplier.
    - `:retry_codes` (`Array<String>`) - The error codes that should trigger a retry.
- **directed_read_options** (Hash) *(defaults to: nil)* --- Client options used to set the directed_read_options for all ReadRequests and ExecuteSqlRequests that indicates which replicas or regions should be used for non-transactional reads or queries. Will represent [`Google::Cloud::Spanner::V1::DirectedReadOptions`](https://cloud.google.com/ruby/docs/reference/google-cloud-spanner-v1/latest/Google-Cloud-Spanner-V1-DirectedReadOptions)
**Returns**

- ([Google::Cloud::Spanner::Results](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Results)) --- The results of the query execution.
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query "SELECT * FROM users"

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using query parameters:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query(
   "SELECT * FROM users " \
   "WHERE active = @active",
   params: { active: true }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query with a SQL STRUCT query parameter as a Hash:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_hash = { id: 1, name: "Charlie", active: false }

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_hash }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Specify the SQL STRUCT type using Fields object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_type = batch_client.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
user_hash = { id: 1, name: nil, active: false }

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_hash },
  types: { user_struct: user_type }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Or, query with a SQL STRUCT as a typed Data object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_type = batch_client.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
user_data = user_type.struct id: 1, name: nil, active: false

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_data }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using query options:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query \
  "SELECT * FROM users",
  query_options: {
    optimizer_version: "1",
    optimizer_statistics_package: "auto_20191128_14_47_22UTC"
  }

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using custom timeout and retry policy:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

timeout = 30.0
retry_policy = {
  initial_delay: 0.25,
  max_delay:     32.0,
  multiplier:    1.3,
  retry_codes:   ["UNAVAILABLE"]
}
call_options = { timeout: timeout, retry_policy: retry_policy }

results = batch_snapshot.execute_query \
   "SELECT * FROM users",
   call_options: call_options

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

### #partition_query

    def partition_query(sql, params: nil, types: nil, partition_size_bytes: nil, max_partitions: nil, query_options: nil, call_options: nil, data_boost_enabled: false, directed_read_options: nil) -> Array<Google::Cloud::Spanner::Partition>

Returns a list of [Partition](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Partition "Google::Cloud::Spanner::Partition (class)") objects to execute a batch query against
a database.

These partitions can be executed across multiple processes, even
across different machines. The partition size and count can be
configured, although the values given may not necessarily be honored
depending on the query and options in the request.

The request will fail if the query is not root partitionable. For a
query to be root partitionable, it needs to satisfy a few conditions.
For example, if the query execution plan contains a [distributed
union](https://cloud.google.com/spanner/docs/query-execution-operators#distributed_union)
operator, then it must be the first operator in the plan. For more
information about other conditions, see [Read data in
parallel](https://cloud.google.com/spanner/docs/reads#read_data_in_parallel).

The following settings can be provided:

- `:exclude_replicas` (Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.
- `:include_replicas` (Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
**Parameters**

- **sql** (String) --- The SQL query string. See [Query
  syntax](https://cloud.google.com/spanner/docs/query-syntax).


  The SQL query string can contain parameter placeholders. A parameter
  placeholder consists of "@" followed by the parameter name.
  Parameter names consist of any combination of letters, numbers, and
  underscores.
- **params** (Hash) *(defaults to: nil)* --- SQL parameters for the query string. The parameter placeholders, minus the "@", are the the hash keys, and the literal values are the hash values. If the query string contains something like "WHERE id \> @msg_id", then the params must contain something like `:msg_id => 1`.


  Ruby types are mapped to Spanner types as follows:

  \| Spanner \| Ruby \| Notes \|
  \|---\|---\|---\|
  \| `BOOL` \| `true`/`false` \| \|
  \| `INT64` \| `Integer` \| \|
  \| `FLOAT64` \| `Float` \| \|
  \| `FLOAT32` \| `Float` \| \|
  \| `STRING` \| `String` \| \|
  \| `UUID` \| `String` \| \|
  \| `DATE` \| `Date` \| \|
  \| `TIMESTAMP` \| `Time`, `DateTime` \| \|
  \| `BYTES` \| `File`, `IO`, `StringIO`, or similar \| \|
  \| `ARRAY` \| `Array` \| Nested arrays are not supported. \|
  \| `STRUCT` \| `Hash`, [Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") \| \|

  See [Data
  types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).

  See [Data Types - Constructing a
  STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
- **types** (Hash) *(defaults to: nil)* --- Types of the SQL parameters in `params`. It is not always possible for Cloud Spanner to infer the right SQL type from a value in `params`. In these cases, the `types` hash must be used to specify the SQL type for these values.


  The keys of the hash should be query string parameter placeholders,
  minus the "@". The values of the hash should be Cloud Spanner type
  codes from the following list:
  - `:BOOL`
  - `:BYTES`
  - `:DATE`
  - `:FLOAT64`
  - `:FLOAT32`
  - `:INT64`
  - `:STRING`
  - `:TIMESTAMP`
  - `:UUID`
  - `Array` - Lists are specified by providing the type code in an array. For example, an array of integers are specified as `[:INT64]`.
  - [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") - Types for STRUCT values (`Hash`/[Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") objects) are specified using a [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") object.

  Types are optional.
- **partition_size_bytes** (Integer) *(defaults to: nil)* --- The desired data size for each partition generated. This is only a hint. The actual size of each partition may be smaller or larger than this size request.
- **max_partitions** (Integer) *(defaults to: nil)* --- The desired maximum number of partitions to return. For example, this may be set to the number of workers available. This is only a hint and may provide different results based on the request.
- **query_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  query options for executing SQL query. Query options are optional.
  The following settings can be provided:
  - `:optimizer_version` (String) The version of optimizer to use. Empty to use database default. "latest" to use the latest available optimizer version.
  - `:optimizer_statistics_package` (String) Statistics package to use. Empty to use the database default.
- **call_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  call options, e.g., timeout, retries, etc. Call options are
  optional. The following settings can be provided:
  - `:timeout` (Numeric) A numeric value of custom timeout in seconds that overrides the default setting.
  - `:retry_policy` (Hash) A hash of values that overrides the default setting of retry policy with the following keys:
    - `:initial_delay` (`Numeric`) - The initial delay in seconds.
    - `:max_delay` (`Numeric`) - The max delay in seconds.
    - `:multiplier` (`Numeric`) - The incremental backoff multiplier.
    - `:retry_codes` (`Array<String>`) - The error codes that should trigger a retry.
- **data_boost_enabled** (Boolean) *(defaults to: false)* --- If this field is set `true`, the request will be executed via offline access. Defaults to `false`.
- **directed_read_options** (Hash) *(defaults to: nil)* --- Client options used to set the directed_read_options for all ReadRequests and ExecuteSqlRequests that indicates which replicas or regions should be used for non-transactional reads or queries. Will represent [`Google::Cloud::Spanner::V1::DirectedReadOptions`](https://cloud.google.com/ruby/docs/reference/google-cloud-spanner-v1/latest/Google-Cloud-Spanner-V1-DirectedReadOptions)
**Returns**

- (Array\<[Google::Cloud::Spanner::Partition](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Partition)\>) --- The partitions created by the query partition.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

sql = "SELECT u.id, u.active FROM users AS u \
       WHERE u.id < 2000 AND u.active = false"
partitions = batch_snapshot.partition_query sql

partition = partitions.first
results = batch_snapshot.execute_partition partition

batch_snapshot.close
```

### #partition_read

    def partition_read(table, columns, keys: nil, index: nil, partition_size_bytes: nil, max_partitions: nil, call_options: nil, data_boost_enabled: false, directed_read_options: nil) -> Array<Google::Cloud::Spanner::Partition>

Returns a list of [Partition](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Partition "Google::Cloud::Spanner::Partition (class)") objects to read zero or more rows from a
database.

These partitions can be executed across multiple processes, even
across different machines. The partition size and count can be
configured, although the values given may not necessarily be honored
depending on the query and options in the request.

The following settings can be provided:

- `:exclude_replicas` (Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.
- `:include_replicas` (Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
**Parameters**

- **table** (String) --- The name of the table in the database to be read.
- **columns** (Array\<String, Symbol\>) --- The columns of table to be returned for each row matching this request.
- **keys** (Object, Array\<Object\>) *(defaults to: nil)* --- A single, or list of keys or key ranges to match returned data to. Values should have exactly as many elements as there are columns in the primary key.
- **index** (String) *(defaults to: nil)* --- The name of an index to use instead of the table's primary key when interpreting `id` and sorting result rows. Optional.
- **partition_size_bytes** (Integer) *(defaults to: nil)* --- The desired data size for each partition generated. This is only a hint. The actual size of each partition may be smaller or larger than this size request.
- **max_partitions** (Integer) *(defaults to: nil)* --- The desired maximum number of partitions to return. For example, this may be set to the number of workers available. This is only a hint and may provide different results based on the request.
- **call_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  call options, e.g., timeout, retries, etc. Call options are
  optional. The following settings can be provided:
  - `:timeout` (Numeric) A numeric value of custom timeout in seconds that overrides the default setting.
  - `:retry_policy` (Hash) A hash of values that overrides the default setting of retry policy with the following keys:
    - `:initial_delay` (`Numeric`) - The initial delay in seconds.
    - `:max_delay` (`Numeric`) - The max delay in seconds.
    - `:multiplier` (`Numeric`) - The incremental backoff multiplier.
    - `:retry_codes` (`Array<String>`) - The error codes that should trigger a retry.
- **data_boost_enabled** (Boolean) *(defaults to: false)* --- If this field is set `true`, the request will be executed via offline access. Defaults to `false`.
- **directed_read_options** (Hash) *(defaults to: nil)* --- Client options used to set the directed_read_options for all ReadRequests and ExecuteSqlRequests that indicates which replicas or regions should be used for non-transactional reads or queries. Will represent [`Google::Cloud::Spanner::V1::DirectedReadOptions`](https://cloud.google.com/ruby/docs/reference/google-cloud-spanner-v1/latest/Google-Cloud-Spanner-V1-DirectedReadOptions)
**Returns**

- (Array\<[Google::Cloud::Spanner::Partition](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Partition)\>) --- The partitions created by the read partition.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first
results = batch_snapshot.execute_partition partition

batch_snapshot.close
```

### #query

    def query(sql, params: nil, types: nil, query_options: nil, call_options: nil, directed_read_options: nil) -> Google::Cloud::Spanner::Results

**Alias Of** : [#execute_query](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_execute_query_instance_) Executes a SQL query.

The following settings can be provided:

- `:exclude_replicas` (Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.
- `:include_replicas` (Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
**Parameters**

- **sql** (String) --- The SQL query string. See [Query
  syntax](https://cloud.google.com/spanner/docs/query-syntax).


  The SQL query string can contain parameter placeholders. A parameter
  placeholder consists of "@" followed by the parameter name.
  Parameter names consist of any combination of letters, numbers, and
  underscores.
- **params** (Hash) *(defaults to: nil)* --- SQL parameters for the query string. The parameter placeholders, minus the "@", are the the hash keys, and the literal values are the hash values. If the query string contains something like "WHERE id \> @msg_id", then the params must contain something like `:msg_id => 1`.


  Ruby types are mapped to Spanner types as follows:

  \| Spanner \| Ruby \| Notes \|
  \|---\|---\|---\|
  \| `BOOL` \| `true`/`false` \| \|
  \| `INT64` \| `Integer` \| \|
  \| `FLOAT64` \| `Float` \| \|
  \| `FLOAT32` \| `Float` \| \|
  \| `STRING` \| `String` \| \|
  \| `UUID` \| `String` \| \|
  \| `DATE` \| `Date` \| \|
  \| `TIMESTAMP` \| `Time`, `DateTime` \| \|
  \| `BYTES` \| `File`, `IO`, `StringIO`, or similar \| \|
  \| `ARRAY` \| `Array` \| Nested arrays are not supported. \|
  \| `STRUCT` \| `Hash`, [Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") \| \|

  See [Data
  types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).

  See [Data Types - Constructing a
  STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
- **types** (Hash) *(defaults to: nil)* --- Types of the SQL parameters in `params`. It is not always possible for Cloud Spanner to infer the right SQL type from a value in `params`. In these cases, the `types` hash must be used to specify the SQL type for these values.


  The keys of the hash should be query string parameter placeholders,
  minus the "@". The values of the hash should be Cloud Spanner type
  codes from the following list:
  - `:BOOL`
  - `:BYTES`
  - `:DATE`
  - `:FLOAT64`
  - `:FLOAT32`
  - `:INT64`
  - `:STRING`
  - `:TIMESTAMP`
  - `:UUID`
  - `Array` - Lists are specified by providing the type code in an array. For example, an array of integers are specified as `[:INT64]`.
  - [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") - Types for STRUCT values (`Hash`/[Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") objects) are specified using a [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") object.

  Types are optional.
- **query_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  query options for executing SQL query. Query options are optional.
  The following settings can be provided:
  - `:optimizer_version` (String) The version of optimizer to use. Empty to use database default. "latest" to use the latest available optimizer version.
  - `:optimizer_statistics_package` (String) Statistics package to use. Empty to use the database default.
- **call_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  call options, e.g., timeout, retries, etc. Call options are
  optional. The following settings can be provided:
  - `:timeout` (Numeric) A numeric value of custom timeout in seconds that overrides the default setting.
  - `:retry_policy` (Hash) A hash of values that overrides the default setting of retry policy with the following keys:
    - `:initial_delay` (`Numeric`) - The initial delay in seconds.
    - `:max_delay` (`Numeric`) - The max delay in seconds.
    - `:multiplier` (`Numeric`) - The incremental backoff multiplier.
    - `:retry_codes` (`Array<String>`) - The error codes that should trigger a retry.
- **directed_read_options** (Hash) *(defaults to: nil)* --- Client options used to set the directed_read_options for all ReadRequests and ExecuteSqlRequests that indicates which replicas or regions should be used for non-transactional reads or queries. Will represent [`Google::Cloud::Spanner::V1::DirectedReadOptions`](https://cloud.google.com/ruby/docs/reference/google-cloud-spanner-v1/latest/Google-Cloud-Spanner-V1-DirectedReadOptions)
**Returns**

- ([Google::Cloud::Spanner::Results](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Results)) --- The results of the query execution.
**Examples**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query "SELECT * FROM users"

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using query parameters:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query(
   "SELECT * FROM users " \
   "WHERE active = @active",
   params: { active: true }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query with a SQL STRUCT query parameter as a Hash:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_hash = { id: 1, name: "Charlie", active: false }

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_hash }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Specify the SQL STRUCT type using Fields object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_type = batch_client.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
user_hash = { id: 1, name: nil, active: false }

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_hash },
  types: { user_struct: user_type }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Or, query with a SQL STRUCT as a typed Data object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

user_type = batch_client.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
user_data = user_type.struct id: 1, name: nil, active: false

results = batch_snapshot.execute_query(
  "SELECT * FROM users WHERE " \
  "ID = @user_struct.id " \
  "AND name = @user_struct.name " \
  "AND active = @user_struct.active",
  params: { user_struct: user_data }
)

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using query options:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.execute_query \
  "SELECT * FROM users",
  query_options: {
    optimizer_version: "1",
    optimizer_statistics_package: "auto_20191128_14_47_22UTC"
  }

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

Query using custom timeout and retry policy:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

timeout = 30.0
retry_policy = {
  initial_delay: 0.25,
  max_delay:     32.0,
  multiplier:    1.3,
  retry_codes:   ["UNAVAILABLE"]
}
call_options = { timeout: timeout, retry_policy: retry_policy }

results = batch_snapshot.execute_query \
   "SELECT * FROM users",
   call_options: call_options

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

### #read

    def read(table, columns, keys: nil, index: nil, limit: nil, call_options: nil, directed_read_options: nil) -> Google::Cloud::Spanner::Results

Read rows from a database table, as a simple alternative to
[#execute_query](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_execute_query_instance_ "Google::Cloud::Spanner::BatchSnapshot#execute_query (method)").

The following settings can be provided:

- `:exclude_replicas` (Hash) Exclude_replicas indicates what replicas should be excluded from serving requests. Spanner will not route requests to the replicas in this list.
- `:include_replicas` (Hash) Include_replicas indicates the order of replicas to process the request. If auto_failover_disabled is set to true and all replicas are exhausted without finding a healthy replica, Spanner will wait for a replica in the list to become available, requests may fail due to DEADLINE_EXCEEDED errors.
**Parameters**

- **table** (String) --- The name of the table in the database to be read.
- **columns** (Array\<String, Symbol\>) --- The columns of table to be returned for each row matching this request.
- **keys** (Object, Array\<Object\>) *(defaults to: nil)* --- A single, or list of keys or key ranges to match returned data to. Values should have exactly as many elements as there are columns in the primary key.
- **index** (String) *(defaults to: nil)* --- The name of an index to use instead of the table's primary key when interpreting `id` and sorting result rows. Optional.
- **limit** (Integer) *(defaults to: nil)* --- If greater than zero, no more than this number of rows will be returned. The default is no limit.
- **call_options** (Hash) *(defaults to: nil)* ---

  A hash of values to specify the custom
  call options, e.g., timeout, retries, etc. Call options are
  optional. The following settings can be provided:
  - `:timeout` (Numeric) A numeric value of custom timeout in seconds that overrides the default setting.
  - `:retry_policy` (Hash) A hash of values that overrides the default setting of retry policy with the following keys:
    - `:initial_delay` (`Numeric`) - The initial delay in seconds.
    - `:max_delay` (`Numeric`) - The max delay in seconds.
    - `:multiplier` (`Numeric`) - The incremental backoff multiplier.
    - `:retry_codes` (`Array<String>`) - The error codes that should trigger a retry.
- **directed_read_options** (Hash) *(defaults to: nil)* --- Client options used to set the directed_read_options for all ReadRequests and ExecuteSqlRequests that indicates which replicas or regions should be used for non-transactional reads or queries. Will represent [`Google::Cloud::Spanner::V1::DirectedReadOptions`](https://cloud.google.com/ruby/docs/reference/google-cloud-spanner-v1/latest/Google-Cloud-Spanner-V1-DirectedReadOptions)
**Returns**

- ([Google::Cloud::Spanner::Results](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Results)) --- The results of the read operation.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

results = batch_snapshot.read "users", [:id, :name]

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end
```

### #serialize

    def serialize() -> String

**Alias Of** : [#dump](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_dump_instance_) Serializes the batch snapshot object so it can be recreated on another
process. See [Google::Cloud::Spanner::BatchClient#load_batch_snapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchClient#Google__Cloud__Spanner__BatchClient_load_batch_snapshot_instance_ "Google::Cloud::Spanner::BatchClient#load_batch_snapshot (method)").
**Returns**

- (String) --- The serialized representation of the batch snapshot.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first

serialized_snapshot = batch_snapshot.dump
serialized_partition = partition.dump

# In a separate process
new_batch_snapshot = batch_client.load_batch_snapshot \
  serialized_snapshot

new_partition = batch_client.load_partition \
  serialized_partition

results = new_batch_snapshot.execute_partition \
  new_partition
```

### #timestamp

    def timestamp() -> Time

The read timestamp chosen for batch snapshot.
**Returns**

- (Time) --- The chosen timestamp.

### #transaction_id

    def transaction_id() -> String

Identifier of the batch snapshot transaction.
**Returns**

- (String) --- The transaction id.