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

Skip to content

Commit 47388a9

Browse files
committed
scripts fixed:
1. need to return value on fields script 2. toDouble not supported 3. floor ,sqrt , round changed to Math.floor ..
1 parent 617ce6a commit 47388a9

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class SQLFunctions {
2727
);
2828

2929

30-
public static Tuple<String, String> function(String methodName, List<KVValue> paramers, String name) {
30+
public static Tuple<String, String> function(String methodName, List<KVValue> paramers, String name,boolean returnValue) {
3131
Tuple<String, String> functionStr = null;
3232
switch (methodName) {
3333
case "split":
@@ -113,6 +113,12 @@ public static Tuple<String, String> function(String methodName, List<KVValue> pa
113113
default:
114114

115115
}
116+
if(returnValue){
117+
String generatedFieldName = functionStr.v1();
118+
String returnCommand = ";return " + generatedFieldName +";" ;
119+
String newScript = functionStr.v2() + returnCommand;
120+
functionStr = new Tuple(generatedFieldName, newScript);
121+
}
116122
return functionStr;
117123
}
118124

@@ -143,12 +149,14 @@ public static Tuple<String, String> concat_ws(String split, List<SQLExpr> column
143149
//split(Column str, java.lang.String pattern)
144150
public static Tuple<String, String> split(String strColumn, String pattern, int index, String valueName) {
145151
String name = "split_" + random();
152+
String script = "";
146153
if (valueName == null) {
147-
return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value.split('" + pattern + "')[" + index + "]");
154+
script = "def " + name + " = doc['" + strColumn + "'].value.split('" + pattern + "')[" + index + "]";
155+
148156
} else {
149-
return new Tuple(name, strColumn + "; def " + name + " = " + valueName + ".split('" + pattern + "')[" + index + "]");
157+
script = "; def " + name + " = " + valueName + ".split('" + pattern + "')[" + index + "]";
150158
}
151-
159+
return new Tuple(name, script);
152160
}
153161

154162
public static Tuple<String, String> date_format(String strColumn, String pattern, String valueName) {
@@ -190,11 +198,10 @@ public static Tuple<String, String> divide(SQLExpr a, SQLExpr b) {
190198
public static Tuple<String, String> binaryOpertator(String methodName, String operator, SQLExpr a, SQLExpr b) {
191199

192200
String name = methodName + "_" + random();
193-
194201
return new Tuple(name,
195202
scriptDeclare(a) + scriptDeclare(b) +
196203
convertType(a) + convertType(b) +
197-
" def " + name + " = " + extractName(a) + " " + operator + " " + extractName(b));
204+
" def " + name + " = " + extractName(a) + " " + operator + " " + extractName(b) ) ;
198205
}
199206

200207
private static boolean isProperty(SQLExpr expr) {
@@ -227,7 +234,8 @@ private static String convertType(SQLExpr script) {
227234
if (newScript.trim().startsWith("def ")) {
228235
//for now ,if variant is string,then change to double.
229236
String temp = newScript.substring(4).split("=")[0].trim();
230-
return " if( " + temp + " instanceof String) " + temp + "=" + temp.trim() + ".toDouble(); ";
237+
238+
return " if( " + temp + " instanceof String) " + temp + "= Double.parseDouble(" + temp.trim() + "); ";
231239
} else return "";
232240

233241

@@ -248,13 +256,13 @@ public static Tuple<String, String> log10(String strColumn, String valueName) {
248256

249257
public static Tuple<String, String> sqrt(String strColumn, String valueName) {
250258

251-
return mathSingleValueTemplate("sqrt", strColumn, valueName);
259+
return mathSingleValueTemplate("Math.sqrt", "sqrt", strColumn, valueName);
252260

253261
}
254262

255263
public static Tuple<String, String> round(String strColumn, String valueName) {
256264

257-
return mathSingleValueTemplate("round", strColumn, valueName);
265+
return mathSingleValueTemplate("Math.round","round", strColumn, valueName);
258266

259267
}
260268

@@ -265,7 +273,10 @@ public static Tuple<String, String> trim(String strColumn, String valueName) {
265273
}
266274

267275
public static Tuple<String, String> mathSingleValueTemplate(String methodName, String strColumn, String valueName) {
268-
String name = methodName + "_" + random();
276+
return mathSingleValueTemplate(methodName,methodName, strColumn,valueName);
277+
}
278+
public static Tuple<String, String> mathSingleValueTemplate(String methodName,String fieldName, String strColumn, String valueName) {
279+
String name = fieldName + "_" + random();
269280
if (valueName == null) {
270281
return new Tuple(name, "def " + name + " = " + methodName + "(doc['" + strColumn + "'].value)");
271282
} else {
@@ -277,7 +288,7 @@ public static Tuple<String, String> mathSingleValueTemplate(String methodName, S
277288
public static Tuple<String, String> strSingleValueTemplate(String methodName, String strColumn, String valueName) {
278289
String name = methodName + "_" + random();
279290
if (valueName == null) {
280-
return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value." + methodName + "()");
291+
return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value." + methodName + "()" );
281292
} else {
282293
return new Tuple(name, strColumn + "; def " + name + " = " + valueName + "." + methodName + "()");
283294
}
@@ -286,7 +297,7 @@ public static Tuple<String, String> strSingleValueTemplate(String methodName, St
286297

287298
public static Tuple<String, String> floor(String strColumn, String valueName) {
288299

289-
return mathSingleValueTemplate("floor", strColumn, valueName);
300+
return mathSingleValueTemplate("Math.floor", "floor",strColumn, valueName);
290301

291302
}
292303

@@ -306,7 +317,7 @@ public static Tuple<String, String> substring(String strColumn, int pos, int len
306317
public static Tuple<String, String> split(String strColumn, String pattern, String valueName) {
307318
String name = "split_" + random();
308319
if (valueName == null) {
309-
return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value.split('" + pattern + "')");
320+
return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value.split('" + pattern + "')" );
310321
} else {
311322
return new Tuple(name, strColumn + "; def " + name + " = " + valueName + ".split('" + pattern + "')");
312323
}

src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@ public static MethodField makeMethodField(String name, List<SQLExpr> arguments,
267267
if (alias == null && first) {
268268
alias = "field_" + SQLFunctions.random();//paramers.get(0).value.toString();
269269
}
270+
//should check if field and first .
270271
Tuple<String, String> newFunctions = SQLFunctions.function(finalMethodName, paramers,
271-
paramers.get(0).key);
272+
paramers.get(0).key,first);
272273
paramers.clear();
273274
if (!first) {
274275
//variance

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public void functionFieldAliasAndGroupByAlias() throws Exception {
4848
"floor(substring(address,0,3)*20) as key," +
4949
"sum(age) cvalue FROM " + TestsConstants.TEST_INDEX + "/account where address is not null " +
5050
"group by key order by cvalue desc limit 10 ";
51+
SearchDao searchDao = MainTestSuite.getSearchDao() != null ? MainTestSuite.getSearchDao() : getSearchDao();
52+
System.out.println(searchDao.explain(query).explain().explain());
5153

5254
CSVResult csvResult = getCsvResult(false, query);
5355
List<String> headers = csvResult.getHeaders();
@@ -249,6 +251,8 @@ public void split_field() throws Exception {
249251
" split(address,' ')[0],age from " +
250252
TestsConstants.TEST_INDEX + "/account where address is not null " +
251253
" limit 10 ";
254+
SearchDao searchDao = MainTestSuite.getSearchDao() != null ? MainTestSuite.getSearchDao() : getSearchDao();
255+
System.out.println(searchDao.explain(query).explain().explain());
252256

253257
CSVResult csvResult = getCsvResult(false, query);
254258
List<String> headers = csvResult.getHeaders();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ public void scriptFiledPlusLiteralTest() throws SqlParseException {
447447
MethodField scriptMethod = (MethodField) field;
448448
Assert.assertEquals("script", scriptMethod.getName().toLowerCase());
449449
Assert.assertEquals(2, scriptMethod.getParams().size());
450-
Assert.assertTrue(scriptMethod.getParams().get(1).toString().endsWith("doc['field1'].value + 3"));
450+
Assert.assertTrue(scriptMethod.getParams().get(1).toString().contains("doc['field1'].value + 3"));
451451
}
452452

453453
@Test
@@ -462,7 +462,7 @@ public void scriptFieldPlusFieldTest() throws SqlParseException {
462462
MethodField scriptMethod = (MethodField) field;
463463
Assert.assertEquals("script", scriptMethod.getName().toLowerCase());
464464
Assert.assertEquals(2, scriptMethod.getParams().size());
465-
Assert.assertTrue(scriptMethod.getParams().get(1).toString().endsWith("doc['field1'].value + doc['field2'].value"));
465+
Assert.assertTrue(scriptMethod.getParams().get(1).toString().contains("doc['field1'].value + doc['field2'].value"));
466466
}
467467

468468

@@ -478,7 +478,7 @@ public void scriptLiteralPlusLiteralTest() throws SqlParseException {
478478
MethodField scriptMethod = (MethodField) field;
479479
Assert.assertEquals("script", scriptMethod.getName().toLowerCase());
480480
Assert.assertEquals(2, scriptMethod.getParams().size());
481-
Assert.assertTrue(scriptMethod.getParams().get(1).toString().endsWith("1 + 2"));
481+
Assert.assertTrue(scriptMethod.getParams().get(1).toString().contains("1 + 2"));
482482
}
483483

484484

src/test/resources/elasticsearch.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
script.inline: true
22
script.indexed: true
3+
script.max_compilations_per_minute: 100

0 commit comments

Comments
 (0)