uranusjr commented on code in PR #77080: URL: https://github.com/apache/airflow/pull/77080#discussion_r4626271638
########## airflow-core/adr/yaml-dags/0001-task-mapping-authoring-interface.md: ########## @@ -0,0 +1,211 @@ +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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. + --> + +# ADR-0001: Task Mapping — YAML Authoring Interface + +## Status + +Proposed. + +## Context + +What a mapped task is, and the rules every authoring surface follows, are in [Language SDK ADR-0016](../lang-sdk/0016-mapped-tasks-across-lang-sdks.md). This ADR records their spelling in YAML. `map` is deferred since it requires a way to reference a callable (see Decision 4 below). + +YAML has no method calls to chain. ADR-0016 makes `zip` and `concat` methods on a reference, so they chain in declaration order. YAML nests them instead. Since YAML already marks a value in place (`{$x: ...}` for an upstream output), an expanded argument is marked the same way: + +```yaml +tasks: + # Returns e.g. [1, 2, 3] + - id: list_files + run: {} + + # Equivalent to process.partial(bucket="raw").expand(path=list_files) + - id: process + run: + bucket: raw + path: {$expand: {$x: list_files}} + + # Reducing; equivalent to summarize(sizes=process) + - id: summarize + run: + sizes: {$x: process} +``` + +## Decision + +1. **An expanded argument is marked in place with `$expand`. Every unmarked argument is a `partial()`.** + - For example, `path: {$expand: <source>}` under `with:` or `run:` is Python's `.expand(path=<source>)`. + - Several `$expand` arguments give a cross product, as several `expand()` keywords do. It is not allowed to nest `$expand` inside a construct. + +2. **Expansion is possible on a literal list, a `$x`, or one of `$zip` and `$concat`.** + - A list gives one instance per item. Its items are literal values; a marker inside it is an error. Any other literal is an error. + - `{$x: task}` expands over the task's return value. When `task` is itself mapped, the instances line up one-to-one by map index. `$x` with a `key` is rejected, as in Python. + +3. **`$zip` and `$concat` spell Python's `zip()` and `concat()`.** + - `{$zip: [a, b, ...]}`, optionally with `default: v` beside the `$zip` key. Without `default`, the zip is as long as its shortest source; with it, as long as its longest, and `v` stands in for the missing items of the shorter ones. + - A marker's operand is the value of its `$` key, and its options are plain keys beside it. A key that is not one of that marker's options is an error. + - `{$concat: [a, b, ...]}`. + - What each takes as an item, and where either may appear, follows Language SDK ADR-0016 (Decision 4 and its authoring rules). + +4. **`map()` is deferred until YAML can reference a callable.** + - YAML cannot define one, and no mechanism yet references a callable in a task handler's language, so `$map` is rejected for now. + - When one exists, the intended spelling is `{$map: v, apply: f}`: the source is the operand, as for `$zip` and `$concat`, and `apply` is one callable. Nesting `$map` such as `{$map: {$map: v, apply: f1}, apply: f2}` is allowed. + +5. **`$expand` as a key of `with:` or `run:` is Python's `expand_kwargs()`.** + + ```yaml + id: foo + run: + path: {$: expand} + bucket: raw + size: {$: expand} + $expand: {$x: rows} + ``` + + is roughly equivalent to + + ```python + @task.stub + def foo(path, bucket, size): ... + + + foo.partial(bucket="raw").expand_kwargs(rows) + ``` + + - The value of a `$expand` indicates the source, as in Decision 2, except that a literal list's items must be mappings or `$x`. Each element is one instance's keyword arguments. Review Comment: It’s less worthwhile IMO to add a rule that would be lifted later anyway. Doing this fails cleanly at runtime anyway, so there’s no real harm not catching it at parse-time. I added a sentence to make it clearer this is not an oversight. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
