From 345444046ea901476a2f8d4386c43b583d8cb2df Mon Sep 17 00:00:00 2001 From: Marcin Raba Date: Mon, 29 Jan 2024 17:47:38 +0100 Subject: [PATCH 1/5] SNOW-1019480-sql-filter-nonsql-statements: strip comments from streamd queries --- src/snowflake/cli/plugins/sql/manager.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/snowflake/cli/plugins/sql/manager.py b/src/snowflake/cli/plugins/sql/manager.py index 18ab55c80..eaf8eef12 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)) From e1cc1d42d83f9c76f20b766d456bf59f4995abad Mon Sep 17 00:00:00 2001 From: Marcin Raba Date: Wed, 31 Jan 2024 09:11:33 +0100 Subject: [PATCH 2/5] SNOW-1019480-sql-filter-nonsql-statements: test for railing query --- tests_integration/test_sql.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests_integration/test_sql.py b/tests_integration/test_sql.py index 5a35dc7d7..09dfca23a 100644 --- a/tests_integration/test_sql.py +++ b/tests_integration/test_sql.py @@ -100,3 +100,14 @@ def _log( assert query_0 == "13" assert time_1 - time_0 >= 10.0 assert "waited 10 seconds" in query_1 + + +@pytest.mark.integration +@pytest.mark.usefixtures("snowflake_session, test_root_path") +def test_trailing_comments_queries(runner): + 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"], + ] From 9fbca6126837a4871c841ad3d5962ac9e6116d1c Mon Sep 17 00:00:00 2001 From: Marcin Raba Date: Wed, 31 Jan 2024 09:14:28 +0100 Subject: [PATCH 3/5] SNOW-1019480-sql-filter-nonsql-statements: fix test response for failing query --- tests_integration/test_sql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests_integration/test_sql.py b/tests_integration/test_sql.py index 09dfca23a..2b62b6bbe 100644 --- a/tests_integration/test_sql.py +++ b/tests_integration/test_sql.py @@ -109,5 +109,5 @@ def test_trailing_comments_queries(runner): result = runner.invoke_with_connection_json(["sql", "-q", trailin_comment_query]) assert result.exit_code == 0 assert result.json == [ - ["1"], + {"1": 1}, ] From 966588ad15f76bc747c446cb6251c3f70f270121 Mon Sep 17 00:00:00 2001 From: Marcin Raba Date: Wed, 31 Jan 2024 09:24:23 +0100 Subject: [PATCH 4/5] SNOW-1019480-sql-filter-nonsql-statements: remove usefixtures from test --- tests_integration/test_sql.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests_integration/test_sql.py b/tests_integration/test_sql.py index 2b62b6bbe..f3cb0403a 100644 --- a/tests_integration/test_sql.py +++ b/tests_integration/test_sql.py @@ -103,8 +103,7 @@ def _log( @pytest.mark.integration -@pytest.mark.usefixtures("snowflake_session, test_root_path") -def test_trailing_comments_queries(runner): +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 From affdf8b66194cf0c765bd70e66c6789b490b2e31 Mon Sep 17 00:00:00 2001 From: Marcin Raba Date: Wed, 31 Jan 2024 09:31:40 +0100 Subject: [PATCH 5/5] SNOW-1019480-sql-filter-nonsql-statements: fix expected response value --- tests_integration/test_sql.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests_integration/test_sql.py b/tests_integration/test_sql.py index f3cb0403a..09d8b98be 100644 --- a/tests_integration/test_sql.py +++ b/tests_integration/test_sql.py @@ -108,5 +108,7 @@ def test_trailing_comments_queries(runner, snowflake_session, test_root_path): result = runner.invoke_with_connection_json(["sql", "-q", trailin_comment_query]) assert result.exit_code == 0 assert result.json == [ - {"1": 1}, + [ + {"1": 1}, + ], ]