From 2a8e47927789d3d8b3d87794bfce0e22bb94aae9 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Mon, 4 Apr 2022 15:56:01 +0200 Subject: [PATCH 1/3] ExecutableUserDefinedFunction prevent function execution during query analysis --- src/Common/ProfileEvents.cpp | 2 ++ src/Common/ShellCommand.cpp | 6 ++++++ src/Interpreters/UserDefinedExecutableFunctionFactory.cpp | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/src/Common/ProfileEvents.cpp b/src/Common/ProfileEvents.cpp index 074ec02394b..3f55970f3aa 100644 --- a/src/Common/ProfileEvents.cpp +++ b/src/Common/ProfileEvents.cpp @@ -112,6 +112,8 @@ M(CompileExpressionsMicroseconds, "Total time spent for compilation of expressions to LLVM code.") \ M(CompileExpressionsBytes, "Number of bytes used for expressions compilation.") \ \ + M(ExecuteShellCommand, "Number of shell command executions.") \ + \ M(ExternalSortWritePart, "") \ M(ExternalSortMerge, "") \ M(ExternalAggregationWritePart, "") \ diff --git a/src/Common/ShellCommand.cpp b/src/Common/ShellCommand.cpp index 0093d72e766..229807c868e 100644 --- a/src/Common/ShellCommand.cpp +++ b/src/Common/ShellCommand.cpp @@ -29,6 +29,11 @@ namespace }; } +namespace ProfileEvents +{ + extern const int ExecuteShellCommand; +} + namespace DB { @@ -158,6 +163,7 @@ std::unique_ptr ShellCommand::executeImpl( const Config & config) { logCommand(filename, argv); + ProfileEvents::increment(ProfileEvents::ExecuteShellCommand); #if !defined(USE_MUSL) /** Here it is written that with a normal call `vfork`, there is a chance of deadlock in multithreaded programs, diff --git a/src/Interpreters/UserDefinedExecutableFunctionFactory.cpp b/src/Interpreters/UserDefinedExecutableFunctionFactory.cpp index 6d7dee7a4c7..d3a38f42e21 100644 --- a/src/Interpreters/UserDefinedExecutableFunctionFactory.cpp +++ b/src/Interpreters/UserDefinedExecutableFunctionFactory.cpp @@ -57,6 +57,10 @@ public: ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override { + /// Do not start user defined script during query analysis. Because user script startup could be heavy. + if (input_rows_count == 0) + return result_type->createColumn(); + auto coordinator = executable_function->getCoordinator(); const auto & coordinator_configuration = coordinator->getConfiguration(); const auto & configuration = executable_function->getConfiguration(); From 482c8f667cee0bbc713ada47f9e129037988a72d Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Mon, 4 Apr 2022 16:10:19 +0200 Subject: [PATCH 2/3] Added tests --- ...table_user_defined_function_short_circuit.reference | 1 + ..._executable_user_defined_function_short_circuit.sql | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/queries/0_stateless/02252_executable_user_defined_function_short_circuit.reference create mode 100644 tests/queries/0_stateless/02252_executable_user_defined_function_short_circuit.sql diff --git a/tests/queries/0_stateless/02252_executable_user_defined_function_short_circuit.reference b/tests/queries/0_stateless/02252_executable_user_defined_function_short_circuit.reference new file mode 100644 index 00000000000..573541ac970 --- /dev/null +++ b/tests/queries/0_stateless/02252_executable_user_defined_function_short_circuit.reference @@ -0,0 +1 @@ +0 diff --git a/tests/queries/0_stateless/02252_executable_user_defined_function_short_circuit.sql b/tests/queries/0_stateless/02252_executable_user_defined_function_short_circuit.sql new file mode 100644 index 00000000000..a475ba33740 --- /dev/null +++ b/tests/queries/0_stateless/02252_executable_user_defined_function_short_circuit.sql @@ -0,0 +1,10 @@ +SELECT number FROM numbers(10) WHERE number > 15 and test_function(number, number) == 4; + +SYSTEM FLUSH LOGS; + +SELECT ProfileEvents['ExecuteShellCommand'] FROM system.query_log WHERE + current_database = currentDatabase() + AND type = 'QueryFinish' + AND query == 'SELECT number FROM numbers(10) WHERE number > 15 and test_function(number, number) == 4;' + AND event_date >= yesterday() AND event_time > now() - interval 10 minute + LIMIT 1; From 6e1d8374394b993811c56cfe962ae29f13efad8e Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Tue, 5 Apr 2022 11:24:33 +0200 Subject: [PATCH 3/3] Fixed style check --- src/Common/ShellCommand.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/ShellCommand.cpp b/src/Common/ShellCommand.cpp index 229807c868e..f24add7acf0 100644 --- a/src/Common/ShellCommand.cpp +++ b/src/Common/ShellCommand.cpp @@ -31,7 +31,7 @@ namespace namespace ProfileEvents { - extern const int ExecuteShellCommand; + extern const Event ExecuteShellCommand; } namespace DB