Skip to main content

to_protobuf function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 16.1 and above

Converts a STRUCT into a Protobuf-encoded BINARY value using the specified Protobuf message definition.

Syntax​

to_protobuf(data, messageName [, descFilePath [, options] ])
to_protobuf(data, messageName, options)

Arguments​

  • data: A STRUCT expression to encode.
  • messageName: A constant STRING expression naming the Protobuf message to encode.
  • descFilePath: An optional constant STRING path to a Protobuf descriptor file or a constant BINARY value containing the descriptor file. Generate the descriptor file with protoc --descriptor_set_out and --include_imports. Omit descFilePath only when options configures a Schema Registry.
  • options: An optional constant MAP<STRING, STRING> expression containing conversion options.

Returns​

A Protobuf-encoded BINARY value.

If data is NULL, the result is NULL.

The schema of data must match the specified Protobuf message definition.

Options​

For a complete list of options, see Protobuf.

Examples​

The following example uses a descriptor file generated from this Protobuf message:

Protobuf
message Person {
string name = 1;
int32 id = 2;
}

Encode a struct as a binary Protobuf value:

SQL
> SELECT hex(to_protobuf(
named_struct('name', 'Alice', 'id', 42),
'Person',
'/path/to/person.desc'));
0A05416C696365102A