hello-stephen commented on code in PR #71197:
URL: https://github.com/apache/doris/pull/71197#discussion_r4782644164


##########
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/BindRelationTest.java:
##########
@@ -101,6 +117,44 @@ void rejectIncrementalReadWithoutChangelog() {
         Assertions.assertEquals("INCR query requires changelog enabled on base 
table.", exception.getMessage());
     }
 
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void systemTableBindingDisablesSqlCache(boolean nativeScan) {
+        String systemTableName = "t$metadata";
+        List<String> qualifiedName = List.of("internal", DB1, systemTableName);
+        ExternalTable sourceTable = Mockito.mock(ExternalTable.class);
+        SysTable systemTable;
+        if (nativeScan) {
+            NativeSysTable nativeTable = Mockito.mock(NativeSysTable.class);
+            ExternalTable scanTable = Mockito.mock(ExternalTable.class);
+            
Mockito.when(nativeTable.createSysExternalTable(sourceTable)).thenReturn(scanTable);
+            systemTable = nativeTable;
+        } else {
+            TvfSysTable tvfTable = Mockito.mock(TvfSysTable.class);
+            Mockito.when(tvfTable.createFunction("internal", DB1, 
systemTableName))
+                    .thenReturn(new Numbers(new Properties(Map.of("number", 
"1"))));
+            systemTable = tvfTable;
+        }
+        
Mockito.when(sourceTable.findSysTable(systemTableName)).thenReturn(Optional.of(systemTable));
+
+        boolean previous = 
connectContext.getSessionVariable().isEnableSqlCache();
+        connectContext.getSessionVariable().setEnableSqlCache(true);
+        String sql = "SELECT * FROM internal.db1.`t$metadata`";
+        try (StatementContext statement = new StatementContext(connectContext, 
new OriginStatement(sql, 0))) {
+            statement.setTables(Map.of(qualifiedName, sourceTable));
+            UnboundRelation relation = new 
UnboundRelation(statement.getNextRelationId(), qualifiedName);
+            CascadesContext cascades = CascadesContext.initContext(statement, 
relation, PhysicalProperties.ANY);
+            
Assertions.assertTrue(statement.getSqlCacheContext().orElseThrow().supportSqlCache());
+
+            Plan bound = new BindRelation().build().transform(relation, 
cascades).get(0);
+            Assertions.assertEquals(nativeScan ? PlanType.LOGICAL_FILE_SCAN : 
PlanType.LOGICAL_TVF_RELATION,
+                    bound.getType());
+            
Assertions.assertFalse(statement.getSqlCacheContext().orElseThrow().supportSqlCache());

Review Comment:
   [P3] Cover the stale-result scenario after the system-table scan disappears
   
   These assertions verify the binding flag. A failure in cache publication or 
lookup after optimization could still pass both cases. An existing Iceberg 
SQL-cache fixture can exercise ``SELECT EXISTS (SELECT 1 FROM 
ice.db.`t$snapshots` LIMIT 0)``. `SubExprAnalyzer` binds the nested relation 
before folding `EXISTS` to a constant, which reaches the FE result-cache path 
without a scan. Run the query twice with caching enabled, check for the absence 
of `PhysicalSqlCache`, then drop the source table and require a missing-table 
error from the same SQL. The existing ordinary Iceberg query provides a 
cache-hit control.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java:
##########
@@ -718,6 +718,9 @@ private Optional<LogicalPlan> handleMetaTable(TableIf 
table, UnboundRelation unb
         }
 
         SysTableResolver.SysTablePlan sysTablePlan = sysTablePlanOpt.get();
+        // System tables lack SQL cache dependencies, even when a rewrite 
eliminates the scan.
+        cascadesContext.getStatementContext().getSqlCacheContext()
+                .ifPresent(cache -> cache.setHasUnsupportedTables(true));

Review Comment:
   [P3] Prevent BE cache writes when a hint enables SQL caching after parsing 
starts
   
   With session `enable_sql_cache=false`, consider a MySQL query such as 
``SELECT /*+ SET_VAR(enable_sql_cache=true) */ k FROM internal.db.olap_t UNION 
ALL SELECT snapshot_id FROM ice.db.`t$snapshots` WHERE FALSE``. The parser 
creates `StatementContext` before applying the hint, so this `ifPresent` has no 
effect. The false filter and empty-union rewrites leave only the OLAP scan. If 
that table is quiet and returns a small nonempty result, `CacheAnalyzer` can 
still select SQL cache mode and `StmtExecutor` sends the rows to the BE cache 
before `tryAddBeCache` checks for a context. The missing context prevents the 
FE from indexing the entry, so the query pays for row copies and the update RPC 
without gaining a reusable result. Require an eligible statement cache context 
before collecting rows for the BE cache, or record the system-table 
disqualifier on the statement itself.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to