diff --git a/src/Client/ClientBase.cpp b/src/Client/ClientBase.cpp index ef0063df886..38d7f78fdac 100644 --- a/src/Client/ClientBase.cpp +++ b/src/Client/ClientBase.cpp @@ -2171,7 +2171,8 @@ bool ClientBase::executeMultiQuery(const String & all_queries_text) const char * this_query_begin = all_queries_text.data() + test_tags_length; const char * this_query_end; const char * all_queries_end = all_queries_text.data() + all_queries_text.size(); - UInt64 current_line = std::count(all_queries_text.c_str(), this_query_begin, '\n'); + const char * last_query_begin = this_query_begin; + UInt64 current_line = std::count(all_queries_text.c_str(), this_query_begin, '\n') + 1; String full_query; // full_query is the query + inline INSERT data + trailing comments (the latter is our best guess for now). String query_to_execute; @@ -2182,12 +2183,12 @@ bool ClientBase::executeMultiQuery(const String & all_queries_text) while (true) { - const char * last_query_begin = this_query_begin; auto stage = analyzeMultiQueryText(this_query_begin, this_query_end, all_queries_end, query_to_execute, parsed_query, all_queries_text, current_exception); - current_line += std::count(last_query_begin, this_query_begin, '\n') + 1; + current_line += std::count(last_query_begin, this_query_begin, '\n'); client_context->setSetting("script_line_number", current_line); + last_query_begin = this_query_begin; switch (stage) { @@ -2682,7 +2683,6 @@ bool ClientBase::processMultiQueryFromFile(const String & file_name) settings.log_comment = fs::absolute(fs::path(file_name)); client_context->setSettings(settings); } - return executeMultiQuery(queries_from_file); } diff --git a/src/Interpreters/QueryLog.cpp b/src/Interpreters/QueryLog.cpp index 74a56e72939..773213371c2 100644 --- a/src/Interpreters/QueryLog.cpp +++ b/src/Interpreters/QueryLog.cpp @@ -230,6 +230,7 @@ void QueryLogElement::appendToBlock(MutableColumns & columns) const columns[i++]->insert(ClickHouseRevision::getVersionRevision()); columns[i++]->insertData(log_comment.data(), log_comment.size()); + columns[i++]->insert(script_line_number); { Array threads_array; diff --git a/tests/queries/0_stateless/03221_script_line_number.reference b/tests/queries/0_stateless/03221_script_line_number.reference new file mode 100644 index 00000000000..fd279ddd7dc --- /dev/null +++ b/tests/queries/0_stateless/03221_script_line_number.reference @@ -0,0 +1,15 @@ +QueryStart script_line_number_test DROP DATABASE IF EXISTS \n03221_db; 7 +QueryStart script_line_number_test DROP DATABASE IF EXISTS \n03221_db1; 9 +QueryStart script_line_number_test DROP DATABASE IF EXISTS \n03221_db2; 13 +QueryStart script_line_number_test CREATE DATABASE 03221_db; 16 +QueryStart script_line_number_test CREATE DATABASE\n03221_db1; 17 +QueryStart script_line_number_test CREATE DATABASE\n\n03221_db2; 19 +QueryStart script_line_number_test CREATE TABLE 03221_db.t (n Int8) ENGINE=MergeTree ORDER BY n; 24 +QueryStart script_line_number_test CREATE TABLE 03221_db1.t1 (n Int8) ENGINE=MergeTree ORDER BY n; 25 +QueryStart script_line_number_test CREATE TABLE 03221_db2.t2 (n Int8) ENGINE=MergeTree ORDER BY n; 26 +QueryStart script_line_number_test INSERT INTO 03221_db.t \nSELECT * FROM numbers(10); 28 +QueryStart script_line_number_test INSERT \nINTO \n03221_db1.t1\nSELECT\n*\nFROM\nnumbers(10); 31 +QueryStart script_line_number_test INSERT INTO 03221_db2.t2 \n\nSELECT * FROM numbers(10); 39 +QueryStart script_line_number_test DROP \nDATABASE 03221_db; 46 +QueryStart script_line_number_test DROP DATABASE\n03221_db1; 48 +QueryStart script_line_number_test DROP\nDATABASE\n03221_db2; 50 diff --git a/tests/queries/0_stateless/03221_script_line_number.sql b/tests/queries/0_stateless/03221_script_line_number.sql new file mode 100644 index 00000000000..77e3269ac63 --- /dev/null +++ b/tests/queries/0_stateless/03221_script_line_number.sql @@ -0,0 +1,55 @@ + + + + +SET log_comment = 'script_line_number_test', log_queries = 1; + +DROP DATABASE IF EXISTS +03221_db; +DROP DATABASE IF EXISTS +03221_db1; + + +DROP DATABASE IF EXISTS +03221_db2; + +CREATE DATABASE 03221_db; +CREATE DATABASE +03221_db1; +CREATE DATABASE + +03221_db2; + + +CREATE TABLE 03221_db.t (n Int8) ENGINE=MergeTree ORDER BY n; +CREATE TABLE 03221_db1.t1 (n Int8) ENGINE=MergeTree ORDER BY n; +CREATE TABLE 03221_db2.t2 (n Int8) ENGINE=MergeTree ORDER BY n; + +INSERT INTO 03221_db.t +SELECT * FROM numbers(10); + +INSERT +INTO +03221_db1.t1 +SELECT +* +FROM +numbers(10); + +INSERT INTO 03221_db2.t2 + +SELECT * FROM numbers(10); + + + + +DROP +DATABASE 03221_db; +DROP DATABASE +03221_db1; +DROP +DATABASE +03221_db2; + +SYSTEM FLUSH LOGS; +SELECT type, log_comment, query, script_line_number FROM system.query_log WHERE log_comment = 'script_line_number_test' AND type = 1 AND current_database = currentDatabase(); \ No newline at end of file