Skip to content

Commit 8740c02

Browse files
committed
Minor bug fixing and refining size estimation.
1 parent 1b0ee9b commit 8740c02

15 files changed

+78
-72
lines changed

CypherCustomVisitor.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import Query.Engine.QueryPlanner;
55
import Query.Entities.PlanTree;
66
import Query.Execution.ResultTable;
7+
import Query.Plan.Plan;
78
import Utility.DBUtil;
89
import org.antlr.v4.runtime.tree.ErrorNode;
910
import org.antlr.v4.runtime.tree.ParseTree;
@@ -51,13 +52,28 @@ void executeQuery() {
5152
indexer
5253
);
5354

54-
55+
System.out.println("Planning Query\n");
56+
System.out.println("========================");
5557
PlanTree bestPlan = planner.plan();
58+
for(Plan plan : bestPlan.plans){
59+
System.out.println(plan.getName() + "|" + plan.getVariable() + "|" + plan.getParams());
60+
}
61+
System.out.println("========================\n\n\n");
62+
System.out.println("Query Plan generated. ");
5663

64+
System.out.println("Executing query...");
5765
QueryExecution execution = new QueryExecution(new DBUtil(conn), bestPlan);
5866
ResultTable table = execution.execute();
59-
System.out.println(table.toString());
67+
System.out.println("Finished. ");
6068

69+
System.out.println("Query content: ");
70+
System.out.println("========================");
71+
System.out.println(table.toString());
72+
System.out.println("========================\n\n\n");
73+
System.out.println("SQL: ");
74+
System.out.println("========================");
75+
System.out.println(String.join("\n", execution.getUsedSQL()));
76+
System.out.println("========================\n\n\n");
6177
}
6278

6379
@Override

DataImport/FileParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public void run() throws IOException, SQLException {
279279
// Distinguish node property from edge property.
280280

281281
String line;
282-
if(false) {
282+
if(true) {
283283
// All relations are of type 0.
284284
// Typeid of nodes starts from 1.
285285
int typeId = 1;

Main.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ public static void main(String[] args) throws IOException, SQLException {
6161
Connection connection = DriverManager.getConnection(url, username, password);
6262
System.out.println("MySQL connected.");
6363

64-
// if(false){
65-
// System.out.println("Parsing file...");
66-
// FileParser fileParser = new FileParser(fileName, connection);
67-
// fileParser.run();
68-
// System.out.println("Parsing complete.");
69-
// }
64+
65+
// System.out.println("Parsing file...");
66+
// FileParser fileParser = new FileParser(fileName, connection);
67+
// fileParser.run();
68+
// System.out.println("Parsing complete.");
7069

7170

7271
System.out.println("Creating index...");

Query/Engine/QueryExecution.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
public class QueryExecution {
1818
private DBUtil dbUtil;
1919
private PlanTree tree;
20+
21+
public List<String> getUsedSQL() {
22+
return usedSQL;
23+
}
24+
25+
private List<String> usedSQL = new ArrayList<>();
26+
2027
public QueryExecution(DBUtil util, PlanTree tree){
2128
this.dbUtil = util;
2229
this.tree = tree;
@@ -57,28 +64,33 @@ public ResultTable execute(){
5764
}
5865

5966
Stack<ResultTable> resStack = new Stack<>();
67+
List<String> querySQL = new ArrayList<>();
6068
for(Execution execution : executionList){
6169
switch (execution.operandCount()){
6270
case 0:
6371
resStack.push(execution.execute());
72+
querySQL.addAll(execution.getQuerySQL());
6473
break;
6574
case 1:
6675
ResultTable table = resStack.peek();
6776
resStack.pop();
6877
resStack.push(execution.execute(table));
78+
querySQL.addAll(execution.getQuerySQL());
6979
break;
7080
case 2:
7181
ResultTable table1 = resStack.peek();
7282
resStack.pop();
7383
ResultTable table2 = resStack.peek();
7484
resStack.pop();
7585
resStack.push(execution.execute(table1, table2));
86+
querySQL.addAll(execution.getQuerySQL());
7687
break;
7788
default:
7889
break;
7990
}
8091
}
8192
assert resStack.size() == 1;
93+
this.usedSQL = querySQL;
8294
return resStack.get(0);
8395

8496
}

Query/Engine/QueryIndexer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ private void load() throws IOException {
5151
Map<String, Object> mem = JsonParser.jsonToMap(object);
5252
numberOfNodes = (Integer) mem.get("numberOfNodes");
5353
numberOfRelations = (Integer) mem.get("numberOfRelations");
54+
maxNodeOutgoing = (Integer) mem.get("maxNodeOutgoing");
55+
maxNodeIncoming = (Integer) mem.get("maxNodeIncoming");
5456
labelRelation = (Map<String, Integer>) mem.get("labelRelation");
5557
labelNodes = (Map<String, Integer>) mem.get("labelNodes");
5658
propertyCountOfNodes = (Map<String, Integer>) mem.get("propertyCountOfNodes");
@@ -67,6 +69,8 @@ private void dump() throws IOException {
6769
JSONObject object = new JSONObject();
6870
object.put("numberOfNodes", numberOfNodes);
6971
object.put("numberOfRelations", numberOfRelations);
72+
object.put("maxNodeIncoming", maxNodeIncoming);
73+
object.put("maxNodeOutgoing", maxNodeOutgoing);
7074
object.put("labelRelation", labelRelation);
7175
object.put("labelNodes", labelNodes);
7276
object.put("propertyCountOfNodes", propertyCountOfNodes);
@@ -216,7 +220,8 @@ public int getNumberOfRelations(){
216220
return this.numberOfRelations;
217221
}
218222

219-
public int getMaxEdgesOfNode(){
220-
return this.maxNodeIncoming > this.maxNodeOutgoing ? this.maxNodeIncoming : this.maxNodeOutgoing;
223+
public int getAvgEdgesOfNode(){
224+
225+
return (int)(this.numberOfRelations * 1.0 / this.numberOfNodes);
221226
}
222227
}

Query/Engine/QueryPlanner.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,7 @@ public PlanTree plan() {
186186
ProduceResultPlan produceResultPlan = new ProduceResultPlan(indexer, bestPlan, retList);
187187
produceResultPlan.applyTo(bestPlan);
188188

189-
for(Plan plan : bestPlan.plans.toList()){
190-
System.out.println(plan.getName() + "|" + plan.getVariable() + "|" + plan.getParams());
191-
}
192-
193189
return bestPlan.plans;
194-
195190
}
196191

197192
private void removeRelated(Plan plan){

Query/Execution/ExecutionUtility.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public void startRecording() {
2121
}
2222

2323
public List<String> getHistory() {
24-
return this.dbUtil.stopAndReturn();
24+
List<String> newHistory = new ArrayList<>();
25+
newHistory.addAll( this.dbUtil.stopAndReturn());
26+
return newHistory;
2527
}
2628

2729
public List<String> getAllNodes() {

Query/Execution/ExpandAllExec.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public ResultTable execute(ResultTable table) {
5959

6060
Set<String> startNodes = new HashSet<>(table.getAll(insideNode));
6161
Map<String, List<List<String>>> candidates = new HashMap<>();
62-
List<String> retItems = new ArrayList<>(Arrays.asList("to", "name"));
62+
List<String> retItems;
6363

6464
for(String node : startNodes){
6565
Map<String, String> condition = new HashMap<>();
@@ -68,27 +68,35 @@ public ResultTable execute(ResultTable table) {
6868
case "-->" :
6969
if(insideNode.equals(edge.start)){
7070
condition.put("from", node);
71+
retItems = new ArrayList<>(Arrays.asList("to", "name"));
7172
}else{
7273
condition.put("to", node);
74+
retItems = new ArrayList<>(Arrays.asList("from", "name"));
7375
}
7476
res = exeUtil.getEdgesBy(retItems, condition, relationTypes);
7577
candidates.put(node, res);
7678
break;
7779
case "<--":
7880
if(insideNode.equals(edge.start)){
7981
condition.put("to", node);
82+
retItems = new ArrayList<>();
83+
retItems.add("from");
8084
}else{
85+
retItems = new ArrayList<>();
86+
retItems.add("to");
8187
condition.put("from", node);
8288
}
8389
res = exeUtil.getEdgesBy(retItems, condition, relationTypes);
8490
candidates.put(node, res);
8591
break;
8692
case "--" :case "<-->":
8793
condition.put("to", node);
94+
retItems = new ArrayList<>(Arrays.asList("from", "name"));
8895
res = exeUtil.getEdgesBy(retItems, condition, relationTypes);
8996
condition.remove("to");
9097

9198
condition.put("from", node);
99+
retItems = new ArrayList<>(Arrays.asList("to", "name"));
92100
res.addAll(exeUtil.getEdgesBy(retItems, condition, relationTypes));
93101
candidates.put(node, res);
94102
break;

Query/Execution/FilterRelationEqualityExec.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ public ResultTable execute(ResultTable table1) {
2323
for(Equality equality : equalities){
2424
String relation1 = equality.var1;
2525
String relation2 = equality.var2;
26-
27-
//TODO: Implement other equality types
28-
assert "==".equals(equality.equality);
29-
table1.shrinkByEquality(relation1, relation2);
26+
table1.shrinkByEquality(relation1, relation2, equality.equality);
3027
}
3128
this.querySQL = exeUtil.getHistory();
3229
return table1;

Query/Execution/ResultTable.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,23 @@ public void shrinkBySet(String key, Set<String> values){
6363
table = newTable;
6464
}
6565

66-
public void shrinkByEquality(String key1, String key2){
66+
public void shrinkByEquality(String key1, String key2, String equality){
6767
int index1 = keyToIdx.get(key1), index2 = keyToIdx.get(key2);
6868
List<List<String>> newTable = new ArrayList<>();
6969
for(List<String> row : table){
70-
if(row.get(index1).equals(row.get(index2))){
71-
newTable.add(row);
70+
switch (equality){
71+
case "==" :
72+
if(row.get(index1).equals(row.get(index2))){
73+
newTable.add(row);
74+
}
75+
break;
76+
case "!=":
77+
if(!row.get(index1).equals(row.get(index2))){
78+
newTable.add(row);
79+
}
80+
break;
81+
default:
82+
break;
7283
}
7384
}
7485
table = newTable;

0 commit comments

Comments
 (0)