Skip to content

Commit 74ad0e3

Browse files
authored
[fix](sql cache) fix prepare statement with sql cache throw NullPointerException (apache#48902)
fix prepare statement with sql cache throw NullPointerException, introduced by apache#33262. forbid sql cache when useServerPrepStmts=true, the mysql protocol has different between COM_QUERY and COM_STMT_EXECUTE, the sql cache use the result of COM_QUERY, so we can not provide the result of sql cache for COM_STMT_EXECUTE
1 parent 542366f commit 74ad0e3

8 files changed

Lines changed: 71 additions & 23 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/common/cache/NereidsSqlCacheManager.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ private static Cache<String, SqlCacheContext> buildSqlCaches(int sqlCacheNum, lo
130130
* tryAddFeCache
131131
*/
132132
public void tryAddFeSqlCache(ConnectContext connectContext, String sql) {
133+
switch (connectContext.getCommand()) {
134+
case COM_STMT_EXECUTE:
135+
case COM_STMT_PREPARE:
136+
return;
137+
default: { }
138+
}
139+
133140
Optional<SqlCacheContext> sqlCacheContextOpt = connectContext.getStatementContext().getSqlCacheContext();
134141
if (!sqlCacheContextOpt.isPresent()) {
135142
return;
@@ -149,6 +156,12 @@ public void tryAddFeSqlCache(ConnectContext connectContext, String sql) {
149156
* tryAddBeCache
150157
*/
151158
public void tryAddBeCache(ConnectContext connectContext, String sql, CacheAnalyzer analyzer) {
159+
switch (connectContext.getCommand()) {
160+
case COM_STMT_EXECUTE:
161+
case COM_STMT_PREPARE:
162+
return;
163+
default: { }
164+
}
152165
Optional<SqlCacheContext> sqlCacheContextOpt = connectContext.getStatementContext().getSqlCacheContext();
153166
if (!sqlCacheContextOpt.isPresent()) {
154167
return;
@@ -180,6 +193,12 @@ public void tryAddBeCache(ConnectContext connectContext, String sql, CacheAnalyz
180193
* tryParseSql
181194
*/
182195
public Optional<LogicalSqlCache> tryParseSql(ConnectContext connectContext, String sql) {
196+
switch (connectContext.getCommand()) {
197+
case COM_STMT_EXECUTE:
198+
case COM_STMT_PREPARE:
199+
return Optional.empty();
200+
default: { }
201+
}
183202
String key = generateCacheKey(connectContext, normalizeSql(sql.trim()));
184203
SqlCacheContext sqlCacheContext = sqlCaches.getIfPresent(key);
185204
if (sqlCacheContext == null) {

‎fe/fe-core/src/main/java/org/apache/doris/nereids/StatementContext.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public enum TableFrom {
166166
private final Stack<CloseableResource> plannerResources = new Stack<>();
167167

168168
// placeholder params for prepared statement
169-
private List<Placeholder> placeholders;
169+
private List<Placeholder> placeholders = new ArrayList<>();
170170

171171
// all tables in query
172172
private boolean needLockTables = true;

‎fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilderForEncryption.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ public LogicalPlan visitCreateTable(DorisParser.CreateTableContext ctx) {
107107
if (ctx.propertyClause() != null) {
108108
List<DorisParser.PropertyClauseContext> propertyClauseContexts = ctx.propertyClause();
109109
for (DorisParser.PropertyClauseContext propertyClauseContext : propertyClauseContexts) {
110-
encryptProperty(visitPropertyClause(propertyClauseContext),
111-
propertyClauseContext.fileProperties.start.getStartIndex(),
112-
propertyClauseContext.fileProperties.stop.getStopIndex());
110+
if (propertyClauseContext != null) {
111+
encryptProperty(visitPropertyClause(propertyClauseContext),
112+
propertyClauseContext.fileProperties.start.getStartIndex(),
113+
propertyClauseContext.fileProperties.stop.getStopIndex());
114+
}
113115
}
114116
}
115117
return super.visitCreateTable(ctx);

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExecuteCommand.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.apache.doris.nereids.trees.expressions.Expression;
2626
import org.apache.doris.nereids.trees.plans.PlanType;
2727
import org.apache.doris.nereids.trees.plans.commands.insert.OlapGroupCommitInsertExecutor;
28+
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
29+
import org.apache.doris.nereids.trees.plans.logical.LogicalSqlCache;
2830
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
2931
import org.apache.doris.planner.GroupCommitPlanner;
3032
import org.apache.doris.qe.ConnectContext;
@@ -68,9 +70,13 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
6870
throw new AnalysisException(
6971
"prepare statement " + stmtName + " not found, maybe expired");
7072
}
71-
PrepareCommand prepareCommand = (PrepareCommand) preparedStmtCtx.command;
72-
LogicalPlanAdapter planAdapter = new LogicalPlanAdapter(prepareCommand.getLogicalPlan(), executor.getContext()
73-
.getStatementContext());
73+
PrepareCommand prepareCommand = preparedStmtCtx.command;
74+
LogicalPlan logicalPlan = prepareCommand.getLogicalPlan();
75+
if (logicalPlan instanceof LogicalSqlCache) {
76+
throw new AnalysisException("Unsupported sql cache for server prepared statement");
77+
}
78+
LogicalPlanAdapter planAdapter = new LogicalPlanAdapter(
79+
logicalPlan, executor.getContext().getStatementContext());
7480
executor.setParsedStmt(planAdapter);
7581
// If it's not a short circuit query or schema version is different(indicates schema changed) or
7682
// has nondeterministic functions in statement, then need to do reanalyze and plan

‎fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,17 @@ public void executeQuery(String originStmt) throws Exception {
399399
private List<StatementBase> parseFromSqlCache(String originStmt) {
400400
StatementContext statementContext = new StatementContext(ctx, new OriginStatement(originStmt, 0));
401401
ctx.setStatementContext(statementContext);
402+
403+
// the mysql protocol has different between COM_QUERY and COM_STMT_EXECUTE,
404+
// the sql cache use the result of COM_QUERY, so we can not provide the
405+
// result of sql cache for COM_STMT_EXECUTE/COM_STMT_PREPARE
406+
switch (ctx.getCommand()) {
407+
case COM_STMT_EXECUTE:
408+
case COM_STMT_PREPARE:
409+
return null;
410+
default: { }
411+
}
412+
402413
try {
403414
Optional<Pair<ExplainOptions, String>> explainPlan = NereidsParser.tryParseExplainPlan(originStmt);
404415
String cacheSqlKey = originStmt;

‎regression-test/framework/src/main/groovy/org/apache/doris/regression/action/TestAction.groovy‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class TestAction implements SuiteAction {
6868
} else {
6969
if (exception != null || result.exception != null) {
7070
def msg = result.exception?.toString()
71-
log.info("Exception: ${msg}")
71+
log.error("Exception: ${msg}", exception != null ? exception : result.exception)
7272
Assert.assertTrue("Expect exception msg contains '${exception}', but meet '${msg}'",
7373
msg != null && exception != null && msg.contains(exception))
7474
}

‎regression-test/suites/nereids_p0/cache/parse_sql_from_sql_cache.groovy‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ suite("parse_sql_from_sql_cache") {
3333
}
3434

3535
def dbName = (sql "select database()")[0][0].toString()
36-
foreachFrontends { fe ->
37-
def url = "jdbc:mysql://${fe.Host}:${fe.QueryPort}/${dbName}"
38-
connect(context.config.jdbcUser, context.config.jdbcPassword, url) {
39-
sql "ADMIN SET FRONTEND CONFIG ('cache_last_version_interval_second' = '10')"
40-
}
41-
}
36+
sql "ADMIN SET ALL FRONTENDS CONFIG ('cache_last_version_interval_second' = '10')"
4237

4338
// make sure if the table has been dropped, the cache should invalidate,
4439
// so we should retry multiple times to check

‎regression-test/suites/nereids_p0/cache/prepare_stmt_with_sql_cache.groovy‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,10 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
import com.mysql.cj.ServerPreparedQuery
19-
import com.mysql.cj.jdbc.ConnectionImpl
20-
import com.mysql.cj.jdbc.JdbcStatement
21-
import com.mysql.cj.jdbc.ServerPreparedStatement
22-
import com.mysql.cj.jdbc.StatementImpl
2318
import org.apache.doris.regression.util.JdbcUtils
2419

25-
import java.lang.reflect.Field
2620
import java.sql.PreparedStatement
2721
import java.sql.ResultSet
28-
import java.util.concurrent.CopyOnWriteArrayList
2922

3023
suite("prepare_stmt_with_sql_cache") {
3124

@@ -38,11 +31,13 @@ suite("prepare_stmt_with_sql_cache") {
3831
insert into test_prepare_stmt_with_sql_cache select * from numbers('number'='100');
3932
"""
4033

34+
sql "ADMIN SET ALL FRONTENDS CONFIG ('cache_last_version_interval_second' = '10')"
35+
4136
def db = (sql "select database()")[0][0].toString()
4237

43-
def url = getServerPrepareJdbcUrl(context.config.jdbcUrl, db)
38+
def serverPrepareUrl = getServerPrepareJdbcUrl(context.config.jdbcUrl, db)
4439

45-
connect(context.config.jdbcUser, context.config.jdbcPassword, url) {
40+
connect(context.config.jdbcUser, context.config.jdbcPassword, serverPrepareUrl) {
4641
sql "set enable_sql_cache=true"
4742
for (def i in 0..<10) {
4843
try (PreparedStatement pstmt = prepareStatement("select * from test_prepare_stmt_with_sql_cache where id=?")) {
@@ -54,4 +49,24 @@ suite("prepare_stmt_with_sql_cache") {
5449
}
5550
}
5651
}
52+
53+
sleep(10 * 1000)
54+
55+
connect(context.config.jdbcUser, context.config.jdbcPassword, context.config.jdbcUrl) {
56+
sql "use ${db}"
57+
sql "set enable_sql_cache=true"
58+
test {
59+
sql "select * from test_prepare_stmt_with_sql_cache where id=10"
60+
result([[10]])
61+
}
62+
}
63+
64+
connect(context.config.jdbcUser, context.config.jdbcPassword, serverPrepareUrl) {
65+
sql "use ${db}"
66+
sql "set enable_sql_cache=true"
67+
test {
68+
sql "select * from test_prepare_stmt_with_sql_cache where id=10"
69+
result(([[10]]))
70+
}
71+
}
5772
}

0 commit comments

Comments
 (0)