Reference documentation and code samples for the Cloud Spanner V1 Client class KeyRange.
KeyRange represents a range of rows in a table or index.
A range has a start key and an end key. These keys can be open or
closed, indicating if the range includes rows with that key.
Keys are represented by lists, where the ith value in the list
corresponds to the ith component of the table or index primary key.
Individual values are encoded as described
here.
For example, consider the following table definition:
CREATE TABLE UserEvents (
UserName STRING(MAX),
EventDate STRING(10)
) PRIMARY KEY(UserName, EventDate);
The following keys name rows in this table:
["Bob", "2014-09-23"]
["Alfred", "2015-06-12"]
Since the UserEvents table's PRIMARY KEY clause names two
columns, each UserEvents key has two elements; the first is the
UserName, and the second is the EventDate.
Key ranges with multiple components are interpreted
lexicographically by component using the table or index key's declared
sort order. For example, the following range returns all events for
user "Bob" that occurred in the year 2015:
"start_closed": ["Bob", "2015-01-01"]
"end_closed": ["Bob", "2015-12-31"]
Start and end keys can omit trailing key components. This affects the
inclusion and exclusion of rows that exactly match the provided key
components: if the key is closed, then rows that exactly match the
provided components are included; if the key is open, then rows
that exactly match are not included.
For example, the following range includes all events for "Bob" that
occurred during and after the year 2000:
"start_closed": ["Bob", "2000-01-01"]
"end_closed": ["Bob"]
The next example retrieves all events for "Bob":
"start_closed": ["Bob"]
"end_closed": ["Bob"]
To retrieve events before the year 2000:
"start_closed": ["Bob"]
"end_open": ["Bob", "2000-01-01"]
The following range includes all rows in the table:
"start_closed": []
"end_closed": []
This range returns all users whose UserName begins with any
character from A to C:
"start_closed": ["A"]
"end_open": ["D"]
This range returns all users whose UserName begins with B:
"start_closed": ["B"]
"end_open": ["C"]
Key ranges honor column sort order. For example, suppose a table is
defined as follows:
CREATE TABLE DescendingSortedTable {
Key INT64,
...
) PRIMARY KEY(Key DESC);
The following range retrieves all rows with key values between 1
and 100 inclusive:
"start_closed": ["100"]
"end_closed": ["1"]
Note that 100 is passed as the start, and 1 is passed as the end,
because Key is a descending column in the schema.
Generated from protobuf message google.spanner.v1.KeyRange
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Spanner V1 Client - Class KeyRange (1.104.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.104.0 (latest)](/php/docs/reference/cloud-spanner/latest/V1.KeyRange)\n- [1.103.0](/php/docs/reference/cloud-spanner/1.103.0/V1.KeyRange)\n- [1.102.0](/php/docs/reference/cloud-spanner/1.102.0/V1.KeyRange)\n- [1.101.0](/php/docs/reference/cloud-spanner/1.101.0/V1.KeyRange)\n- [1.100.0](/php/docs/reference/cloud-spanner/1.100.0/V1.KeyRange)\n- [1.98.0](/php/docs/reference/cloud-spanner/1.98.0/V1.KeyRange)\n- [1.97.0](/php/docs/reference/cloud-spanner/1.97.0/V1.KeyRange)\n- [1.96.0](/php/docs/reference/cloud-spanner/1.96.0/V1.KeyRange)\n- [1.95.0](/php/docs/reference/cloud-spanner/1.95.0/V1.KeyRange)\n- [1.94.0](/php/docs/reference/cloud-spanner/1.94.0/V1.KeyRange)\n- [1.93.1](/php/docs/reference/cloud-spanner/1.93.1/V1.KeyRange)\n- [1.92.1](/php/docs/reference/cloud-spanner/1.92.1/V1.KeyRange)\n- [1.91.0](/php/docs/reference/cloud-spanner/1.91.0/V1.KeyRange)\n- [1.90.0](/php/docs/reference/cloud-spanner/1.90.0/V1.KeyRange)\n- [1.89.0](/php/docs/reference/cloud-spanner/1.89.0/V1.KeyRange)\n- [1.88.0](/php/docs/reference/cloud-spanner/1.88.0/V1.KeyRange)\n- [1.87.0](/php/docs/reference/cloud-spanner/1.87.0/V1.KeyRange)\n- [1.86.0](/php/docs/reference/cloud-spanner/1.86.0/V1.KeyRange)\n- [1.85.0](/php/docs/reference/cloud-spanner/1.85.0/V1.KeyRange)\n- [1.84.0](/php/docs/reference/cloud-spanner/1.84.0/V1.KeyRange)\n- [1.83.0](/php/docs/reference/cloud-spanner/1.83.0/V1.KeyRange)\n- [1.82.0](/php/docs/reference/cloud-spanner/1.82.0/V1.KeyRange)\n- [1.81.0](/php/docs/reference/cloud-spanner/1.81.0/V1.KeyRange)\n- [1.80.0](/php/docs/reference/cloud-spanner/1.80.0/V1.KeyRange)\n- [1.79.0](/php/docs/reference/cloud-spanner/1.79.0/V1.KeyRange)\n- [1.78.0](/php/docs/reference/cloud-spanner/1.78.0/V1.KeyRange)\n- [1.77.0](/php/docs/reference/cloud-spanner/1.77.0/V1.KeyRange)\n- [1.76.1](/php/docs/reference/cloud-spanner/1.76.1/V1.KeyRange)\n- [1.68.0](/php/docs/reference/cloud-spanner/1.68.0/V1.KeyRange)\n- [1.67.0](/php/docs/reference/cloud-spanner/1.67.0/V1.KeyRange)\n- [1.66.0](/php/docs/reference/cloud-spanner/1.66.0/V1.KeyRange)\n- [1.65.0](/php/docs/reference/cloud-spanner/1.65.0/V1.KeyRange)\n- [1.64.0](/php/docs/reference/cloud-spanner/1.64.0/V1.KeyRange)\n- [1.63.2](/php/docs/reference/cloud-spanner/1.63.2/V1.KeyRange)\n- [1.62.1](/php/docs/reference/cloud-spanner/1.62.1/V1.KeyRange)\n- [1.61.0](/php/docs/reference/cloud-spanner/1.61.0/V1.KeyRange)\n- [1.60.0](/php/docs/reference/cloud-spanner/1.60.0/V1.KeyRange)\n- [1.59.0](/php/docs/reference/cloud-spanner/1.59.0/V1.KeyRange)\n- [1.58.4](/php/docs/reference/cloud-spanner/1.58.4/V1.KeyRange)\n- [1.57.0](/php/docs/reference/cloud-spanner/1.57.0/V1.KeyRange)\n- [1.56.0](/php/docs/reference/cloud-spanner/1.56.0/V1.KeyRange)\n- [1.55.0](/php/docs/reference/cloud-spanner/1.55.0/V1.KeyRange)\n- [1.54.2](/php/docs/reference/cloud-spanner/1.54.2/V1.KeyRange) \nReference documentation and code samples for the Cloud Spanner V1 Client class KeyRange.\n\nKeyRange represents a range of rows in a table or index.\n\nA range has a start key and an end key. These keys can be open or\nclosed, indicating if the range includes rows with that key.\nKeys are represented by lists, where the ith value in the list\ncorresponds to the ith component of the table or index primary key.\nIndividual values are encoded as described\n[here](/php/docs/reference/cloud-spanner/latest/V1.TypeCode).\nFor example, consider the following table definition:\nCREATE TABLE UserEvents (\nUserName STRING(MAX),\nEventDate STRING(10)\n) PRIMARY KEY(UserName, EventDate);\nThe following keys name rows in this table:\n\\[\"Bob\", \"2014-09-23\"\\]\n\\[\"Alfred\", \"2015-06-12\"\\]\nSince the `UserEvents` table's `PRIMARY KEY` clause names two\ncolumns, each `UserEvents` key has two elements; the first is the\n`UserName`, and the second is the `EventDate`.\nKey ranges with multiple components are interpreted\nlexicographically by component using the table or index key's declared\nsort order. For example, the following range returns all events for\nuser `\"Bob\"` that occurred in the year 2015:\n\"start_closed\": \\[\"Bob\", \"2015-01-01\"\\]\n\"end_closed\": \\[\"Bob\", \"2015-12-31\"\\]\nStart and end keys can omit trailing key components. This affects the\ninclusion and exclusion of rows that exactly match the provided key\ncomponents: if the key is closed, then rows that exactly match the\nprovided components are included; if the key is open, then rows\nthat exactly match are not included.\nFor example, the following range includes all events for `\"Bob\"` that\noccurred during and after the year 2000:\n\"start_closed\": \\[\"Bob\", \"2000-01-01\"\\]\n\"end_closed\": \\[\"Bob\"\\]\nThe next example retrieves all events for `\"Bob\"`:\n\"start_closed\": \\[\"Bob\"\\]\n\"end_closed\": \\[\"Bob\"\\]\nTo retrieve events before the year 2000:\n\"start_closed\": \\[\"Bob\"\\]\n\"end_open\": \\[\"Bob\", \"2000-01-01\"\\]\nThe following range includes all rows in the table:\n\"start_closed\": \\[\\]\n\"end_closed\": \\[\\]\nThis range returns all users whose `UserName` begins with any\ncharacter from A to C:\n\"start_closed\": \\[\"A\"\\]\n\"end_open\": \\[\"D\"\\]\nThis range returns all users whose `UserName` begins with B:\n\"start_closed\": \\[\"B\"\\]\n\"end_open\": \\[\"C\"\\]\nKey ranges honor column sort order. For example, suppose a table is\ndefined as follows:\nCREATE TABLE DescendingSortedTable {\nKey INT64,\n...\n) PRIMARY KEY(Key DESC);\nThe following range retrieves all rows with key values between 1\nand 100 inclusive:\n\"start_closed\": \\[\"100\"\\]\n\"end_closed\": \\[\"1\"\\]\nNote that 100 is passed as the start, and 1 is passed as the end,\nbecause `Key` is a descending column in the schema.\n\nGenerated from protobuf message `google.spanner.v1.KeyRange`\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Spanner \\\\ V1\n\nMethods\n-------\n\n### __construct\n\nConstructor.\n\n### getStartClosed\n\nIf the start is closed, then the range includes all rows whose\nfirst `len(start_closed)` key columns exactly match `start_closed`.\n\n### hasStartClosed\n\n### setStartClosed\n\nIf the start is closed, then the range includes all rows whose\nfirst `len(start_closed)` key columns exactly match `start_closed`.\n\n### getStartOpen\n\nIf the start is open, then the range excludes rows whose first\n`len(start_open)` key columns exactly match `start_open`.\n\n### hasStartOpen\n\n### setStartOpen\n\nIf the start is open, then the range excludes rows whose first\n`len(start_open)` key columns exactly match `start_open`.\n\n### getEndClosed\n\nIf the end is closed, then the range includes all rows whose\nfirst `len(end_closed)` key columns exactly match `end_closed`.\n\n### hasEndClosed\n\n### setEndClosed\n\nIf the end is closed, then the range includes all rows whose\nfirst `len(end_closed)` key columns exactly match `end_closed`.\n\n### getEndOpen\n\nIf the end is open, then the range excludes rows whose first\n`len(end_open)` key columns exactly match `end_open`.\n\n### hasEndOpen\n\n### setEndOpen\n\nIf the end is open, then the range excludes rows whose first\n`len(end_open)` key columns exactly match `end_open`.\n\n### getStartKeyType\n\n### getEndKeyType"]]