snowflake with snowpark error: sql() missing 1 required positional argument: 'query'

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

snowflake with snowpark error: sql() missing 1 required positional argument: 'query'

问题

我正在尝试在Snowpark上执行一些简单的代码,但出现了错误。我熟悉这个错误,只是不确定为什么会出现这个错误:

代码如下:

# Python工作表需要Snowpark包。
# 你可以通过选择Packages控制并导入更多包来添加更多包。

import snowflake.snowpark as snowpark
from snowflake.snowpark.functions import col
from snowflake.snowpark.session import Session

df = Session.sql('select * from snowflake_sample_data.tpch_sf10.lineitem')

我收到以下错误:

英文:

I am trying to execute some simple code on Snowpark, but getting an error. I am familiar with the error, just not sure why I should be getting the error:

The code is as follows:

# The Snowpark package is required for Python Worksheets. 
# You can add more packages by selecting them using the Packages control and then importing them.

import snowflake.snowpark as snowpark
from snowflake.snowpark.functions import col
from snowflake.snowpark.session import Session

df = Session.sql('select * from snowflake_sample_data.tpch_sf10.lineitem')

I'm getting the following error:

snowflake with snowpark error: sql() missing 1 required positional argument: 'query'

Any thoughts?

答案1

得分: 1

以下是您要翻译的内容:

当使用Python工作表时,您需要使用已提供的“session”:

第一个参数是“self”,这就是它抱怨缺少“query”的原因。

sql() 不是静态/类方法。

尝试在Python工作表内创建另一个会话也会失败:

import snowflake.snowpark as snowpark
from snowflake.snowpark.session import Session

df = Session.builder.create().sql('select* from snowflake_sample_data.tpch_sf10.lineitem')

尝试在“main”之外使用“session”:

解决方案:

import snowflake.snowpark as snowpark

def main(session: snowpark.Session):
    df = session.sql('select top 10 * from snowflake_sample_data.tpch_sf10.lineitem')
    return df
英文:

When using Python Worksheet you need to use session that is already provided:

snowflake with snowpark error: sql() missing 1 required positional argument: 'query'

The first argument is "self", that is the reason it is complaining about missing "query".

sql() is NOT a static/class method.

> snowflake.snowpark.Session.sql
>
> def sql(self, query: str, params: Optional[Sequence[Any]] = None)-> DataFrame

Trying to create another session inside Python Worksheet will also fail:

import snowflake.snowpark as snowpark
from snowflake.snowpark.session import Session

df = Session.builder.create().sql('select* from snowflake_sample_data.tpch_sf10.lineitem')

> snowflake.connector.errors.ProgrammingError: Connection was already created. We don't allow user to create their own connection inside a stored procedure. Please use the connection in the provided session from the handler.

Trying to use session outside main:

snowflake with snowpark error: sql() missing 1 required positional argument: 'query'


Solution:

import snowflake.snowpark as snowpark

def main(session: snowpark.Session):
    df = session.sql('select top 10 * from snowflake_sample_data.tpch_sf10.lineitem')
    return df

snowflake with snowpark error: sql() missing 1 required positional argument: 'query'

huangapple
  • 本文由 发表于 2023年5月14日 00:08:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243704.html
匿名

发表评论

匿名网友

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

确定