Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Fixed issue of log10 function gets inaccurate results (#298)
Browse files Browse the repository at this point in the history
*  Fixed the inaccurate result of log10 function issue.
*  Updated the tests.
  • Loading branch information
chloe-zh authored Nov 21, 2019
1 parent 332cb48 commit 4744915
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,17 @@ public Tuple<String, String> function(String methodName, List<KVValue> paramers,
functionStr = log(SQLUtils.toSQLExpr("2"), (SQLExpr) paramers.get(0).value, name);
break;
case "log10":
functionStr = log(SQLUtils.toSQLExpr("10"), (SQLExpr) paramers.get(0).value, name);
functionStr = log10((SQLExpr) paramers.get(0).value);
break;
case "log":
if (paramers.size() > 1) {
functionStr = log((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value, name);
} else {
functionStr = log((SQLUtils.toSQLExpr("Math.E")), (SQLExpr) paramers.get(0).value, name);
functionStr = ln((SQLExpr) paramers.get(0).value);
}
break;
case "ln":
functionStr = log(SQLUtils.toSQLExpr("Math.E"), (SQLExpr) paramers.get(0).value, name);
functionStr = ln((SQLExpr) paramers.get(0).value);
break;
case "assign":
functionStr = assign((SQLExpr) paramers.get(0).value);
Expand Down Expand Up @@ -653,6 +653,16 @@ public Tuple<String, String> log(SQLExpr base, SQLExpr field, String valueName)
return new Tuple<>(name, result);
}

public Tuple<String, String> log10(SQLExpr field) {
String name = nextId("log10");
return new Tuple<>(name, def(name, StringUtils.format("Math.log10(%s)", getPropertyOrValue(field))));
}

public Tuple<String, String> ln(SQLExpr field) {
String name = nextId("ln");
return new Tuple<>(name, def(name, StringUtils.format("Math.log(%s)", getPropertyOrValue(field))));
}

public Tuple<String, String> trim(SQLExpr field, String valueName) {
return strSingleValueTemplate("trim", field, valueName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ public void logInAggregationShouldPass() {
);
}

@Test
public void log10Test() throws IOException{
SearchHit[] hits = query("SELECT log10(1000) AS log10");
double log10 = (double) getField(hits[0], "log10");
assertThat(log10, equalTo(3.0));
}

@Test
public void ln() throws IOException {
SearchHit[] hits = query("SELECT LN(5) AS ln");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,18 @@ public void logWithTwoParams() {
"Math.log(doc['age'].value)/Math.log(3)"));
}

@Test
public void log10Test() {
String query = "SELECT LOG10(age) FROM accounts";
ScriptField scriptField = CheckScriptContents.getScriptFieldFromQuery(query);
assertTrue(
CheckScriptContents.scriptContainsString(
scriptField,
"Math.log10(doc['age'].value)"
)
);
}

@Test
public void lnTest() {
String query = "SELECT LN(age) FROM age WHERE LN(age) = 5.0";
Expand Down

0 comments on commit 4744915

Please sign in to comment.