Returns a copy of a protocol buffer, replacing the values in one or more fields.
field_path is a delimited path to the protocol buffer field that's replaced.
When using replace_fields, the following limitations apply:
If value is NULL, it un-sets field_path or returns an error if the
last component of field_path is a required field.
Replacing subfields will succeed only if the message containing the field is
set.
Replacing subfields of repeated field isn't allowed.
A repeated field can be replaced with an ARRAY value.
Return type
Type of proto_expression
Examples
The following example uses protocol buffer messages Book and BookDetails.
This statement replaces the values of the field title and subfield chapters
of proto type Book. Note that field details must be set for the statement
to succeed.
SELECTREPLACE_FIELDS(NEWBook("The Hummingbird"AStitle,NEWBookDetails(10ASchapters)ASdetails),"The Hummingbird II"AStitle,11ASdetails.chapters)ASproto;/*-----------------------------------------------------------------------------* | proto | +-----------------------------------------------------------------------------+ |{title: "The Hummingbird II" details: {chapters: 11 }} | *-----------------------------------------------------------------------------*/
The function can replace value of repeated fields.
SELECTREPLACE_FIELDS(NEWBook("The Hummingbird"AStitle,NEWBookDetails(10ASchapters)ASdetails),["A good read!","Highly recommended."]ASreviews)ASproto;/*-----------------------------------------------------------------------------* | proto | +-----------------------------------------------------------------------------+ |{title: "The Hummingbird" review: "A good read" review: "Highly recommended."| | details: {chapters: 10 }} | *-----------------------------------------------------------------------------*/
The function can also set a field to NULL.
SELECTREPLACE_FIELDS(NEWBook("The Hummingbird"AStitle,NEWBookDetails(10ASchapters)ASdetails),NULLASdetails)ASproto;/*-----------------------------------------------------------------------------* | proto | +-----------------------------------------------------------------------------+ |{title: "The Hummingbird" } | *-----------------------------------------------------------------------------*/
[[["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-08-28 UTC."],[],[],null,["# Protocol buffer functions in GoogleSQL\n\nGoogleSQL for Spanner supports the following protocol buffer functions.\n\nFunction list\n-------------\n\n`REPLACE_FIELDS`\n----------------\n\n REPLACE_FIELDS(proto_expression, value AS field_path [, ... ])\n\n**Description**\n\nReturns a copy of a protocol buffer, replacing the values in one or more fields.\n`field_path` is a delimited path to the protocol buffer field that's replaced.\nWhen using `replace_fields`, the following limitations apply:\n\n- If `value` is `NULL`, it un-sets `field_path` or returns an error if the last component of `field_path` is a required field.\n- Replacing subfields will succeed only if the message containing the field is set.\n- Replacing subfields of repeated field isn't allowed.\n- A repeated field can be replaced with an `ARRAY` value.\n\n**Return type**\n\nType of `proto_expression`\n\n**Examples**\n\nThe following example uses protocol buffer messages `Book` and `BookDetails`. \n\n message Book {\n required string title = 1;\n repeated string reviews = 2;\n optional BookDetails details = 3;\n };\n\n message BookDetails {\n optional string author = 1;\n optional int32 chapters = 2;\n };\n\nThis statement replaces the values of the field `title` and subfield `chapters`\nof proto type `Book`. Note that field `details` must be set for the statement\nto succeed. \n\n SELECT REPLACE_FIELDS(\n NEW Book(\n \"The Hummingbird\" AS title,\n NEW BookDetails(10 AS chapters) AS details),\n \"The Hummingbird II\" AS title,\n 11 AS details.chapters)\n AS proto;\n\n /*-----------------------------------------------------------------------------*\n | proto |\n +-----------------------------------------------------------------------------+\n |{title: \"The Hummingbird II\" details: {chapters: 11 }} |\n *-----------------------------------------------------------------------------*/\n\nThe function can replace value of repeated fields. \n\n SELECT REPLACE_FIELDS(\n NEW Book(\"The Hummingbird\" AS title,\n NEW BookDetails(10 AS chapters) AS details),\n [\"A good read!\", \"Highly recommended.\"] AS reviews)\n AS proto;\n\n /*-----------------------------------------------------------------------------*\n | proto |\n +-----------------------------------------------------------------------------+\n |{title: \"The Hummingbird\" review: \"A good read\" review: \"Highly recommended.\"|\n | details: {chapters: 10 }} |\n *-----------------------------------------------------------------------------*/\n\nThe function can also set a field to `NULL`. \n\n SELECT REPLACE_FIELDS(\n NEW Book(\"The Hummingbird\" AS title,\n NEW BookDetails(10 AS chapters) AS details),\n NULL AS details)\n AS proto;\n\n /*-----------------------------------------------------------------------------*\n | proto |\n +-----------------------------------------------------------------------------+\n |{title: \"The Hummingbird\" } |\n *-----------------------------------------------------------------------------*/"]]