hello-stephen commented on code in PR #68499: URL: https://github.com/apache/doris/pull/68499#discussion_r4748921482
########## fe/fe-core/src/test/java/org/apache/doris/nereids/spm/SPMRound39SafetyTest.java: ########## @@ -0,0 +1,270 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.spm; + +import org.apache.doris.nereids.StatementContext; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.parser.NereidsParser; +import org.apache.doris.nereids.spm.manager.BaselineManager; +import org.apache.doris.nereids.spm.manager.BaselineManagerTestAccess; +import org.apache.doris.nereids.spm.placeholder.SPMPlaceholderBuilder; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.OriginStatement; +import org.apache.doris.qe.SessionVariable; +import org.apache.doris.qe.SqlModeHelper; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Review fixes without their own regression suite: + * + * - #1 (SPMPlanner#limitContractPreserved): an UNBOUNDED caller must never replay a + * frozen plan that retains a row cap (a manual planSql 'SELECT k FROM t LIMIT 1' over + * the unbounded bind 'SELECT k FROM t'), because the early return skipped the retained- + * cap check and the replay silently returned one row instead of all rows. + * - #15 (SPMPlanTreeSupport#rejectScanSelectorMismatch): scan selectors are compared per + * OCCURRENCE (alias-attached), not by the per-table walk-order list: a reversed self + * join produced the identical list 't -> [p1, p2]' and every replay then returned the + * other pairing. + */ +public class SPMRound39SafetyTest { + + private BaselineManager manager; + + @BeforeEach + public void setUp() { + manager = BaselineManager.getInstance(); + BaselineManagerTestAccess.clear(manager); + } + + @AfterEach + public void tearDown() { + ConnectContext.remove(); + } + + private static LogicalPlan parse(String sql) { + return (LogicalPlan) new NereidsParser().parseSingle(sql); + } + + private static void installConnectContext() { + ConnectContext ctx = new ConnectContext(); + ctx.setSessionVariable(new SessionVariable()); + ctx.setThreadLocalInfo(); + ctx.setStatementContext(new StatementContext(ctx, new OriginStatement("SELECT 1", 0))); + } + + /** + * Hand-builds a baseline whose planSql is a frozen plan text (the equivalent of what + * the CREATE path stores after a successful decompile), exactly like + * SPMFrozenTreeReplayTest#frozenBaseline. + */ + private static BaselinePlan frozenBaseline(String bindSql, String frozenPlanSql) + throws Exception { + LogicalPlan bindPlan = parse(bindSql); + SPMPlaceholderBuilder builder = new SPMPlaceholderBuilder(); + LogicalPlan parameterizedBind = SPMPlanTreeSupport.transform( + bindPlan, expr -> expr.accept(builder, null)); + + BaselinePlan baseline = new BaselinePlan(); + baseline.setCreatorSqlMode(SqlModeHelper.currentMode()); + baseline.setBindSql(bindSql); + baseline.setBindSqlDigest(bindPlan.toSpmDigest()); + baseline.setBindSqlHash(SPMUtils.hashOf(bindPlan.toSpmDigest())); + baseline.setPlanSql(frozenPlanSql); + baseline.setParameterizedBindPlan(parameterizedBind); + baseline.setCost(0.0); + return baseline; + } + + // ==================== #1: unbounded callers and retained plan caps ==================== + + /** + * The reviewer's case: CREATE BASELINE PLAN 'SELECT k FROM t' WITH 'SELECT k FROM t + * LIMIT 1' passes creation (the bind is unbounded), and the later unbounded SELECT + * matches that bind. The frozen plan RETAINS LIMIT 1; the old early return + * (userLimit == null -> true) skipped the retained-cap check, so the replay + * silently returned ONE row instead of all rows. The candidate must be skipped. + */ + @Test + public void testUnboundedCallerNeverReplaysACappedFrozenPlan() throws Exception { + installConnectContext(); + SPMPlanner planner = new SPMPlanner(); + manager.createBaseline(frozenBaseline("SELECT k FROM t", "SELECT k FROM t LIMIT 1")); Review Comment: [P2] Mark the cap-rejection fixture as frozen `frozenBaseline()` leaves both `planFrozen` and `parameterizedPlanPlan` null. This marker-free `SELECT k FROM t LIMIT 1` is therefore rejected by `rewriteFromFrozenTree()` as legacy non-frozen text, and the raw fallback skips its null tree. Consequently, `assertNull(rewritten)` passes without reaching `limitContractPreserved()`, even if the unbounded-caller cap check is removed. Set frozen provenance on this fixture and add a marker-free uncapped control that successfully rewrites, so the regression actually protects the retained-LIMIT safeguard. -- 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]
