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
-
Enable Redpanda SQL on your Redpanda Bring Your Own Cloud (BYOC) cluster.
-
Connect to Redpanda SQL with
psqlor another PostgreSQL client. -
Have a Redpanda Cloud user with the SQL: Manage data-plane RBAC permission, which
CREATE TABLErequires. See Manage access to Redpanda SQL. -
Register a Protobuf schema for the topic in Schema Registry.
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.Durationmaps tointerval, andgoogle.protobuf.Int64Valuemaps tobigint. -
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.Durationmaps to a user-defined type withsecondsandnanosfields. When your cluster has an Iceberg REST catalog configured, Redpanda Cloud linksdefault_redpanda_catalogto it, so this mapping applies to every table indefault_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 |
|---|---|---|
|
||
|
|
|
|
|
|
|
|
A user-defined type with one |
|
|
|
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
A user-defined type with the message’s fields, such as |
|
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
repeatedfield becomes an array of the mapped type, such asjson[]orinterval[]. -
With
COMPOUND, amapfield becomes amapcolumn with values of the mapped type. For example,map<string, google.protobuf.Value>becomesmap(text,json), andmap<int32, google.protobuf.Duration>becomesmap(integer,interval). WithJSON, amapfield becomes a singlejsoncolumn, 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 withJSON.
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;
}
-
Map the topic as a SQL table:
CREATE TABLE default_redpanda_catalog=>events WITH ( topic = 'events', schema_subject = 'events-value' );The
payloadcolumn has thejsontype, and theattributescolumn has themap(text,json)type. -
Query the contents of the
jsoncolumn 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;
}
-
Map the topic as a SQL table:
CREATE TABLE default_redpanda_catalog=>tasks WITH ( topic = 'tasks', schema_subject = 'tasks-value' ); -
Run
DESCRIBE TABLE default_redpanda_catalog⇒tasksto see the column types. The types depend on the catalog:Column Catalog not linked to Iceberg Catalog linked to Iceberg elapsedintervalgoogle_protobuf_Durationretriesbigintgoogle_protobuf_Int64Valueupdated_fieldstextgoogle_protobuf_FieldMaskdetailgoogle_protobuf_Anygoogle_protobuf_Anydonegoogle_protobuf_Emptygoogle_protobuf_Empty -
Query the columns.
In a Redpanda catalog that isn’t linked to an Iceberg catalog, query
interval,bigint, andtextcolumns directly. To access the fields of agoogle.protobuf.Any, use the(column).fieldform: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).fieldform: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
-
CREATE TABLE: full reference for mapping a topic as a SQL table, including
struct_mapping_policyanderror_handling_policy. -
Query Iceberg-enabled Topics: query live records and Iceberg history together.
-
Query Topics with Nested Fields: map nested messages as user-defined types.
-
JSON: the
jsondata type and its operators.