1
1
package org .nlpcn .es4sql ;
2
2
3
+ import com .alibaba .druid .sql .SQLUtils ;
3
4
import com .alibaba .druid .sql .ast .SQLExpr ;
4
5
import com .alibaba .druid .sql .ast .expr .*;
5
6
import com .google .common .base .Joiner ;
6
7
import com .google .common .collect .Lists ;
7
8
import com .google .common .collect .Sets ;
8
9
import org .elasticsearch .common .collect .Tuple ;
10
+ import org .elasticsearch .plugin .nlpcn .executors .CSVResult ;
9
11
import org .nlpcn .es4sql .domain .KVValue ;
10
12
11
13
import java .util .List ;
@@ -19,7 +21,7 @@ public class SQLFunctions {
19
21
20
22
//Groovy Built In Functions
21
23
public final static Set <String > buildInFunctions = Sets .newHashSet (
22
- "exp" , "log" , "log10" , "sqrt" , "cbrt" , "ceil" , "floor" , "rint" , "pow" , "round" ,
24
+ "exp" , "log" , "log2" , "log10" , " log10" , "sqrt" , "cbrt" , "ceil" , "floor" , "rint" , "pow" , "round" ,
23
25
"random" , "abs" , //nummber operator
24
26
"split" , "concat_ws" , "substring" , "trim" ,//string operator
25
27
"add" , "multiply" , "divide" , "subtract" , "modulus" ,//binary operator
@@ -61,19 +63,20 @@ public static Tuple<String, String> function(String methodName, List<KVValue> pa
61
63
name );
62
64
break ;
63
65
64
- case "floor " :
66
+ case "abs " :
65
67
case "round" :
66
- case "log" :
67
- case "log10" :
68
+ case "floor" :
68
69
case "ceil" :
69
70
case "cbrt" :
70
71
case "rint" :
71
- case "pow" :
72
72
case "exp" :
73
73
case "sqrt" :
74
74
functionStr = mathSingleValueTemplate ("Math." +methodName ,methodName ,Util .expr2Object ((SQLExpr ) paramers .get (0 ).value ).toString (), name );
75
75
break ;
76
76
77
+ case "pow" :
78
+ functionStr = mathDoubleValueTemplate ("Math." +methodName , methodName , Util .expr2Object ((SQLExpr ) paramers .get (0 ).value ).toString (), Util .expr2Object ((SQLExpr ) paramers .get (1 ).value ).toString (), name );
79
+ break ;
77
80
78
81
case "substring" :
79
82
functionStr = substring (Util .expr2Object ((SQLExpr ) paramers .get (0 ).value ).toString (),
@@ -107,6 +110,24 @@ public static Tuple<String, String> function(String methodName, List<KVValue> pa
107
110
functionStr = field (Util .expr2Object ((SQLExpr ) paramers .get (0 ).value ).toString ());
108
111
break ;
109
112
113
+ case "log2" :
114
+ functionStr = log (SQLUtils .toSQLExpr ("2" ), (SQLExpr ) paramers .get (0 ).value , name );
115
+ break ;
116
+ case "log10" :
117
+ functionStr = log (SQLUtils .toSQLExpr ("10" ), (SQLExpr ) paramers .get (0 ).value , name );
118
+ break ;
119
+ case "log" :
120
+ List <SQLExpr > logs = Lists .newArrayList ();
121
+ for (int i = 0 ; i < paramers .size (); i ++) {
122
+ logs .add ((SQLExpr ) paramers .get (0 ).value );
123
+ }
124
+ if (logs .size () > 1 ) {
125
+ functionStr = log (logs .get (0 ), logs .get (1 ), name );
126
+ } else {
127
+ functionStr = log (SQLUtils .toSQLExpr ("Math.E" ), logs .get (0 ), name );
128
+ }
129
+ break ;
130
+
110
131
default :
111
132
112
133
}
@@ -252,6 +273,20 @@ public static Tuple<String, String> log10(String strColumn, String valueName) {
252
273
return mathSingleValueTemplate ("log10" , strColumn , valueName );
253
274
254
275
}
276
+ public static Tuple <String , String > log (SQLExpr base , SQLExpr strColumn , String valueName ) {
277
+ String name = "log_" + random ();
278
+ String result ;
279
+ if (valueName == null ) {
280
+ if (isProperty (strColumn )) {
281
+ result = "def " + name + " = Math.log(doc['" + Util .expr2Object (strColumn ).toString () + "'].value)/Math.log(" +Util .expr2Object (base ).toString ()+")" ;
282
+ } else {
283
+ result = "def " + name + " = Math.log(" + Util .expr2Object (strColumn ).toString () + ")/Math.log(" +Util .expr2Object (base ).toString ()+")" ;
284
+ }
285
+ } else {
286
+ result = Util .expr2Object (strColumn ).toString ()+";def " +name +" = Math.log(" +valueName +")/Math.log(" +Util .expr2Object (base ).toString ()+")" ;
287
+ }
288
+ return new Tuple (name , result );
289
+ }
255
290
256
291
public static Tuple <String , String > sqrt (String strColumn , String valueName ) {
257
292
@@ -271,6 +306,15 @@ public static Tuple<String, String> trim(String strColumn, String valueName) {
271
306
272
307
}
273
308
309
+ private static Tuple <String , String > mathDoubleValueTemplate (String methodName , String fieldName , String val1 , String val2 , String valueName ) {
310
+ String name = fieldName + "_" + random ();
311
+ if (valueName == null ) {
312
+ return new Tuple (name , "def " +name +" = " +methodName +"(doc['" +val1 +"'].value, " +val2 +")" );
313
+ } else {
314
+ return new Tuple (name , val1 + ";def " +name +" = " +methodName +"(" +valueName +", " +val2 +")" );
315
+ }
316
+ }
317
+
274
318
private static Tuple <String , String > mathSingleValueTemplate (String methodName , String strColumn , String valueName ) {
275
319
return mathSingleValueTemplate (methodName ,methodName , strColumn ,valueName );
276
320
}
0 commit comments