@@ -25,13 +25,34 @@ public class SQLFunctions {
25
25
"random" , "abs" , //nummber operator
26
26
"split" , "concat_ws" , "substring" , "trim" ,//string operator
27
27
"add" , "multiply" , "divide" , "subtract" , "modulus" ,//binary operator
28
- "field" , "date_format"
28
+ "field" , "date_format" , "if"
29
29
);
30
30
31
31
32
32
public static Tuple <String , String > function (String methodName , List <KVValue > paramers , String name ,boolean returnValue ) {
33
33
Tuple <String , String > functionStr = null ;
34
- switch (methodName ) {
34
+ switch (methodName .toLowerCase ()) {
35
+ case "if" :
36
+ String nameIF = "" ;
37
+ String caseString = "" ;
38
+ if (paramers .get (0 ).value instanceof SQLInListExpr ){
39
+ nameIF += methodName +"(" +((SQLInListExpr ) paramers .get (0 ).value ).getExpr ()+" in (" ;
40
+ String left = "doc['" +((SQLInListExpr ) paramers .get (0 ).value ).getExpr ().toString ()+"'].value" ;
41
+ List <SQLExpr > targetList = ((SQLInListExpr ) paramers .get (0 ).value ).getTargetList ();
42
+ for (SQLExpr a :targetList ){
43
+ caseString += left + " == '" + a .toString () + "' ||" ;
44
+ nameIF += a .toString ()+"," ;
45
+ }
46
+ caseString = caseString .substring (0 ,caseString .length ()-2 );
47
+ nameIF = nameIF .substring (0 ,nameIF .length ()-1 )+")," ;
48
+ }else {
49
+ String left = "doc['" +paramers .get (0 ).key +"'].value" ;
50
+ caseString += left + " == '" + paramers .get (0 ).value +"'" ;
51
+ nameIF = methodName +"(" +paramers .get (0 ).toString ()+"," ;
52
+ }
53
+ nameIF += paramers .get (1 ).value +"," +paramers .get (2 ).value +")" ;
54
+ functionStr = new Tuple <>(nameIF ,"if((" +caseString +")){" +paramers .get (1 ).value +"} else {" +paramers .get (2 ).value +"}" );
55
+ break ;
35
56
case "split" :
36
57
if (paramers .size () == 3 ) {
37
58
functionStr = split (Util .expr2Object ((SQLExpr ) paramers .get (0 ).value ).toString (),
@@ -66,6 +87,12 @@ public static Tuple<String, String> function(String methodName, List<KVValue> pa
66
87
case "abs" :
67
88
case "round" :
68
89
case "floor" :
90
+ if (paramers .size () == 2 ) {
91
+ //zhongshu-comment es的round()默认是保留到个位,这里给round()函数加上精确到小数点后第几位的功能
92
+ int decimalPrecision = Integer .parseInt (paramers .get (1 ).value .toString ());
93
+ functionStr = mathRoundTemplate ("Math." +methodName ,methodName ,Util .expr2Object ((SQLExpr ) paramers .get (0 ).value ).toString (), name , decimalPrecision );
94
+ break ;
95
+ }
69
96
case "ceil" :
70
97
case "cbrt" :
71
98
case "rint" :
@@ -131,7 +158,7 @@ public static Tuple<String, String> function(String methodName, List<KVValue> pa
131
158
default :
132
159
133
160
}
134
- if (returnValue ){
161
+ if (returnValue && ! methodName . equalsIgnoreCase ( "if" ) ){
135
162
String generatedFieldName = functionStr .v1 ();
136
163
String returnCommand = ";return " + generatedFieldName +";" ;
137
164
String newScript = functionStr .v2 () + returnCommand ;
@@ -328,6 +355,23 @@ private static Tuple<String, String> mathSingleValueTemplate(String methodName,
328
355
329
356
}
330
357
358
+ private static Tuple <String , String > mathRoundTemplate (String methodName , String fieldName , String strColumn , String valueName , int decimalPrecision ) {
359
+
360
+ StringBuilder sb = new StringBuilder ("1" );
361
+ for (int i = 0 ; i < decimalPrecision ; i ++) {
362
+ sb .append ("0" );
363
+ }
364
+ double num = Double .parseDouble (sb .toString ());
365
+
366
+ String name = fieldName + "_" + random ();
367
+ if (valueName == null ) {
368
+ return new Tuple <>(name , "def " + name + " = " + methodName + "((doc['" + strColumn + "'].value) * " + num + ")/" + num );
369
+ } else {
370
+ return new Tuple <>(name , strColumn + ";def " + name + " = " + methodName + "((" + valueName + ") * " + num + ")/" + num );
371
+ }
372
+
373
+ }
374
+
331
375
public static Tuple <String , String > strSingleValueTemplate (String methodName , String strColumn , String valueName ) {
332
376
String name = methodName + "_" + random ();
333
377
if (valueName == null ) {
0 commit comments