|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect } from 'chai'; |
| 19 | +import { evaluateToResult, evaluateToValue, expectEqual } from './utils'; |
| 20 | +import { |
| 21 | + arrayContains, |
| 22 | + arrayContainsAll, |
| 23 | + arrayContainsAny, |
| 24 | + arrayLength, |
| 25 | + BooleanExpr, |
| 26 | + constant, |
| 27 | + field, |
| 28 | + not |
| 29 | +} from '../../../../src/lite-api/expressions'; |
| 30 | +import { constantArray, constantMap } from '../../../util/pipelines'; |
| 31 | +import { |
| 32 | + FALSE_VALUE, |
| 33 | + MIN_VALUE, |
| 34 | + TRUE_VALUE |
| 35 | +} from '../../../../src/model/values'; |
| 36 | +import { EvaluateResult } from '../../../../src/core/expressions'; |
| 37 | +import { VectorValue } from '../../../../src'; |
| 38 | + |
| 39 | +describe('Array Expressions', () => { |
| 40 | + describe('arrayContainsAll', () => { |
| 41 | + it('containsAll', () => { |
| 42 | + expect( |
| 43 | + evaluateToValue( |
| 44 | + arrayContainsAll( |
| 45 | + constantArray([ |
| 46 | + '1', |
| 47 | + 42, |
| 48 | + true, |
| 49 | + 'additional', |
| 50 | + 'values', |
| 51 | + 'in', |
| 52 | + 'array' |
| 53 | + ]), |
| 54 | + [constant('1'), constant(42), constant(true)] |
| 55 | + ) |
| 56 | + ) |
| 57 | + ).to.deep.equal(TRUE_VALUE); |
| 58 | + }); |
| 59 | + |
| 60 | + it('doesNotContainAll', () => { |
| 61 | + expect( |
| 62 | + evaluateToValue( |
| 63 | + arrayContainsAll(constantArray(['1', 42, true]), [ |
| 64 | + constant('1'), |
| 65 | + constant(99) |
| 66 | + ]) |
| 67 | + ) |
| 68 | + ).to.deep.equal(FALSE_VALUE); |
| 69 | + }); |
| 70 | + |
| 71 | + it('equivalentNumerics', () => { |
| 72 | + expect( |
| 73 | + evaluateToValue( |
| 74 | + arrayContainsAll( |
| 75 | + constantArray([42, true, 'additional', 'values', 'in', 'array']), |
| 76 | + [constant(42.0), constant(true)] |
| 77 | + ) |
| 78 | + ) |
| 79 | + ).to.deep.equal(TRUE_VALUE); |
| 80 | + }); |
| 81 | + |
| 82 | + it('arrayToSearch_isEmpty', () => { |
| 83 | + expect( |
| 84 | + evaluateToValue( |
| 85 | + arrayContainsAll(constantArray([]), [constant(42.0), constant(true)]) |
| 86 | + ) |
| 87 | + ).to.deep.equal(FALSE_VALUE); |
| 88 | + }); |
| 89 | + |
| 90 | + it('searchValue_isEmpty', () => { |
| 91 | + expect( |
| 92 | + evaluateToValue(arrayContainsAll(constantArray([42.0, true]), [])) |
| 93 | + ).to.deep.equal(TRUE_VALUE); |
| 94 | + }); |
| 95 | + |
| 96 | + it('searchValue_isNaN', () => { |
| 97 | + expect( |
| 98 | + evaluateToValue( |
| 99 | + arrayContainsAll(constantArray([NaN, 42.0]), [constant(NaN)]) |
| 100 | + ) |
| 101 | + ).to.deep.equal(FALSE_VALUE); |
| 102 | + }); |
| 103 | + |
| 104 | + it('searchValue_hasDuplicates', () => { |
| 105 | + expect( |
| 106 | + evaluateToValue( |
| 107 | + arrayContainsAll(constantArray([true, 'hi']), [ |
| 108 | + constant(true), |
| 109 | + constant(true), |
| 110 | + constant(true) |
| 111 | + ]) |
| 112 | + ) |
| 113 | + ).to.deep.equal(TRUE_VALUE); |
| 114 | + }); |
| 115 | + |
| 116 | + it('arrayToSearch_isEmpty_searchValue_isEmpty', () => { |
| 117 | + expect( |
| 118 | + evaluateToValue(arrayContainsAll(constantArray([]), [])) |
| 119 | + ).to.deep.equal(TRUE_VALUE); |
| 120 | + }); |
| 121 | + |
| 122 | + it('largeNumberOfElements', () => { |
| 123 | + const elements = Array.from({ length: 500 }, (_, i) => i + 1); |
| 124 | + expect( |
| 125 | + evaluateToValue( |
| 126 | + arrayContainsAll( |
| 127 | + constantArray(elements), |
| 128 | + elements.map(e => constant(e)) |
| 129 | + ) |
| 130 | + ) |
| 131 | + ).to.deep.equal(TRUE_VALUE); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('arrayContainsAny', () => { |
| 136 | + const ARRAY_TO_SEARCH = constantArray([42, 'matang', true]); |
| 137 | + const SEARCH_VALUES = [constant('matang'), constant(false)]; |
| 138 | + |
| 139 | + it('valueFoundInArray', () => { |
| 140 | + expect( |
| 141 | + evaluateToValue(arrayContainsAny(ARRAY_TO_SEARCH, SEARCH_VALUES)) |
| 142 | + ).to.deep.equal(TRUE_VALUE); |
| 143 | + }); |
| 144 | + |
| 145 | + it('equivalentNumerics', () => { |
| 146 | + expect( |
| 147 | + evaluateToValue( |
| 148 | + arrayContainsAny(ARRAY_TO_SEARCH, [constant(42.0), constant(2)]) |
| 149 | + ) |
| 150 | + ).to.deep.equal(TRUE_VALUE); |
| 151 | + }); |
| 152 | + |
| 153 | + it('valuesNotFoundInArray', () => { |
| 154 | + expect( |
| 155 | + evaluateToValue( |
| 156 | + arrayContainsAny(ARRAY_TO_SEARCH, [constant(99), constant('false')]) |
| 157 | + ) |
| 158 | + ).to.deep.equal(FALSE_VALUE); |
| 159 | + }); |
| 160 | + |
| 161 | + // TODO(pipeline): Nested arrays are not supported in documents. We need to |
| 162 | + // support creating nested arrays as expressions however. |
| 163 | + it.skip('bothInputTypeIsArray', () => { |
| 164 | + expect( |
| 165 | + evaluateToValue( |
| 166 | + arrayContainsAny( |
| 167 | + constantArray([ |
| 168 | + [1, 2, 3], |
| 169 | + [4, 5, 6], |
| 170 | + [7, 8, 9] |
| 171 | + ]), |
| 172 | + [constantArray([1, 2, 3]), constantArray([4, 5, 6])] |
| 173 | + ) |
| 174 | + ) |
| 175 | + ).to.deep.equal(TRUE_VALUE); |
| 176 | + }); |
| 177 | + |
| 178 | + it('search_isNull_returnsNull', () => { |
| 179 | + expect( |
| 180 | + evaluateToResult( |
| 181 | + arrayContainsAny(constantArray([null, 1, 'matang', true]), [ |
| 182 | + constant(null) |
| 183 | + ]) |
| 184 | + ) |
| 185 | + ).to.deep.equal(EvaluateResult.newNull()); |
| 186 | + }); |
| 187 | + |
| 188 | + it('array_isNotArrayType_returnsError', () => { |
| 189 | + expect( |
| 190 | + evaluateToValue(arrayContainsAny(constant('matang'), SEARCH_VALUES)) |
| 191 | + ).to.be.undefined; |
| 192 | + }); |
| 193 | + |
| 194 | + it('search_isNotArrayType_returnsError', () => { |
| 195 | + expect( |
| 196 | + evaluateToValue( |
| 197 | + arrayContainsAny(constant('values'), [constant('values')]) |
| 198 | + ) |
| 199 | + ).to.be.undefined; |
| 200 | + }); |
| 201 | + |
| 202 | + it('array_notFound_returnsError', () => { |
| 203 | + expect( |
| 204 | + evaluateToValue(arrayContainsAny(field('not-exist'), SEARCH_VALUES)) |
| 205 | + ).to.be.undefined; |
| 206 | + }); |
| 207 | + |
| 208 | + it('searchNotFound_returnsError', () => { |
| 209 | + expect( |
| 210 | + evaluateToValue(arrayContainsAny(ARRAY_TO_SEARCH, [field('not-exist')])) |
| 211 | + ).to.be.undefined; |
| 212 | + }); |
| 213 | + }); // end describe('arrayContainsAny') |
| 214 | + |
| 215 | + describe('arrayContains', () => { |
| 216 | + const ARRAY_TO_SEARCH = constantArray([42, 'matang', true]); |
| 217 | + |
| 218 | + it('valueFoundInArray', () => { |
| 219 | + expect( |
| 220 | + evaluateToValue( |
| 221 | + arrayContains(constantArray(['hello', 'world']), constant('hello')) |
| 222 | + ) |
| 223 | + ).to.deep.equal(TRUE_VALUE); |
| 224 | + }); |
| 225 | + |
| 226 | + it('valueNotFoundInArray', () => { |
| 227 | + expect( |
| 228 | + evaluateToValue(arrayContains(ARRAY_TO_SEARCH, constant(4))) |
| 229 | + ).to.deep.equal(FALSE_VALUE); |
| 230 | + }); |
| 231 | + |
| 232 | + it('notArrayContainsFunction_valueNotFoundInArray', () => { |
| 233 | + const child = arrayContains(ARRAY_TO_SEARCH, constant(4)); |
| 234 | + const f = not(child as BooleanExpr); |
| 235 | + expect(evaluateToValue(f)).to.deep.equal(TRUE_VALUE); |
| 236 | + }); |
| 237 | + |
| 238 | + it('equivalentNumerics', () => { |
| 239 | + expect( |
| 240 | + evaluateToValue(arrayContains(ARRAY_TO_SEARCH, constant(42.0))) |
| 241 | + ).to.deep.equal(TRUE_VALUE); |
| 242 | + }); |
| 243 | + |
| 244 | + // TODO(pipeline): Nested arrays are not supported in documents. We need to |
| 245 | + // support creating nested arrays as expressions however. |
| 246 | + it.skip('bothInputTypeIsArray', () => { |
| 247 | + expect( |
| 248 | + evaluateToValue( |
| 249 | + arrayContains( |
| 250 | + constantArray([ |
| 251 | + [1, 2, 3], |
| 252 | + [4, 5, 6], |
| 253 | + [7, 8, 9] |
| 254 | + ]), |
| 255 | + constantArray([1, 2, 3]) |
| 256 | + ) |
| 257 | + ) |
| 258 | + ).to.deep.equal(TRUE_VALUE); |
| 259 | + }); |
| 260 | + |
| 261 | + it('searchValue_isNull_returnsNull', () => { |
| 262 | + expect( |
| 263 | + evaluateToValue( |
| 264 | + arrayContains( |
| 265 | + constantArray([null, 1, 'matang', true]), |
| 266 | + constant(null) |
| 267 | + ) |
| 268 | + ) |
| 269 | + ).to.deep.equal(MIN_VALUE); |
| 270 | + }); |
| 271 | + |
| 272 | + it('searchValue_isNull_emptyValuesArray_returnsNull', () => { |
| 273 | + expect( |
| 274 | + evaluateToValue(arrayContains(constantArray([]), constant(null))) |
| 275 | + ).to.deep.equal(MIN_VALUE); |
| 276 | + }); |
| 277 | + |
| 278 | + it('searchValue_isMap', () => { |
| 279 | + expect( |
| 280 | + evaluateToValue( |
| 281 | + arrayContains( |
| 282 | + constantArray([123, { foo: 123 }, { bar: 42 }, { foo: 42 }]), |
| 283 | + constantMap({ foo: 42 }) |
| 284 | + ) |
| 285 | + ) |
| 286 | + ).to.deep.equal(TRUE_VALUE); |
| 287 | + }); |
| 288 | + |
| 289 | + it('searchValue_isNaN', () => { |
| 290 | + expect( |
| 291 | + evaluateToValue( |
| 292 | + arrayContains(constantArray([NaN, 'foo']), constant(NaN)) |
| 293 | + ) |
| 294 | + ).to.deep.equal(FALSE_VALUE); |
| 295 | + }); |
| 296 | + |
| 297 | + it('arrayToSearch_isNotArrayType_returnsError', () => { |
| 298 | + expect( |
| 299 | + evaluateToValue(arrayContains(constant('matang'), constant('values'))) |
| 300 | + ).to.be.undefined; |
| 301 | + }); |
| 302 | + |
| 303 | + it('arrayToSearch_notFound_returnsError', () => { |
| 304 | + expect( |
| 305 | + evaluateToValue(arrayContains(field('not-exist'), constant('matang'))) |
| 306 | + ).to.be.undefined; |
| 307 | + }); |
| 308 | + |
| 309 | + it('arrayToSearch_isEmpty_returnsFalse', () => { |
| 310 | + expect( |
| 311 | + evaluateToValue(arrayContains(constantArray([]), constant('matang'))) |
| 312 | + ).to.deep.equal(FALSE_VALUE); |
| 313 | + }); |
| 314 | + |
| 315 | + it('searchValue_reference_notFound_returnsError', () => { |
| 316 | + expect( |
| 317 | + evaluateToValue(arrayContains(ARRAY_TO_SEARCH, field('not-exist'))) |
| 318 | + ).to.be.undefined; |
| 319 | + }); |
| 320 | + }); // end describe('arrayContains') |
| 321 | + |
| 322 | + describe('arrayLength', () => { |
| 323 | + it('length', () => { |
| 324 | + expectEqual( |
| 325 | + evaluateToValue(arrayLength(constantArray(['1', 42, true]))), |
| 326 | + constant(3), |
| 327 | + `arrayLength(['1', 42, true])` |
| 328 | + ); |
| 329 | + }); |
| 330 | + |
| 331 | + it('emptyArray', () => { |
| 332 | + expectEqual( |
| 333 | + evaluateToValue(arrayLength(constantArray([]))), |
| 334 | + constant(0), |
| 335 | + `arrayLength([])` |
| 336 | + ); |
| 337 | + }); |
| 338 | + |
| 339 | + it('arrayWithDuplicateElements', () => { |
| 340 | + expectEqual( |
| 341 | + evaluateToValue(arrayLength(constantArray([true, true]))), |
| 342 | + constant(2), |
| 343 | + `arrayLength([true, true])` |
| 344 | + ); |
| 345 | + }); |
| 346 | + |
| 347 | + it('notArrayType_returnsError', () => { |
| 348 | + expect( |
| 349 | + evaluateToValue(arrayLength(constant(new VectorValue([0.0, 1.0])))) |
| 350 | + ).to.be.undefined; // Assuming double[] is not considered an array |
| 351 | + expect(evaluateToValue(arrayLength(constant('notAnArray')))).to.be |
| 352 | + .undefined; |
| 353 | + }); |
| 354 | + }); // end describe('arrayLength') |
| 355 | +}); |
0 commit comments