Hi folks, one pattern that we keep running into is...
# ask-community
j
Hi folks, one pattern that we keep running into is using Parameters to override the result of a task. For example, we want to use some value from a database or the Parameter specified by the user.
Copy code
with Flow("Update Data") as flow:
    last_export = get_last_export("my_table")

    update_from = Parameter("update_from", default=last_export)

    insert_query = parse_insert_query(update_from=update_from)

    run_execute_query(insert_query)
This doesn’t work but I hope it illustrates what I’m trying to do. Is there any way to do this without having to write an intermediate task to “merge” the two inputs? I realize this pattern is wasteful by running
get_last_export
for no reason if parameter is provided.
a
I think what you’re looking for can be addressed by using the if/else control flow, but I don’t have much experience with it https://docs.prefect.io/api/latest/tasks/control_flow.html#functions hope this helps
j
You could do this with control flow, something like the following should work:
Copy code
from prefect import case, Flow, Parameter
from prefect.tasks.control_flow import merge

with Flow("example"):
    update_from_input = Parameter("update_from", default=None)
    with case(update_from_input, None):
        last_export = get_last_export("my_table")
    update_from = merge(last_export, update_from_input)
I think pushing the optional parameter check into
get_last_export
might be cleaner though, but up to you.
Copy code
from prefect import task, Flow, Parameter

@task
def maybe_get_last_export(update_from, table):
    if update_from is not None:
        return update_from
    return get_last_export(table)


with Flow("example"):
    update_from_input = Parameter("update_from", default=None)
    update_from = maybe_get_last_export(update_from_input)
j
Oh I should RTFM…thanks folks. As far as putting the logic into
get_last_export
, the Task definition is gonna get added to an internal Task library, and I think keeping the merge logic visible in the flow is better. Of course if this was going to be a functionaltask I’d definitely agree with you.