"Execute Python Script" 模块中的 Azure ML:sqlite3 不支持通用 table 表达式

Azure ML in "Execute Python Script" module :Common table expressions is not supported in sqlite3

我昨天 运行 解决了这个问题,同时尝试使用我在 Azure ML 的 "Apply SQL Transformation" 模块中使用的相同 sqlite 脚本,在 Azure ML 的 Python 模块上的 Sqlite 中:

with tbl as (select * from t1)
select * from tbl

这是我得到的错误:

[Critical]     Error: Error 0085: The following error occurred during script evaluation, please view the output log for more information:
---------- Start of error message from Python interpreter ----------
  File "C:\server\invokepy.py", line 169, in batch
data:text/plain,Caught exception while executing function: Traceback (most recent call last):
    odfs = mod.azureml_main(*idfs)
  File "C:\pyhome\lib\site-packages\pandas\io\sql.py", line 388, in read_sql
  File "C:\temp\azuremod.py", line 193, in azureml_main
    results = pd.read_sql(query,con)
    coerce_float=coerce_float, parse_dates=parse_dates)
  File "C:\pyhome\lib\site-packages\pandas\io\sql.py", line 1017, in execute
  File "C:\pyhome\lib\site-packages\pandas\io\sql.py", line 1022, in read_sql
    cursor = self.execute(*args)
    raise_with_traceback(ex)
  File "C:\pyhome\lib\site-packages\pandas\io\sql.py", line 1006, in execute
---------- End of error message from Python  interpreter  ----------
    cur.execute(*args)
DatabaseError: Execution failed on sql:  with tbl as (select * from t1)
                    select * from tbl

和Python代码:

def azureml_main(dataframe1 = None, dataframe2 = None):
    import pandas as pd
    import sqlite3 as lite
    import sys
    con = lite.connect('data1.db')
    con.text_factory = str
    with con:
        cur = con.cursor()

        if (dataframe1 is not None):
            cur.execute("DROP TABLE IF EXISTS t1")
            dataframe1.to_sql('t1',con)
        query = '''with tbl as (select * from t1)
                    select * from tbl'''                      
        results = pd.read_sql(query,con)    

    return results,

将查询替换为:

select * from t1

它按预期工作。 正如您可能知道的那样,通用 table 表达式是 Sqlite 中的一个关键特性,运行 递归代码的能力在任何函数式语言(例如 Sqlite)中都是 "must have"。

我还尝试在 Azure 的 Jupyter Notebook 中 运行 我的 Python 脚本,它也按预期工作。

我们在 Python 模块中的 Sqlite 配置是否可能与在 Jupyter Notebook 和 "Apply SQL Transformation" 模块中的配置不同?

我重现了您的问题并查看了 pandas.io.sqlhttp://pandas.pydata.org/pandas-docs/stable/io.html#sql-queriesSQL Queries 文档。我尝试用read_sql_query来解决,但是失败了。

根据 pandas 文档,tt 似乎 Pandas 不支持此 SQL 语法的用法。

根据我的经验和您的 SQL,我尝试使用 SQL select * from (select * from t1) as tbl 而不是您的 SQL Pandas .

希望对您有所帮助。最好的祝福。