|
22 | 22 | import org.apache.doris.analysis.TableScanParams; |
23 | 23 | import org.apache.doris.catalog.Column; |
24 | 24 | import org.apache.doris.catalog.DatabaseIf; |
| 25 | +import org.apache.doris.catalog.MysqlColType; |
25 | 26 | import org.apache.doris.catalog.OlapTable; |
26 | 27 | import org.apache.doris.catalog.PrimitiveType; |
27 | 28 | import org.apache.doris.catalog.ScalarType; |
|
31 | 32 | import org.apache.doris.datasource.mvcc.MvccTable; |
32 | 33 | import org.apache.doris.nereids.StatementContext; |
33 | 34 | import org.apache.doris.nereids.analyzer.UnboundRelation; |
| 35 | +import org.apache.doris.nereids.glue.LogicalPlanAdapter; |
34 | 36 | import org.apache.doris.nereids.parser.NereidsParser; |
| 37 | +import org.apache.doris.nereids.trees.expressions.Cast; |
| 38 | +import org.apache.doris.nereids.trees.expressions.Expression; |
| 39 | +import org.apache.doris.nereids.trees.expressions.Placeholder; |
35 | 40 | import org.apache.doris.nereids.trees.expressions.SubqueryExpr; |
| 41 | +import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; |
| 42 | +import org.apache.doris.nereids.trees.expressions.literal.StringLiteral; |
| 43 | +import org.apache.doris.nereids.trees.expressions.literal.VarBinaryLiteral; |
36 | 44 | import org.apache.doris.nereids.trees.plans.commands.merge.MergeIntoCommand; |
37 | 45 | import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; |
| 46 | +import org.apache.doris.nereids.types.StringType; |
| 47 | +import org.apache.doris.planner.GroupCommitPlanner; |
38 | 48 | import org.apache.doris.planner.OlapScanNode; |
39 | 49 | import org.apache.doris.planner.Planner; |
40 | 50 | import org.apache.doris.qe.ConnectContext; |
41 | 51 | import org.apache.doris.qe.OriginStatement; |
| 52 | +import org.apache.doris.qe.PointQueryExecutor; |
42 | 53 | import org.apache.doris.qe.PreparedStatementContext; |
43 | 54 | import org.apache.doris.qe.SessionVariable; |
44 | 55 | import org.apache.doris.qe.ShortCircuitQueryContext; |
45 | 56 | import org.apache.doris.qe.StmtExecutor; |
46 | 57 | import org.apache.doris.thrift.TQueryOptions; |
47 | 58 |
|
48 | 59 | import com.google.common.collect.ImmutableMap; |
| 60 | +import org.junit.jupiter.api.AfterEach; |
49 | 61 | import org.junit.jupiter.api.Assertions; |
50 | 62 | import org.junit.jupiter.api.Test; |
| 63 | +import org.mockito.MockedStatic; |
51 | 64 | import org.mockito.Mockito; |
52 | 65 |
|
53 | 66 | import java.util.ArrayList; |
|
59 | 72 |
|
60 | 73 | public class ExecuteCommandTest { |
61 | 74 |
|
| 75 | + @AfterEach |
| 76 | + public void tearDown() { |
| 77 | + ConnectContext.remove(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testExpressionBindingRefreshesCachedPointQuery() throws Exception { |
| 82 | + ConnectContext context = new ConnectContext(); |
| 83 | + PreparedStatementContext prepared = prepareParameterStatement(context, "select * from tbl where k = ?"); |
| 84 | + StatementContext initialContext = prepared.getStatementContext(); |
| 85 | + initialContext.setShortCircuitQuery(true); |
| 86 | + ShortCircuitQueryContext cached = Mockito.mock(ShortCircuitQueryContext.class); |
| 87 | + Mockito.when(cached.isReusable(context)).thenReturn(true); |
| 88 | + prepared.shortCircuitQueryContext = Optional.of(cached); |
| 89 | + StmtExecutor executor = parameterExecutor(context); |
| 90 | + |
| 91 | + try (MockedStatic<PointQueryExecutor> pointQueries = Mockito.mockStatic(PointQueryExecutor.class)) { |
| 92 | + bindAndExecute(context, prepared, executor, new StringLiteral("ASCII")); |
| 93 | + pointQueries.verify(() -> PointQueryExecutor.directExecuteShortCircuitQuery( |
| 94 | + executor, prepared, prepared.getStatementContext())); |
| 95 | + |
| 96 | + Expression binary = new Cast(new VarBinaryLiteral(new byte[] {(byte) 0xff}), StringType.INSTANCE); |
| 97 | + bindAndExecute(context, prepared, executor, binary); |
| 98 | + StatementContext refreshed = prepared.getStatementContext(); |
| 99 | + Assertions.assertFalse(refreshed.isShortCircuitQuery()); |
| 100 | + Assertions.assertNull(refreshed.getShortCircuitQueryContext()); |
| 101 | + Assertions.assertTrue(prepared.shortCircuitQueryContext.isEmpty()); |
| 102 | + Assertions.assertNotSame(initialContext, refreshed); |
| 103 | + Assertions.assertEquals(binary, refreshed.getIdToPlaceholderRealExpr().get( |
| 104 | + prepared.command.getPlaceholders().get(0).getPlaceholderId())); |
| 105 | + Assertions.assertEquals(MysqlColType.MYSQL_TYPE_VARSTRING, |
| 106 | + prepared.command.getPlaceholders().get(0).getMysqlColType()); |
| 107 | + Mockito.verify(executor).execute(); |
| 108 | + pointQueries.verifyNoMoreInteractions(); |
| 109 | + |
| 110 | + bindAndExecute(context, prepared, executor, new StringLiteral("ASCII again")); |
| 111 | + Mockito.verify(executor, Mockito.times(2)).execute(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testExpressionBindingClearsUncachedPointQueryFlag() throws Exception { |
| 117 | + ConnectContext context = new ConnectContext(); |
| 118 | + PreparedStatementContext prepared = prepareParameterStatement(context, "select * from tbl where k = ?"); |
| 119 | + // A point-query execution can fail before publishing its cached context. |
| 120 | + prepared.getStatementContext().setShortCircuitQuery(true); |
| 121 | + StmtExecutor executor = parameterExecutor(context); |
| 122 | + |
| 123 | + bindAndExecute(context, prepared, executor, |
| 124 | + new Cast(new VarBinaryLiteral(new byte[] {(byte) 0xff}), StringType.INSTANCE)); |
| 125 | + |
| 126 | + Assertions.assertFalse(prepared.getStatementContext().isShortCircuitQuery()); |
| 127 | + Assertions.assertNull(prepared.getStatementContext().getShortCircuitQueryContext()); |
| 128 | + Mockito.verify(executor).execute(); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testExpressionBindingSkipsPreparedGroupCommit() throws Exception { |
| 133 | + ConnectContext context = new ConnectContext(); |
| 134 | + context.getSessionVariable().groupCommit = "sync_mode"; |
| 135 | + Assertions.assertTrue(context.getSessionVariable().enableGroupCommitFullPrepare); |
| 136 | + PreparedStatementContext prepared = prepareParameterStatement(context, "insert into test.tbl values (?)"); |
| 137 | + // A previous literal execution has already populated the full-prepare cache. |
| 138 | + GroupCommitPlanner cached = Mockito.mock(GroupCommitPlanner.class); |
| 139 | + prepared.groupCommitPlanner = Optional.of(cached); |
| 140 | + StmtExecutor executor = parameterExecutor(context); |
| 141 | + try (MockedStatic<GroupCommitPlanner> groupCommit = Mockito.mockStatic(GroupCommitPlanner.class)) { |
| 142 | + bindAndExecute(context, prepared, executor, new StringLiteral("ASCII")); |
| 143 | + groupCommit.verify(() -> GroupCommitPlanner.executeGroupCommitInsert( |
| 144 | + context, prepared, prepared.getStatementContext())); |
| 145 | + groupCommit.clearInvocations(); |
| 146 | + |
| 147 | + Expression binary = new Cast(new VarBinaryLiteral(new byte[] {(byte) 0xff}), StringType.INSTANCE); |
| 148 | + bindAndExecute(context, prepared, executor, binary); |
| 149 | + Mockito.verify(executor).execute(); |
| 150 | + Assertions.assertEquals(binary, prepared.getStatementContext().getIdToPlaceholderRealExpr().get( |
| 151 | + prepared.command.getPlaceholders().get(0).getPlaceholderId())); |
| 152 | + groupCommit.verifyNoInteractions(); |
| 153 | + |
| 154 | + bindAndExecute(context, prepared, executor, new NullLiteral()); |
| 155 | + groupCommit.verify(() -> GroupCommitPlanner.executeGroupCommitInsert( |
| 156 | + context, prepared, prepared.getStatementContext())); |
| 157 | + Assertions.assertSame(cached, prepared.groupCommitPlanner.orElseThrow()); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private PreparedStatementContext prepareParameterStatement(ConnectContext context, String sql) throws Exception { |
| 162 | + context.setThreadLocalInfo(); |
| 163 | + LogicalPlanAdapter parsed = (LogicalPlanAdapter) new NereidsParser().parseSQL(sql).get(0); |
| 164 | + StatementContext statementContext = parsed.getStatementContext(); |
| 165 | + statementContext.setConnectContext(context); |
| 166 | + List<Placeholder> placeholders = statementContext.getPlaceholders().stream() |
| 167 | + .map(placeholder -> placeholder.withNewMysqlColType(MysqlColType.MYSQL_TYPE_VARSTRING.getCode())) |
| 168 | + .toList(); |
| 169 | + statementContext.setPlaceholders(placeholders); |
| 170 | + PrepareCommand command = new PrepareCommand("stmt", parsed.getLogicalPlan(), placeholders, |
| 171 | + new OriginStatement(sql, 0)); |
| 172 | + PreparedStatementContext prepared = new PreparedStatementContext(command, context, statementContext, "stmt"); |
| 173 | + context.setStatementContext(statementContext); |
| 174 | + context.addPreparedStatementContext("stmt", prepared); |
| 175 | + return prepared; |
| 176 | + } |
| 177 | + |
| 178 | + private StmtExecutor parameterExecutor(ConnectContext context) { |
| 179 | + StmtExecutor executor = Mockito.mock(StmtExecutor.class); |
| 180 | + Mockito.when(executor.getContext()).thenReturn(context); |
| 181 | + Mockito.doAnswer(invocation -> { |
| 182 | + context.setStatementContext(invocation.getArgument(0)); |
| 183 | + return null; |
| 184 | + }).when(executor).setStatementContext(Mockito.any(StatementContext.class)); |
| 185 | + return executor; |
| 186 | + } |
| 187 | + |
| 188 | + private void bindAndExecute(ConnectContext context, PreparedStatementContext prepared, |
| 189 | + StmtExecutor executor, Expression value) throws Exception { |
| 190 | + context.setGroupCommit(false); |
| 191 | + prepared.getStatementContext().getIdToPlaceholderRealExpr().put( |
| 192 | + prepared.command.getPlaceholders().get(0).getPlaceholderId(), value); |
| 193 | + new ExecuteCommand("stmt", prepared.command, prepared.getStatementContext()).run(context, executor); |
| 194 | + } |
| 195 | + |
62 | 196 | @Test |
63 | 197 | public void testResolvedScanOptionsAreResetForEveryExecute() throws Exception { |
64 | 198 | String sql = "select * from p@options('scan.mode'='latest')"; |
|
0 commit comments