2

We'd like to use configure_sql_data_node() where the read_query argument would be dynamic, in the sense that we would get some data from a data node and use that data to construct the query. E.g., you'd have a value month and the query would be in python f"SELECT * from mytable where MONTH={month}" so that the value of month is used in the query. It seems that the read_query argument has to be a static string.

How can we make the query that is used in an SQLDataNode be dynamic and dependent on other data?

Irv
  • 540
  • 4
  • 13
  • Hey Irv, Feel free to join our Discord Server discord.com/invite/SNNsCDj9? and connect with like-minded individuals and collaborate on your web application buildings :) – Rym Guerbi Michaut Jul 17 '23 at 09:55

1 Answers1

2

If it is an input Data Node, you can change the query before submitting a scenario:

scenario = tp.create_scenario(sc_config)
scenario.month.write("JAN")
scenario.month_data.read_query = f"SELECT * from mytable where MONTH={scenario.month.read()}"
scenario.submit()

Could you describe your use case? Is your SQL Data Node an Input Data Node, and would it solve your issue?

If it is not an input, the case is more complex. We can add a functionality to SQL data nodes in Taipy. Something similar to write_query_builder. A new property called read_query_builder. This builder would be a function executed at runtime before reading the data to build the query. You will need to change it at runtime too.

scenario = tp.create_scenario(sc_config)
def scenario_read_query_builder(scenario):
     return f"SELECT * from mytable where MONTH={scenario.month.read()}"
scenario.month_data.read_query_builder = scenario_read_query_builder
scenario.submit()
scenario.month_data.read()

The complexity comes from the fact that this query builder must have a generic signature since it is called automatically by the Taipy orchestrator. Plus, it must be serializable, so we can't really use partials here.

Florian Jacta
  • 1,143
  • 1
  • 2
  • 5
  • 1
    It's an input data node, so the solution you initially suggested about changing the `read_query` after the scenario is created will work. – Irv Jun 05 '23 at 14:16