2424
2525#include < ydb/core/base/cputime.h>
2626
27- namespace NKikimr {
28- namespace NKqp {
27+ namespace NKikimr ::NKqp {
2928
3029static const TString YqlName = " CompileActor" ;
3130
@@ -69,7 +68,8 @@ class TKqpCompileActor : public TActorBootstrapped<TKqpCompileActor> {
6968 , UserToken(userToken)
7069 , ClientAddress(clientAddress)
7170 , DbCounters(dbCounters)
72- , Config(MakeIntrusive<TKikimrConfiguration>())
71+ , KqpSettings(kqpSettings)
72+ , TableServiceConfig(tableServiceConfig)
7373 , QueryServiceConfig(queryServiceConfig)
7474 , CompilationTimeout(TDuration::MilliSeconds(tableServiceConfig.GetCompileTimeoutMs()))
7575 , SplitCtx(std::move(splitCtx))
@@ -80,26 +80,49 @@ class TKqpCompileActor : public TActorBootstrapped<TKqpCompileActor> {
8080 , CollectFullDiagnostics(collectFullDiagnostics)
8181 , CompileAction(compileAction)
8282 , QueryAst(std::move(queryAst))
83+ , EnforcedSqlVersion(tableServiceConfig.GetEnforceSqlVersionV1())
8384 {
84- Config->Init (kqpSettings->DefaultSettings .GetDefaultSettings (), QueryId.Cluster , kqpSettings->Settings , false );
85+ Config = BuildConfiguration (tableServiceConfig);
86+ PerStatementResult = perStatementResult && Config->EnablePerStatementQueryExecution ;
87+ }
88+
89+ TKikimrConfiguration::TPtr BuildConfiguration (const TTableServiceConfig& tableServiceConfig) {
90+ NYql::TKikimrConfiguration::TPtr config = MakeIntrusive<TKikimrConfiguration>();
91+
92+ config->Init (KqpSettings->DefaultSettings .GetDefaultSettings (), QueryId.Cluster , KqpSettings->Settings , false );
8593
8694 if (!QueryId.Database .empty ()) {
87- Config ->_KqpTablePathPrefix = QueryId.Database ;
95+ config ->_KqpTablePathPrefix = QueryId.Database ;
8896 }
8997
90- ApplyServiceConfig (*Config, tableServiceConfig);
98+ ApplyServiceConfig (*config, tableServiceConfig);
99+
100+ if (!tableServiceConfig.HasSqlVersion () || tableServiceConfig.GetSqlVersion () != 0 ) {
101+ EnforcedSqlVersion = false ;
102+ } else if (EnforcedSqlVersion) {
103+ LOG_DEBUG_S (*TlsActivationContext, NKikimrServices::KQP_COMPILE_ACTOR,
104+ " Enforced SQL version 1, "
105+ << " current sql version: " << tableServiceConfig.GetSqlVersion ()
106+ << " queryText: " << EscapeC (QueryId.Text )
107+ );
108+
109+ config->_KqpYqlSyntaxVersion = 1 ;
110+ } else {
111+ EnforcedSqlVersion = false ;
112+ }
91113
92114 if (QueryId.Settings .QueryType == NKikimrKqp::QUERY_TYPE_SQL_GENERIC_SCRIPT || QueryId.Settings .QueryType == NKikimrKqp::QUERY_TYPE_SQL_GENERIC_QUERY) {
93115 ui32 scriptResultRowsLimit = QueryServiceConfig.GetScriptResultRowsLimit ();
94116 if (scriptResultRowsLimit > 0 ) {
95- Config ->_ResultRowsLimit = scriptResultRowsLimit;
117+ config ->_ResultRowsLimit = scriptResultRowsLimit;
96118 } else {
97- Config ->_ResultRowsLimit .Clear ();
119+ config ->_ResultRowsLimit .Clear ();
98120 }
99121 }
100- PerStatementResult = perStatementResult && Config->EnablePerStatementQueryExecution ;
101122
102- Config->FreezeDefaults ();
123+ config->FreezeDefaults ();
124+
125+ return config;
103126 }
104127
105128 void Bootstrap (const TActorContext& ctx) {
@@ -253,7 +276,12 @@ class TKqpCompileActor : public TActorBootstrapped<TKqpCompileActor> {
253276 TYqlLogScope logScope (ctx, NKikimrServices::KQP_YQL, YqlName, UserRequestContext->TraceId );
254277
255278 auto prepareSettings = PrepareCompilationSettings (ctx);
279+ StartCompilationWithSettings (prepareSettings);
280+ Continue (ctx);
281+ Become (&TKqpCompileActor::CompileState);
282+ }
256283
284+ void StartCompilationWithSettings (IKqpHost::TPrepareSettings& prepareSettings) {
257285 NCpuTime::TCpuTimer timer (CompileCpuTime);
258286
259287 switch (QueryId.Settings .QueryType ) {
@@ -286,9 +314,6 @@ class TKqpCompileActor : public TActorBootstrapped<TKqpCompileActor> {
286314 default :
287315 YQL_ENSURE (false , " Unexpected query type: " << QueryId.Settings .QueryType );
288316 }
289-
290- Continue (ctx);
291- Become (&TKqpCompileActor::CompileState);
292317 }
293318
294319 void Continue (const TActorContext &ctx) {
@@ -305,6 +330,7 @@ class TKqpCompileActor : public TActorBootstrapped<TKqpCompileActor> {
305330 }
306331
307332 IKqpHost::TPrepareSettings PrepareCompilationSettings (const TActorContext &ctx) {
333+ // If CurrentSqlVersion differs from the frozen Config, create a new Config with updated SqlVersion
308334 TKqpRequestCounters::TPtr counters = new TKqpRequestCounters;
309335 counters->Counters = Counters;
310336 counters->DbCounters = DbCounters;
@@ -532,6 +558,25 @@ class TKqpCompileActor : public TActorBootstrapped<TKqpCompileActor> {
532558 return ;
533559 }
534560
561+ // If compilation failed and we tried SqlVersion = 1, retry with SqlVersion = 0
562+ if (EnforcedSqlVersion && status != Ydb::StatusIds::SUCCESS) {
563+ Counters->ReportCompileEnforceConfigFailed (DbCounters);
564+ LOG_ERROR_S (ctx, NKikimrServices::KQP_COMPILE_ACTOR, " Compilation with SqlVersion = 1 failed, retrying with SqlVersion = 0"
565+ << " , self: " << ctx.SelfID
566+ << " , database: " << QueryId.Database
567+ << " , text: \" " << EscapeC (QueryId.Text ) << " \" " );
568+
569+ EnforcedSqlVersion = false ;
570+ Config = BuildConfiguration (TableServiceConfig);
571+ auto prepareSettings = PrepareCompilationSettings (ctx);
572+
573+ StartCompilationWithSettings (prepareSettings);
574+ Continue (ctx);
575+ return ;
576+ } else if (EnforcedSqlVersion && status == Ydb::StatusIds::SUCCESS) {
577+ Counters->ReportCompileEnforceConfigSuccess (DbCounters);
578+ }
579+
535580 auto database = QueryId.Database ;
536581 if (kqpResult.SqlVersion ) {
537582 Counters->ReportSqlVersion (DbCounters, *kqpResult.SqlVersion );
@@ -613,6 +658,8 @@ class TKqpCompileActor : public TActorBootstrapped<TKqpCompileActor> {
613658 TString ClientAddress;
614659 TKqpDbCountersPtr DbCounters;
615660 TKikimrConfiguration::TPtr Config;
661+ TKqpSettings::TConstPtr KqpSettings;
662+ TTableServiceConfig TableServiceConfig;
616663 TQueryServiceConfig QueryServiceConfig;
617664 TDuration CompilationTimeout;
618665 TInstant StartTime;
@@ -638,6 +685,7 @@ class TKqpCompileActor : public TActorBootstrapped<TKqpCompileActor> {
638685 bool PerStatementResult;
639686 ECompileActorAction CompileAction;
640687 TMaybe<TQueryAst> QueryAst;
688+ bool EnforcedSqlVersion;
641689};
642690
643691void ApplyServiceConfig (TKikimrConfiguration& kqpConfig, const TTableServiceConfig& serviceConfig) {
@@ -731,5 +779,4 @@ IActor* CreateKqpCompileActor(const TActorId& owner, const TKqpSettings::TConstP
731779 std::move (splitCtx), std::move (splitExpr));
732780}
733781
734- } // namespace NKqp
735- } // namespace NKikimr
782+ } // namespace NKikimr::NKqp
0 commit comments