Skip to content

Commit

Permalink
Deprecate json_spec col_defs List[Tuple[str, DType]]
Browse files Browse the repository at this point in the history
  • Loading branch information
devinrsmith committed Aug 15, 2024
1 parent 835c2fc commit 0b9103c
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions py/server/deephaven/stream/kafka/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from deephaven.dherror import DHError
from deephaven.dtypes import DType
from deephaven.jcompat import j_hashmap, j_properties, j_array_list
from deephaven.table import Table, PartitionedTable
from deephaven.table import Table, TableDefinition, TableDefinitionLike, PartitionedTable

_JKafkaTools = jpy.get_type("io.deephaven.kafka.KafkaTools")
_JKafkaTools_Consume = jpy.get_type("io.deephaven.kafka.KafkaTools$Consume")
Expand Down Expand Up @@ -427,13 +427,13 @@ def avro_spec(
raise DHError(e, "failed to create a Kafka key/value spec") from e


def json_spec(col_defs: Union[Dict[str, DType], List[Tuple[str, DType]]], mapping: Dict = None) -> KeyValueSpec:
def json_spec(col_defs: Union[TableDefinitionLike, List[Tuple[str, DType]]], mapping: Dict = None) -> KeyValueSpec:
"""Creates a spec for how to use JSON data when consuming a Kafka stream to a Deephaven table.
Args:
col_defs (Union[Dict[str, DType], List[Tuple[str, DType]]): the column definitions, either a map of column
names and Deephaven types, or a list of tuples with two elements, a string for column name and a Deephaven
type for column data type.
col_defs (Union[TableDefinitionLike, List[Tuple[str, DType]]): the table definition, preferably specified as
TableDefinitionLike. A list of tuples with two elements, a string for column name and a Deephaven type for
column data type also works, but is deprecated for removal.
mapping (Dict): a dict mapping JSON fields to column names defined in the col_defs
argument. Fields starting with a '/' character are interpreted as a JSON Pointer (see RFC 6901,
ISSN: 2070-1721 for details, essentially nested fields are represented like "/parent/nested").
Expand All @@ -448,9 +448,19 @@ def json_spec(col_defs: Union[Dict[str, DType], List[Tuple[str, DType]]], mappin
DHError
"""
try:
if isinstance(col_defs, dict):
col_defs = [col_def(k, v).j_column_definition for k, v in col_defs.items()]
try:
table_def = TableDefinition(col_defs)
except DHError:
table_def = None

if table_def:
col_defs = [col.j_column_definition for col in table_def.values()]
else:
warn(
'json_spec col_defs for List[Tuple[str, DType]] is deprecated for removal, prefer TableDefinitionLike',
DeprecationWarning,
stacklevel=2,
)
col_defs = [col_def(*t).j_column_definition for t in col_defs]

if mapping is None:
Expand Down

0 comments on commit 0b9103c

Please sign in to comment.