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

Skip to content

Commit bef138a

Browse files
committed
fix power function parameter error
change pow(a) to pow(a,b)
1 parent 5600da3 commit bef138a

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.common.collect.Lists;
88
import com.google.common.collect.Sets;
99
import org.elasticsearch.common.collect.Tuple;
10+
import org.elasticsearch.plugin.nlpcn.executors.CSVResult;
1011
import org.nlpcn.es4sql.domain.KVValue;
1112

1213
import java.util.List;
@@ -62,17 +63,20 @@ public static Tuple<String, String> function(String methodName, List<KVValue> pa
6263
name);
6364
break;
6465

65-
case "floor":
66+
case "abs":
6667
case "round":
68+
case "floor":
6769
case "ceil":
6870
case "cbrt":
6971
case "rint":
70-
case "pow":
7172
case "exp":
7273
case "sqrt":
7374
functionStr = mathSingleValueTemplate("Math."+methodName,methodName,Util.expr2Object((SQLExpr) paramers.get(0).value).toString(), name);
7475
break;
7576

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

7781
case "substring":
7882
functionStr = substring(Util.expr2Object((SQLExpr) paramers.get(0).value).toString(),
@@ -302,6 +306,15 @@ public static Tuple<String, String> trim(String strColumn, String valueName) {
302306

303307
}
304308

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

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,18 +247,26 @@ public void concat_ws_fields() throws Exception {
247247

248248
@Test
249249
public void functionLogs() throws Exception {
250-
MainTestSuite.setUp();
251250
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";
251+
+ TEST_INDEX_ACCOUNT + "/account limit 1";
253252
CSVResult csvResult = getCsvResult(false, query);
254253
List<String> content = csvResult.getLines();
255-
System.out.println(content.toString());
256254
Assert.assertTrue(content.toString().contains("2.0"));
257255
Assert.assertTrue(content.toString().contains("1.0"));
258256
Assert.assertTrue(content.toString().contains("0.0"));
259257
Assert.assertTrue(content.toString().contains("3.0"));
260258
}
261259

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+
262270
// todo: change when split is back on language
263271
// @Test
264272
// public void split_field() throws Exception {

0 commit comments

Comments
 (0)