Skip to content

Commit 5600da3

Browse files
author
zhangdt
committed
support multiple type of logs function
1) log2(param) 2) log10(param) 3) log(param) base=e 4) log(base_number, param)
1 parent 01f6d3c commit 5600da3

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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;
@@ -19,7 +20,7 @@ public class SQLFunctions {
1920

2021
//Groovy Built In Functions
2122
public final static Set<String> buildInFunctions = Sets.newHashSet(
22-
"exp", "log", "log10", "sqrt", "cbrt", "ceil", "floor", "rint", "pow", "round",
23+
"exp", "log", "log2", "log10", "log10", "sqrt", "cbrt", "ceil", "floor", "rint", "pow", "round",
2324
"random", "abs", //nummber operator
2425
"split", "concat_ws", "substring", "trim",//string operator
2526
"add", "multiply", "divide", "subtract", "modulus",//binary operator
@@ -63,8 +64,6 @@ public static Tuple<String, String> function(String methodName, List<KVValue> pa
6364

6465
case "floor":
6566
case "round":
66-
case "log":
67-
case "log10":
6867
case "ceil":
6968
case "cbrt":
7069
case "rint":
@@ -107,6 +106,24 @@ public static Tuple<String, String> function(String methodName, List<KVValue> pa
107106
functionStr = field(Util.expr2Object((SQLExpr) paramers.get(0).value).toString());
108107
break;
109108

109+
case "log2":
110+
functionStr = log(SQLUtils.toSQLExpr("2"), (SQLExpr) paramers.get(0).value, name);
111+
break;
112+
case "log10":
113+
functionStr = log(SQLUtils.toSQLExpr("10"), (SQLExpr) paramers.get(0).value, name);
114+
break;
115+
case "log":
116+
List<SQLExpr> logs = Lists.newArrayList();
117+
for (int i = 0; i < paramers.size(); i++) {
118+
logs.add((SQLExpr) paramers.get(0).value);
119+
}
120+
if (logs.size() > 1) {
121+
functionStr = log(logs.get(0), logs.get(1), name);
122+
} else {
123+
functionStr = log(SQLUtils.toSQLExpr("Math.E"), logs.get(0), name);
124+
}
125+
break;
126+
110127
default:
111128

112129
}
@@ -252,6 +269,20 @@ public static Tuple<String, String> log10(String strColumn, String valueName) {
252269
return mathSingleValueTemplate("log10", strColumn, valueName);
253270

254271
}
272+
public static Tuple<String, String> log(SQLExpr base, SQLExpr strColumn, String valueName) {
273+
String name = "log_" + random();
274+
String result;
275+
if (valueName == null) {
276+
if (isProperty(strColumn)) {
277+
result = "def " + name + " = Math.log(doc['" + Util.expr2Object(strColumn).toString() + "'].value)/Math.log("+Util.expr2Object(base).toString()+")";
278+
} else {
279+
result = "def " + name + " = Math.log(" + Util.expr2Object(strColumn).toString() + ")/Math.log("+Util.expr2Object(base).toString()+")";
280+
}
281+
} else {
282+
result = Util.expr2Object(strColumn).toString()+";def "+name+" = Math.log("+valueName+")/Math.log("+Util.expr2Object(base).toString()+")";
283+
}
284+
return new Tuple(name, result);
285+
}
255286

256287
public static Tuple<String, String> sqrt(String strColumn, String valueName) {
257288

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,20 @@ 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+
MainTestSuite.setUp();
251+
String query = "SELECT log10(100) as a, log(1) as b, log(2, 4) as c, log2(8) as d from "
252+
+ TestsConstants.TEST_INDEX + "/account limit 1";
253+
CSVResult csvResult = getCsvResult(false, query);
254+
List<String> content = csvResult.getLines();
255+
System.out.println(content.toString());
256+
Assert.assertTrue(content.toString().contains("2.0"));
257+
Assert.assertTrue(content.toString().contains("1.0"));
258+
Assert.assertTrue(content.toString().contains("0.0"));
259+
Assert.assertTrue(content.toString().contains("3.0"));
260+
}
261+
248262
// todo: change when split is back on language
249263
// @Test
250264
// public void split_field() throws Exception {

0 commit comments

Comments
 (0)