public TblColRef findPKByFK(TblColRef fk, String joinType) { assert isFactTable(fk.getTable()); TblColRef candidate = null; for (LookupDesc dim : lookups) { JoinDesc join = dim.getJoin(); if (join == null) continue; if (joinType != null && !joinType.equals(join.getType())) continue; int find = ArrayUtils.indexOf(join.getForeignKeyColumns(), fk); if (find >= 0) { candidate = join.getPrimaryKeyColumns()[find]; if (join.getForeignKeyColumns().length == 1) { // is single // column join? break; } } } return candidate; }
private void initJoinColumns(Map<String, TableDesc> tables) { // join columns may or may not present in cube; // here we don't modify 'allColumns' and 'dimensionColumns'; // initDimensionColumns() will do the update for (LookupDesc lookup : this.lookups) { lookup.setTable(lookup.getTable().toUpperCase()); TableDesc dimTable = tables.get(lookup.getTable()); if (dimTable == null) { throw new IllegalStateException( "Table " + lookup.getTable() + " does not exist for " + this); } JoinDesc join = lookup.getJoin(); if (join == null) continue; StringUtil.toUpperCaseArray(join.getForeignKey(), join.getForeignKey()); StringUtil.toUpperCaseArray(join.getPrimaryKey(), join.getPrimaryKey()); // primary key String[] pks = join.getPrimaryKey(); TblColRef[] pkCols = new TblColRef[pks.length]; for (int i = 0; i < pks.length; i++) { ColumnDesc col = dimTable.findColumnByName(pks[i]); if (col == null) { throw new IllegalStateException( "Can't find column " + pks[i] + " in table " + dimTable.getIdentity()); } TblColRef colRef = new TblColRef(col); pks[i] = colRef.getName(); pkCols[i] = colRef; } join.setPrimaryKeyColumns(pkCols); // foreign key TableDesc factTable = tables.get(this.factTable.toUpperCase()); if (factTable == null) { throw new IllegalStateException("Fact table does not exist:" + this.getFactTable()); } String[] fks = join.getForeignKey(); TblColRef[] fkCols = new TblColRef[fks.length]; for (int i = 0; i < fks.length; i++) { ColumnDesc col = factTable.findColumnByName(fks[i]); if (col == null) { throw new IllegalStateException( "Can't find column " + fks[i] + " in table " + this.getFactTable()); } TblColRef colRef = new TblColRef(col); fks[i] = colRef.getName(); fkCols[i] = colRef; } join.setForeignKeyColumns(fkCols); // Validate join in dimension if (pkCols.length != fkCols.length) { throw new IllegalStateException( "Primary keys(" + lookup.getTable() + ")" + Arrays.toString(pks) + " are not consistent with Foreign keys(" + this.getFactTable() + ") " + Arrays.toString(fks)); } for (int i = 0; i < fkCols.length; i++) { if (!fkCols[i].getDatatype().equals(pkCols[i].getDatatype())) { throw new IllegalStateException( "Primary key " + lookup.getTable() + "." + pkCols[i].getName() + "." + pkCols[i].getDatatype() + " are not consistent with Foreign key " + this.getFactTable() + "." + fkCols[i].getName() + "." + fkCols[i].getDatatype()); } } } }