diff --git a/core/src/doc/index.rs b/core/src/doc/index.rs index 221cde4b..5ccd9882 100644 --- a/core/src/doc/index.rs +++ b/core/src/doc/index.rs @@ -132,11 +132,7 @@ impl Combinator { if !f { // Iterator for not flattened values if let Value::Array(v) = v { - iterators.push(Box::new(MultiValuesIterator { - vals: v.0, - done: false, - current: 0, - })); + iterators.push(Box::new(MultiValuesIterator::new(v.0))); continue; } } @@ -182,6 +178,28 @@ struct MultiValuesIterator { vals: Vec, done: bool, current: usize, + end: usize, +} + +impl MultiValuesIterator { + fn new(vals: Vec) -> Self { + let len = vals.len(); + if len == 0 { + Self { + vals, + done: true, + current: 0, + end: 0, + } + } else { + Self { + vals, + done: false, + current: 0, + end: len - 1, + } + } + } } impl ValuesIterator for MultiValuesIterator { @@ -189,7 +207,7 @@ impl ValuesIterator for MultiValuesIterator { if self.done { return false; } - if self.current == self.vals.len() - 1 { + if self.current == self.end { self.done = true; return false; } diff --git a/lib/tests/define.rs b/lib/tests/define.rs index 810b7b6d..0115bd23 100644 --- a/lib/tests/define.rs +++ b/lib/tests/define.rs @@ -2672,3 +2672,23 @@ async fn define_table_relation() -> Result<(), Error> { // Ok(()) } + +#[tokio::test] +async fn define_statement_index_empty_array() -> Result<(), Error> { + let sql = r" + DEFINE TABLE indexTest; + INSERT INTO indexTest { arr: [] }; + DEFINE INDEX idx_arr ON TABLE indexTest COLUMNS arr; + "; + let dbs = new_ds().await?; + let ses = Session::owner().with_ns("test").with_db("test"); + let res = &mut dbs.execute(sql, &ses, None).await?; + assert_eq!(res.len(), 3); + // + for _ in 0..3 { + let tmp = res.remove(0).result; + assert!(tmp.is_ok()); + } + // + Ok(()) +}