from_protobuf function

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

Converts a binary Protobuf value into a STRUCT using the specified Protobuf message definition.

Syntax

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

Arguments

  • data: A BINARY expression containing a Protobuf message.
  • messageName: A constant STRING expression naming the Protobuf message to decode.
  • 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 STRUCT whose fields and types match the Protobuf message definition.

If data is NULL, the result is NULL.

The Protobuf message definition must match the schema of data. If the schemas do not match, the function can fail or return an arbitrary result.

Options

For a complete list of options, see Protobuf.

Examples

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

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

Decode a binary Protobuf value:

> SELECT from_protobuf(
    unhex('0A05416C696365102A'),
    'Person',
    '/path/to/person.desc');
  {"name":"Alice","id":42}