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

Fixed issue of log10 function gets inaccurate results #298

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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