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

Commit 4048fab

Browse files
authored
support number field (#68)
ex. select 1 from
1 parent f6c9eaf commit 4048fab

File tree

6 files changed

+174
-2
lines changed

6 files changed

+174
-2
lines changed

src/main/java/com/amazon/opendistroforelasticsearch/sql/parser/FieldMaker.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public static Field makeField(SQLExpr expr, String alias, String tableAlias) thr
8585
methodParameters.add(new KVValue(alias));
8686
methodParameters.add(new KVValue(scriptCode));
8787
return new MethodField("script", methodParameters, null, alias);
88+
} else if (expr instanceof SQLNumericLiteralExpr) {
89+
SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr("assign", null);
90+
methodInvokeExpr.addParameter(expr);
91+
return makeMethodField(methodInvokeExpr.getMethodName(), methodInvokeExpr.getParameters(), null, alias, tableAlias, true);
8892
} else {
8993
throw new SqlParseException("unknown field name : " + expr);
9094
}

src/main/java/com/amazon/opendistroforelasticsearch/sql/utils/SQLFunctions.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SQLFunctions {
6060
"day_of_week", "hour_of_day", "minute_of_day", "minute_of_hour", "second_of_minute"
6161
);
6262

63-
private final static Set<String> utilityFunctions = Sets.newHashSet("field");
63+
private final static Set<String> utilityFunctions = Sets.newHashSet("field", "assign");
6464

6565
public final static Set<String> builtInFunctions = Stream.of(
6666
numberOperators,
@@ -229,7 +229,9 @@ public static Tuple<String, String> function(String methodName, List<KVValue> pa
229229
functionStr = log(SQLUtils.toSQLExpr("Math.E"), logs.get(0), name);
230230
}
231231
break;
232-
232+
case "assign":
233+
functionStr = assign((SQLExpr) paramers.get(0).value);
234+
break;
233235
default:
234236

235237
}
@@ -341,6 +343,12 @@ public static Tuple<String, String> add(SQLExpr a, SQLExpr b) {
341343
return binaryOpertator("add", "+", a, b);
342344
}
343345

346+
public static Tuple<String, String> assign(SQLExpr a) {
347+
String name = randomize("assign");
348+
return new Tuple<>(name,
349+
def(name, extractName(a)));
350+
}
351+
344352
private static Tuple<String, String> modulus(SQLExpr a, SQLExpr b) {
345353
return binaryOpertator("modulus", "%", a, b);
346354
}

src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/SQLFunctionsIT.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,35 @@
1616
package com.amazon.opendistroforelasticsearch.sql.esintgtest;
1717

1818

19+
import org.elasticsearch.action.search.SearchResponse;
20+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
21+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
22+
import org.elasticsearch.common.xcontent.XContentFactory;
23+
import org.elasticsearch.common.xcontent.XContentParser;
24+
import org.elasticsearch.common.xcontent.XContentType;
25+
import org.elasticsearch.search.SearchHit;
26+
import org.elasticsearch.search.SearchHits;
1927
import org.json.JSONObject;
2028
import org.junit.Assert;
2129
import org.junit.Ignore;
2230
import org.junit.Test;
2331

32+
import java.io.IOException;
2433
import java.util.stream.IntStream;
2534

2635
import static com.amazon.opendistroforelasticsearch.sql.esintgtest.TestsConstants.TEST_INDEX_ACCOUNT;
2736
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.hitAny;
2837
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.kvDouble;
2938
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.kvString;
39+
import static org.hamcrest.Matchers.allOf;
3040
import static org.hamcrest.Matchers.both;
41+
import static org.hamcrest.Matchers.contains;
3142
import static org.hamcrest.Matchers.containsString;
3243
import static org.hamcrest.Matchers.endsWith;
3344
import static org.hamcrest.Matchers.equalTo;
45+
import static org.hamcrest.Matchers.hasEntry;
46+
import static org.hamcrest.Matchers.hasValue;
47+
import static org.hamcrest.Matchers.is;
3448
import static org.hamcrest.Matchers.isEmptyOrNullString;
3549
import static org.hamcrest.Matchers.not;
3650

@@ -191,4 +205,51 @@ public void split_field() throws Exception {
191205
TestsConstants.TEST_INDEX + "/account where address is not null " +
192206
" limit 10 ";
193207
}
208+
209+
@Test
210+
public void literal() throws Exception {
211+
String query = "SELECT 10 "+
212+
"from " + TEST_INDEX_ACCOUNT + "/account limit 1";
213+
final SearchHit[] hits = query(query).getHits();
214+
assertThat(hits[0].getFields(), hasValue(contains(10)));
215+
}
216+
217+
@Test
218+
public void literalWithDoubleValue() throws Exception {
219+
String query = "SELECT 10.0 "+
220+
"from " + TEST_INDEX_ACCOUNT + "/account limit 1";
221+
222+
final SearchHit[] hits = query(query).getHits();
223+
assertThat(hits[0].getFields(), hasValue(contains(10.0)));
224+
}
225+
226+
@Test
227+
public void literalWithAlias() throws Exception {
228+
String query = "SELECT 10 as key "+
229+
"from " + TEST_INDEX_ACCOUNT + "/account limit 1";
230+
final SearchHit[] hits = query(query).getHits();
231+
232+
assertThat(hits.length, is(1));
233+
assertThat(hits[0].getFields(), hasEntry(is("key"), contains(10)));
234+
}
235+
236+
@Test
237+
public void literalMultiField() throws Exception {
238+
String query = "SELECT 1, 2 "+
239+
"from " + TEST_INDEX_ACCOUNT + "/account limit 1";
240+
final SearchHit[] hits = query(query).getHits();
241+
242+
assertThat(hits.length, is(1));
243+
assertThat(hits[0].getFields(), allOf(hasValue(contains(1)), hasValue(contains(2))));
244+
}
245+
246+
private SearchHits query(String query) throws IOException {
247+
final String rsp = executeQueryWithStringOutput(query);
248+
249+
final XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(
250+
NamedXContentRegistry.EMPTY,
251+
LoggingDeprecationHandler.INSTANCE,
252+
rsp);
253+
return SearchResponse.fromXContent(parser).getHits();
254+
}
194255
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amazon.opendistroforelasticsearch.sql.unittest.parser;
17+
18+
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
19+
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
20+
import com.amazon.opendistroforelasticsearch.sql.domain.MethodField;
21+
import com.amazon.opendistroforelasticsearch.sql.exception.SqlParseException;
22+
import com.amazon.opendistroforelasticsearch.sql.parser.FieldMaker;
23+
import org.junit.Test;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertTrue;
27+
28+
public class FieldMakerTest {
29+
30+
private static final String ALIAS = "a";
31+
32+
private static final String TABLE_ALIAS = "t";
33+
34+
@Test
35+
public void makeFieldAssign() throws SqlParseException {
36+
final SQLIntegerExpr sqlExpr = new SQLIntegerExpr(10);
37+
final MethodField field = (MethodField) FieldMaker.makeField(sqlExpr, ALIAS, TABLE_ALIAS);
38+
39+
assertEquals("script", field.getName());
40+
assertEquals(ALIAS, field.getParams().get(0).value);
41+
assertTrue(((String)field.getParams().get(1).value).matches("def assign_[0-9]+ = 10;return assign_[0-9]+;"));
42+
assertEquals(ALIAS, field.getAlias());
43+
}
44+
45+
@Test
46+
public void makeFieldAssignDouble() throws SqlParseException {
47+
final SQLNumberExpr sqlExpr = new SQLNumberExpr(10.0);
48+
final MethodField field = (MethodField) FieldMaker.makeField(sqlExpr, ALIAS, TABLE_ALIAS);
49+
50+
assertEquals("script", field.getName());
51+
assertEquals(ALIAS, field.getParams().get(0).value);
52+
assertTrue(((String)field.getParams().get(1).value).matches("def assign_[0-9]+ = 10.0;return assign_[0-9]+;"));
53+
assertEquals(ALIAS, field.getAlias());
54+
}
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amazon.opendistroforelasticsearch.sql.unittest.utils;
17+
18+
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
19+
import com.amazon.opendistroforelasticsearch.sql.domain.KVValue;
20+
import com.amazon.opendistroforelasticsearch.sql.utils.SQLFunctions;
21+
import com.google.common.collect.ImmutableList;
22+
import org.elasticsearch.common.collect.Tuple;
23+
import org.junit.Test;
24+
25+
import static org.junit.Assert.assertTrue;
26+
27+
public class SQLFunctionsTest {
28+
29+
@Test
30+
public void testAssign() {
31+
final SQLIntegerExpr sqlIntegerExpr = new SQLIntegerExpr(10);
32+
final Tuple<String, String> assign = SQLFunctions.function("assign",
33+
ImmutableList.of(new KVValue(null, sqlIntegerExpr)),
34+
null,
35+
true);
36+
37+
assertTrue(assign.v1().matches("assign_[0-9]+"));
38+
assertTrue(assign.v2().matches("def assign_[0-9]+ = 10;return assign_[0-9]+;"));
39+
}
40+
}

src/test/java/com/amazon/opendistroforelasticsearch/sql/util/MatcherUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,8 @@ public static Matcher<JSONObject> kvString(String key, Matcher<String> matcher)
102102
public static Matcher<JSONObject> kvDouble(String key, Matcher<Double> matcher) {
103103
return featureValueOf("Json Match", matcher, actual -> (Double) actual.query(key));
104104
}
105+
106+
public static Matcher<JSONObject> kvInt(String key, Matcher<Integer> matcher) {
107+
return featureValueOf("Json Match", matcher, actual -> (Integer) actual.query(key));
108+
}
105109
}

0 commit comments

Comments
 (0)