This is an automated email from the ASF dual-hosted git repository.
domoritz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 709c1d38a3 ARROW-16371: [JS] Fix error iterating tables with no
batches (#13287)
709c1d38a3 is described below
commit 709c1d38a3370725a10e0dd0287caecf3f6e42b3
Author: Paul Taylor <[email protected]>
AuthorDate: Wed Jun 1 11:31:46 2022 -0700
ARROW-16371: [JS] Fix error iterating tables with no batches (#13287)
Authored-by: ptaylor <[email protected]>
Signed-off-by: Dominik Moritz <[email protected]>
---
js/src/table.ts | 5 ++++-
js/src/vector.ts | 2 +-
js/test/unit/table-tests.ts | 4 ++++
3 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/js/src/table.ts b/js/src/table.ts
index 4f7dec182f..26f77d74f5 100644
--- a/js/src/table.ts
+++ b/js/src/table.ts
@@ -224,7 +224,10 @@ export class Table<T extends TypeMap = any> {
* Iterator for rows in this Table.
*/
public [Symbol.iterator]() {
- return iteratorVisitor.visit(new Vector(this.data)) as
IterableIterator<Struct<T>['TValue']>;
+ if (this.batches.length > 0) {
+ return iteratorVisitor.visit(new Vector(this.data)) as
IterableIterator<Struct<T>['TValue']>;
+ }
+ return (new Array(0))[Symbol.iterator]();
}
/**
diff --git a/js/src/vector.ts b/js/src/vector.ts
index a67b5527b3..362a141389 100644
--- a/js/src/vector.ts
+++ b/js/src/vector.ts
@@ -67,7 +67,7 @@ export class Vector<T extends DataType = any> {
const data: Data<T>[] = input[0] instanceof Vector
? (input as Vector<T>[]).flatMap(x => x.data)
: input as Data<T>[];
- if (data.some((x) => !(x instanceof Data))) {
+ if (data.length === 0 || data.some((x) => !(x instanceof Data))) {
throw new TypeError('Vector constructor expects an Array of Data
instances.');
}
const type = data[0]?.type;
diff --git a/js/test/unit/table-tests.ts b/js/test/unit/table-tests.ts
index 92159c1398..50c8565f0f 100644
--- a/js/test/unit/table-tests.ts
+++ b/js/test/unit/table-tests.ts
@@ -99,6 +99,10 @@ describe(`Table`, () => {
expect(new Table().numRows).toBe(0);
});
+ test(`empty table produces an empty iterator`, () => {
+ expect([...new Table()]).toHaveLength(0);
+ });
+
describe(`constructor`, () => {
test(`creates an empty Table with Columns`, () => {
let i32 = new Vector([makeData({ type: new Int32 })]);