Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 908f6c4

Browse files
authored
Merge pull request NLPchina#672 from tbbrave/master
fix log, abs, pow functions
2 parents 01f6d3c + bef138a commit 908f6c4

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/main/java/org/nlpcn/es4sql/SQLFunctions.java

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.nlpcn.es4sql;
22

3+
import com.alibaba.druid.sql.SQLUtils;
34
import com.alibaba.druid.sql.ast.SQLExpr;
45
import com.alibaba.druid.sql.ast.expr.*;
56
import com.google.common.base.Joiner;
67
import com.google.common.collect.Lists;
78
import com.google.common.collect.Sets;
89
import org.elasticsearch.common.collect.Tuple;
10+
import org.elasticsearch.plugin.nlpcn.executors.CSVResult;
911
import org.nlpcn.es4sql.domain.KVValue;
1012

1113
import java.util.List;
@@ -19,7 +21,7 @@ public class SQLFunctions {
1921

2022
//Groovy Built In Functions
2123
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",
2325
"random", "abs", //nummber operator
2426
"split", "concat_ws", "substring", "trim",//string operator
2527
"add", "multiply", "divide", "subtract", "modulus",//binary operator
@@ -61,19 +63,20 @@ public static Tuple<String, String> function(String methodName, List<KVValue> pa
6163
name);
6264
break;
6365

64-
case "floor":
66+
case "abs":
6567
case "round":
66-
case "log":
67-
case "log10":
68+
case "floor":
6869
case "ceil":
6970
case "cbrt":
7071
case "rint":
71-
case "pow":
7272
case "exp":
7373
case "sqrt":
7474
functionStr = mathSingleValueTemplate("Math."+methodName,methodName,Util.expr2Object((SQLExpr) paramers.get(0).value).toString(), name);
7575
break;
7676

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;
7780

7881
case "substring":
7982
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
107110
functionStr = field(Util.expr2Object((SQLExpr) paramers.get(0).value).toString());
108111
break;
109112

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+
110131
default:
111132

112133
}
@@ -252,6 +273,20 @@ public static Tuple<String, String> log10(String strColumn, String valueName) {
252273
return mathSingleValueTemplate("log10", strColumn, valueName);
253274

254275
}
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+
}
255290

256291
public static Tuple<String, String> sqrt(String strColumn, String valueName) {
257292

@@ -271,6 +306,15 @@ public static Tuple<String, String> trim(String strColumn, String valueName) {
271306

272307
}
273308

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+
274318
private static Tuple<String, String> mathSingleValueTemplate(String methodName, String strColumn, String valueName) {
275319
return mathSingleValueTemplate(methodName,methodName, strColumn,valueName);
276320
}

src/test/java/org/nlpcn/es4sql/SQLFunctionsTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,28 @@ public void concat_ws_fields() throws Exception {
245245
Assert.assertTrue(contents.get(0).contains("-"));
246246
}
247247

248+
@Test
249+
public void functionLogs() throws Exception {
250+
String query = "SELECT log10(100) as a, log(1) as b, log(2, 4) as c, log2(8) as d from "
251+
+ TEST_INDEX_ACCOUNT + "/account limit 1";
252+
CSVResult csvResult = getCsvResult(false, query);
253+
List<String> content = csvResult.getLines();
254+
Assert.assertTrue(content.toString().contains("2.0"));
255+
Assert.assertTrue(content.toString().contains("1.0"));
256+
Assert.assertTrue(content.toString().contains("0.0"));
257+
Assert.assertTrue(content.toString().contains("3.0"));
258+
}
259+
260+
@Test
261+
public void functionPow() throws Exception {
262+
String query = "SELECT pow(account_number, 2) as key,"+
263+
"abs(age - 60) as new_age from " + TEST_INDEX_ACCOUNT + "/account limit 1";
264+
CSVResult csvResult = getCsvResult(false, query);
265+
List<String> content = csvResult.getLines();
266+
Assert.assertTrue(content.toString().contains("625"));
267+
Assert.assertTrue(content.toString().contains("21"));
268+
}
269+
248270
// todo: change when split is back on language
249271
// @Test
250272
// public void split_field() throws Exception {

0 commit comments

Comments
 (0)