|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +from google.protobuf.json_format import MessageToDict as _MessageToDict |
| 4 | +from flytekit import __version__ |
| 5 | + |
| 6 | +from flytekit.common import constants as _constants |
| 7 | +from flytekit.common.tasks import task as _base_task |
| 8 | +from flytekit.models import ( |
| 9 | + interface as _interface_model |
| 10 | +) |
| 11 | +from flytekit.models import literals as _literals, types as _types, \ |
| 12 | + task as _task_model |
| 13 | + |
| 14 | +from flytekit.common import interface as _interface |
| 15 | +import datetime as _datetime |
| 16 | +from flytekit.models import presto as _presto_models |
| 17 | +from flytekit.common.exceptions.user import \ |
| 18 | + FlyteValueException as _FlyteValueException |
| 19 | +from flytekit.common.exceptions import scopes as _exception_scopes |
| 20 | + |
| 21 | + |
| 22 | +class SdkPrestoTask(_base_task.SdkTask): |
| 23 | + """ |
| 24 | + This class includes the logic for building a task that executes as a Presto task. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + statement, |
| 30 | + output_schema, |
| 31 | + routing_group=None, |
| 32 | + catalog=None, |
| 33 | + schema=None, |
| 34 | + task_inputs=None, |
| 35 | + interruptible=False, |
| 36 | + discoverable=False, |
| 37 | + discovery_version=None, |
| 38 | + retries=1, |
| 39 | + timeout=None, |
| 40 | + ): |
| 41 | + """ |
| 42 | + :param Text statement: Presto query specification |
| 43 | + :param flytekit.common.types.schema.Schema output_schema: Schema that represents that data queried from Presto |
| 44 | + :param Text routing_group: The routing group that a Presto query should be sent to for the given environment |
| 45 | + :param Text catalog: The catalog to set for the given Presto query |
| 46 | + :param Text schema: The schema to set for the given Presto query |
| 47 | + :param dict[Text,flytekit.common.types.base_sdk_types.FlyteSdkType] task_inputs: Optional inputs to the Presto task |
| 48 | + :param bool discoverable: |
| 49 | + :param Text discovery_version: String describing the version for task discovery purposes |
| 50 | + :param int retries: Number of retries to attempt |
| 51 | + :param datetime.timedelta timeout: |
| 52 | + """ |
| 53 | + |
| 54 | + # Set as class fields which are used down below to configure implicit |
| 55 | + # parameters |
| 56 | + self._routing_group = routing_group or "" |
| 57 | + self._catalog = catalog or "" |
| 58 | + self._schema = schema or "" |
| 59 | + |
| 60 | + metadata = _task_model.TaskMetadata( |
| 61 | + discoverable, |
| 62 | + # This needs to have the proper version reflected in it |
| 63 | + _task_model.RuntimeMetadata( |
| 64 | + _task_model.RuntimeMetadata.RuntimeType.FLYTE_SDK, __version__, |
| 65 | + "python"), |
| 66 | + timeout or _datetime.timedelta(seconds=0), |
| 67 | + _literals.RetryStrategy(retries), |
| 68 | + interruptible, |
| 69 | + discovery_version, |
| 70 | + "This is deprecated!" |
| 71 | + ) |
| 72 | + |
| 73 | + presto_query = _presto_models.PrestoQuery( |
| 74 | + routing_group=routing_group or "", |
| 75 | + catalog=catalog or "", |
| 76 | + schema=schema or "", |
| 77 | + statement=statement |
| 78 | + ) |
| 79 | + |
| 80 | + # Here we set the routing_group, catalog, and schema as implicit |
| 81 | + # parameters for caching purposes |
| 82 | + i = _interface.TypedInterface( |
| 83 | + { |
| 84 | + "__implicit_routing_group": _interface_model.Variable( |
| 85 | + type=_types.LiteralType(simple=_types.SimpleType.STRING), |
| 86 | + description="The routing group set as an implicit input" |
| 87 | + ), |
| 88 | + "__implicit_catalog": _interface_model.Variable( |
| 89 | + type=_types.LiteralType(simple=_types.SimpleType.STRING), |
| 90 | + description="The catalog set as an implicit input" |
| 91 | + ), |
| 92 | + "__implicit_schema": _interface_model.Variable( |
| 93 | + type=_types.LiteralType(simple=_types.SimpleType.STRING), |
| 94 | + description="The schema set as an implicit input" |
| 95 | + ) |
| 96 | + }, |
| 97 | + { |
| 98 | + # Set the schema for the Presto query as an output |
| 99 | + "results": _interface_model.Variable( |
| 100 | + type=_types.LiteralType(schema=output_schema.schema_type), |
| 101 | + description="The schema for the Presto query" |
| 102 | + ) |
| 103 | + }) |
| 104 | + |
| 105 | + super(SdkPrestoTask, self).__init__( |
| 106 | + _constants.SdkTaskType.PRESTO_TASK, |
| 107 | + metadata, |
| 108 | + i, |
| 109 | + _MessageToDict(presto_query.to_flyte_idl()), |
| 110 | + ) |
| 111 | + |
| 112 | + # Set user provided inputs |
| 113 | + task_inputs(self) |
| 114 | + |
| 115 | + # Override method in order to set the implicit inputs |
| 116 | + def __call__(self, *args, **kwargs): |
| 117 | + kwargs["__implicit_routing_group"] = self.routing_group |
| 118 | + kwargs["__implicit_catalog"] = self.catalog |
| 119 | + kwargs["__implicit_schema"] = self.schema |
| 120 | + |
| 121 | + return super(SdkPrestoTask, self).__call__( |
| 122 | + *args, **kwargs |
| 123 | + ) |
| 124 | + |
| 125 | + @_exception_scopes.system_entry_point |
| 126 | + def add_inputs(self, inputs): |
| 127 | + """ |
| 128 | + Adds the inputs to this task. This can be called multiple times, but it will fail if an input with a given |
| 129 | + name is added more than once, a name collides with an output, or if the name doesn't exist as an arg name in |
| 130 | + the wrapped function. |
| 131 | + :param dict[Text, flytekit.models.interface.Variable] inputs: names and variables |
| 132 | + """ |
| 133 | + self._validate_inputs(inputs) |
| 134 | + self.interface.inputs.update(inputs) |
| 135 | + |
| 136 | + @property |
| 137 | + def routing_group(self): |
| 138 | + """ |
| 139 | + The routing group that a Presto query should be sent to for the given environment |
| 140 | + :rtype: Text |
| 141 | + """ |
| 142 | + return self._routing_group |
| 143 | + |
| 144 | + @property |
| 145 | + def catalog(self): |
| 146 | + """ |
| 147 | + The catalog to set for the given Presto query |
| 148 | + :rtype: Text |
| 149 | + """ |
| 150 | + return self._catalog |
| 151 | + |
| 152 | + @property |
| 153 | + def schema(self): |
| 154 | + """ |
| 155 | + The schema to set for the given Presto query |
| 156 | + :rtype: Text |
| 157 | + """ |
| 158 | + return self._schema |
0 commit comments