You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Called Session.sql() for a query that ends with a ; then called DataFrame.collect() and DataFrame.count()
What did you expect to see?
I expected to see the same behavior from a SQL compilation perspective. DataFrame.collect() worked as expected, DataFrame.count() raised a SQL compilation error
The same query without the semicolon runs fine in both cases.
from dotenv import load_dotenv
import logging
import os
import snowflake.snowpark as sp
def main():
load_dotenv()
args = {
"account": os.getenv("SNOWFLAKE_ACCOUNT"),
"user": os.getenv("SNOWFLAKE_USER"),
"authenticator": "externalbrowser",
}
queries = ("select query_start_time from snowflake.account_usage.access_history limit 1", "select query_start_time from snowflake.account_usage.access_history limit 1;")
session = sp.Session.builder.configs(args).create()
for query in queries:
print()
df = None
result = None
count = None
print(f"{query=}")
df = session.sql(query=query)
try:
result = df.collect()
print(f"{result=}")
except Exception as ex:
print(f"{ex=}")
try:
count = df.count()
print(f"{count=}")
except Exception as ex:
print(f"{ex=}")
if __name__ == "__main__":
for logger_name in ('snowflake.snowpark', 'snowflake.connector'):
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(logging.Formatter('%(asctime)s - %(threadName)s %(filename)s:%(lineno)d - %(funcName)s() - %(levelname)s - %(message)s'))
logger.addHandler(ch)
main()
Can you set logging to DEBUG and collect the logs?
The semicolon (;) is typically used to terminate SQL statements in interactive environments like SnowSQL, Snowflake Worksheets, or UI-based tools, where multiple queries can be executed in sequence.
However, when executing SQL queries programmatically (such as through the Snowpark API, Python connectors, or other programmatic interfaces), the semicolon is not required and can lead to errors if included. Programmatic interfaces treat each query separately, so the semicolon is unnecessary and can cause syntax errors.
To fix the issue:
You can remove the semi-colon
or
clean_query = query.rstrip(';')
queries = (
"select query_start_time from snowflake.account_usage.access_history limit 1",
"select query_start_time from snowflake.account_usage.access_history limit 1;"
)
for query in queries:
print()
df = None
result = None
count = None
# Remove trailing semicolon if it exists
clean_query = query.rstrip(';')
print(f"{clean_query=}")
df = session.sql(clean_query)
try:
result = df.collect()
print(f"{result=}")
except Exception as ex:
print(f"{ex=}")
try:
count = df.count()
print(f"{count=}")
except Exception as ex:
print(f"{ex=}")
Please answer these questions before submitting your issue. Thanks!
Python 3.11.10 (main, Sep 7 2024, 01:03:31) [Clang 15.0.0 (clang-1500.3.9.4)]
macOS-14.6.1-arm64-arm-64bit
pip freeze
)?asn1crypto==1.5.1
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.3.2
cloudpickle==2.2.1
cryptography==43.0.1
filelock==3.16.0
idna==3.8
packaging==24.1
platformdirs==4.3.2
pycparser==2.22
PyJWT==2.9.0
pyOpenSSL==24.2.1
python-dotenv==1.0.1
pytz==2024.2
PyYAML==6.0.2
requests==2.32.3
snowflake-connector-python==3.12.1
snowflake-snowpark-python==1.21.1
sortedcontainers==2.4.0
tomlkit==0.13.2
typing_extensions==4.12.2
urllib3==2.2.2
Called
Session.sql()
for a query that ends with a;
then calledDataFrame.collect()
andDataFrame.count()
I expected to see the same behavior from a SQL compilation perspective.
DataFrame.collect()
worked as expected,DataFrame.count()
raised aSQL compilation error
The same query without the semicolon runs fine in both cases.
The text was updated successfully, but these errors were encountered: