forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFusionUtil.java
More file actions
190 lines (164 loc) · 7.17 KB
/
DataFusionUtil.java
File metadata and controls
190 lines (164 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package sqlancer.datafusion;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import sqlancer.datafusion.DataFusionProvider.DataFusionGlobalState;
public final class DataFusionUtil {
private DataFusionUtil() {
dfAssert(false, "Utility class cannot be instantiated");
}
// Display tables in `fromTableNames`
public static String displayTables(DataFusionGlobalState state, List<String> fromTableNames) {
StringBuilder resultStringBuilder = new StringBuilder();
for (String tableName : fromTableNames) {
String query = String.format("select * from %s", tableName);
try (Statement stat = state.getConnection().createStatement();
ResultSet wholeTable = stat.executeQuery(query)) {
ResultSetMetaData metaData = wholeTable.getMetaData();
int columnCount = metaData.getColumnCount();
resultStringBuilder.append("Table: ").append(tableName).append("\n");
for (int i = 1; i <= columnCount; i++) {
resultStringBuilder.append(metaData.getColumnName(i)).append(" (")
.append(metaData.getColumnTypeName(i)).append(")");
if (i < columnCount) {
resultStringBuilder.append(", ");
}
}
resultStringBuilder.append("\n");
while (wholeTable.next()) {
for (int i = 1; i <= columnCount; i++) {
resultStringBuilder.append(wholeTable.getString(i));
if (i < columnCount) {
resultStringBuilder.append(", ");
}
}
resultStringBuilder.append("\n");
}
resultStringBuilder.append("----------------------------------------\n\n");
} catch (SQLException err) {
resultStringBuilder.append("Table: ").append(tableName).append("\n");
resultStringBuilder.append("----------------------------------------\n\n");
// resultStringBuilder.append("Error retrieving data from table ").append(tableName).append(":
// ").append(err.getMessage()).append("\n");
}
}
return resultStringBuilder.toString();
}
// During development, you might want to manually let this function call exit(1) to fail fast
public static void dfAssert(boolean condition, String message) {
if (!condition) {
// // Development mode assertion failure
// String methodName = Thread.currentThread().getStackTrace()[2]// .getMethodName();
// System.err.println("DataFusion assertion failed in function '" + methodName + "': " + message);
// exit(1);
throw new AssertionError(message);
}
}
/*
* Fetch all DMLs from logs/database*-cur.log
*/
public static String getReplay(String dbname) {
String path = "./logs/datafusion/" + dbname + "-cur.log";
String absolutePath = Paths.get(path).toAbsolutePath().toString();
StringBuilder reproducer = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(absolutePath))) {
String line;
while ((line = reader.readLine()) != null) {
// Check if the line contains the /*DML*/ marker
if (line.contains("/*DML*/")) {
reproducer.append(line).append("\n");
}
}
} catch (IOException e) {
System.err.println("Error reading from file: " + e.getMessage());
}
return reproducer.toString();
}
// UID for different fuzzer runs
public static class DataFusionInstanceID {
private final String id;
public DataFusionInstanceID(String dfID) {
id = dfID;
}
@Override
public String toString() {
return id; // Return the id field when toString is called
}
}
/*
* Extra logs stored in 'logs/datafusion_custom_log/' In case re-run overwrite previous logs
*/
public static class DataFusionLogger {
private final DataFusionInstanceID dfID;
private final DataFusionGlobalState state;
/*
* Log file handles
*/
private final File errorLogFile;
public DataFusionLogger(DataFusionGlobalState globalState, DataFusionInstanceID id) throws Exception {
this.state = globalState;
this.dfID = id;
// Setup datafusion_custom_log folder
File baseDir = new File("logs/datafusion_custom_log/");
if (!baseDir.exists() && !baseDir.mkdirs()) {
throw new IOException("Failed to create 'datafusion_custom_log' directory/");
}
// Setup error.log
errorLogFile = new File(baseDir, "error_report.log");
errorLogFile.createNewFile();
}
// Caller is responsible for adding '\n' at the end of logContent
public void appendToLog(DataFusionLogType logType, String logContent) {
FileWriter logFileWriter = null;
// Determine which log file to use based on the LogType
String logLineHeader = "";
switch (logType) {
case ERROR:
try {
logFileWriter = new FileWriter(errorLogFile, true);
} catch (IOException e) {
dfAssert(false, "Failed to create FileWriter for errorLogFIle");
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = LocalDateTime.now().format(formatter);
logLineHeader = "Run@" + formattedDateTime + " (" + dfID + ")\n";
break;
case DML:
logFileWriter = state.getLogger().getCurrentFileWriter();
logLineHeader = "/*DML*/";
break;
case SELECT:
logFileWriter = state.getLogger().getCurrentFileWriter();
break;
default:
dfAssert(false, "All branch should be covered");
}
// Append content to the appropriate log file
if (logFileWriter != null) {
try {
logFileWriter.write(logLineHeader);
logFileWriter.write(logContent);
logFileWriter.flush();
} catch (IOException e) {
String err = "Failed to write to " + logType + " log: " + e.getMessage();
dfAssert(false, err);
}
} else {
dfAssert(false, "appending to log failed");
}
}
public enum DataFusionLogType {
ERROR, DML, SELECT
}
}
}