Skip to content
This repository was archived by the owner on Mar 2, 2026. It is now read-only.

Commit d1e3da8

Browse files
committed
added array_get
1 parent 9a35dfe commit d1e3da8

3 files changed

Lines changed: 105 additions & 0 deletions

File tree

google/cloud/firestore_v1/pipeline_expressions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,25 @@ def not_equal_any(
629629
],
630630
)
631631

632+
@expose_as_static
633+
def array_get(self, offset: Expression | int) -> "Function":
634+
"""
635+
Creates an expression that indexes into an array from the beginning or end and returns the
636+
element. A negative offset starts from the end.
637+
638+
Example:
639+
>>> Array([1,2,3]).array_get(0)
640+
641+
Args:
642+
offset: the index of the element to return
643+
644+
Returns:
645+
A new `Expression` representing the `array_get` operation.
646+
"""
647+
return Function(
648+
"array_get", [self, self._cast_to_expr_or_convert_to_constant(offset)]
649+
)
650+
632651
@expose_as_static
633652
def array_contains(
634653
self, element: Expression | CONSTANT_TYPE

tests/system/pipeline_e2e/array.yaml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,79 @@ tests:
386386
name: array
387387
name: array_concat
388388
name: select
389+
- description: testArrayGet
390+
pipeline:
391+
- Collection: books
392+
- Where:
393+
- Function.equal:
394+
- Field: title
395+
- Constant: "The Hitchhiker's Guide to the Galaxy"
396+
- Select:
397+
- AliasedExpression:
398+
- Function.array_get:
399+
- Field: tags
400+
- Constant: 0
401+
- "firstTag"
402+
assert_results:
403+
- firstTag: "comedy"
404+
assert_proto:
405+
pipeline:
406+
stages:
407+
- args:
408+
- referenceValue: /books
409+
name: collection
410+
- args:
411+
- functionValue:
412+
args:
413+
- fieldReferenceValue: title
414+
- stringValue: "The Hitchhiker's Guide to the Galaxy"
415+
name: equal
416+
name: where
417+
- args:
418+
- mapValue:
419+
fields:
420+
firstTag:
421+
functionValue:
422+
args:
423+
- fieldReferenceValue: tags
424+
- integerValue: '0'
425+
name: array_get
426+
name: select
427+
- description: testArrayGet_NegativeOffset
428+
pipeline:
429+
- Collection: books
430+
- Where:
431+
- Function.equal:
432+
- Field: title
433+
- Constant: "The Hitchhiker's Guide to the Galaxy"
434+
- Select:
435+
- AliasedExpression:
436+
- Function.array_get:
437+
- Field: tags
438+
- Constant: -1
439+
- "lastTag"
440+
assert_results:
441+
- lastTag: "adventure"
442+
assert_proto:
443+
pipeline:
444+
stages:
445+
- args:
446+
- referenceValue: /books
447+
name: collection
448+
- args:
449+
- functionValue:
450+
args:
451+
- fieldReferenceValue: title
452+
- stringValue: "The Hitchhiker's Guide to the Galaxy"
453+
name: equal
454+
name: where
455+
- args:
456+
- mapValue:
457+
fields:
458+
lastTag:
459+
functionValue:
460+
args:
461+
- fieldReferenceValue: tags
462+
- integerValue: '-1'
463+
name: array_get
464+
name: select

tests/unit/v1/test_pipeline_expressions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,16 @@ def test_or(self):
706706
assert instance.params == [arg1, arg2]
707707
assert repr(instance) == "Or(Arg1, Arg2)"
708708

709+
def test_array_get(self):
710+
arg1 = self._make_arg("ArrayField")
711+
arg2 = self._make_arg("Offset")
712+
instance = Expression.array_get(arg1, arg2)
713+
assert instance.name == "array_get"
714+
assert instance.params == [arg1, arg2]
715+
assert repr(instance) == "ArrayField.array_get(Offset)"
716+
infix_istance = arg1.array_get(arg2)
717+
assert infix_istance == instance
718+
709719
def test_array_contains(self):
710720
arg1 = self._make_arg("ArrayField")
711721
arg2 = self._make_arg("Element")

0 commit comments

Comments
 (0)