2011-10-30 11:30:52 +00:00
# pragma once
2017-04-01 09:19:00 +00:00
# include <Core/QueryProcessingStage.h>
2021-09-16 17:40:42 +00:00
# include <Formats/FormatSettings.h>
2023-06-16 10:20:40 +00:00
# include <Interpreters/Context_fwd.h>
# include <Interpreters/QueryLog.h>
# include <QueryPipeline/BlockIO.h>
# include <memory>
# include <optional>
2011-10-30 11:30:52 +00:00
namespace DB
{
2023-06-16 10:20:40 +00:00
class IInterpreter ;
2019-05-25 14:15:22 +00:00
class ReadBuffer ;
class WriteBuffer ;
2023-08-01 10:06:56 +00:00
class IOutputFormat ;
2023-06-16 10:20:40 +00:00
struct QueryStatusInfo ;
2019-05-25 14:15:22 +00:00
2023-02-13 16:44:54 +00:00
struct QueryResultDetails
{
String query_id ;
2023-08-23 12:07:02 +00:00
std : : optional < String > content_type = { } ;
std : : optional < String > format = { } ;
std : : optional < String > timezone = { } ;
2023-02-13 16:44:54 +00:00
} ;
using SetResultDetailsFunc = std : : function < void ( const QueryResultDetails & ) > ;
2023-08-01 10:06:56 +00:00
using HandleExceptionInOutputFormatFunc = std : : function < void ( IOutputFormat & output_format ) > ;
2011-10-30 11:30:52 +00:00
2017-01-18 13:53:20 +00:00
/// Parse and execute a query.
2011-10-30 11:30:52 +00:00
void executeQuery (
2017-07-27 18:44:55 +00:00
ReadBuffer & istr , /// Where to read query from (and data for INSERT, if present).
WriteBuffer & ostr , /// Where to write query output to.
2017-01-18 13:53:20 +00:00
bool allow_into_outfile , /// If true and the query contains INTO OUTFILE section, redirect output to that file.
2021-07-16 10:10:56 +00:00
ContextMutablePtr context , /// DB, tables, data types, storage engines, functions, aggregate functions...
2021-08-01 22:12:15 +00:00
SetResultDetailsFunc set_result_details , /// If a non-empty callback is passed, it will be called with the query id, the content-type, the format, and the timezone.
2023-08-01 10:06:56 +00:00
const std : : optional < FormatSettings > & output_format_settings = std : : nullopt , /// Format settings for output format, will be calculated from the context if not set.
HandleExceptionInOutputFormatFunc handle_exception_in_output_format = { } /// If a non-empty callback is passed, it will be called on exception with created output format.
2019-10-29 10:27:19 +00:00
) ;
2011-10-30 11:30:52 +00:00
2012-03-11 08:52:56 +00:00
2017-01-18 13:53:20 +00:00
/// More low-level function for server-to-server interaction.
/// Prepares a query for execution but doesn't execute it.
/// Returns a pair of block streams which, when used, will result in query execution.
/// This means that the caller can to the extent control the query execution pipeline.
///
/// To execute:
/// * if present, write INSERT data into BlockIO::out
/// * then read the results from BlockIO::in.
///
/// If the query doesn't involve data insertion or returning of results, out and in respectively
/// will be equal to nullptr.
///
/// Correctly formatting the results (according to INTO OUTFILE and FORMAT sections)
/// must be done separately.
2012-03-11 08:52:56 +00:00
BlockIO executeQuery (
2020-03-28 03:02:26 +00:00
const String & query , /// Query text without INSERT data. The latter must be written to BlockIO::out.
2021-05-31 14:49:02 +00:00
ContextMutablePtr context , /// DB, tables, data types, storage engines, functions, aggregate functions...
2017-01-18 13:53:20 +00:00
bool internal = false , /// If true, this query is caused by another query and thus needn't be registered in the ProcessList.
2021-02-15 18:57:35 +00:00
QueryProcessingStage : : Enum stage = QueryProcessingStage : : Complete /// To which stage the query must be executed.
2019-10-29 10:27:19 +00:00
) ;
2012-03-11 08:52:56 +00:00
2020-05-19 14:06:33 +00:00
/// Old interface with allow_processors flag. For compatibility.
BlockIO executeQuery (
2021-06-28 20:43:37 +00:00
bool allow_processors , /// If can use processors pipeline
2020-05-19 14:06:33 +00:00
const String & query ,
2021-05-31 14:49:02 +00:00
ContextMutablePtr context ,
2021-06-28 20:43:37 +00:00
bool internal = false ,
QueryProcessingStage : : Enum stage = QueryProcessingStage : : Complete
2019-03-26 18:28:37 +00:00
) ;
2021-07-01 13:21:38 +00:00
/// Executes BlockIO returned from executeQuery(...)
/// if built pipeline does not require any input and does not produce any output.
void executeTrivialBlockIO ( BlockIO & streams , ContextPtr context ) ;
2023-06-16 10:20:40 +00:00
/// Prepares a QueryLogElement and, if enabled, logs it to system.query_log
QueryLogElement logQueryStart (
const std : : chrono : : time_point < std : : chrono : : system_clock > & query_start_time ,
const ContextMutablePtr & context ,
const String & query_for_logging ,
const ASTPtr & query_ast ,
const QueryPipeline & pipeline ,
const std : : unique_ptr < IInterpreter > & interpreter ,
bool internal ,
const String & query_database ,
2023-06-19 11:39:30 +00:00
const String & query_table ,
2023-06-20 07:52:11 +00:00
bool async_insert ) ;
2023-06-16 10:20:40 +00:00
void logQueryFinish (
QueryLogElement & elem ,
const ContextMutablePtr & context ,
const ASTPtr & query_ast ,
const QueryPipeline & query_pipeline ,
bool pulling_pipeline ,
std : : shared_ptr < OpenTelemetry : : SpanHolder > query_span ,
2023-07-20 18:36:00 +00:00
QueryCache : : Usage query_cache_usage ,
2023-06-16 10:20:40 +00:00
bool internal ) ;
void logQueryException (
QueryLogElement & elem ,
const ContextMutablePtr & context ,
const Stopwatch & start_watch ,
const ASTPtr & query_ast ,
std : : shared_ptr < OpenTelemetry : : SpanHolder > query_span ,
2023-06-19 11:39:30 +00:00
bool internal ,
bool log_error ) ;
2023-06-16 10:20:40 +00:00
void logExceptionBeforeStart (
const String & query_for_logging ,
ContextPtr context ,
ASTPtr ast ,
const std : : shared_ptr < OpenTelemetry : : SpanHolder > & query_span ,
2023-06-20 09:37:56 +00:00
UInt64 elapsed_millliseconds ) ;
2011-10-30 11:30:52 +00:00
}