for (Node node : nodes) {
if (BitSets.range(node.startCol, node.endCol).intersects(columnSet)) {
use(usedNodes, node);
}
}
final SqlDialect dialect = SqlDialect.DatabaseProduct.OPTIQ.getDialect();
final StringBuilder buf = new StringBuilder("SELECT ");
final StringBuilder groupBuf = new StringBuilder("\nGROUP BY ");
int k = 0;
final Set<String> columnNames = Sets.newHashSet();
for (int i : BitSets.toIter(groupSet)) {
if (k++ > 0) {
buf.append(", ");
groupBuf.append(", ");
}
final Column column = columns.get(i);
dialect.quoteIdentifier(buf, column.identifiers());
dialect.quoteIdentifier(groupBuf, column.identifiers());
final String fieldName = uniqueColumnNames.get(i);
columnNames.add(fieldName);
if (!column.alias.equals(fieldName)) {
buf.append(" AS ");
dialect.quoteIdentifier(buf, fieldName);
}
}
int m = 0;
for (Measure measure : aggCallList) {
if (k++ > 0) {
buf.append(", ");
}
buf.append(measure.agg.getName())
.append("(");
if (measure.args.isEmpty()) {
buf.append("*");
} else {
int z = 0;
for (Column arg : measure.args) {
if (z++ > 0) {
buf.append(", ");
}
dialect.quoteIdentifier(buf, arg.identifiers());
}
}
buf.append(") AS ");
String measureName;
while (!columnNames.add(measureName = "m" + m)) {
++m;
}
dialect.quoteIdentifier(buf, measureName);
}
buf.append("\nFROM ");
for (Node node : usedNodes) {
if (node.parent != null) {
buf.append("\nJOIN ");
}
dialect.quoteIdentifier(buf, node.scan.getTable().getQualifiedName());
buf.append(" AS ");
dialect.quoteIdentifier(buf, node.alias);
if (node.parent != null) {
buf.append(" ON ");
k = 0;
for (IntPair pair : node.link) {
if (k++ > 0) {
buf.append(" AND ");
}
final Column left = columns.get(node.parent.startCol + pair.source);
dialect.quoteIdentifier(buf, left.identifiers());
buf.append(" = ");
final Column right = columns.get(node.startCol + pair.target);
dialect.quoteIdentifier(buf, right.identifiers());
}
}
}
if (OptiqPrepareImpl.DEBUG) {
System.out.println("Lattice SQL:\n" + buf);