Skip to content

Commit 7570a2b

Browse files
committed
Feat: Additional support for sum operator in what-if SQL query
1 parent d4d4453 commit 7570a2b

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

traindb-core/src/main/java/traindb/jdbc/TrainDBWhatIfQueryTransformer.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,76 @@ public static boolean containsWhatIfToClause(String query) {
1414
}
1515

1616
// Function to print debug information
17-
public static void printDebugInfo(String asClause, String multiplier, String countColumn,
18-
String tableName, String whereCondition, String whatIfCondition) {
17+
public static void printDebugInfo(String asClause, String multiplier, String aggregationFunction,
18+
String columnExpression, String tableName, String whereCondition, String whatIfCondition) {
1919
System.out.println("asClause: " + asClause);
2020
System.out.println("multiplier: " + multiplier);
21-
System.out.println("countColumn: " + countColumn);
21+
System.out.println("aggregationFunction: " + aggregationFunction);
22+
System.out.println("columnExpression: " + columnExpression);
2223
System.out.println("tableName: " + tableName);
2324
System.out.println("whereCondition: " + whereCondition);
2425
System.out.println("whatIfCondition: " + whatIfCondition);
2526
}
2627

28+
// Helper function to generate appropriate alias based on the aggregation function
29+
public static String generateAlias(String aggregationFunction) {
30+
switch (aggregationFunction.toLowerCase()) {
31+
case "count":
32+
return "cnt_res";
33+
case "sum":
34+
return "sum_res";
35+
default:
36+
return "res"; // Fallback for any other aggregation functions
37+
}
38+
}
39+
2740
// Function to transform the query if it contains the WHATIF ~ TO clause
2841
public static String transformQuery(String inputQuery) {
2942
// Define the regex pattern for SELECT ~ FROM ~ WHERE ~ WHATIF ~ TO
30-
String whatIfToPatternString = "SELECT\\s+APPROXIMATE\\s+count\\((\\*|\\w+)\\)(?:\\s+as\\s+(\\w+))?\\s+FROM\\s+(\\w+\\.\\w+)(?:\\s+WHERE\\s+(.+?))?\\s+WHATIF\\s+(.+)\\s+TO\\s+(\\d+\\.\\d+)";
43+
String whatIfToPatternString = "SELECT\\s+APPROXIMATE\\s+(count|sum)\\(([^)]+)\\)(?:\\s+as\\s+(\\w+))?\\s+FROM\\s+(\\w+\\.\\w+)(?:\\s+WHERE\\s+(.+?))?\\s+WHATIF\\s+(.+)\\s+TO\\s+(\\d+\\.\\d+)";
3144
Pattern whatIfToPattern = Pattern.compile(whatIfToPatternString, Pattern.CASE_INSENSITIVE);
3245
Matcher whatIfToMatcher = whatIfToPattern.matcher(inputQuery.trim());
3346

3447
if (whatIfToMatcher.find()) {
3548
// Extract components from the input query
36-
String countColumn = whatIfToMatcher.group(1); // e.g., "*" or "productid"
37-
String alias = whatIfToMatcher.group(2); // e.g., "total" (could be null)
38-
String tableName = whatIfToMatcher.group(3); // e.g., "myschema.sales"
39-
String whereCondition = whatIfToMatcher.group(4); // e.g., "sales.price > 5" (could be null)
40-
String whatIfCondition = whatIfToMatcher.group(5); // e.g., "sales.productid > 5 and sales.productid < 10"
41-
String multiplier = whatIfToMatcher.group(6); // e.g., "1.2"
49+
String aggregationFunction = whatIfToMatcher.group(1); // e.g., "count" or "sum"
50+
String columnExpression = whatIfToMatcher.group(2); // e.g., "*" or "productid * price * num"
51+
String alias = whatIfToMatcher.group(3); // e.g., "total_sum" (could be null)
52+
String tableName = whatIfToMatcher.group(4); // e.g., "myschema.sales"
53+
String whereCondition = whatIfToMatcher.group(5); // e.g., "sales.price > 5" (could be null)
54+
String whatIfCondition = whatIfToMatcher.group(6); // e.g., "sales.productid > 5 and sales.productid < 10"
55+
String multiplier = whatIfToMatcher.group(7); // e.g., "1.2"
4256

4357
// Determine the "as" clause part for the output query
4458
String asClause = (alias != null) ? " as " + alias : "";
4559

4660
// Check each part individually before passing to String.format
47-
//printDebugInfo(asClause, multiplier, countColumn, tableName, whereCondition, whatIfCondition);
61+
//printDebugInfo(asClause, multiplier, aggregationFunction, columnExpression, tableName, whereCondition, whatIfCondition);
4862

63+
// In your transformation code
4964
String outputQuery = String.format(
50-
"SELECT sum(cnt)%s\n" + // asClause
65+
"SELECT SUM(%s)%s\n" + // Use dynamic alias
5166
"FROM (\n" +
52-
" SELECT APPROXIMATE %s * count(%s) as cnt\n" + // multiplier, countColumn
67+
" SELECT APPROXIMATE %s * %s(%s) AS %s\n" + // multiplier, aggregationFunction, columnExpression, dynamic alias
5368
" FROM %s\n" + // tableName
5469
" WHERE %s%s\n" + // whereCondition and whatIfCondition
5570
" UNION ALL\n" +
56-
" SELECT APPROXIMATE count(%s) as cnt\n" + // countColumn
71+
" SELECT APPROXIMATE %s(%s) AS %s\n" + // aggregationFunction, columnExpression, dynamic alias
5772
" FROM %s\n" + // tableName
5873
" WHERE %s%s\n" + // whereCondition and whatIfCondition for NOT clause
5974
") as t",
75+
generateAlias(aggregationFunction), // Dynamic alias for sum in outer query
6076
asClause != null ? " " + asClause : "", // %s for asClause
6177
multiplier, // %s for multiplier
62-
countColumn, // %s for countColumn
78+
aggregationFunction, // %s for aggregationFunction
79+
columnExpression, // %s for columnExpression
80+
generateAlias(aggregationFunction), // Dynamic alias for inner query
6381
tableName, // %s for tableName
6482
whereCondition != null ? whereCondition : "", // %s for whereCondition (with null handling)
6583
whereCondition != null ? " AND " + whatIfCondition : whatIfCondition, // %s for whatIfCondition (with null handling)
66-
countColumn, // %s for countColumn
84+
aggregationFunction, // %s for aggregationFunction
85+
columnExpression, // %s for columnExpression
86+
generateAlias(aggregationFunction), // Dynamic alias for inner query
6787
tableName, // %s for tableName
6888
whereCondition != null ? whereCondition : "", // %s for whereCondition (with null handling)
6989
whereCondition != null ? " AND NOT (" + whatIfCondition + ")" : "NOT (" + whatIfCondition + ")" // %s for NOT whatIfCondition (with null handling)

0 commit comments

Comments
 (0)