Skip to content

Commit

Permalink
fix: distinctAggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
getrebuild committed Jul 28, 2024
1 parent 9646d53 commit 971286c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,32 @@ private String compileQuery(AST select, Filter filter) {
final JoinTree aJTree = new JoinTree(entity.getPhysicalName(),
nestedSelectContext == null ? -1 : nestedSelectContext.getTableIncrease() /* nested-sql */ );

boolean distinctAggregator = false;
Set<JoinField> distinctFields = new HashSet<>();
Map<JoinField, AST> mutliFieldsAggregators = new HashMap<>();

List<JoinField> selectFields = new LinkedList<>();
AST item = select.getFirstChild();
do {
JoinField aJF;

if (item.getType() == AjQLParserTokenTypes.DISTINCT) {
item = item.getNextSibling();
AST next = item.getNextSibling();
// eg. distinct DATE_FORMAT(x, x)
if (ParserHelper.isAggregator(next.getType())) {
distinctAggregator = true;
continue;
}

item = next;
aJF = bindJoinField(aJTree, entity, item, SelectItemType.Field);
distinctFields.add(aJF);

} else if (item.getType() == AjQLParserTokenTypes.CONCAT) {
aJF = bindJoinField(aJTree, entity, item, SelectItemType.Aggregator);
aJF.setAggregator(item.getText(), null);
mutliFieldsAggregators.put(aJF, item);

} else if (ParserHelper.isAggregator(item.getType())) {
AST column = item.getFirstChild();
boolean withDistinct = column.getType() == AjQLParserTokenTypes.DISTINCT;
Expand All @@ -207,11 +215,16 @@ private String compileQuery(AST select, Filter filter) {
aJF.setAggregatorMode(mode);
}
}


if (distinctAggregator) {
distinctFields.add(aJF);
distinctAggregator = false;
}

} else {
aJF = bindJoinField(aJTree, entity, item, SelectItemType.Field);
}

selectFields.add(aJF);
} while ((item = item.getNextSibling()) != null);
selectList.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ public void testSelectItems() {

@Test
public void testFulltextMatch() {
// String ajql = "select tPrimary,tInt,tBool from TestAllType where tLong > 100 and match(tReference.tLong,tText) against ('123 abc NB' in boolean mode)";
// QueryCompiler compiler = createCompiler(ajql);
// System.out.println(ajql + "\n>> FULLTEXT MATCH\n" + compiler.getCompiledSql());

String ajql2 = "select tPrimary from TestAllType where (tInt > 100 and tText match '123 abc NB')";
QueryCompiler compiler2 = createCompiler(ajql2);
System.out.println(ajql2 + "\n>> FULLTEXT MATCH\n" + compiler2.getCompiledSql());
// 推荐
String ajqlV2 = "select tPrimary,tInt,tBool from TestAllType where match2 (tReference, '123 abc NB')";
QueryCompiler compilerV2 = createCompiler(ajqlV2);
System.out.println(ajqlV2 + "\n>> FULLTEXT MATCH\n" + compilerV2.getCompiledSql());

String ajql = "select tPrimary from TestAllType where tInt > 100 and tText match '123 abc NB'";
QueryCompiler compiler = createCompiler(ajql);
System.out.println(ajql + "\n>> FULLTEXT MATCH\n" + compiler.getCompiledSql());
}

@Test
Expand All @@ -150,7 +151,7 @@ public void testBAnd() {
assertEquals(compiler.getCompiledSql(),
"select _t0.`T_PRIMARY` as _c0 from `test_all_type` as _t0 where ( _t0.`T_INT` & 1 = 0 ) and _t0.`T_INT` & 1");
}

@Test
public void testCount() {
String ajql = "select count(tPrimary) from TestAllType";
Expand Down Expand Up @@ -222,4 +223,15 @@ public void testGroupConcat() {
compiler = createCompiler(ajql);
System.out.println(ajql + "\n>>\n" + compiler.getCompiledSql());
}

@Test
public void testFnDistinct() {
String ajql = "select distinct DATE_FORMAT(tDate, '%Y-%m') from TestAllType";
QueryCompiler compiler = createCompiler(ajql);
System.out.println(ajql + "\n>>\n" + compiler.getCompiledSql());

String ajql2 = "select distinct tDate from TestAllType";
QueryCompiler compiler2 = createCompiler(ajql2);
System.out.println(ajql2 + "\n>>\n" + compiler2.getCompiledSql());
}
}

0 comments on commit 971286c

Please sign in to comment.