From 5c67dddb0ee3caf6588ed510ed9123dd665c7a18 Mon Sep 17 00:00:00 2001 From: shaofengshi Date: Wed, 3 Dec 2014 16:10:36 +0800 Subject: [PATCH] Allow specify dimension column without table name (if the column name is unique among all tables); Also updated some test case. --- .../kylinolap/cube/dataGen/FactTableGenerator.java | 5 +++ .../java/com/kylinolap/cube/model/CubeDesc.java | 17 ++++++++- .../com/kylinolap/cube/model/DimensionDesc.java | 44 ++++++++++++---------- .../rest/controller/CubeControllerTest.java | 3 +- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/cube/src/main/java/com/kylinolap/cube/dataGen/FactTableGenerator.java b/cube/src/main/java/com/kylinolap/cube/dataGen/FactTableGenerator.java index 7781e61..15d435d 100644 --- a/cube/src/main/java/com/kylinolap/cube/dataGen/FactTableGenerator.java +++ b/cube/src/main/java/com/kylinolap/cube/dataGen/FactTableGenerator.java @@ -305,6 +305,11 @@ private String generate() throws Exception { if (jDesc == null) { // column on fact table used directly as a dimension for (String aColumn : dim.getColumn()) { + int lastIndexOfDot = aColumn.lastIndexOf("."); + if (lastIndexOfDot >= 0) { + aColumn = aColumn.substring(lastIndexOfDot + 1); + } + if (!factTableCol2LookupCol.containsKey(aColumn)) usedCols.add(aColumn); } diff --git a/cube/src/main/java/com/kylinolap/cube/model/CubeDesc.java b/cube/src/main/java/com/kylinolap/cube/model/CubeDesc.java index 56745ad..688c605 100644 --- a/cube/src/main/java/com/kylinolap/cube/model/CubeDesc.java +++ b/cube/src/main/java/com/kylinolap/cube/model/CubeDesc.java @@ -466,9 +466,24 @@ public void init(KylinConfig config, Map tables) { if(this.model == null) { this.addError("No data model found with name '" + modelName + "'."); } + + Map> columnTableMap = new HashMap>(); + + String colName; + for(TableDesc table : tables.values()) { + for(ColumnDesc col : table.getColumns()) { + colName = col.getName(); + List tableNames = columnTableMap.get(colName); + if(tableNames == null) { + tableNames = new ArrayList(3); + columnTableMap.put(colName, tableNames); + } + tableNames.add(table.getName()); + } + } for (DimensionDesc dim : dimensions) { - dim.init(this, tables); + dim.init(this, tables, columnTableMap); } sortDimAndMeasure(); diff --git a/cube/src/main/java/com/kylinolap/cube/model/DimensionDesc.java b/cube/src/main/java/com/kylinolap/cube/model/DimensionDesc.java index c041037..47d8361 100644 --- a/cube/src/main/java/com/kylinolap/cube/model/DimensionDesc.java +++ b/cube/src/main/java/com/kylinolap/cube/model/DimensionDesc.java @@ -45,6 +45,7 @@ private HierarchyDesc[] hierarchy; @JsonProperty("hierarchy") private boolean isHierarchy; + @JsonProperty("table") private String table; private String database; @JsonProperty("column") @@ -67,7 +68,6 @@ public boolean isHierarchyColumn(TblColRef col) { return false; } - public String getTable() { return table.toUpperCase(); } @@ -131,7 +131,6 @@ public void setDerived(String[] derived) { public void setDerivedColRefs(TblColRef[] derivedColRefs) { this.derivedColRefs = derivedColRefs; } - @Override public boolean equals(Object o) { @@ -162,42 +161,50 @@ public String toString() { return "DimensionDesc [name=" + name + ", join=" + join + ", hierarchy=" + Arrays.toString(hierarchy) + ", table=" + table + ", column=" + Arrays.toString(column) + ", derived=" + Arrays.toString(derived) + "]"; } - public void init(CubeDesc cubeDesc, Map tables) { + public void init(CubeDesc cubeDesc, Map tables, Map> columnTableMap) { if (name != null) name = name.toUpperCase(); - + this.table = null; this.database = null; this.join = null; - - for(int i=0, n = this.column.length; i 1) { String thisTable = splits[splits.length - 2].toUpperCase(); - if(table == null) { + if (table == null) { table = thisTable; } else if (thisTable != null && !table.equalsIgnoreCase(thisTable)) { - throw new IllegalStateException("One dimension can only refer to the columns on the same table."); + throw new IllegalStateException("One dimension can only refer to the columns on the same table: '" + table + "' and '" + thisTable + "'."); } - + if (database == null && splits.length > 2) { database = splits[splits.length - 3].toUpperCase(); - } - - this.column[i] = splits[splits.length - 1].toUpperCase(); + } + + //this.column[i] = splits[splits.length - 1].toUpperCase(); } else { - // if no table specified, assume it is on fact table - table = cubeDesc.getFactTable(); + // if no table specified, seek the table among all tables + List tableNames = columnTableMap.get(thisColumn); + if (tableNames != null && tableNames.size() == 1) { + table = tableNames.get(0); + } else { + if (tableNames == null) + throw new IllegalStateException("The column '" + thisColumn + "' doesn't belong to any table."); + if (tableNames.size() > 1) + throw new IllegalStateException("The column '" + thisColumn + "' is ambiguous; the name appeared in more than one tables, please specify table name together with the column name."); + } } } - + TableDesc tableDesc = tables.get(table); if (tableDesc == null) throw new IllegalStateException("Can't find table " + table + " on dimension " + name); - - for(LookupDesc lookup : cubeDesc.getModel().getLookups()) { - if(lookup.getTable().equalsIgnoreCase(table)) { + + for (LookupDesc lookup : cubeDesc.getModel().getLookups()) { + if (lookup.getTable().equalsIgnoreCase(table)) { this.join = lookup.getJoin(); break; } @@ -220,7 +227,6 @@ public void init(CubeDesc cubeDesc, Map tables) { hierarchy = null; if (derived != null && derived.length == 0) derived = null; - if (hierarchy != null) { for (HierarchyDesc h : hierarchy) diff --git a/server/src/test/java/com/kylinolap/rest/controller/CubeControllerTest.java b/server/src/test/java/com/kylinolap/rest/controller/CubeControllerTest.java index 3f68729..19bf726 100644 --- a/server/src/test/java/com/kylinolap/rest/controller/CubeControllerTest.java +++ b/server/src/test/java/com/kylinolap/rest/controller/CubeControllerTest.java @@ -66,11 +66,12 @@ public void testBasics() throws IOException { CubeDesc newCube = new CubeDesc(); String newCubeName = cube.getName() + "_test_save"; newCube.setName(newCubeName); + newCube.setModelName(cube.getModelName()); + newCube.setModel(cube.getModel()); newCube.setDimensions(cube.getDimensions()); newCube.setHBaseMapping(cube.getHBaseMapping()); newCube.setMeasures(cube.getMeasures()); newCube.setConfig(cube.getConfig()); - newCube.getModel().setFactTable(cube.getFactTable()); newCube.setRowkey(cube.getRowkey()); ObjectMapper mapper = new ObjectMapper();