16
16
from deephaven .column import InputColumn , Column
17
17
from deephaven .dtypes import DType , Duration , Instant
18
18
from deephaven .execution_context import ExecutionContext
19
- from deephaven .jcompat import j_lambda
20
- from deephaven .jcompat import to_sequence
19
+ from deephaven .jcompat import j_lambda , j_list_to_list , to_sequence
21
20
from deephaven .table import Table
22
21
from deephaven .update_graph import auto_locking_ctx
23
22
24
23
_JTableFactory = jpy .get_type ("io.deephaven.engine.table.TableFactory" )
25
24
_JTableTools = jpy .get_type ("io.deephaven.engine.util.TableTools" )
26
25
_JDynamicTableWriter = jpy .get_type ("io.deephaven.engine.table.impl.util.DynamicTableWriter" )
26
+ _JBaseArrayBackedInputTable = jpy .get_type ("io.deephaven.engine.table.impl.util.BaseArrayBackedInputTable" )
27
27
_JAppendOnlyArrayBackedInputTable = jpy .get_type (
28
28
"io.deephaven.engine.table.impl.util.AppendOnlyArrayBackedInputTable" )
29
29
_JKeyedArrayBackedInputTable = jpy .get_type ("io.deephaven.engine.table.impl.util.KeyedArrayBackedInputTable" )
@@ -231,52 +231,20 @@ def write_row(self, *values: Any) -> None:
231
231
232
232
233
233
class InputTable (Table ):
234
- """InputTable is a subclass of Table that allows the users to dynamically add/delete/modify data in it. There are two
235
- types of InputTable - append-only and keyed.
234
+ """InputTable is a subclass of Table that allows the users to dynamically add/delete/modify data in it.
236
235
237
- The append-only input table is not keyed, all rows are added to the end of the table, and deletions and edits are
238
- not permitted.
239
-
240
- The keyed input tablet has keys for each row and supports addition/deletion/modification of rows by the keys.
236
+ Users should always create InputTables through factory methods rather than directly from the constructor.
241
237
"""
238
+ j_object_type = _JBaseArrayBackedInputTable
242
239
243
- def __init__ (self , col_defs : Dict [str , DType ] = None , init_table : Table = None ,
244
- key_cols : Union [str , Sequence [str ]] = None ):
245
- """Creates an InputTable instance from either column definitions or initial table. When key columns are
246
- provided, the InputTable will be keyed, otherwise it will be append-only.
247
-
248
- Args:
249
- col_defs (Dict[str, DType]): the column definitions
250
- init_table (Table): the initial table
251
- key_cols (Union[str, Sequence[str]): the name(s) of the key column(s)
252
-
253
- Raises:
254
- DHError
255
- """
256
- try :
257
- if col_defs is None and init_table is None :
258
- raise ValueError ("either column definitions or init table should be provided." )
259
- elif col_defs and init_table :
260
- raise ValueError ("both column definitions and init table are provided." )
261
-
262
- if col_defs :
263
- j_arg_1 = _JTableDefinition .of (
264
- [Column (name = n , data_type = t ).j_column_definition for n , t in col_defs .items ()])
265
- else :
266
- j_arg_1 = init_table .j_table
267
-
268
- key_cols = to_sequence (key_cols )
269
- if key_cols :
270
- super ().__init__ (_JKeyedArrayBackedInputTable .make (j_arg_1 , key_cols ))
271
- else :
272
- super ().__init__ (_JAppendOnlyArrayBackedInputTable .make (j_arg_1 ))
273
- self .j_input_table = self .j_table .getAttribute (_J_INPUT_TABLE_ATTRIBUTE )
274
- self .key_columns = key_cols
275
- except Exception as e :
276
- raise DHError (e , "failed to create a InputTable." ) from e
240
+ def __init__ (self , j_table : jpy .JType ):
241
+ super ().__init__ (j_table )
242
+ self .j_input_table = self .j_table .getAttribute (_J_INPUT_TABLE_ATTRIBUTE )
243
+ if not self .j_input_table :
244
+ raise DHError ("the provided table input is not suitable for input tables." )
277
245
278
246
def add (self , table : Table ) -> None :
279
- """Writes rows from the provided table to this input table. If this is a keyed input table, added rows with keys
247
+ """Synchronously writes rows from the provided table to this input table. If this is a keyed input table, added rows with keys
280
248
that match existing rows will replace those rows.
281
249
282
250
Args:
@@ -291,8 +259,8 @@ def add(self, table: Table) -> None:
291
259
raise DHError (e , "add to InputTable failed." ) from e
292
260
293
261
def delete (self , table : Table ) -> None :
294
- """Deletes the keys contained in the provided table from this keyed input table. If this method is called on an
295
- append-only input table, a PermissionError will be raised.
262
+ """Synchronously deletes the keys contained in the provided table from this keyed input table. If this method is called on an
263
+ append-only input table, an error will be raised.
296
264
297
265
Args:
298
266
table (Table): the table with the keys to delete
@@ -301,18 +269,33 @@ def delete(self, table: Table) -> None:
301
269
DHError
302
270
"""
303
271
try :
304
- if not self .key_columns :
305
- raise PermissionError ("deletion on an append-only input table is not allowed." )
306
272
self .j_input_table .delete (table .j_table )
307
273
except Exception as e :
308
274
raise DHError (e , "delete data in the InputTable failed." ) from e
309
275
276
+ @property
277
+ def key_names (self ) -> List [str ]:
278
+ """The names of the key columns of the InputTable."""
279
+ return j_list_to_list (self .j_input_table .getKeyNames ())
280
+
281
+ @property
282
+ def value_names (self ) -> List [str ]:
283
+ """The names of the value columns. By default, any column not marked as a key column is a value column."""
284
+ return j_list_to_list (self .j_input_table .getValueNames ())
285
+
310
286
311
287
def input_table (col_defs : Dict [str , DType ] = None , init_table : Table = None ,
312
288
key_cols : Union [str , Sequence [str ]] = None ) -> InputTable :
313
- """Creates an InputTable from either column definitions or initial table. When key columns are
289
+ """Creates an in-memory InputTable from either column definitions or an initial table. When key columns are
314
290
provided, the InputTable will be keyed, otherwise it will be append-only.
315
291
292
+ There are two types of in-memory InputTable - append-only and keyed.
293
+
294
+ The append-only input table is not keyed, all rows are added to the end of the table, and deletions and edits are
295
+ not permitted.
296
+
297
+ The keyed input table has keys for each row and supports addition/deletion/modification of rows by the keys.
298
+
316
299
Args:
317
300
col_defs (Dict[str, DType]): the column definitions
318
301
init_table (Table): the initial table
@@ -324,7 +307,28 @@ def input_table(col_defs: Dict[str, DType] = None, init_table: Table = None,
324
307
Raises:
325
308
DHError
326
309
"""
327
- return InputTable (col_defs = col_defs , init_table = init_table , key_cols = key_cols )
310
+
311
+ try :
312
+ if col_defs is None and init_table is None :
313
+ raise ValueError ("either column definitions or init table should be provided." )
314
+ elif col_defs and init_table :
315
+ raise ValueError ("both column definitions and init table are provided." )
316
+
317
+ if col_defs :
318
+ j_arg_1 = _JTableDefinition .of (
319
+ [Column (name = n , data_type = t ).j_column_definition for n , t in col_defs .items ()])
320
+ else :
321
+ j_arg_1 = init_table .j_table
322
+
323
+ key_cols = to_sequence (key_cols )
324
+ if key_cols :
325
+ j_table = _JKeyedArrayBackedInputTable .make (j_arg_1 , key_cols )
326
+ else :
327
+ j_table = _JAppendOnlyArrayBackedInputTable .make (j_arg_1 )
328
+ except Exception as e :
329
+ raise DHError (e , "failed to create an in-memory InputTable." ) from e
330
+
331
+ return InputTable (j_table )
328
332
329
333
330
334
def ring_table (parent : Table , capacity : int , initialize : bool = True ) -> Table :
0 commit comments