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

Commit

Permalink
Merge branch 'greatestFunction-temp' into greatestLeastFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
cip999 committed Aug 22, 2021
2 parents f5ea58b + 83107e0 commit 3644545
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.amazon.opendistroforelasticsearch.sql.legacy;

import static com.amazon.opendistroforelasticsearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT;
import static com.amazon.opendistroforelasticsearch.sql.legacy.TestsConstants.TEST_INDEX_DATE;
import static com.amazon.opendistroforelasticsearch.sql.legacy.TestsConstants.TEST_INDEX_DATE_TIME;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.hitAny;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.kvDouble;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.kvInt;
Expand Down Expand Up @@ -68,6 +70,7 @@ protected void init() throws Exception {
loadIndex(Index.BANK);
loadIndex(Index.ONLINE);
loadIndex(Index.DATE);
loadIndex(Index.DATETIME);
}

@Test
Expand Down Expand Up @@ -877,6 +880,18 @@ public void literalMultiField() throws Exception {
assertThat(hits[0].getFields(), allOf(hasValue(contains(1)), hasValue(contains(2))));
}

@Test
public void greatestWithAliasAndStrings() throws Exception {
JSONObject response =
executeJdbcRequest("SELECT greatest(firstname, lastname) AS max " +
"FROM " + TEST_INDEX_ACCOUNT + " LIMIT 3");

verifyDataRows(response,
rows("duke"),
rows("hattie"),
rows("nanette"));
}

private SearchHits query(String query) throws IOException {
final String rsp = executeQueryWithStringOutput(query);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,19 @@ public enum ScalarFunction implements TypeExpression {
EXP(func(T(NUMBER)).to(T)),
EXPM1(func(T(NUMBER)).to(T)),
FLOOR(func(T(NUMBER)).to(T)),
GREATEST(
func(T(NUMBER), NUMBER).to(T),
func(T(STRING), STRING).to(T),
func(ESDataType.DATE, ESDataType.DATE).to(ESDataType.DATE)
),
IF(func(BOOLEAN, ES_TYPE, ES_TYPE).to(ES_TYPE)),
IFNULL(func(ES_TYPE, ES_TYPE).to(ES_TYPE)),
ISNULL(func(ES_TYPE).to(INTEGER)),
LEAST(
func(T(NUMBER), NUMBER).to(T),
func(T(STRING), STRING).to(T),
func(ESDataType.DATE, ESDataType.DATE).to(ESDataType.DATE)
),
LEFT(func(T(STRING), INTEGER).to(T)),
LENGTH(func(STRING).to(INTEGER)),
LN(func(T(NUMBER)).to(DOUBLE)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class SQLFunctions {
);

private static final Set<String> binaryOperators = Sets.newHashSet(
"add", "multiply", "divide", "subtract", "modulus"
"add", "multiply", "divide", "subtract", "modulus", "greatest", "least"
);

private static final Set<String> dateFunctions = Sets.newHashSet(
Expand Down Expand Up @@ -332,6 +332,13 @@ public Tuple<String, String> function(String methodName, List<KVValue> paramers,
functionStr = modulus((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
break;

case "greatest":
functionStr = greatest((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
break;
case "least":
functionStr = least((SQLExpr) paramers.get(0).value, (SQLExpr) paramers.get(1).value);
break;

case "field":
functionStr = field(Util.expr2Object((SQLExpr) paramers.get(0).value).toString());
break;
Expand Down Expand Up @@ -596,6 +603,57 @@ private Tuple<String, String> binaryOpertator(String methodName, String operator
+ def(name, extractName(a) + " " + operator + " " + extractName(b)));
}

private Tuple<String, String> greatest(SQLExpr a, SQLExpr b) {
String name = nextId("greatest");

return comparisonFunctionTemplate(a, b, name, ">");
}

private Tuple<String, String> least(SQLExpr a, SQLExpr b) {
String name = nextId("least");

return comparisonFunctionTemplate(a, b, name, "<");
}

private Tuple<String, String> comparisonFunctionTemplate(SQLExpr a, SQLExpr b,
String name, String comparisonOperator) {
String value_a = (a instanceof SQLTextLiteralExpr
? getPropertyOrStringValue(a) : getPropertyOrValue(a));
String value_b = (b instanceof SQLTextLiteralExpr
? getPropertyOrStringValue(b) : getPropertyOrValue(b));

String nameString_a = nextId("string_a");
String nameString_b = nextId("string_b");

String numberComparison = String.format("%s = (%s %s %s ? %s : %s);",
name, value_a, comparisonOperator, value_b, value_a, value_b);
String stringOrDateComparison = def(nameString_a, convertToString(value_a)) + ";"
+ def(nameString_b, convertToString(value_b)) + ";"
+ String.format("%s = (%s.compareTo(%s) %s 0 ? %s : %s);",
name, nameString_a, nameString_b, comparisonOperator, value_a, value_b);

String definition = "def " + name + ";"
+ String.format("if (%s) {%s = %s;} ", checkIfNull(a), name, value_b)
+ String.format("else if (%s) {%s = %s;} ", checkIfNull(b), name, value_a)
+ "else {";

if (isProperty(a) && isProperty(b)) {
definition += String.format("if (%s instanceof Number) {", value_a)
+ numberComparison
+ "} else {"
+ stringOrDateComparison
+ "} ";
} else if (a instanceof SQLNumericLiteralExpr || b instanceof SQLNumericLiteralExpr) {
definition += numberComparison;
} else {
definition += stringOrDateComparison;
}

definition += String.format("} %s = %s", name, name);

return new Tuple<>(name, definition);
}

private static boolean isProperty(SQLExpr expr) {
return (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr
|| expr instanceof SQLVariantRefExpr);
Expand All @@ -604,6 +662,8 @@ private static boolean isProperty(SQLExpr expr) {
private static String getPropertyOrValue(SQLExpr expr) {
if (isProperty(expr)) {
return doc(expr) + ".value";
} else if (expr instanceof SQLNullExpr) {
return "null";
} else {
return exprString(expr);
}
Expand All @@ -628,7 +688,6 @@ private static String getPropertyOrStringValue(SQLExpr expr) {
}

private static String scriptDeclare(SQLExpr a) {

if (isProperty(a) || a instanceof SQLNumericLiteralExpr) {
return "";
} else {
Expand Down Expand Up @@ -668,6 +727,22 @@ private static String convertType(SQLExpr script) {

}

private static String checkIfNull(SQLExpr expr) {
if (isProperty(expr)) {
return doc(expr) + ".size() == 0 || " + getPropertyOrValue(expr) + " == null";
} else if (expr instanceof SQLNullExpr) {
return "0 == 0";
}
else {
return "0 == 1";
}
}

private static String convertToString(String name) {
return String.format("(%s instanceof String ? (String) %s : %s.toString())",
name, name, name);
}

private String getScriptText(MethodField field) {
String content = ((SQLTextLiteralExpr) field.getParams().get(1).value).getText();
return content;
Expand Down

0 comments on commit 3644545

Please sign in to comment.