From 5e0052b78d3284697ac295226eebccf53693fc6a Mon Sep 17 00:00:00 2001 From: Marcin Raba Date: Wed, 31 Jan 2024 10:08:34 +0100 Subject: [PATCH] =?UTF-8?q?SNOW-1019480-sql-filter-nonsql-statements:=20st?= =?UTF-8?q?rip=20comments=20from=20stream=E2=80=A6=20(#700)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * SNOW-1019480-sql-filter-nonsql-statements: strip comments from streamed queries --- src/snowflake/cli/plugins/sql/manager.py | 9 +++++++-- tests_integration/test_sql.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/snowflake/cli/plugins/sql/manager.py b/src/snowflake/cli/plugins/sql/manager.py index 18ab55c80f..eaf8eef12b 100644 --- a/src/snowflake/cli/plugins/sql/manager.py +++ b/src/snowflake/cli/plugins/sql/manager.py @@ -28,5 +28,10 @@ def execute( elif file: query = file.read_text() - single_statement = len(list(split_statements(StringIO(query)))) == 1 - return single_statement, self._execute_string(query) + statements = tuple( + statement + for statement, _ in split_statements(StringIO(query), remove_comments=True) + ) + single_statement = len(statements) == 1 + + return single_statement, self._execute_string("\n".join(statements)) diff --git a/tests_integration/test_sql.py b/tests_integration/test_sql.py index 5a35dc7d7a..09d8b98be4 100644 --- a/tests_integration/test_sql.py +++ b/tests_integration/test_sql.py @@ -100,3 +100,15 @@ def _log( assert query_0 == "13" assert time_1 - time_0 >= 10.0 assert "waited 10 seconds" in query_1 + + +@pytest.mark.integration +def test_trailing_comments_queries(runner, snowflake_session, test_root_path): + trailin_comment_query = "select 1;\n\n-- trailing comment\n" + result = runner.invoke_with_connection_json(["sql", "-q", trailin_comment_query]) + assert result.exit_code == 0 + assert result.json == [ + [ + {"1": 1}, + ], + ]