Skip to content

Commit

Permalink
[CUBRIDQA-1236] add support for full query plans in SQL test cases wi…
Browse files Browse the repository at this point in the history
…th the keyword '--@fullPlan' (#686)

Drivers covered
- jdbc (sql) 
- cci (sql_by_cci)i
  • Loading branch information
junsklee authored Nov 5, 2024
1 parent 162eb04 commit 847b0ef
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 15 deletions.
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,103 @@ 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)) {
message = message.replaceAll("[0-9]+", "?");
ret.append(message + separator);
} else if ("stmt".equals(flag)) {
if (stmtCount == 0) {
message = message.replaceAll("[0-9]+", "?");
ret.append(message + separator);
stmtCount++;
} else if (message.startsWith("/")) {
message = message.replaceAll("[0-9]+", "?");
ret.append(message + separator);
}
}
}
}

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

0 comments on commit 847b0ef

Please sign in to comment.