Cloud

Query Topics with Protobuf Well-Known Types

Protobuf well-known types are the message types that Protobuf defines in google/protobuf/*.proto, such as google.protobuf.Timestamp and google.protobuf.Struct. Redpanda SQL supports every well-known type in a topic's schema. Each one maps to a SQL type that depends on whether the Redpanda catalog is linked to an Iceberg catalog, and on the table’s struct_mapping_policy.

After reading this page, you will be able to:

  • Identify the SQL type that Redpanda SQL maps each Protobuf well-known type to

  • Query google.protobuf.Struct, google.protobuf.Value, and google.protobuf.ListValue fields as JSON

  • Query google.protobuf.Duration, wrapper, google.protobuf.Any, and google.protobuf.Empty fields

Prerequisites

How well-known types map to SQL types

Redpanda SQL maps well-known types in one of two ways, depending on the Redpanda catalog that you create the table in:

  • Redpanda catalog not linked to an Iceberg catalog: Well-known types map to native SQL types where SQL has one. For example, google.protobuf.Duration maps to interval, and google.protobuf.Int64Value maps to bigint.

  • Redpanda catalog linked to an Iceberg catalog: Well-known types map to the same shape that Redpanda writes to the Iceberg table, so records from the topic and from the Iceberg table have the same columns. For example, google.protobuf.Duration maps to a user-defined type with seconds and nanos fields. When your cluster has an Iceberg REST catalog configured, Redpanda Cloud links default_redpanda_catalog to it, so this mapping applies to every table in default_redpanda_catalog, including tables for topics that aren’t Iceberg-enabled. See How the query catalogs are set up.

The following table shows the mapping with struct_mapping_policy = 'COMPOUND' (the default):

Well-known type Catalog not linked to Iceberg Catalog linked to Iceberg

google.protobuf.Timestamp

timestamp

timestamptz

google.protobuf.Struct, google.protobuf.Value, google.protobuf.ListValue

json

json

google.protobuf.Duration

interval

google_protobuf_Duration (seconds bigint, nanos integer)

google.protobuf.DoubleValue, google.protobuf.FloatValue

double precision, real

A user-defined type with one value field of the type listed for a catalog not linked to Iceberg, such as google_protobuf_Int64Value (value bigint)

google.protobuf.Int64Value, google.protobuf.Int32Value

bigint, integer

google.protobuf.UInt64Value

text

google.protobuf.UInt32Value

bigint

google.protobuf.BoolValue

boolean

google.protobuf.StringValue

text

google.protobuf.BytesValue

bytea

google.protobuf.FieldMask

text, with the paths separated by commas, such as name,address.city

google_protobuf_FieldMask (paths text[])

google.protobuf.Any

google_protobuf_Any (type_url text, value bytea)

google.protobuf.Empty

google_protobuf_Empty, a user-defined type without fields. See Query google.protobuf.Empty fields.

google.protobuf.Api, google.protobuf.Type, and the other messages in google/protobuf/api.proto, google/protobuf/type.proto, and google/protobuf/source_context.proto

A user-defined type with the message’s fields, such as google_protobuf_SourceContext (file_name text)

Redpanda SQL names the user-defined type for a well-known type after its full Protobuf name, with dots replaced by underscores, such as google_protobuf_Duration. A message in your own schema with the same short name, such as com.example.Duration, doesn’t conflict with it.

With struct_mapping_policy = 'JSON', each well-known type that maps to a user-defined type with COMPOUND maps to a json column instead. The other well-known types map as with COMPOUND. A google.protobuf.Any becomes a JSON object with type_url and value keys, where value holds the packed message’s bytes in base64, such as {"type_url": "type.googleapis.com/com.example.Detail", "value": "CgNhYmM="}.

A well-known type maps the same way wherever it appears in a schema:

  • A repeated field becomes an array of the mapped type, such as json[] or interval[].

  • With COMPOUND, a map field becomes a map column with values of the mapped type. For example, map<string, google.protobuf.Value> becomes map(text,json), and map<int32, google.protobuf.Duration> becomes map(integer,interval). With JSON, a map field becomes a single json column, as for any other map.

  • A field inside a nested message becomes a field of that message’s user-defined type with COMPOUND, or part of the message’s JSON document with JSON.

A field that maps to a scalar SQL type reads as SQL NULL when the field is absent from a record. For example, in a catalog that isn’t linked to Iceberg, wrapper types and google.protobuf.Duration map to scalar types.

You don’t need to register the google/protobuf/*.proto files as schema references in Schema Registry. Redpanda SQL resolves these imports itself.

Query JSON fields

Redpanda SQL maps google.protobuf.Struct, google.protobuf.Value, and google.protobuf.ListValue fields to json values that hold the value in its ProtoJSON form. A google.protobuf.Value that is present but has no kind set reads as the JSON null value. A field that is absent from a record reads as SQL NULL.

This example uses an events topic. Its value schema has google.protobuf.Value fields:

syntax = "proto3";

package com.example.events;

import "google/protobuf/struct.proto";

message Event {
  string id = 1;
  google.protobuf.Value payload = 2;
  map<string, google.protobuf.Value> attributes = 3;
}
  1. Map the topic as a SQL table:

    CREATE TABLE default_redpanda_catalog=>events
    WITH (
      topic = 'events',
      schema_subject = 'events-value'
    );

    The payload column has the json type, and the attributes column has the map(text,json) type.

  2. Query the contents of the json column with the JSON operators or JSON functions:

    SELECT id, payload->>'status' AS status
    FROM default_redpanda_catalog=>events;

Query other well-known types

This example uses a tasks topic. Its value schema has fields of several other well-known types:

syntax = "proto3";

package com.example.tasks;

import "google/protobuf/any.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/field_mask.proto";
import "google/protobuf/wrappers.proto";

message Task {
  string id = 1;
  google.protobuf.Duration elapsed = 2;
  google.protobuf.Int64Value retries = 3;
  google.protobuf.FieldMask updated_fields = 4;
  google.protobuf.Any detail = 5;
  google.protobuf.Empty done = 6;
}
  1. Map the topic as a SQL table:

    CREATE TABLE default_redpanda_catalog=>tasks
    WITH (
      topic = 'tasks',
      schema_subject = 'tasks-value'
    );
  2. Run DESCRIBE TABLE default_redpanda_catalog⇒tasks to see the column types. The types depend on the catalog:

    Column Catalog not linked to Iceberg Catalog linked to Iceberg

    elapsed

    interval

    google_protobuf_Duration

    retries

    bigint

    google_protobuf_Int64Value

    updated_fields

    text

    google_protobuf_FieldMask

    detail

    google_protobuf_Any

    google_protobuf_Any

    done

    google_protobuf_Empty

    google_protobuf_Empty

  3. Query the columns.

    In a Redpanda catalog that isn’t linked to an Iceberg catalog, query interval, bigint, and text columns directly. To access the fields of a google.protobuf.Any, use the (column).field form:

    SELECT id, elapsed, retries, (detail).type_url
    FROM default_redpanda_catalog=>tasks
    WHERE elapsed > INTERVAL '5 seconds';

    In a Redpanda catalog that is linked to an Iceberg catalog, access the fields of each user-defined type with the (column).field form:

    SELECT id, (elapsed).seconds, (elapsed).nanos, (retries).value, (updated_fields).paths, (detail).type_url
    FROM default_redpanda_catalog=>tasks
    WHERE (elapsed).seconds > 5;

The value field of a google.protobuf.Any holds the packed message’s serialized bytes. Redpanda SQL doesn’t decode the packed message, even when the topic’s schema defines its type.

A google.protobuf.Duration or google.protobuf.Timestamp value outside the range that Protobuf allows fails to decode, and the table’s error_handling_policy applies to the record.

Query google.protobuf.Empty fields

A google.protobuf.Empty field that is set in a record reads as an empty row, (). A field that is absent reads as SQL NULL. As in PostgreSQL, a row is NULL when all its fields are NULL. So IS NULL is true for an Empty field, whether or not the field is set. To find the records where the field is set, use IS NOT NULL:

SELECT id
FROM default_redpanda_catalog=>tasks
WHERE done IS NOT NULL;

count(done) and done::text also tell a set field (()) from an absent one (NULL).

Change the type of a field

Redpanda SQL decodes every record in the topic with the latest schema version registered for the subject. If you change the type of an existing field while keeping its field number, for example from google.protobuf.Value to string, int64, or google.protobuf.Struct, records produced before the change no longer decode correctly. Some of those records fail to decode, and the query fails. Others decode into incorrect values without an error. error_handling_policy applies only to records that fail to decode, so DROP_RECORD and FILL_NULL don’t remove the incorrect values from query results.

To change a field’s type, add a new field and reserve the old field number instead. For example:

syntax = "proto3";

package com.example.events;

message Event {
  string id = 1;
  reserved 2;              // was: google.protobuf.Value payload = 2;
  string payload_json = 3; // the same value, in ProtoJSON form
}

This change is backward compatible, so Schema Registry accepts it under the default compatibility level. For records produced before the change, the new field contains an empty string, because those records don’t include it.

Iceberg-enabled topics

For an Iceberg-enabled topic, Redpanda writes each well-known type to the Iceberg table with the shape listed for a catalog linked to Iceberg in How well-known types map to SQL types. google.protobuf.Struct, google.protobuf.Value, and google.protobuf.ListValue fields are JSON text in the Iceberg table. When you query an Iceberg-enabled topic through its SQL table, Redpanda SQL reads these fields as json. This applies to records from both the Iceberg table and the topic. As a result, a record returns the same values from either source. If you query the Iceberg table directly through the Iceberg catalog, these fields are text columns, and map fields are map columns with text values.

google.protobuf.Empty is the exception, because the Iceberg table can’t store an empty row. A set Empty field reads as () for records from the topic. For records from the Iceberg table, it reads as NULL.

A map field whose values are google.protobuf.Empty, such as map<string, google.protobuf.Empty>, can’t be read from the Iceberg table. Queries that read records from the Iceberg table fail with an error similar to Parquet non-leaf node has no children.

Schemas that redefine a well-known type

Redpanda SQL recognizes a well-known type by its full name. If a topic’s schema defines its own message with the full name of a well-known type, such as google.protobuf.Duration, but with different fields, CREATE TABLE fails with an error similar to:

Failed to build proto file descriptor 'schema_1.proto': it defines google.protobuf.Duration with fields other than the well-known type's

Import the well-known type from its google/protobuf/*.proto file instead, or give your message a different name.

Suggested reading