Append ARRAY with Data Chunk #204
-
Hi! I am trying to append float arrays by tweaking this code example, but without success... await connection.run(
`create or replace table target_table(i integer, v varchar)`
);
const appender = await connection.createAppender('target_table');
const chunk = DuckDBDataChunk.create([INTEGER, VARCHAR]);
chunk.setColumns([
[42, 123, 17],
['duck', 'mallad', 'goose'],
]);
// OR:
// chunk.setRows([
// [42, 'duck'],
// [123, 'mallard'],
// [17, 'goose'],
// ]);
appender.appendDataChunk(chunk);
appender.flushSync(); How would you insert a third column with this data: [ [1.23, 2.45, 3.45], [1.23, 2.45, 3.45], [1.23, 2.45, 3.45] ] Also, do you always have to create a new table, or can you add just another column to an existing table? Thank you! 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
To add a third column of type const chunk = DuckDBDataChunk.create([INTEGER, VARCHAR, ARRAY(FLOAT, 3)]);
chunk.setColumns([
[42, 123, 17],
['duck', 'mallad', 'goose'],
[ arrayValue([1.23, 2.45, 3.45]), arrayValue([1.23, 2.45, 3.45]), arrayValue([1.23, 2.45, 3.45]) ]
]); Note that you need to wrap the JS arrays with const chunk = DuckDBDataChunk.create([INTEGER, VARCHAR, LIST(FLOAT)]);
chunk.setColumns([
[42, 123, 17],
['duck', 'mallad', 'goose'],
[ listValue([1.23, 2.45, 3.45]), listValue([1.23, 2.45, 3.45]), listValue([1.23, 2.45, 3.45]) ]
]); |
Beta Was this translation helpful? Give feedback.
-
To your second question: You don't need to create a new table to use the appender. You just need to pass the name of a table that has the appropriate column types - those matching what you intend to append. |
Beta Was this translation helpful? Give feedback.
-
Thank you so much, @jraymakers ! It works! :) |
Beta Was this translation helpful? Give feedback.
To add a third column of type
FLOAT[3]
(i.e. ARRAY of FLOAT, length 3):Note that you need to wrap the JS arrays with
arrayValue
. This is because DuckDB supports multiple array-like data types. ARRAY values are for fixed-length, while LIST values are variable-length. To add a third column of typeFLOAT[]
(i.e. LIST of FLOAT, variable length), you'd do: