Skip to content

Commit 4b7e5b7

Browse files
committed
fix count distinct
1 parent fee2cbd commit 4b7e5b7

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>org.nlpcn</groupId>
55
<artifactId>elasticsearch-sql</artifactId>
6-
<version>8.7.0.0</version>
6+
<version>8.7.0.1</version>
77
<packaging>jar</packaging>
88
<description>Query elasticsearch using SQL</description>
99
<name>elasticsearch-sql</name>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public static Field makeField(SQLExpr expr, String alias, String tableAlias) thr
6868
return makeFieldSortMethodField(mExpr, alias);
6969
}
7070

71-
return makeMethodField(methodName, mExpr.getParameters(), null, alias, tableAlias, true);
71+
SQLAggregateOption option = mExpr instanceof SQLAggregateExpr ? ((SQLAggregateExpr) mExpr).getOption() : null;
72+
return makeMethodField(methodName, mExpr.getArguments(), option, alias, tableAlias, true);
7273
} else if (expr instanceof SQLAggregateExpr) {
7374
SQLAggregateExpr sExpr = (SQLAggregateExpr) expr;
7475
return makeMethodField(sExpr.getMethodName(), sExpr.getArguments(), sExpr.getOption(), alias, tableAlias, true);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ public void testSpanNearQueryExplain() throws SqlParseException, SQLFeatureNotSu
102102
}
103103

104104
@Test
105-
public void testCountDistinctExplain() throws SqlParseException, SQLFeatureNotSupportedException {
106-
System.out.println(explain("SELECT COUNT(DISTINCT sourceIP.keyword) AS size FROM dataflow WHERE startTime > 525757149439 AND startTime < 1525757449439 GROUP BY appName.keyword ORDER BY size DESC"));
105+
public void testCountDistinctExplain() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
106+
String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/aggregation_cardinality_explain.json"), StandardCharsets.UTF_8).replaceAll("\r", "");
107+
String result = explain("SELECT COUNT(DISTINCT sourceIP.keyword) AS size FROM dataflow WHERE startTime > 525757149439 AND startTime < 1525757449439 GROUP BY appName.keyword ORDER BY size DESC");
108+
assertThat(result.replaceAll("\\s+", ""), equalTo(expectedOutput.replaceAll("\\s+", "")));
107109
}
108110

109111
@Test
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"from": 0,
3+
"size": 0,
4+
"query": {
5+
"bool": {
6+
"filter": [
7+
{
8+
"bool": {
9+
"must": [
10+
{
11+
"bool": {
12+
"must": [
13+
{
14+
"range": {
15+
"startTime": {
16+
"gt": 525757149439,
17+
"boost": 1.0
18+
}
19+
}
20+
},
21+
{
22+
"range": {
23+
"startTime": {
24+
"lt": 1525757449439,
25+
"boost": 1.0
26+
}
27+
}
28+
}
29+
],
30+
"boost": 1.0
31+
}
32+
}
33+
],
34+
"boost": 1.0
35+
}
36+
}
37+
],
38+
"boost": 1.0
39+
}
40+
},
41+
"_source": {
42+
"includes": [
43+
"COUNT"
44+
],
45+
"excludes": []
46+
},
47+
"aggregations": {
48+
"appName.keyword": {
49+
"terms": {
50+
"field": "appName.keyword",
51+
"size": 1000,
52+
"shard_size": 20000,
53+
"min_doc_count": 1,
54+
"shard_min_doc_count": 0,
55+
"show_term_doc_count_error": false,
56+
"order": [
57+
{
58+
"size": "desc"
59+
},
60+
{
61+
"_key": "asc"
62+
}
63+
]
64+
},
65+
"aggregations": {
66+
"size": {
67+
"cardinality": {
68+
"field": "sourceIP.keyword"
69+
}
70+
}
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)