use of mondrian.rolap.RolapAggregator in project mondrian by pentaho.
the class AggGen method insertIntoLost.
/**
* Return the sql code to populate a lost dimension table from the fact
* table.
*/
public String insertIntoLost() {
StringWriter sw = new StringWriter(512);
PrintWriter pw = new PrintWriter(sw);
String prefix = " ";
String factTableName = getFactTableName();
SqlQuery sqlQuery = getSqlQuery();
pw.print("INSERT INTO ");
pw.print(makeLostAggregateTableName(getFactTableName()));
pw.println(" (");
for (JdbcSchema.Table.Column.Usage usage : notLostColumnUsages) {
JdbcSchema.Table.Column c = usage.getColumn();
pw.print(prefix);
pw.print(c.getName());
pw.println(',');
}
for (JdbcSchema.Table.Column.Usage usage : measures) {
JdbcSchema.Table.Column c = usage.getColumn();
pw.print(prefix);
String name = getUsageName(usage);
pw.print(name);
pw.println(',');
}
// do fact_count
pw.print(prefix);
pw.print(getFactCount());
pw.println(")");
pw.println("SELECT");
for (JdbcSchema.Table.Column.Usage usage : notLostColumnUsages) {
JdbcSchema.Table.Column c = usage.getColumn();
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(factTableName, c.getName()));
pw.print(" AS ");
pw.print(sqlQuery.getDialect().quoteIdentifier(c.getName()));
pw.println(',');
}
for (JdbcSchema.Table.Column.Usage usage : measures) {
JdbcSchema.Table.Column c = usage.getColumn();
RolapAggregator agg = usage.getAggregator();
pw.print(prefix);
pw.print(agg.getExpression(sqlQuery.getDialect().quoteIdentifier(factTableName, c.getName())));
pw.print(" AS ");
pw.print(sqlQuery.getDialect().quoteIdentifier(c.getName()));
pw.println(',');
}
// do fact_count
pw.print(prefix);
pw.print("COUNT(*) AS ");
pw.println(sqlQuery.getDialect().quoteIdentifier(getFactCount()));
pw.println("FROM ");
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(factTableName));
pw.print(" ");
pw.println(sqlQuery.getDialect().quoteIdentifier(factTableName));
pw.println("GROUP BY ");
int k = 0;
for (JdbcSchema.Table.Column.Usage notLostColumnUsage : notLostColumnUsages) {
if (k++ > 0) {
pw.println(",");
}
JdbcSchema.Table.Column.Usage usage = notLostColumnUsage;
JdbcSchema.Table.Column c = usage.getColumn();
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(factTableName, c.getName()));
}
pw.println(';');
return sw.toString();
}
use of mondrian.rolap.RolapAggregator in project mondrian by pentaho.
the class AggGen method init.
/**
* The columns are the RolapStar columns taking part in an aggregation
* request. This is what happens.
* First, for each column, walk up the column's table until one level below
* the base fact table. The left join condition contains the base fact table
* and the foreign key column name. This column should not be lost.
* Get the base fact table's measure columns.
* With a list of columns that should not be lost and measure, one can
* create lost create and insert commands.
*/
private void init() {
JdbcSchema db = JdbcSchema.makeDB(star.getDataSource());
try {
db.load();
} catch (SQLException ex) {
getLogger().error(ex);
return;
}
JdbcSchema.Table factTable = getTable(db, getFactTableName());
if (factTable == null) {
getLogger().warn("Init: " + "No fact table with name \"" + getFactTableName() + "\"");
return;
}
try {
factTable.load();
} catch (SQLException ex) {
getLogger().error(ex);
return;
}
if (getLogger().isDebugEnabled()) {
getLogger().debug("Init: " + "RolapStar:" + Util.nl + getFactTable() + Util.nl + "FactTable:" + Util.nl + factTable);
}
// do foreign keys
for (RolapStar.Column column : columns) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("Init: " + "Column: " + column);
}
RolapStar.Table table = column.getTable();
if (table.getParentTable() == null) {
// in the RolapStar is this hiearchy captured - ugg.
if (!addSpecialCollapsedColumn(db, column)) {
return;
}
MondrianDef.Expression expr = column.getExpression();
if (expr instanceof MondrianDef.Column) {
MondrianDef.Column exprColumn = (MondrianDef.Column) expr;
String name = exprColumn.getColumnName();
JdbcSchema.Table.Column c = getColumn(factTable, name);
if (c == null) {
getLogger().warn("Init: " + "FactTable:" + getFactTableName() + Util.nl + "No Column with name \"" + name + "\"");
return;
}
if (getLogger().isDebugEnabled()) {
getLogger().debug(" Jdbc Column: c=" + c);
}
addForeignKeyToNotLostColumnUsages(c);
}
} else {
if (!addCollapsedColumn(db, column)) {
return;
}
while (table.getParentTable().getParentTable() != null) {
table = table.getParentTable();
}
RolapStar.Condition cond = table.getJoinCondition();
if (getLogger().isDebugEnabled()) {
getLogger().debug(" RolapStar.Condition: cond=" + cond);
}
MondrianDef.Expression left = cond.getLeft();
if (left instanceof MondrianDef.Column) {
MondrianDef.Column leftColumn = (MondrianDef.Column) left;
String name = leftColumn.getColumnName();
JdbcSchema.Table.Column c = getColumn(factTable, name);
if (c == null) {
getLogger().warn("Init: " + "FactTable:" + getFactTableName() + Util.nl + "No Column with name \"" + name + "\"");
return;
}
if (getLogger().isDebugEnabled()) {
getLogger().debug(" Jdbc Column: c=" + c);
}
addForeignKeyToNotLostColumnUsages(c);
}
}
}
// do measures
for (RolapStar.Column rColumn : getFactTable().getColumns()) {
String name = getRolapStarColumnName(rColumn);
if (name == null) {
getLogger().warn("Init: " + "For fact table \"" + getFactTableName() + "\", could not get column name for RolapStar.Column: " + rColumn);
return;
}
if (!(rColumn instanceof RolapStar.Measure)) {
// TODO: whats the solution to this?
// its a funky dimension column in the fact table!!!
getLogger().warn("not a measure: " + name);
continue;
}
RolapStar.Measure rMeasure = (RolapStar.Measure) rColumn;
if (!rMeasure.getCubeName().equals(cubeName)) {
continue;
}
final RolapAggregator aggregator = rMeasure.getAggregator();
JdbcSchema.Table.Column c = getColumn(factTable, name);
if (c == null) {
getLogger().warn("For RolapStar: \"" + getFactTable().getAlias() + "\" measure with name, " + name + ", is not a column name. " + "The measure's column name may be an expression" + " and currently AggGen does not handle expressions." + " You will have to add this measure to the" + " aggregate table definition by hand.");
continue;
}
if (getLogger().isDebugEnabled()) {
getLogger().debug(" Jdbc Column m=" + c);
}
JdbcSchema.Table.Column.Usage usage = null;
if (c.hasUsage(JdbcSchema.UsageType.MEASURE)) {
for (Iterator<JdbcSchema.Table.Column.Usage> uit = c.getUsages(JdbcSchema.UsageType.MEASURE); uit.hasNext(); ) {
JdbcSchema.Table.Column.Usage tmpUsage = uit.next();
if ((tmpUsage.getAggregator() == aggregator) && tmpUsage.getSymbolicName().equals(rColumn.getName())) {
usage = tmpUsage;
break;
}
}
}
if (usage == null) {
usage = c.newUsage(JdbcSchema.UsageType.MEASURE);
usage.setAggregator(aggregator);
usage.setSymbolicName(rColumn.getName());
}
measures.add(usage);
}
// If we got to here, then everything is ok.
isReady = true;
}
use of mondrian.rolap.RolapAggregator in project mondrian by pentaho.
the class AggGen method insertIntoCollapsed.
/**
* Return the sql code to populate a collapsed dimension table from
* the fact table.
*/
public String insertIntoCollapsed() {
StringWriter sw = new StringWriter(512);
PrintWriter pw = new PrintWriter(sw);
String prefix = " ";
String factTableName = getFactTableName();
SqlQuery sqlQuery = getSqlQuery();
pw.print("INSERT INTO ");
pw.print(makeCollapsedAggregateTableName(getFactTableName()));
pw.println(" (");
for (List<JdbcSchema.Table.Column.Usage> list : collapsedColumnUsages.values()) {
for (JdbcSchema.Table.Column.Usage usage : list) {
JdbcSchema.Table.Column c = usage.getColumn();
pw.print(prefix);
if (usage.usagePrefix != null) {
pw.print(usage.usagePrefix);
}
pw.print(c.getName());
pw.println(',');
}
}
for (JdbcSchema.Table.Column.Usage usage : measures) {
JdbcSchema.Table.Column c = usage.getColumn();
pw.print(prefix);
String name = getUsageName(usage);
pw.print(name);
// pw.print(c.getName());
pw.println(',');
}
// do fact_count
pw.print(prefix);
pw.print(getFactCount());
pw.println(")");
pw.println("SELECT");
for (List<JdbcSchema.Table.Column.Usage> list : collapsedColumnUsages.values()) {
for (JdbcSchema.Table.Column.Usage usage : list) {
JdbcSchema.Table.Column c = usage.getColumn();
JdbcSchema.Table t = c.getTable();
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(t.getName(), c.getName()));
pw.print(" AS ");
String n = (usage.usagePrefix == null) ? c.getName() : usage.usagePrefix + c.getName();
pw.print(sqlQuery.getDialect().quoteIdentifier(n));
pw.println(',');
}
}
for (JdbcSchema.Table.Column.Usage usage : measures) {
JdbcSchema.Table.Column c = usage.getColumn();
JdbcSchema.Table t = c.getTable();
RolapAggregator agg = usage.getAggregator();
pw.print(prefix);
pw.print(agg.getExpression(sqlQuery.getDialect().quoteIdentifier(t.getName(), c.getName())));
pw.print(" AS ");
pw.print(sqlQuery.getDialect().quoteIdentifier(c.getName()));
pw.println(',');
}
// do fact_count
pw.print(prefix);
pw.print("COUNT(*) AS ");
pw.println(sqlQuery.getDialect().quoteIdentifier(getFactCount()));
pw.println("FROM ");
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(factTableName));
pw.print(" ");
pw.print(sqlQuery.getDialect().quoteIdentifier(factTableName));
pw.println(',');
// add dimension tables
int k = 0;
for (RolapStar.Table rt : collapsedColumnUsages.keySet()) {
if (k++ > 0) {
pw.println(',');
}
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(rt.getAlias()));
pw.print(" AS ");
pw.print(sqlQuery.getDialect().quoteIdentifier(rt.getAlias()));
// walk up tables
if (rt.getParentTable() != null) {
while (rt.getParentTable().getParentTable() != null) {
rt = rt.getParentTable();
pw.println(',');
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(rt.getAlias()));
pw.print(" AS ");
pw.print(sqlQuery.getDialect().quoteIdentifier(rt.getAlias()));
}
}
}
pw.println();
pw.println("WHERE ");
k = 0;
for (RolapStar.Table rt : collapsedColumnUsages.keySet()) {
if (k++ > 0) {
pw.println(" and");
}
RolapStar.Condition cond = rt.getJoinCondition();
if (cond == null) {
continue;
}
pw.print(prefix);
pw.print(cond.toString(sqlQuery));
if (rt.getParentTable() != null) {
while (rt.getParentTable().getParentTable() != null) {
rt = rt.getParentTable();
cond = rt.getJoinCondition();
pw.println(" and");
pw.print(prefix);
pw.print(cond.toString(sqlQuery));
}
}
}
pw.println();
pw.println("GROUP BY ");
k = 0;
for (List<JdbcSchema.Table.Column.Usage> list : collapsedColumnUsages.values()) {
for (JdbcSchema.Table.Column.Usage usage : list) {
if (k++ > 0) {
pw.println(",");
}
JdbcSchema.Table.Column c = usage.getColumn();
JdbcSchema.Table t = c.getTable();
String n = (usage.usagePrefix == null) ? c.getName() : usage.usagePrefix + c.getName();
pw.print(prefix);
pw.print(sqlQuery.getDialect().quoteIdentifier(t.getName(), n));
}
}
pw.println(';');
return sw.toString();
}
Aggregations