Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.zstack.zql.ast.visitors.plugin;

import org.apache.commons.lang.StringUtils;
import org.zstack.header.zql.ASTNode;

import java.util.List;
import java.util.stream.Collectors;

import static org.zstack.zql.ast.visitors.constants.MySqlKeyword.isDistinct;

public class SimpleCountPlugin extends AbstractQueryVisitorPlugin {
public SimpleCountPlugin() {
Expand All @@ -24,13 +24,13 @@ public String selectTarget() {
String queryTarget;
List<String> fieldNames = super.targetFields();

if (fieldNames.isEmpty()) {
queryTarget = entityAlias;
} else {
if (getClauseType() == ClauseType.COUNT || isDistinct(node.getTarget().getFunction())) {
// TODO: Compatibility changes: hql count do not support multiple fields, even if distinct modified.
queryTarget = String.format("%s.%s", inventory.simpleInventoryName(), fieldNames.get(0));
queryTarget = fieldNames.isEmpty() ? entityAlias : String.format("%s.%s", inventory.simpleInventoryName(), fieldNames.get(0));
return String.format("count(%s)", String.format(functions(), queryTarget));
} else {
queryTarget = entityAlias;
return String.format("count(%s)", queryTarget);
}

return String.format("count(%s)", String.format(functions(), queryTarget));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CountGroupByCase extends SubCase {
env.create {
testCountGroupBy()
testQuery()
testFieldQueryTotalWithNullableFirstField()
testGroupLimit()
testEmpty()
}
Expand Down Expand Up @@ -80,6 +81,13 @@ class CountGroupByCase extends SubCase {
assert ret2.total == 102
}

void testFieldQueryTotalWithNullableFirstField() {
def ret = ZQL.fromString("query instanceoffering.description,uuid return with (total) limit 100").getSingleResult() as ZQLQueryReturn
assert ret.inventories.size() == 100
assert ret.total == 102
assert ret.inventories.every { it.description == null }
}

void testGroupLimit() {
def ret = ZQL.fromString("count instanceoffering group by name,cpuNum limit 2").getSingleResult() as ZQLQueryReturn
assert ret.inventoryCounts.size() == 2
Expand Down