Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CUBRIDQA-1236] add support for full query plans in SQL test cases with the keyword '--@fullPlan' #686

Merged
merged 10 commits into from
Nov 5, 2024
7 changes: 6 additions & 1 deletion CTP/sql/src/com/navercorp/cubridqa/cqt/common/SQLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public static List<Sql> parseSqlFile(String sqlFile, String codeset, boolean isN
boolean isCall = false;
boolean isNewStatement = true;
boolean isQueryplan = false;
boolean isJoingraph = false;
boolean isJoingraph = false;
boolean isFullplan = false;

LineScanner lineScanner = new LineScanner();

Expand All @@ -76,6 +77,8 @@ public static List<Sql> parseSqlFile(String sqlFile, String codeset, boolean isN
isQueryplan = true;
} else if ("--@joingraph".equals(line.trim())) {
isJoingraph = true;
} else if ("--@fullplan".equals(line.trim())) {
isFullplan = true;
} else {
String controlCmd = getControlCommand(line);
if (controlCmd != null) {
Expand Down Expand Up @@ -119,12 +122,14 @@ public static List<Sql> parseSqlFile(String sqlFile, String codeset, boolean isN
Sql sql = new Sql(connId, ret.toString(), paramList, isCall);
sql.setQueryplan(isQueryplan);
sql.setJoingraph(isJoingraph);
sql.setFullplan(isFullplan);
list.add(sql);

// initialize state variables
isNewStatement = true;
isQueryplan = false;
isJoingraph = false;
isFullplan = false;
ret.setLength(0);
paramList = null;
isCall = false;
Expand Down
11 changes: 11 additions & 0 deletions CTP/sql/src/com/navercorp/cubridqa/cqt/console/bean/Sql.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class Sql {
// Only join graph xxx
private boolean isJoingraph = false;

// Adds both join graph and query plan
private boolean isFullplan = false;

private int type;

private String result = "";
Expand Down Expand Up @@ -171,4 +174,12 @@ public boolean isJoingraph() {
public void setJoingraph(boolean isJoingraph) {
this.isJoingraph = isJoingraph;
}

public boolean isFullplan() {
return isFullplan;
}

public void setFullplan(boolean isFullplan) {
this.isFullplan = isFullplan;
}
}
18 changes: 13 additions & 5 deletions CTP/sql/src/com/navercorp/cubridqa/cqt/console/dao/ConsoleDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,8 @@ private void executePrepareStatement(Connection conn, Sql sql, boolean isPrintQu
}
String script = sql.getScript().trim().toUpperCase();
boolean isOnlyJoinGraph = sql.isJoingraph();
if (((isPrintQueryPlan && script.startsWith("SELECT")) || sql.isQueryplan()) || isOnlyJoinGraph) {
boolean isFullplan = sql.isFullplan();
if (((isPrintQueryPlan && script.startsWith("SELECT")) || sql.isQueryplan()) || isOnlyJoinGraph || isFullplan) {
Method method2 =
ps.getClass().getMethod("setQueryInfo", new Class[] {boolean.class});
method2.invoke(ps, new Object[] {true});
Expand All @@ -627,13 +628,16 @@ private void executePrepareStatement(Connection conn, Sql sql, boolean isPrintQu
isRs = ps.execute();
}
getAllResult(ps, isRs, sql);
if (((isPrintQueryPlan && script.startsWith("SELECT")) || sql.isQueryplan()) || isOnlyJoinGraph) {
if (((isPrintQueryPlan && script.startsWith("SELECT")) || sql.isQueryplan()) || isOnlyJoinGraph || isFullplan) {
Method method = ps.getClass().getMethod("getQueryplan", new Class[] {});
String queryPlan = (String) method.invoke(ps, new Object[] {});
queryPlan = queryPlan + System.getProperty("line.separator");
if (isOnlyJoinGraph) {
queryPlan = StringUtil.replaceJoingraph(queryPlan);
} else {
} else if (isFullplan) {
queryPlan = StringUtil.replaceFullplan(queryPlan);
}
else {
queryPlan = StringUtil.replaceQureyPlan(queryPlan);
}
sql.setResult(sql.getResult() + queryPlan);
Expand Down Expand Up @@ -675,13 +679,17 @@ private void executeStatement(Connection conn, Sql sql, boolean isPrintQueryPlan
getAllResult(st, isRs, sql);
String script = sql.getScript().trim().toUpperCase();
boolean isOnlyJoinGraph = sql.isJoingraph();
if (((isPrintQueryPlan && script.startsWith("SELECT")) || sql.isQueryplan()) || isOnlyJoinGraph) {
boolean isFullplan = sql.isFullplan();
if (((isPrintQueryPlan && script.startsWith("SELECT")) || sql.isQueryplan()) || isOnlyJoinGraph || isFullplan) {
Method method = st.getClass().getMethod("getQueryplan", stringType);
String queryPlan = (String) method.invoke(st, new Object[] {sql.getScript()});
queryPlan = queryPlan + System.getProperty("line.separator");
if (isOnlyJoinGraph) {
queryPlan = StringUtil.replaceJoingraph(queryPlan);
} else {
} else if (isFullplan) {
queryPlan = StringUtil.replaceFullplan(queryPlan);
}
else {
queryPlan = StringUtil.replaceQureyPlan(queryPlan);
}
sql.setResult(sql.getResult() + queryPlan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,98 @@ public static String replaceJoingraph(String queryPlan) {
return ret.toString();
}

/**
* replace the full query plan (query plan + join graph + query stmt)
*
* @param queryPlan
* @return
*/
public static String replaceFullplan(String queryPlan) {
if (queryPlan == null) {
return null;
}

SystemModel systemModel =
(SystemModel)
XstreamHelper.fromXml(
EnvGetter.getenv("CTP_HOME")
+ File.separator
+ "sql/configuration/System.xml");
StringBuilder ret = new StringBuilder();

String flag = "default";
int stmtCount = 0;
String separator = System.getProperty("line.separator");
BufferedReader reader = null;
try {
reader = new BufferedReader(new StringReader(queryPlan));

String message = reader.readLine();
while (message != null) {
if (message.trim().equals("")) {
message = reader.readLine();
continue;
}

// Handle join graph section
if (message.startsWith("Join graph")) {
flag = "join";
ret.append(message + separator);
message = reader.readLine();
continue;
}

// Handle query plan section
if (message.startsWith("Query plan:")) {
flag = "plan";
ret.append(message + separator);
message = reader.readLine();
continue;
}

// Handle query statement section
if (message.startsWith("Query stmt:")) {
flag = "stmt";
ret.append(message + separator);
stmtCount = 0;
message = reader.readLine();
continue;
}

// Append messages according to the current flag
if ("join".equals(flag)) {
// Modify join graph details if needed
message = message.replaceAll("sel [0-9]+\\.[0-9]+", "sel ?");
ret.append(message + separator);
} else if ("plan".equals(flag) || "stmt".equals(flag)) {
if (systemModel.isQueryPlan()) {
ret.append(message + separator);
} else {
if ("plan".equals(flag)) {
ret.append(message + separator);
} else if ("stmt".equals(flag)) {
if (stmtCount == 0 || message.startsWith("/")) {
ret.append(message + separator);
stmtCount++;
}
}
}
}

message = reader.readLine();
}
} catch (Exception e) {
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
return ret.toString();
}

/**
* translate byte array to Hex String .
*
Expand Down
Loading
Loading