23
23
import org .apache .doris .catalog .OlapTable ;
24
24
import org .apache .doris .catalog .Partition ;
25
25
import org .apache .doris .catalog .TableIf ;
26
+ import org .apache .doris .catalog .TableIf .TableType ;
26
27
import org .apache .doris .catalog .View ;
27
28
import org .apache .doris .common .Config ;
28
29
import org .apache .doris .common .ConfigBase .DefaultConfHandler ;
39
40
import org .apache .doris .nereids .SqlCacheContext .FullColumnName ;
40
41
import org .apache .doris .nereids .SqlCacheContext .FullTableName ;
41
42
import org .apache .doris .nereids .SqlCacheContext .ScanTable ;
43
+ import org .apache .doris .nereids .SqlCacheContext .TableVersion ;
42
44
import org .apache .doris .nereids .StatementContext ;
43
45
import org .apache .doris .nereids .analyzer .UnboundVariable ;
44
46
import org .apache .doris .nereids .parser .NereidsParser ;
@@ -199,14 +201,14 @@ public Optional<LogicalSqlCache> tryParseSql(ConnectContext connectContext, Stri
199
201
.getSqlCacheContext ().ifPresent (ctx -> ctx .setCacheKeyType (CacheKeyType .MD5 ));
200
202
201
203
if (sqlCacheContextWithVariable != null ) {
202
- return tryParseSqlWithoutCheckVariable (
203
- connectContext , md5CacheKey , sqlCacheContextWithVariable , currentUserIdentity
204
+ return tryParseSql (
205
+ connectContext , md5CacheKey , sqlCacheContextWithVariable , currentUserIdentity , true
204
206
);
205
207
} else {
206
208
return Optional .empty ();
207
209
}
208
210
} else {
209
- return tryParseSqlWithoutCheckVariable (connectContext , key , sqlCacheContext , currentUserIdentity );
211
+ return tryParseSql (connectContext , key , sqlCacheContext , currentUserIdentity , false );
210
212
}
211
213
}
212
214
@@ -223,9 +225,9 @@ private String normalizeSql(String sql) {
223
225
return NereidsParser .removeCommentAndTrimBlank (sql );
224
226
}
225
227
226
- private Optional <LogicalSqlCache > tryParseSqlWithoutCheckVariable (
227
- ConnectContext connectContext , String key ,
228
- SqlCacheContext sqlCacheContext , UserIdentity currentUserIdentity ) {
228
+ private Optional <LogicalSqlCache > tryParseSql (
229
+ ConnectContext connectContext , String key , SqlCacheContext sqlCacheContext ,
230
+ UserIdentity currentUserIdentity , boolean checkUserVariable ) {
229
231
Env env = connectContext .getEnv ();
230
232
231
233
if (!tryLockTables (connectContext , env , sqlCacheContext )) {
@@ -259,8 +261,12 @@ private Optional<LogicalSqlCache> tryParseSqlWithoutCheckVariable(
259
261
try {
260
262
Optional <ResultSet > resultSetInFe = sqlCacheContext .getResultSetInFe ();
261
263
262
- List <Variable > currentVariables = resolveUserVariables (sqlCacheContext );
263
- boolean usedVariablesChanged = usedVariablesChanged (currentVariables , sqlCacheContext );
264
+ List <Variable > currentVariables = ImmutableList .of ();
265
+ if (checkUserVariable ) {
266
+ currentVariables = resolveUserVariables (sqlCacheContext );
267
+ }
268
+ boolean usedVariablesChanged
269
+ = checkUserVariable && usedVariablesChanged (currentVariables , sqlCacheContext );
264
270
if (resultSetInFe .isPresent () && !usedVariablesChanged ) {
265
271
MetricRepo .COUNTER_CACHE_HIT_SQL .increase (1L );
266
272
@@ -274,9 +280,15 @@ private Optional<LogicalSqlCache> tryParseSqlWithoutCheckVariable(
274
280
}
275
281
276
282
Status status = new Status ();
277
- PUniqueId cacheKeyMd5 = usedVariablesChanged
278
- ? sqlCacheContext .doComputeCacheKeyMd5 (Utils .fastToImmutableSet (currentVariables ))
279
- : sqlCacheContext .getOrComputeCacheKeyMd5 ();
283
+
284
+ PUniqueId cacheKeyMd5 ;
285
+ if (usedVariablesChanged ) {
286
+ invalidateCache (key );
287
+ cacheKeyMd5 = sqlCacheContext .doComputeCacheKeyMd5 (Utils .fastToImmutableSet (currentVariables ));
288
+ } else {
289
+ cacheKeyMd5 = sqlCacheContext .getOrComputeCacheKeyMd5 ();
290
+ }
291
+
280
292
InternalService .PFetchCacheResult cacheData =
281
293
SqlCache .getCacheData (sqlCacheContext .getCacheProxy (),
282
294
cacheKeyMd5 , sqlCacheContext .getLatestPartitionId (),
@@ -308,20 +320,36 @@ private boolean tablesOrDataChanged(Env env, SqlCacheContext sqlCacheContext) {
308
320
return true ;
309
321
}
310
322
311
- for (ScanTable scanTable : sqlCacheContext .getScanTables ()) {
312
- FullTableName fullTableName = scanTable .fullTableName ;
313
- TableIf tableIf = findTableIf (env , fullTableName );
314
- if (!(tableIf instanceof OlapTable )) {
323
+ // the query maybe scan empty partition of the table, we should check these table version too,
324
+ // but the table not exists in sqlCacheContext.getScanTables(), so we need check here.
325
+ // check table type and version
326
+ for (Entry <FullTableName , TableVersion > scanTable : sqlCacheContext .getUsedTables ().entrySet ()) {
327
+ TableVersion tableVersion = scanTable .getValue ();
328
+ if (tableVersion .type != TableType .OLAP ) {
329
+ return true ;
330
+ }
331
+ TableIf tableIf = findTableIf (env , scanTable .getKey ());
332
+ if (!(tableIf instanceof OlapTable ) || tableVersion .id != tableIf .getId ()) {
315
333
return true ;
316
334
}
335
+
317
336
OlapTable olapTable = (OlapTable ) tableIf ;
318
337
long currentTableVersion = olapTable .getVisibleVersion ();
319
- long cacheTableVersion = scanTable . latestVersion ;
338
+ long cacheTableVersion = tableVersion . version ;
320
339
// some partitions have been dropped, or delete or updated or replaced, or insert rows into new partition?
321
340
if (currentTableVersion != cacheTableVersion ) {
322
341
return true ;
323
342
}
343
+ }
324
344
345
+ // check partition version
346
+ for (ScanTable scanTable : sqlCacheContext .getScanTables ()) {
347
+ FullTableName fullTableName = scanTable .fullTableName ;
348
+ TableIf tableIf = findTableIf (env , fullTableName );
349
+ if (!(tableIf instanceof OlapTable )) {
350
+ return true ;
351
+ }
352
+ OlapTable olapTable = (OlapTable ) tableIf ;
325
353
Collection <Long > partitionIds = scanTable .getScanPartitions ();
326
354
olapTable .getVersionInBatchForCloudMode (partitionIds );
327
355
@@ -392,7 +420,7 @@ private boolean dataMaskPoliciesChanged(
392
420
*/
393
421
private boolean tryLockTables (ConnectContext connectContext , Env env , SqlCacheContext sqlCacheContext ) {
394
422
StatementContext currentStatementContext = connectContext .getStatementContext ();
395
- for (FullTableName fullTableName : sqlCacheContext .getUsedTables ()) {
423
+ for (FullTableName fullTableName : sqlCacheContext .getUsedTables (). keySet () ) {
396
424
TableIf tableIf = findTableIf (env , fullTableName );
397
425
if (tableIf == null ) {
398
426
return false ;
0 commit comments