-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathapi.py
More file actions
691 lines (586 loc) · 24.3 KB
/
api.py
File metadata and controls
691 lines (586 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# Copyright 2021-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import multiprocessing
import warnings
from concurrent.futures import ThreadPoolExecutor
from decimal import Decimal
from typing import Literal
import numpy as np
try:
import pandas as pd
except ImportError:
pd = None
try:
import polars as pl
except ImportError:
pl = None
import pyarrow as pa
import pymongo.errors
from bson import encode
from bson.codec_options import TypeEncoder, TypeRegistry
from bson.decimal128 import Decimal128
from bson.raw_bson import RawBSONDocument
from numpy import ndarray
from pyarrow import Schema as ArrowSchema
from pyarrow import Table, timestamp
from pyarrow.types import (
is_date32,
is_date64,
is_duration,
is_float16,
is_float32,
is_int8,
is_int16,
is_list,
is_uint8,
is_uint16,
is_uint32,
is_uint64,
)
from pymongo.collection import Collection
from pymongo.common import MAX_WRITE_BATCH_SIZE
from pymongo.driver_info import DriverInfo
import pymongoarrow.version as pymongoarrow_version
from pymongoarrow.context import PyMongoArrowContext
from pymongoarrow.errors import ArrowWriteError
from pymongoarrow.result import ArrowWriteResult
from pymongoarrow.schema import Schema
from pymongoarrow.types import _validate_schema, get_numpy_type
__all__ = [
"Schema",
"aggregate_arrow_all",
"aggregate_numpy_all",
"aggregate_pandas_all",
"aggregate_polars_all",
"find_arrow_all",
"find_numpy_all",
"find_pandas_all",
"find_polars_all",
"write",
]
_PATCH_METHODS = [
"aggregate_arrow_all",
"find_arrow_all",
"aggregate_pandas_all",
"find_pandas_all",
"aggregate_numpy_all",
"find_numpy_all",
"aggregate_polars_all",
"find_polars_all",
]
# MongoDB 3.6's maxMessageSizeBytes minus some overhead to account
# for the command plus OP_MSG.
_MAX_MESSAGE_SIZE = 48000000 - 16 * 1024
# The maximum number of bulk write operations in one batch.
_MAX_WRITE_BATCH_SIZE = max(100000, MAX_WRITE_BATCH_SIZE)
def _add_driver_metadata(collection: Collection):
client = collection.database.client
if callable(client.append_metadata):
client.append_metadata(
DriverInfo(name="PyMongoArrow", version=pymongoarrow_version.__version__)
)
def _process_batch(schema, codec_options, allow_invalid, batch):
context = PyMongoArrowContext(schema, codec_options=codec_options, allow_invalid=allow_invalid)
context.process_bson_stream(batch)
return context.finish()
Parallelism = Literal["threads", "processes", "off"]
def find_arrow_all(
collection,
query,
*,
schema=None,
allow_invalid=False,
parallelism: Parallelism = "off",
**kwargs,
):
"""Method that returns the results of a find query as a
:class:`pyarrow.Table` instance.
:Parameters:
- `collection`: Instance of :class:`~pymongo.collection.Collection`.
against which to run the ``find`` operation.
- `query`: A mapping containing the query to use for the find operation.
- `schema` (optional): Instance of :class:`~pymongoarrow.schema.Schema`.
If the schema is not given, it will be inferred using the data in the
result set.
- `allow_invalid` (optional): If set to ``True``,
results will have all fields that do not conform to the schema silently converted to NaN.
- `parallelism` (optional): Controls how batch processing is parallelized.
Possible values are:
- "off": (default) Disable parallelism and use the single-process behavior.
- "threads": Always use a threaded implementation.
- "processes": Always use a multiprocess implementation.
Additional keyword-arguments passed to this method will be passed
directly to the underlying ``find`` operation.
:Returns:
An instance of class:`pyarrow.Table`.
"""
_add_driver_metadata(collection)
for opt in ("cursor_type",):
if kwargs.pop(opt, None):
warnings.warn(
f"Ignoring option {opt!r} as it is not supported by PyMongoArrow",
UserWarning,
stacklevel=2,
)
if schema:
kwargs.setdefault("projection", schema._get_projection())
raw_batch_cursor = collection.find_raw_batches(query, **kwargs)
def args_iterable():
for batch in collection.find_raw_batches(query, **kwargs):
yield (schema, collection.codec_options, allow_invalid, batch)
if parallelism == "threads":
with ThreadPoolExecutor(max_workers=4) as executor:
results = list(executor.map(lambda args: _process_batch(*args), args_iterable()))
return pa.concat_tables(results, promote_options="permissive")
if parallelism == "processes":
with multiprocessing.Pool(processes=4) as pool:
results = pool.starmap(_process_batch, args_iterable())
return pa.concat_tables(results, promote_options="permissive")
context = PyMongoArrowContext(
schema, codec_options=collection.codec_options, allow_invalid=allow_invalid
)
for batch in raw_batch_cursor:
context.process_bson_stream(batch)
return context.finish()
def aggregate_arrow_all(collection, pipeline, *, schema=None, allow_invalid=False, **kwargs):
"""Method that returns the results of an aggregation pipeline as a
:class:`pyarrow.Table` instance.
:Parameters:
- `collection`: Instance of :class:`~pymongo.collection.Collection`.
against which to run the ``aggregate`` operation.
- `pipeline`: A list of aggregation pipeline stages.
- `schema` (optional): Instance of :class:`~pymongoarrow.schema.Schema`.
If the schema is not given, it will be inferred using the data in the
result set.
- `allow_invalid` (optional): If set to ``True``,
results will have all fields that do not conform to the schema silently converted to NaN.
Additional keyword-arguments passed to this method will be passed
directly to the underlying ``aggregate`` operation.
:Returns:
An instance of class:`pyarrow.Table`.
"""
_add_driver_metadata(collection)
context = PyMongoArrowContext(
schema, codec_options=collection.codec_options, allow_invalid=allow_invalid
)
if pipeline and ("$out" in pipeline[-1] or "$merge" in pipeline[-1]):
msg = (
"Aggregation pipelines containing a '$out' or '$merge' stage are "
"not supported by PyMongoArrow"
)
raise ValueError(msg)
for opt in ("batchSize", "useCursor"):
if kwargs.pop(opt, None):
warnings.warn(
f"Ignoring option {opt!r} as it is not supported by PyMongoArrow",
UserWarning,
stacklevel=2,
)
if schema:
pipeline.append({"$project": schema._get_projection()})
raw_batch_cursor = collection.aggregate_raw_batches(pipeline, **kwargs)
for batch in raw_batch_cursor:
context.process_bson_stream(batch)
return context.finish()
def _arrow_to_pandas(arrow_table):
"""Helper function that converts an Arrow Table to a Pandas DataFrame
while minimizing peak memory consumption during conversion. The memory
buffers backing the given Arrow Table are also destroyed after conversion.
See https://arrow.apache.org/docs/python/pandas.html#reducing-memory-use-in-table-to-pandas
for details.
"""
if pd is None:
msg = "pandas is not installed. Try pip install pandas."
raise ValueError(msg)
return arrow_table.to_pandas(split_blocks=True, self_destruct=True)
def find_pandas_all(
collection,
query,
*,
schema=None,
allow_invalid=False,
parallelism: Parallelism = "off",
**kwargs,
):
"""Method that returns the results of a find query as a
:class:`pandas.DataFrame` instance.
:Parameters:
- `collection`: Instance of :class:`~pymongo.collection.Collection`.
against which to run the ``find`` operation.
- `query`: A mapping containing the query to use for the find operation.
- `schema` (optional): Instance of :class:`~pymongoarrow.schema.Schema`.
If the schema is not given, it will be inferred using the data in the
result set.
- `allow_invalid` (optional): If set to ``True``,
results will have all fields that do not conform to the schema silently converted to NaN.
- `parallelism` (optional): Controls how batch processing is parallelized.
Possible values are:
- "off": (default) Disable parallelism and use the single-process behavior.
- "threads": Always use a threaded implementation.
- "processes": Always use a multiprocess implementation.
Additional keyword-arguments passed to this method will be passed
directly to the underlying ``find`` operation.
:Returns:
An instance of class:`pandas.DataFrame`.
"""
return _arrow_to_pandas(
find_arrow_all(
collection,
query,
schema=schema,
allow_invalid=allow_invalid,
parallelism=parallelism,
**kwargs,
)
)
def aggregate_pandas_all(collection, pipeline, *, schema=None, allow_invalid=False, **kwargs):
"""Method that returns the results of an aggregation pipeline as a
:class:`pandas.DataFrame` instance.
:Parameters:
- `collection`: Instance of :class:`~pymongo.collection.Collection`.
against which to run the ``find`` operation.
- `pipeline`: A list of aggregation pipeline stages.
- `schema` (optional): Instance of :class:`~pymongoarrow.schema.Schema`.
If the schema is not given, it will be inferred using the data in the
result set.
- `allow_invalid` (optional): If set to ``True``,
results will have all fields that do not conform to the schema silently converted to NaN.
Additional keyword-arguments passed to this method will be passed
directly to the underlying ``aggregate`` operation.
:Returns:
An instance of class:`pandas.DataFrame`.
"""
return _arrow_to_pandas(
aggregate_arrow_all(
collection, pipeline, schema=schema, allow_invalid=allow_invalid, **kwargs
)
)
def _arrow_to_numpy(arrow_table, schema=None):
"""Helper function that converts an Arrow Table to a dictionary
containing NumPy arrays. The memory buffers backing the given Arrow Table
may be destroyed after conversion if the resulting Numpy array(s) is not a
view on the Arrow data.
See https://arrow.apache.org/docs/python/numpy.html for details.
"""
container = {}
schema = {i.name: i.type for i in arrow_table.schema} if not schema else schema.typemap
for fname in schema:
dtype = get_numpy_type(schema[fname])
container[fname] = arrow_table[fname].to_numpy()
if dtype == np.str_:
container[fname] = container[fname].astype(np.str_)
return container
def find_numpy_all(
collection,
query,
*,
schema=None,
allow_invalid=False,
parallelism: Parallelism = "off",
**kwargs,
):
"""Method that returns the results of a find query as a
:class:`dict` instance whose keys are field names and values are
:class:`~numpy.ndarray` instances bearing the appropriate dtype.
:Parameters:
- `collection`: Instance of :class:`~pymongo.collection.Collection`.
against which to run the ``find`` operation.
- `query`: A mapping containing the query to use for the find operation.
- `schema` (optional): Instance of :class:`~pymongoarrow.schema.Schema`.
If the schema is not given, it will be inferred using the data in the
result set.
- `allow_invalid` (optional): If set to ``True``,
results will have all fields that do not conform to the schema silently converted to NaN.
- `parallelism` (optional): Controls how batch processing is parallelized.
Possible values are:
- "off": (default) Disable parallelism and use the single-process behavior.
- "threads": Always use a threaded implementation.
- "processes": Always use a multiprocess implementation.
Additional keyword-arguments passed to this method will be passed
directly to the underlying ``find`` operation.
This method attempts to create each NumPy array as a view on the Arrow
data corresponding to each field in the result set. When this is not
possible, the underlying data is copied into a new NumPy array. See
:meth:`pyarrow.Array.to_numpy` for more information.
NumPy arrays returned by this method that are views on Arrow data
are not writable. Users seeking to modify such arrays must first
create an editable copy using :meth:`numpy.copy`.
:Returns:
An instance of :class:`dict`.
"""
return _arrow_to_numpy(
find_arrow_all(
collection,
query,
schema=schema,
allow_invalid=allow_invalid,
parallelism=parallelism,
**kwargs,
),
schema,
)
def aggregate_numpy_all(collection, pipeline, *, schema=None, allow_invalid=False, **kwargs):
"""Method that returns the results of an aggregation pipeline as a
:class:`dict` instance whose keys are field names and values are
:class:`~numpy.ndarray` instances bearing the appropriate dtype.
:Parameters:
- `collection`: Instance of :class:`~pymongo.collection.Collection`.
against which to run the ``find`` operation.
- `query`: A mapping containing the query to use for the find operation.
- `schema` (optional): Instance of :class:`~pymongoarrow.schema.Schema`.
If the schema is not given, it will be inferred using the data in the
result set.
- `allow_invalid` (optional): If set to ``True``,
results will have all fields that do not conform to the schema silently converted to NaN.
Additional keyword-arguments passed to this method will be passed
directly to the underlying ``aggregate`` operation.
This method attempts to create each NumPy array as a view on the Arrow
data corresponding to each field in the result set. When this is not
possible, the underlying data is copied into a new NumPy array. See
:meth:`pyarrow.Array.to_numpy` for more information.
NumPy arrays returned by this method that are views on Arrow data
are not writable. Users seeking to modify such arrays must first
create an editable copy using :meth:`numpy.copy`.
:Returns:
An instance of :class:`dict`.
"""
return _arrow_to_numpy(
aggregate_arrow_all(
collection, pipeline, schema=schema, allow_invalid=allow_invalid, **kwargs
),
schema,
)
def _arrow_to_polars(arrow_table: pa.Table):
"""Helper function that converts an Arrow Table to a Polars DataFrame."""
if pl is None:
msg = "polars is not installed. Try pip install polars."
raise ValueError(msg)
return pl.from_arrow(arrow_table)
def find_polars_all(
collection,
query,
*,
schema=None,
allow_invalid=False,
parallelism: Parallelism = "off",
**kwargs,
):
"""Method that returns the results of a find query as a
:class:`polars.DataFrame` instance.
:Parameters:
- `collection`: Instance of :class:`~pymongo.collection.Collection`.
against which to run the ``find`` operation.
- `query`: A mapping containing the query to use for the find operation.
- `schema` (optional): Instance of :class:`~pymongoarrow.schema.Schema`.
If the schema is not given, it will be inferred using the data in the
result set.
- `allow_invalid` (optional): If set to ``True``,
results will have all fields that do not conform to the schema silently converted to NaN.
- `parallelism` (optional): Controls how batch processing is parallelized.
Possible values are:
- "off": (default) Disable parallelism and use the single-process behavior.
- "threads": Always use a threaded implementation.
- "processes": Always use a multiprocess implementation.
Additional keyword-arguments passed to this method will be passed
directly to the underlying ``find`` operation.
:Returns:
An instance of class:`polars.DataFrame`.
.. versionadded:: 1.3
"""
return _arrow_to_polars(
find_arrow_all(
collection,
query,
schema=schema,
allow_invalid=allow_invalid,
parallelism=parallelism,
**kwargs,
)
)
def aggregate_polars_all(collection, pipeline, *, schema=None, allow_invalid=False, **kwargs):
"""Method that returns the results of an aggregation pipeline as a
:class:`polars.DataFrame` instance.
:Parameters:
- `collection`: Instance of :class:`~pymongo.collection.Collection`.
against which to run the ``find`` operation.
- `pipeline`: A list of aggregation pipeline stages.
- `schema` (optional): Instance of :class:`~pymongoarrow.schema.Schema`.
If the schema is not given, it will be inferred using the data in the
result set.
- `allow_invalid` (optional): If set to ``True``,
results will have all fields that do not conform to the schema silently converted to NaN.
Additional keyword-arguments passed to this method will be passed
directly to the underlying ``aggregate`` operation.
:Returns:
An instance of class:`polars.DataFrame`.
"""
return _arrow_to_polars(
aggregate_arrow_all(
collection, pipeline, schema=schema, allow_invalid=allow_invalid, **kwargs
)
)
def _transform_bwe(bwe, offset):
bwe["nInserted"] += offset
for i in bwe["writeErrors"]:
i["index"] += offset
return {
"writeErrors": bwe["writeErrors"],
"nInserted": bwe["nInserted"],
"writeConcernErrors": bwe["writeConcernErrors"],
}
def _tabular_generator(tabular, *, exclude_none=False):
if isinstance(tabular, Table):
for i in tabular.to_batches():
for row in i.to_pylist():
if exclude_none:
yield {k: v for k, v in row.items() if v is not None}
else:
yield row
elif pd is not None and isinstance(tabular, pd.DataFrame):
for row in tabular.to_dict("records"):
if exclude_none:
yield {k: v for k, v in row.items() if not np.isnan(v)}
else:
yield row
elif pl is not None and isinstance(tabular, pl.DataFrame):
yield from _tabular_generator(tabular.to_arrow(), exclude_none=exclude_none)
elif isinstance(tabular, dict):
iter_dict = {k: np.nditer(v) for k, v in tabular.items()}
try:
while True:
yield {k: next(i).item() for k, i in iter_dict.items()}
except StopIteration:
return
class _PandasNACodec(TypeEncoder):
"""A custom type codec for Pandas NA objects."""
@property
def python_type(self):
return pd.NA.__class__
def transform_python(self, _):
"""Transform an NA object into 'None'"""
return
class _DecimalCodec(TypeEncoder):
"""A custom type codec for Decimal objects."""
@property
def python_type(self):
return Decimal
def transform_python(self, value):
"""Transform an Decimal object into a BSON Decimal128 object"""
return Decimal128(value)
def write(
collection: Collection, tabular, *, exclude_none: bool = False, auto_convert: bool = True
):
"""Write data from `tabular` into the given MongoDB `collection`.
:Parameters:
- `collection`: Instance of :class:`~pymongo.collection.Collection`.
against which to run the operation.
- `tabular`: A tabular data store to use for the write operation.
- `exclude_none`: Whether to skip writing `null` fields in documents.
- `auto_convert` (optional): Whether to attempt a best-effort conversion of unsupported types.
:Returns:
An instance of :class:`result.ArrowWriteResult`.
"""
cur_offset = 0
results = {
"insertedCount": 0,
}
tab_size = len(tabular)
if isinstance(tabular, Table):
# Convert date objects to datetime objects.
changed = False
new_types = []
for dtype in tabular.schema.types:
if is_date32(dtype) or is_date64(dtype):
changed = True
dtype = timestamp("ms") # noqa: PLW2901
elif auto_convert:
if is_uint8(dtype) or is_uint16(dtype) or is_int8(dtype) or is_int16(dtype):
changed = True
dtype = pa.int32() # noqa: PLW2901
elif is_uint32(dtype) or is_uint64(dtype) or is_duration(dtype):
changed = True
dtype = pa.int64() # noqa: PLW2901
elif is_float16(dtype) or is_float32(dtype):
changed = True
dtype = pa.float64() # noqa: PLW2901
new_types.append(dtype)
if changed:
cols = [
tabular.column(i).cast(new_types[i])
if not is_list(new_types[i])
else tabular.column(i)
for i in range(tabular.num_columns)
]
tabular = Table.from_arrays(cols, names=tabular.column_names)
_validate_schema(tabular.schema.types)
elif pd is not None and isinstance(tabular, pd.DataFrame):
_validate_schema(ArrowSchema.from_pandas(tabular).types)
elif pl is not None and isinstance(tabular, pl.DataFrame):
tabular = tabular.to_arrow() # zero-copy in most cases and done in tabular_gen anyway
_validate_schema(tabular.schema.types)
elif (
isinstance(tabular, dict)
and len(tabular.values()) >= 1
and ndarray is not None
and all(isinstance(i, ndarray) for i in tabular.values())
):
_validate_schema([i.dtype for i in tabular.values()])
tab_size = len(next(iter(tabular.values())))
else:
msg = (
f"Invalid tabular data object of type {type(tabular)} \n"
"Please ensure that it is one of the supported types: "
"DataFrame, Table, or a dictionary containing NumPy arrays."
)
raise ValueError(msg)
tabular_gen = _tabular_generator(tabular, exclude_none=exclude_none)
# Add handling for special case types.
codec_options = collection.codec_options
base_codecs = []
if hasattr(codec_options.type_registry, "codecs"):
base_codecs = codec_options.type_registry.codecs
if pd is not None:
type_registry = TypeRegistry([*base_codecs, _PandasNACodec(), _DecimalCodec()])
else:
type_registry = TypeRegistry([*base_codecs, _DecimalCodec()])
codec_options = codec_options.with_options(type_registry=type_registry)
_add_driver_metadata(collection)
while cur_offset < tab_size:
cur_size = 0
cur_batch = []
i = 0
while (
cur_size <= _MAX_MESSAGE_SIZE
and len(cur_batch) <= _MAX_WRITE_BATCH_SIZE
and cur_offset + i < tab_size
):
enc_tab = RawBSONDocument(encode(next(tabular_gen), codec_options=codec_options))
cur_batch.append(enc_tab)
cur_size += len(enc_tab.raw)
i += 1
try:
collection.insert_many(cur_batch)
except pymongo.errors.BulkWriteError as bwe:
raise ArrowWriteError(_transform_bwe(dict(bwe.details), cur_offset)) from bwe
except pymongo.errors.PyMongoError as pme:
raise ArrowWriteError(
{
"writeErrors": [{"errmsg": str(pme), "index": cur_offset}],
"nInserted": cur_offset,
"writeConcernErrors": [],
}
) from pme
results["insertedCount"] += i
cur_offset += i
return ArrowWriteResult(results)