Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public void tryAddFeSqlCache(ConnectContext connectContext, String sql) {
}

SqlCacheContext sqlCacheContext = sqlCacheContextOpt.get();
sqlCacheContext.setQueryId(connectContext.queryId());
String key = sqlCacheContext.getCacheKeyType() == CacheKeyType.SQL
? generateCacheKey(connectContext, normalizeSql(sql))
: generateCacheKey(connectContext, DebugUtil.printId(sqlCacheContext.getOrComputeCacheKeyMd5()));
Expand Down Expand Up @@ -174,6 +175,7 @@ public void tryAddBeCache(ConnectContext connectContext, String sql, CacheAnalyz
return;
}
SqlCacheContext sqlCacheContext = sqlCacheContextOpt.get();
sqlCacheContext.setQueryId(connectContext.queryId());
String key = sqlCacheContext.getCacheKeyType() == CacheKeyType.SQL
? generateCacheKey(connectContext, normalizeSql(sql))
: generateCacheKey(connectContext, DebugUtil.printId(sqlCacheContext.getOrComputeCacheKeyMd5()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
public class SqlCacheContext {
private static final Logger LOG = LogManager.getLogger(SqlCacheContext.class);
private final UserIdentity userIdentity;
private final TUniqueId queryId;
private volatile TUniqueId queryId;
// if contains udf/udaf/tableValuesFunction we can not process it and skip use sql cache
private volatile boolean cannotProcessExpression;
private volatile String originSql;
Expand Down Expand Up @@ -99,9 +99,8 @@ public class SqlCacheContext {

private volatile CacheKeyType cacheKeyType = CacheKeyType.SQL;

public SqlCacheContext(UserIdentity userIdentity, TUniqueId queryId) {
public SqlCacheContext(UserIdentity userIdentity) {
this.userIdentity = Objects.requireNonNull(userIdentity, "userIdentity cannot be null");
this.queryId = Objects.requireNonNull(queryId, "queryId cannot be null");
}

public String getPhysicalPlan() {
Expand Down Expand Up @@ -437,6 +436,10 @@ public void setCacheKeyType(CacheKeyType cacheKeyType) {
this.cacheKeyType = cacheKeyType;
}

public void setQueryId(TUniqueId queryId) {
this.queryId = queryId;
}

/** FullTableName */
@lombok.Data
@lombok.AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,10 @@ public StatementContext(ConnectContext connectContext, OriginStatement originSta
this.originStatement = originStatement;
exprIdGenerator = ExprId.createGenerator(initialId);
if (connectContext != null && connectContext.getSessionVariable() != null
&& connectContext.queryId() != null
&& CacheAnalyzer.canUseSqlCache(connectContext.getSessionVariable())) {
// cannot set the queryId here because the queryId for the current query is set in the subsequent steps.
this.sqlCacheContext = new SqlCacheContext(
connectContext.getCurrentUserIdentity(), connectContext.queryId());
connectContext.getCurrentUserIdentity());
if (originStatement != null) {
this.sqlCacheContext.setOriginSql(originStatement.originStmt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,13 @@ public void setQueryId(TUniqueId queryId) {
}
}

public void resetQueryId() {
if (this.queryId != null) {
this.lastQueryId = this.queryId.deepCopy();
}
this.queryId = null;
}

public void setNeedRegenerateInstanceId(TUniqueId needRegenerateInstanceId) {
this.needRegenerateInstanceId = needRegenerateInstanceId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ protected void auditAfterExec(String origStmt, StatementBase parsedStmt,

// only throw an exception when there is a problem interacting with the requesting client
protected void handleQuery(String originStmt) throws ConnectionException {
// Before executing the query, the queryId should be set to empty.
// Otherwise, if SQL parsing fails, the audit log will record the queryId from the previous query.
ctx.resetQueryId();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now, what will be in the audit log? null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.setQueryId(ctx.queryId() == null ? "NaN" : DebugUtil.printId(ctx.queryId()))
NAN

if (Config.isCloudMode()) {
if (!ctx.getCurrentUserIdentity().isRootUser()
&& ((CloudSystemInfoService) Env.getCurrentSystemInfo()).getInstanceStatus()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.nio.channels.SocketChannel;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class ConnectContextTest {
@Mocked
Expand Down Expand Up @@ -274,4 +275,27 @@ public void testInsertQueryTimeoutS() {
result = context.getInsertTimeoutS();
Assert.assertEquals(propertyValue, result);
}

@Test
public void testResetQueryId() {
ConnectContext context = new ConnectContext();
Assert.assertNull(context.queryId);
Assert.assertNull(context.lastQueryId);

UUID uuid = UUID.randomUUID();
TUniqueId queryId = new TUniqueId(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
context.setQueryId(queryId);
Assert.assertEquals(queryId, context.queryId);
Assert.assertNull(context.lastQueryId);

context.resetQueryId();
Assert.assertNull(context.queryId);
Assert.assertEquals(queryId, context.lastQueryId);

UUID uuid2 = UUID.randomUUID();
TUniqueId queryId2 = new TUniqueId(uuid2.getMostSignificantBits(), uuid2.getLeastSignificantBits());
context.setQueryId(queryId2);
Assert.assertEquals(queryId2, context.queryId);
Assert.assertEquals(queryId, context.lastQueryId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ public void testCacheKey() {
queryId.setLo(uuid.getLeastSignificantBits());
UserIdentity admin = new UserIdentity("admin", "127.0.0.1");

SqlCacheContext cacheContext = new SqlCacheContext(admin, queryId);
SqlCacheContext cacheContext = new SqlCacheContext(admin);
cacheContext.setOriginSql("SELECT * FROM tbl");
PUniqueId key1 = cacheContext.doComputeCacheKeyMd5(ImmutableSet.of());

SqlCacheContext cacheContext2 = new SqlCacheContext(admin, queryId);
SqlCacheContext cacheContext2 = new SqlCacheContext(admin);
cacheContext2.setOriginSql(
"-- Same query with comments and extra spaces\n"
+ "/* Comment */ SELECT * FROM tbl "
);
PUniqueId key2 = cacheContext2.doComputeCacheKeyMd5(ImmutableSet.of());
Assertions.assertEquals(key1, key2);

SqlCacheContext cacheContext3 = new SqlCacheContext(admin, queryId);
SqlCacheContext cacheContext3 = new SqlCacheContext(admin);
cacheContext3.setOriginSql(
"-- Same query with comments and extra spaces\n"
+ "/* Comment */ SELeCT * FROM tbl "
Expand Down
Loading