-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
batch.py
922 lines (788 loc) · 33.5 KB
/
batch.py
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
import datetime
import json
import logging
from typing import Any, Callable, Dict, Optional, Set, Union
import great_expectations.exceptions as ge_exceptions
from great_expectations.core.id_dict import BatchKwargs, BatchSpec, IDDict
from great_expectations.core.util import convert_to_json_serializable
from great_expectations.exceptions import InvalidBatchIdError
from great_expectations.types import DictDot, SerializableDictDot, safe_deep_copy
from great_expectations.util import deep_filter_properties_iterable
from great_expectations.validator.metric_configuration import MetricConfiguration
logger = logging.getLogger(__name__)
try:
import pyspark
except ImportError:
pyspark = None
logger.debug(
"Unable to load pyspark; install optional spark dependency if you will be working with Spark dataframes"
)
class BatchDefinition(SerializableDictDot):
def __init__(
self,
datasource_name: str,
data_connector_name: str,
data_asset_name: str,
batch_identifiers: IDDict,
batch_spec_passthrough: Optional[dict] = None,
):
self._validate_batch_definition(
datasource_name=datasource_name,
data_connector_name=data_connector_name,
data_asset_name=data_asset_name,
batch_identifiers=batch_identifiers,
)
assert type(batch_identifiers) == IDDict
self._datasource_name = datasource_name
self._data_connector_name = data_connector_name
self._data_asset_name = data_asset_name
self._batch_identifiers = batch_identifiers
self._batch_spec_passthrough = batch_spec_passthrough
def to_json_dict(self) -> dict:
return convert_to_json_serializable(
{
"datasource_name": self.datasource_name,
"data_connector_name": self.data_connector_name,
"data_asset_name": self.data_asset_name,
"batch_identifiers": self.batch_identifiers,
}
)
def __repr__(self) -> str:
doc_fields_dict: dict = {
"datasource_name": self._datasource_name,
"data_connector_name": self._data_connector_name,
"data_asset_name": self.data_asset_name,
"batch_identifiers": self._batch_identifiers,
}
return str(doc_fields_dict)
@staticmethod
def _validate_batch_definition(
datasource_name: str,
data_connector_name: str,
data_asset_name: str,
batch_identifiers: IDDict,
):
if datasource_name is None:
raise ValueError("A valid datasource must be specified.")
if datasource_name and not isinstance(datasource_name, str):
raise TypeError(
f"""The type of an datasource name must be a string (Python "str"). The type given is
"{str(type(datasource_name))}", which is illegal.
"""
)
if data_connector_name is None:
raise ValueError("A valid data_connector must be specified.")
if data_connector_name and not isinstance(data_connector_name, str):
raise TypeError(
f"""The type of a data_connector name must be a string (Python "str"). The type given is
"{str(type(data_connector_name))}", which is illegal.
"""
)
if data_asset_name is None:
raise ValueError("A valid data_asset_name must be specified.")
if data_asset_name and not isinstance(data_asset_name, str):
raise TypeError(
f"""The type of a data_asset name must be a string (Python "str"). The type given is
"{str(type(data_asset_name))}", which is illegal.
"""
)
if batch_identifiers and not isinstance(batch_identifiers, IDDict):
raise TypeError(
f"""The type of batch_identifiers must be an IDDict object. The type given is \
"{str(type(batch_identifiers))}", which is illegal.
"""
)
@property
def datasource_name(self) -> str:
return self._datasource_name
@property
def data_connector_name(self) -> str:
return self._data_connector_name
@property
def data_asset_name(self) -> str:
return self._data_asset_name
@property
def batch_identifiers(self) -> IDDict:
return self._batch_identifiers
@property
def batch_spec_passthrough(self) -> dict:
return self._batch_spec_passthrough
@batch_spec_passthrough.setter
def batch_spec_passthrough(self, batch_spec_passthrough: Optional[dict]):
self._batch_spec_passthrough = batch_spec_passthrough
@property
def id(self) -> str:
return IDDict(self.to_json_dict()).to_id()
def __eq__(self, other):
if not isinstance(other, self.__class__):
# Delegate comparison to the other instance's __eq__.
return NotImplemented
return self.id == other.id
def __str__(self):
return json.dumps(self.to_json_dict(), indent=2)
def __hash__(self) -> int:
"""Overrides the default implementation"""
_result_hash: int = hash(self.id)
return _result_hash
class BatchRequestBase(SerializableDictDot):
"""
This class is for internal inter-object protocol purposes only.
As such, it contains all attributes of a batch_request, but does not validate them.
See the BatchRequest class, which extends BatchRequestBase and validates the attributes.
BatchRequestBase is used for the internal protocol purposes exclusively, not part of API for the developer users.
Previously, the very same BatchRequest was used for both the internal protocol purposes and as part of the API
exposed to developers. However, while convenient for internal data interchange, using the same BatchRequest class
as arguments to the externally-exported DataContext.get_batch(), DataContext.get_batch_list(), and
DataContext.get_validator() API calls for obtaining batches and/or validators was insufficiently expressive to
fulfill the needs of both. In the user-accessible API, BatchRequest, must enforce that all members of the triple,
consisting of data_source_name, data_connector_name, and data_asset_name, are not NULL. Whereas for the internal
protocol, BatchRequest is used as a flexible bag of attributes, in which any fields are allowed to be NULL. Hence,
now, BatchRequestBase is dedicated for the use as the bag oof attributes for the internal protocol use, whereby NULL
values are allowed as per the internal needs. The BatchRequest class extends BatchRequestBase and adds to it strong
validation (described above plus additional attribute validation) so as to formally validate user specified fields.
"""
def __init__(
self,
datasource_name: str,
data_connector_name: str,
data_asset_name: str,
data_connector_query: Optional[dict] = None,
limit: Optional[int] = None,
runtime_parameters: Optional[dict] = None,
batch_identifiers: Optional[dict] = None,
batch_spec_passthrough: Optional[dict] = None,
):
self._datasource_name = datasource_name
self._data_connector_name = data_connector_name
self._data_asset_name = data_asset_name
self._data_connector_query = data_connector_query
self._limit = limit
self._runtime_parameters = runtime_parameters
self._batch_identifiers = batch_identifiers
self._batch_spec_passthrough = batch_spec_passthrough
@property
def datasource_name(self) -> str:
return self._datasource_name
@datasource_name.setter
def datasource_name(self, value: str):
self._datasource_name = value
@property
def data_connector_name(self) -> str:
return self._data_connector_name
@data_connector_name.setter
def data_connector_name(self, value: str):
self._data_connector_name = value
@property
def data_asset_name(self) -> str:
return self._data_asset_name
@data_asset_name.setter
def data_asset_name(self, data_asset_name):
self._data_asset_name = data_asset_name
@property
def data_connector_query(self) -> dict:
return self._data_connector_query
@data_connector_query.setter
def data_connector_query(self, value: dict):
self._data_connector_query = value
@property
def limit(self) -> int:
return self._limit
@limit.setter
def limit(self, value: int):
self._limit = value
@property
def runtime_parameters(self) -> dict:
return self._runtime_parameters
@runtime_parameters.setter
def runtime_parameters(self, value: dict):
self._runtime_parameters = value
@property
def batch_identifiers(self) -> dict:
return self._batch_identifiers
@batch_identifiers.setter
def batch_identifiers(self, value: dict):
self._batch_identifiers = value
@property
def batch_spec_passthrough(self) -> dict:
return self._batch_spec_passthrough
@batch_spec_passthrough.setter
def batch_spec_passthrough(self, value: dict):
self._batch_spec_passthrough = value
@property
def id(self) -> str:
return IDDict(self.to_json_dict()).to_id()
def to_dict(self) -> dict:
return standardize_batch_request_display_ordering(
batch_request=super().to_dict()
)
def to_json_dict(self) -> dict:
"""
# TODO: <Alex>2/4/2022</Alex>
This implementation of "SerializableDictDot.to_json_dict() occurs frequently and should ideally serve as the
reference implementation in the "SerializableDictDot" class itself. However, the circular import dependencies,
due to the location of the "great_expectations/types/__init__.py" and "great_expectations/core/util.py" modules
make this refactoring infeasible at the present time.
"""
# if batch_data appears in BatchRequest, temporarily replace it with
# str placeholder before calling convert_to_json_serializable so that
# batch_data is not serialized
if batch_request_contains_batch_data(batch_request=self):
batch_data: Union[BatchRequestBase, dict] = self.runtime_parameters[
"batch_data"
]
self.runtime_parameters["batch_data"]: str = str(type(batch_data))
serializeable_dict: dict = convert_to_json_serializable(data=self.to_dict())
# after getting serializable_dict, restore original batch_data
self.runtime_parameters["batch_data"]: Union[
BatchRequestBase, dict
] = batch_data
else:
serializeable_dict: dict = convert_to_json_serializable(data=self.to_dict())
return serializeable_dict
def __deepcopy__(self, memo):
cls = self.__class__
result = cls.__new__(cls)
memo[id(self)] = result
for key, value in self.to_raw_dict().items():
value_copy = safe_deep_copy(data=value, memo=memo)
setattr(result, key, value_copy)
return result
def __eq__(self, other):
if not isinstance(other, self.__class__):
# Delegate comparison to the other instance's __eq__.
return NotImplemented
return self.id == other.id
def __repr__(self) -> str:
"""
# TODO: <Alex>2/4/2022</Alex>
This implementation of a custom "__repr__()" occurs frequently and should ideally serve as the reference
implementation in the "SerializableDictDot" class. However, the circular import dependencies, due to the
location of the "great_expectations/types/__init__.py" and "great_expectations/core/util.py" modules make this
refactoring infeasible at the present time.
"""
json_dict: dict = self.to_json_dict()
deep_filter_properties_iterable(
properties=json_dict,
inplace=True,
)
return json.dumps(json_dict, indent=2)
def __str__(self) -> str:
"""
# TODO: <Alex>2/4/2022</Alex>
This implementation of a custom "__str__()" occurs frequently and should ideally serve as the reference
implementation in the "SerializableDictDot" class. However, the circular import dependencies, due to the
location of the "great_expectations/types/__init__.py" and "great_expectations/core/util.py" modules make this
refactoring infeasible at the present time.
"""
return self.__repr__()
@staticmethod
def _validate_init_parameters(
datasource_name: str,
data_connector_name: str,
data_asset_name: str,
data_connector_query: Optional[dict] = None,
limit: Optional[int] = None,
):
# TODO test and check all logic in this validator!
if not (datasource_name and isinstance(datasource_name, str)):
raise TypeError(
f"""The type of an datasource name must be a string (Python "str"). The type given is
"{str(type(datasource_name))}", which is illegal.
"""
)
if not (data_connector_name and isinstance(data_connector_name, str)):
raise TypeError(
f"""The type of data_connector name must be a string (Python "str"). The type given is
"{str(type(data_connector_name))}", which is illegal.
"""
)
if not (data_asset_name and isinstance(data_asset_name, str)):
raise TypeError(
f"""The type of data_asset name must be a string (Python "str"). The type given is
"{str(type(data_asset_name))}", which is illegal.
"""
)
# TODO Abe 20201015: Switch this to DataConnectorQuery.
if data_connector_query and not isinstance(data_connector_query, dict):
raise TypeError(
f"""The type of data_connector_query must be a dict object. The type given is
"{str(type(data_connector_query))}", which is illegal.
"""
)
if limit and not isinstance(limit, int):
raise TypeError(
f"""The type of limit must be an integer (Python "int"). The type given is "{str(type(limit))}", which
is illegal.
"""
)
class BatchRequest(BatchRequestBase):
"""
This class contains all attributes of a batch_request. See the comments in BatchRequestBase for design specifics.
limit: refers to the number of batches requested (not rows per batch)
"""
include_field_names: Set[str] = {
"datasource_name",
"data_connector_name",
"data_asset_name",
"data_connector_query",
"limit",
"batch_spec_passthrough",
}
def __init__(
self,
datasource_name: str,
data_connector_name: str,
data_asset_name: str,
data_connector_query: Optional[dict] = None,
limit: Optional[int] = None,
batch_spec_passthrough: Optional[dict] = None,
):
self._validate_init_parameters(
datasource_name=datasource_name,
data_connector_name=data_connector_name,
data_asset_name=data_asset_name,
data_connector_query=data_connector_query,
limit=limit,
)
super().__init__(
datasource_name=datasource_name,
data_connector_name=data_connector_name,
data_asset_name=data_asset_name,
data_connector_query=data_connector_query,
limit=limit,
batch_spec_passthrough=batch_spec_passthrough,
)
class RuntimeBatchRequest(BatchRequestBase):
include_field_names: Set[str] = {
"datasource_name",
"data_connector_name",
"data_asset_name",
"runtime_parameters",
"batch_identifiers",
"batch_spec_passthrough",
}
def __init__(
self,
datasource_name: str,
data_connector_name: str,
data_asset_name: str,
runtime_parameters: dict,
batch_identifiers: dict,
batch_spec_passthrough: Optional[dict] = None,
):
self._validate_init_parameters(
datasource_name=datasource_name,
data_connector_name=data_connector_name,
data_asset_name=data_asset_name,
)
self._validate_runtime_batch_request_specific_init_parameters(
runtime_parameters=runtime_parameters,
batch_identifiers=batch_identifiers,
batch_spec_passthrough=batch_spec_passthrough,
)
super().__init__(
datasource_name=datasource_name,
data_connector_name=data_connector_name,
data_asset_name=data_asset_name,
runtime_parameters=runtime_parameters,
batch_identifiers=batch_identifiers,
batch_spec_passthrough=batch_spec_passthrough,
)
@staticmethod
def _validate_runtime_batch_request_specific_init_parameters(
runtime_parameters: dict,
batch_identifiers: dict,
batch_spec_passthrough: Optional[dict] = None,
):
if not (runtime_parameters and (isinstance(runtime_parameters, dict))):
raise TypeError(
f"""The runtime_parameters must be a non-empty dict object.
The type given is "{str(type(runtime_parameters))}", which is an illegal type or an empty dictionary."""
)
if not (batch_identifiers and isinstance(batch_identifiers, dict)):
raise TypeError(
f"""The type for batch_identifiers must be a dict object, with keys being identifiers defined in the
data connector configuration. The type given is "{str(type(batch_identifiers))}", which is illegal."""
)
if batch_spec_passthrough and not (isinstance(batch_spec_passthrough, dict)):
raise TypeError(
f"""The type for batch_spec_passthrough must be a dict object. The type given is \
"{str(type(batch_spec_passthrough))}", which is illegal.
"""
)
# TODO: <Alex>The following class is to support the backward compatibility with the legacy design.</Alex>
class BatchMarkers(BatchKwargs):
"""A BatchMarkers is a special type of BatchKwargs (so that it has a batch_fingerprint) but it generally does
NOT require specific keys and instead captures information about the OUTPUT of a datasource's fetch
process, such as the timestamp at which a query was executed."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if "ge_load_time" not in self:
raise InvalidBatchIdError("BatchMarkers requires a ge_load_time")
@property
def ge_load_time(self):
return self.get("ge_load_time")
# TODO: <Alex>This module needs to be cleaned up.
# We have Batch used for the legacy design, and we also need Batch for the new design.
# However, right now, the Batch from the legacy design is imported into execution engines of the new design.
# As a result, we have multiple, inconsistent versions of BatchMarkers, extending legacy/new classes.</Alex>
# TODO: <Alex>See also "great_expectations/datasource/types/batch_spec.py".</Alex>
class Batch(SerializableDictDot):
def __init__(
self,
data,
batch_request: Optional[Union[BatchRequestBase, dict]] = None,
batch_definition: BatchDefinition = None,
batch_spec: BatchSpec = None,
batch_markers: BatchMarkers = None,
# The remaining parameters are for backward compatibility.
data_context=None,
datasource_name=None,
batch_parameters=None,
batch_kwargs=None,
):
self._data = data
if batch_request is None:
batch_request = {}
self._batch_request = batch_request
if batch_definition is None:
batch_definition = IDDict()
self._batch_definition = batch_definition
if batch_spec is None:
batch_spec = BatchSpec()
self._batch_spec = batch_spec
if batch_markers is None:
batch_markers = BatchMarkers(
{
"ge_load_time": datetime.datetime.now(
datetime.timezone.utc
).strftime("%Y%m%dT%H%M%S.%fZ")
}
)
self._batch_markers = batch_markers
# The remaining parameters are for backward compatibility.
self._data_context = data_context
self._datasource_name = datasource_name
self._batch_parameters = batch_parameters
self._batch_kwargs = batch_kwargs or BatchKwargs()
@property
def data(self):
return self._data
@property
def batch_request(self):
return self._batch_request
@batch_request.setter
def batch_request(self, batch_request):
self._batch_request = batch_request
@property
def batch_definition(self):
return self._batch_definition
@batch_definition.setter
def batch_definition(self, batch_definition):
self._batch_definition = batch_definition
@property
def batch_spec(self):
return self._batch_spec
@property
def batch_markers(self):
return self._batch_markers
# The remaining properties are for backward compatibility.
@property
def data_context(self):
return self._data_context
@property
def datasource_name(self):
return self._datasource_name
@property
def batch_parameters(self):
return self._batch_parameters
@property
def batch_kwargs(self):
return self._batch_kwargs
def to_dict(self) -> dict:
dict_obj: dict = {
"data": str(self.data),
"batch_request": self.batch_request.to_dict(),
"batch_definition": self.batch_definition.to_json_dict()
if isinstance(self.batch_definition, BatchDefinition)
else {},
"batch_spec": self.batch_spec,
"batch_markers": self.batch_markers,
}
return dict_obj
def to_json_dict(self) -> dict:
json_dict: dict = self.to_dict()
deep_filter_properties_iterable(
properties=json_dict["batch_request"],
inplace=True,
)
return json_dict
@property
def id(self):
batch_definition = self._batch_definition
return (
batch_definition.id
if isinstance(batch_definition, BatchDefinition)
else batch_definition.to_id()
)
def __str__(self):
return json.dumps(self.to_json_dict(), indent=2)
def head(self, n_rows=5, fetch_all=False):
# FIXME - we should use a Validator after resolving circularity
# Validator(self._data.execution_engine, batches=(self,)).get_metric(MetricConfiguration("table.head", {"batch_id": self.id}, {"n_rows": n_rows, "fetch_all": fetch_all}))
metric = MetricConfiguration(
"table.head",
{"batch_id": self.id},
{"n_rows": n_rows, "fetch_all": fetch_all},
)
return self._data.execution_engine.resolve_metrics((metric,))[metric.id]
# TODO: <Alex>ALEX -- Make this helper utility of general use.</Alex>
def materialize_batch_request(
batch_request: Optional[Union[BatchRequestBase, dict]] = None,
) -> Optional[BatchRequestBase]:
effective_batch_request: dict = get_batch_request_as_dict(
batch_request=batch_request
)
if not effective_batch_request:
return None
batch_request_class: type
if batch_request_contains_runtime_parameters(batch_request=effective_batch_request):
batch_request_class = RuntimeBatchRequest
else:
batch_request_class = BatchRequest
return batch_request_class(**effective_batch_request)
def batch_request_contains_batch_data(
batch_request: Optional[Union[BatchRequestBase, dict]] = None
) -> bool:
return (
batch_request_contains_runtime_parameters(batch_request=batch_request)
and batch_request["runtime_parameters"].get("batch_data") is not None
)
def batch_request_contains_runtime_parameters(
batch_request: Optional[Union[BatchRequestBase, dict]] = None
) -> bool:
return (
batch_request is not None
and isinstance(batch_request, (dict, DictDot))
and batch_request.get("runtime_parameters") is not None
)
def get_batch_request_as_dict(
batch_request: Optional[Union[BatchRequestBase, dict]] = None
) -> Optional[dict]:
if batch_request is None:
return None
if isinstance(batch_request, (BatchRequest, RuntimeBatchRequest)):
batch_request = batch_request.to_dict()
return batch_request
def get_batch_request_from_acceptable_arguments(
datasource_name: Optional[str] = None,
data_connector_name: Optional[str] = None,
data_asset_name: Optional[str] = None,
*,
batch_request: Optional[BatchRequestBase] = None,
batch_data: Optional[Any] = None,
data_connector_query: Optional[dict] = None,
batch_identifiers: Optional[dict] = None,
limit: Optional[int] = None,
index: Optional[Union[int, list, tuple, slice, str]] = None,
custom_filter_function: Optional[Callable] = None,
batch_spec_passthrough: Optional[dict] = None,
sampling_method: Optional[str] = None,
sampling_kwargs: Optional[dict] = None,
splitter_method: Optional[str] = None,
splitter_kwargs: Optional[dict] = None,
runtime_parameters: Optional[dict] = None,
query: Optional[str] = None,
path: Optional[str] = None,
batch_filter_parameters: Optional[dict] = None,
**kwargs,
) -> Union[BatchRequest, RuntimeBatchRequest]:
"""Obtain formal BatchRequest typed object from allowed attributes (supplied as arguments).
This method applies only to the new (V3) Datasource schema.
Args:
datasource_name
data_connector_name
data_asset_name
batch_request
batch_data
query
path
runtime_parameters
data_connector_query
batch_identifiers
batch_filter_parameters
limit
index
custom_filter_function
sampling_method
sampling_kwargs
splitter_method
splitter_kwargs
batch_spec_passthrough
**kwargs
Returns:
(BatchRequest or RuntimeBatchRequest) The formal BatchRequest or RuntimeBatchRequest object
"""
if batch_request:
if not isinstance(batch_request, (BatchRequest, RuntimeBatchRequest)):
raise TypeError(
f"""batch_request must be an instance of BatchRequest or RuntimeBatchRequest object, not \
{type(batch_request)}"""
)
datasource_name = batch_request.datasource_name
# ensure that the first parameter is datasource_name, which should be a str. This check prevents users
# from passing in batch_request as an unnamed parameter.
if not isinstance(datasource_name, str):
raise ge_exceptions.GreatExpectationsTypeError(
f"the first parameter, datasource_name, must be a str, not {type(datasource_name)}"
)
if len([arg for arg in [batch_data, query, path] if arg is not None]) > 1:
raise ValueError("Must provide only one of batch_data, query, or path.")
if any(
[
batch_data is not None
and runtime_parameters
and "batch_data" in runtime_parameters,
query and runtime_parameters and "query" in runtime_parameters,
path and runtime_parameters and "path" in runtime_parameters,
]
):
raise ValueError(
"If batch_data, query, or path arguments are provided, the same keys cannot appear in the "
"runtime_parameters argument."
)
if batch_request:
# TODO: Raise a warning if any parameters besides batch_requests are specified
return batch_request
batch_request_class: type
batch_request_as_dict: dict
if any([batch_data is not None, query, path, runtime_parameters]):
batch_request_class = RuntimeBatchRequest
runtime_parameters = runtime_parameters or {}
if batch_data is not None:
runtime_parameters["batch_data"] = batch_data
elif query is not None:
runtime_parameters["query"] = query
elif path is not None:
runtime_parameters["path"] = path
if batch_identifiers is None:
batch_identifiers = kwargs
else:
# Raise a warning if kwargs exist
pass
batch_request_as_dict = {
"datasource_name": datasource_name,
"data_connector_name": data_connector_name,
"data_asset_name": data_asset_name,
"runtime_parameters": runtime_parameters,
"batch_identifiers": batch_identifiers,
"batch_spec_passthrough": batch_spec_passthrough,
}
else:
batch_request_class = BatchRequest
if data_connector_query is None:
if batch_filter_parameters is not None and batch_identifiers is not None:
raise ValueError(
'Must provide either "batch_filter_parameters" or "batch_identifiers", not both.'
)
if batch_filter_parameters is None and batch_identifiers is not None:
logger.warning(
'Attempting to build data_connector_query but "batch_identifiers" was provided '
'instead of "batch_filter_parameters". The "batch_identifiers" key on '
'data_connector_query has been renamed to "batch_filter_parameters". Please update '
'your code. Falling back on provided "batch_identifiers".'
)
batch_filter_parameters = batch_identifiers
elif batch_filter_parameters is None and batch_identifiers is None:
batch_filter_parameters = kwargs
else:
# Raise a warning if kwargs exist
pass
data_connector_query_params: dict = {
"batch_filter_parameters": batch_filter_parameters,
"limit": limit,
"index": index,
"custom_filter_function": custom_filter_function,
}
data_connector_query = IDDict(data_connector_query_params)
else:
# Raise a warning if batch_filter_parameters or kwargs exist
data_connector_query = IDDict(data_connector_query)
if batch_spec_passthrough is None:
batch_spec_passthrough = {}
if sampling_method is not None:
sampling_params: dict = {
"sampling_method": sampling_method,
}
if sampling_kwargs is not None:
sampling_params["sampling_kwargs"] = sampling_kwargs
batch_spec_passthrough.update(sampling_params)
if splitter_method is not None:
splitter_params: dict = {
"splitter_method": splitter_method,
}
if splitter_kwargs is not None:
splitter_params["splitter_kwargs"] = splitter_kwargs
batch_spec_passthrough.update(splitter_params)
batch_request_as_dict: dict = {
"datasource_name": datasource_name,
"data_connector_name": data_connector_name,
"data_asset_name": data_asset_name,
"data_connector_query": data_connector_query,
"batch_spec_passthrough": batch_spec_passthrough,
}
deep_filter_properties_iterable(
properties=batch_request_as_dict,
inplace=True,
)
batch_request = batch_request_class(**batch_request_as_dict)
return batch_request
def standardize_batch_request_display_ordering(
batch_request: Dict[str, Union[str, int, Dict[str, Any]]]
) -> Dict[str, Union[str, Dict[str, Any]]]:
datasource_name: str = batch_request["datasource_name"]
data_connector_name: str = batch_request["data_connector_name"]
data_asset_name: str = batch_request["data_asset_name"]
runtime_parameters: str = batch_request.get("runtime_parameters")
batch_identifiers: str = batch_request.get("batch_identifiers")
batch_request.pop("datasource_name")
batch_request.pop("data_connector_name")
batch_request.pop("data_asset_name")
# NOTE: AJB 20211217 The below conditionals should be refactored
if runtime_parameters is not None:
batch_request.pop("runtime_parameters")
if batch_identifiers is not None:
batch_request.pop("batch_identifiers")
if runtime_parameters is not None and batch_identifiers is not None:
batch_request = {
"datasource_name": datasource_name,
"data_connector_name": data_connector_name,
"data_asset_name": data_asset_name,
"runtime_parameters": runtime_parameters,
"batch_identifiers": batch_identifiers,
**batch_request,
}
elif runtime_parameters is not None and batch_identifiers is None:
batch_request = {
"datasource_name": datasource_name,
"data_connector_name": data_connector_name,
"data_asset_name": data_asset_name,
"runtime_parameters": runtime_parameters,
**batch_request,
}
elif runtime_parameters is None and batch_identifiers is not None:
batch_request = {
"datasource_name": datasource_name,
"data_connector_name": data_connector_name,
"data_asset_name": data_asset_name,
"batch_identifiers": batch_identifiers,
**batch_request,
}
else:
batch_request = {
"datasource_name": datasource_name,
"data_connector_name": data_connector_name,
"data_asset_name": data_asset_name,
**batch_request,
}
return batch_request