2323import org .apache .doris .catalog .OlapTable ;
2424import org .apache .doris .catalog .Partition ;
2525import org .apache .doris .catalog .TableIf ;
26+ import org .apache .doris .catalog .TableIf .TableType ;
2627import org .apache .doris .catalog .View ;
2728import org .apache .doris .common .Config ;
2829import org .apache .doris .common .ConfigBase .DefaultConfHandler ;
3940import org .apache .doris .nereids .SqlCacheContext .FullColumnName ;
4041import org .apache .doris .nereids .SqlCacheContext .FullTableName ;
4142import org .apache .doris .nereids .SqlCacheContext .ScanTable ;
43+ import org .apache .doris .nereids .SqlCacheContext .TableVersion ;
4244import org .apache .doris .nereids .StatementContext ;
4345import org .apache .doris .nereids .analyzer .UnboundVariable ;
4446import org .apache .doris .nereids .parser .NereidsParser ;
@@ -199,14 +201,14 @@ public Optional<LogicalSqlCache> tryParseSql(ConnectContext connectContext, Stri
199201 .getSqlCacheContext ().ifPresent (ctx -> ctx .setCacheKeyType (CacheKeyType .MD5 ));
200202
201203 if (sqlCacheContextWithVariable != null ) {
202- return tryParseSqlWithoutCheckVariable (
203- connectContext , md5CacheKey , sqlCacheContextWithVariable , currentUserIdentity
204+ return tryParseSql (
205+ connectContext , md5CacheKey , sqlCacheContextWithVariable , currentUserIdentity , true
204206 );
205207 } else {
206208 return Optional .empty ();
207209 }
208210 } else {
209- return tryParseSqlWithoutCheckVariable (connectContext , key , sqlCacheContext , currentUserIdentity );
211+ return tryParseSql (connectContext , key , sqlCacheContext , currentUserIdentity , false );
210212 }
211213 }
212214
@@ -223,9 +225,9 @@ private String normalizeSql(String sql) {
223225 return NereidsParser .removeCommentAndTrimBlank (sql );
224226 }
225227
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 ) {
229231 Env env = connectContext .getEnv ();
230232
231233 if (!tryLockTables (connectContext , env , sqlCacheContext )) {
@@ -259,8 +261,12 @@ private Optional<LogicalSqlCache> tryParseSqlWithoutCheckVariable(
259261 try {
260262 Optional <ResultSet > resultSetInFe = sqlCacheContext .getResultSetInFe ();
261263
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 );
264270 if (resultSetInFe .isPresent () && !usedVariablesChanged ) {
265271 MetricRepo .COUNTER_CACHE_HIT_SQL .increase (1L );
266272
@@ -274,9 +280,15 @@ private Optional<LogicalSqlCache> tryParseSqlWithoutCheckVariable(
274280 }
275281
276282 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+
280292 InternalService .PFetchCacheResult cacheData =
281293 SqlCache .getCacheData (sqlCacheContext .getCacheProxy (),
282294 cacheKeyMd5 , sqlCacheContext .getLatestPartitionId (),
@@ -308,20 +320,36 @@ private boolean tablesOrDataChanged(Env env, SqlCacheContext sqlCacheContext) {
308320 return true ;
309321 }
310322
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 ()) {
315333 return true ;
316334 }
335+
317336 OlapTable olapTable = (OlapTable ) tableIf ;
318337 long currentTableVersion = olapTable .getVisibleVersion ();
319- long cacheTableVersion = scanTable . latestVersion ;
338+ long cacheTableVersion = tableVersion . version ;
320339 // some partitions have been dropped, or delete or updated or replaced, or insert rows into new partition?
321340 if (currentTableVersion != cacheTableVersion ) {
322341 return true ;
323342 }
343+ }
324344
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 ;
325353 Collection <Long > partitionIds = scanTable .getScanPartitions ();
326354 olapTable .getVersionInBatchForCloudMode (partitionIds );
327355
@@ -392,7 +420,7 @@ private boolean dataMaskPoliciesChanged(
392420 */
393421 private boolean tryLockTables (ConnectContext connectContext , Env env , SqlCacheContext sqlCacheContext ) {
394422 StatementContext currentStatementContext = connectContext .getStatementContext ();
395- for (FullTableName fullTableName : sqlCacheContext .getUsedTables ()) {
423+ for (FullTableName fullTableName : sqlCacheContext .getUsedTables (). keySet () ) {
396424 TableIf tableIf = findTableIf (env , fullTableName );
397425 if (tableIf == null ) {
398426 return false ;
0 commit comments