在 taipy 中的 `configure_sql_data_node()` 中使用动态的 `read_query`。

huangapple go评论74阅读模式
英文:

Using a dynamic `read_query` in `configure_sql_data_node()` in taipy

问题

我们希望在使用configure_sql_data_node()时,read_query参数能够是动态的,也就是说我们可以从数据节点获取一些数据,然后使用这些数据构造查询语句。例如,您可能会有一个名为month的值,查询语句可以是Python中的 f"SELECT * from mytable where MONTH={month}",以便将month的值用于查询。看起来read_query参数必须是一个静态字符串。

如何使在SQLDataNode中使用的查询能够是动态的,并依赖于其他数据呢?

英文:

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?

答案1

得分: 2

如果它是一个输入数据节点,您可以在提交场景之前更改查询:

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()

你能描述一下你的用例吗?你的SQL数据节点是一个输入数据节点吗?它能解决你的问题吗?

如果不是输入节点,情况就更复杂了。
我们可以在Taipy中为SQL数据节点添加一种类似于write_query_builder的功能。一个名为read_query_builder的新属性。这个生成器将在运行时执行,用于在读取数据之前构建查询。您需要在运行时更改它。

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()

复杂之处在于这个查询生成器必须具有通用签名,因为它会被Taipy orchestrator自动调用。而且,它必须是可序列化的,所以我们不能在这里使用部分函数。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月25日 04:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定