@@ -14,56 +14,76 @@ public static boolean containsWhatIfToClause(String query) {
14
14
}
15
15
16
16
// 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 ) {
19
19
System .out .println ("asClause: " + asClause );
20
20
System .out .println ("multiplier: " + multiplier );
21
- System .out .println ("countColumn: " + countColumn );
21
+ System .out .println ("aggregationFunction: " + aggregationFunction );
22
+ System .out .println ("columnExpression: " + columnExpression );
22
23
System .out .println ("tableName: " + tableName );
23
24
System .out .println ("whereCondition: " + whereCondition );
24
25
System .out .println ("whatIfCondition: " + whatIfCondition );
25
26
}
26
27
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
+
27
40
// Function to transform the query if it contains the WHATIF ~ TO clause
28
41
public static String transformQuery (String inputQuery ) {
29
42
// 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+)" ;
31
44
Pattern whatIfToPattern = Pattern .compile (whatIfToPatternString , Pattern .CASE_INSENSITIVE );
32
45
Matcher whatIfToMatcher = whatIfToPattern .matcher (inputQuery .trim ());
33
46
34
47
if (whatIfToMatcher .find ()) {
35
48
// 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"
42
56
43
57
// Determine the "as" clause part for the output query
44
58
String asClause = (alias != null ) ? " as " + alias : "" ;
45
59
46
60
// 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);
48
62
63
+ // In your transformation code
49
64
String outputQuery = String .format (
50
- "SELECT sum(cnt )%s\n " + // asClause
65
+ "SELECT SUM(%s )%s\n " + // Use dynamic alias
51
66
"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
53
68
" FROM %s\n " + // tableName
54
69
" WHERE %s%s\n " + // whereCondition and whatIfCondition
55
70
" UNION ALL\n " +
56
- " SELECT APPROXIMATE count (%s) as cnt \n " + // countColumn
71
+ " SELECT APPROXIMATE %s (%s) AS %s \n " + // aggregationFunction, columnExpression, dynamic alias
57
72
" FROM %s\n " + // tableName
58
73
" WHERE %s%s\n " + // whereCondition and whatIfCondition for NOT clause
59
74
") as t" ,
75
+ generateAlias (aggregationFunction ), // Dynamic alias for sum in outer query
60
76
asClause != null ? " " + asClause : "" , // %s for asClause
61
77
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
63
81
tableName , // %s for tableName
64
82
whereCondition != null ? whereCondition : "" , // %s for whereCondition (with null handling)
65
83
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
67
87
tableName , // %s for tableName
68
88
whereCondition != null ? whereCondition : "" , // %s for whereCondition (with null handling)
69
89
whereCondition != null ? " AND NOT (" + whatIfCondition + ")" : "NOT (" + whatIfCondition + ")" // %s for NOT whatIfCondition (with null handling)
0 commit comments