@Test public void testSelect() { SelectItem simpleFunc = new SelectItem(); simpleFunc.addChildNode(createAggregateFunction("count", "jersey_no")); simpleFunc.addChildNode(new IdentifierNode("cnt")); List<SelectItem> selectItems = Arrays.asList(createColSelectItem("club"), createColSelectItem("position"), simpleFunc); DataFrame afterSelect = groupedDataFrame.select(selectItems).order( Arrays.asList(createOrderItem("club", Direction.ASC), createOrderItem("position", Direction.ASC))); List<List<Object>> expectedData = Arrays.asList(Arrays.asList("FCB", "Striker", 1), Arrays.asList("PSG", "Striker", 2), Arrays.asList("Porto FC", "Goal Keeper", 1), Arrays.asList("Real Madrid", "Defender", 2), Arrays.asList("Real Madrid", "Mid Fielder", 2), Arrays.asList("Real Madrid", "Striker", 1), Arrays.asList("Real Madrid", "Winger", 1)); assertEquals(expectedData, afterSelect.getData()); assertEquals(Arrays.asList("club", "position", "count(jersey_no)"), afterSelect.getColumns()); }
public DataFrame select(List<SelectItem> cols) { List<List<Object>> returnData = new ArrayList<>(); List<String> returnCols = new ArrayList<>(); boolean columnsInitialized = false; for (Map.Entry<List<Object>, List<List<Object>>> entry : groupData.entrySet()) { List<Object> returnRec = new ArrayList<>(); if (entry.getValue().size() == 0) { continue; } for (SelectItem col : cols) { if (col.hasChildType(Column.class)) { int colIndex; String colName = col.getChildType(Column.class, 0).getChildType(IdentifierNode.class, 0).getValue(); if (columnNamesMap.get(colName) != null) { colIndex = columnNamesMap.get(colName); if (!groupIndices.contains(colIndex)) { throw new RuntimeException("Select column " + colName + " should exist in group by clause"); } if (!columnsInitialized) { returnCols.add(colName); } } else if (aliasMapping.containsKey(colName)) { String actualCol = aliasMapping.get(colName); colIndex = columnNamesMap.get(actualCol); if (!groupIndices.contains(colIndex)) { throw new RuntimeException("Select column " + colName + " should exist in group by clause"); } if (!columnsInitialized) { returnCols.add(actualCol); } } else { throw new RuntimeException("Column " + colName + " doesn't exist in table"); } returnRec.add(entry.getValue().get(0).get(colIndex)); } else if (col.hasChildType(FunctionNode.class)) { Object computeResult = computeFunction(col.getChildType(FunctionNode.class, 0), entry.getValue()); returnRec.add(computeResult); if (!columnsInitialized) { returnCols.add(Utilities.createFunctionColName(col.getChildType(FunctionNode.class, 0))); } } } returnData.add(returnRec); columnsInitialized = true; } return new DataFrame(returnData, returnCols, aliasMapping); }
GroupedDataFrame { public DataFrame select(List<SelectItem> cols) { List<List<Object>> returnData = new ArrayList<>(); List<String> returnCols = new ArrayList<>(); boolean columnsInitialized = false; for (Map.Entry<List<Object>, List<List<Object>>> entry : groupData.entrySet()) { List<Object> returnRec = new ArrayList<>(); if (entry.getValue().size() == 0) { continue; } for (SelectItem col : cols) { if (col.hasChildType(Column.class)) { int colIndex; String colName = col.getChildType(Column.class, 0).getChildType(IdentifierNode.class, 0).getValue(); if (columnNamesMap.get(colName) != null) { colIndex = columnNamesMap.get(colName); if (!groupIndices.contains(colIndex)) { throw new RuntimeException("Select column " + colName + " should exist in group by clause"); } if (!columnsInitialized) { returnCols.add(colName); } } else if (aliasMapping.containsKey(colName)) { String actualCol = aliasMapping.get(colName); colIndex = columnNamesMap.get(actualCol); if (!groupIndices.contains(colIndex)) { throw new RuntimeException("Select column " + colName + " should exist in group by clause"); } if (!columnsInitialized) { returnCols.add(actualCol); } } else { throw new RuntimeException("Column " + colName + " doesn't exist in table"); } returnRec.add(entry.getValue().get(0).get(colIndex)); } else if (col.hasChildType(FunctionNode.class)) { Object computeResult = computeFunction(col.getChildType(FunctionNode.class, 0), entry.getValue()); returnRec.add(computeResult); if (!columnsInitialized) { returnCols.add(Utilities.createFunctionColName(col.getChildType(FunctionNode.class, 0))); } } } returnData.add(returnRec); columnsInitialized = true; } return new DataFrame(returnData, returnCols, aliasMapping); } }
GroupedDataFrame { public DataFrame select(List<SelectItem> cols) { List<List<Object>> returnData = new ArrayList<>(); List<String> returnCols = new ArrayList<>(); boolean columnsInitialized = false; for (Map.Entry<List<Object>, List<List<Object>>> entry : groupData.entrySet()) { List<Object> returnRec = new ArrayList<>(); if (entry.getValue().size() == 0) { continue; } for (SelectItem col : cols) { if (col.hasChildType(Column.class)) { int colIndex; String colName = col.getChildType(Column.class, 0).getChildType(IdentifierNode.class, 0).getValue(); if (columnNamesMap.get(colName) != null) { colIndex = columnNamesMap.get(colName); if (!groupIndices.contains(colIndex)) { throw new RuntimeException("Select column " + colName + " should exist in group by clause"); } if (!columnsInitialized) { returnCols.add(colName); } } else if (aliasMapping.containsKey(colName)) { String actualCol = aliasMapping.get(colName); colIndex = columnNamesMap.get(actualCol); if (!groupIndices.contains(colIndex)) { throw new RuntimeException("Select column " + colName + " should exist in group by clause"); } if (!columnsInitialized) { returnCols.add(actualCol); } } else { throw new RuntimeException("Column " + colName + " doesn't exist in table"); } returnRec.add(entry.getValue().get(0).get(colIndex)); } else if (col.hasChildType(FunctionNode.class)) { Object computeResult = computeFunction(col.getChildType(FunctionNode.class, 0), entry.getValue()); returnRec.add(computeResult); if (!columnsInitialized) { returnCols.add(Utilities.createFunctionColName(col.getChildType(FunctionNode.class, 0))); } } } returnData.add(returnRec); columnsInitialized = true; } return new DataFrame(returnData, returnCols, aliasMapping); } GroupedDataFrame(List<Integer> groupIndices, List<List<Object>> data, Map<String, Integer> columnNamesMap,
            Map<String, String> aliasMapping);  GroupedDataFrame(List<Integer> groupIndices, List<List<Object>> data, List<String> columns,
            Map<String, String> aliasMapping); private  GroupedDataFrame(List<Integer> groupIndices, Map<List<Object>, List<List<Object>>> groupData,
            Map<String, Integer> columnNamesMap, Map<String, String> aliasMapping); }
GroupedDataFrame { public DataFrame select(List<SelectItem> cols) { List<List<Object>> returnData = new ArrayList<>(); List<String> returnCols = new ArrayList<>(); boolean columnsInitialized = false; for (Map.Entry<List<Object>, List<List<Object>>> entry : groupData.entrySet()) { List<Object> returnRec = new ArrayList<>(); if (entry.getValue().size() == 0) { continue; } for (SelectItem col : cols) { if (col.hasChildType(Column.class)) { int colIndex; String colName = col.getChildType(Column.class, 0).getChildType(IdentifierNode.class, 0).getValue(); if (columnNamesMap.get(colName) != null) { colIndex = columnNamesMap.get(colName); if (!groupIndices.contains(colIndex)) { throw new RuntimeException("Select column " + colName + " should exist in group by clause"); } if (!columnsInitialized) { returnCols.add(colName); } } else if (aliasMapping.containsKey(colName)) { String actualCol = aliasMapping.get(colName); colIndex = columnNamesMap.get(actualCol); if (!groupIndices.contains(colIndex)) { throw new RuntimeException("Select column " + colName + " should exist in group by clause"); } if (!columnsInitialized) { returnCols.add(actualCol); } } else { throw new RuntimeException("Column " + colName + " doesn't exist in table"); } returnRec.add(entry.getValue().get(0).get(colIndex)); } else if (col.hasChildType(FunctionNode.class)) { Object computeResult = computeFunction(col.getChildType(FunctionNode.class, 0), entry.getValue()); returnRec.add(computeResult); if (!columnsInitialized) { returnCols.add(Utilities.createFunctionColName(col.getChildType(FunctionNode.class, 0))); } } } returnData.add(returnRec); columnsInitialized = true; } return new DataFrame(returnData, returnCols, aliasMapping); } GroupedDataFrame(List<Integer> groupIndices, List<List<Object>> data, Map<String, Integer> columnNamesMap,
            Map<String, String> aliasMapping);  GroupedDataFrame(List<Integer> groupIndices, List<List<Object>> data, List<String> columns,
            Map<String, String> aliasMapping); private  GroupedDataFrame(List<Integer> groupIndices, Map<List<Object>, List<List<Object>>> groupData,
            Map<String, Integer> columnNamesMap, Map<String, String> aliasMapping); DataFrame select(List<SelectItem> cols); GroupedDataFrame having(HavingClause havingClause); }
GroupedDataFrame { public DataFrame select(List<SelectItem> cols) { List<List<Object>> returnData = new ArrayList<>(); List<String> returnCols = new ArrayList<>(); boolean columnsInitialized = false; for (Map.Entry<List<Object>, List<List<Object>>> entry : groupData.entrySet()) { List<Object> returnRec = new ArrayList<>(); if (entry.getValue().size() == 0) { continue; } for (SelectItem col : cols) { if (col.hasChildType(Column.class)) { int colIndex; String colName = col.getChildType(Column.class, 0).getChildType(IdentifierNode.class, 0).getValue(); if (columnNamesMap.get(colName) != null) { colIndex = columnNamesMap.get(colName); if (!groupIndices.contains(colIndex)) { throw new RuntimeException("Select column " + colName + " should exist in group by clause"); } if (!columnsInitialized) { returnCols.add(colName); } } else if (aliasMapping.containsKey(colName)) { String actualCol = aliasMapping.get(colName); colIndex = columnNamesMap.get(actualCol); if (!groupIndices.contains(colIndex)) { throw new RuntimeException("Select column " + colName + " should exist in group by clause"); } if (!columnsInitialized) { returnCols.add(actualCol); } } else { throw new RuntimeException("Column " + colName + " doesn't exist in table"); } returnRec.add(entry.getValue().get(0).get(colIndex)); } else if (col.hasChildType(FunctionNode.class)) { Object computeResult = computeFunction(col.getChildType(FunctionNode.class, 0), entry.getValue()); returnRec.add(computeResult); if (!columnsInitialized) { returnCols.add(Utilities.createFunctionColName(col.getChildType(FunctionNode.class, 0))); } } } returnData.add(returnRec); columnsInitialized = true; } return new DataFrame(returnData, returnCols, aliasMapping); } GroupedDataFrame(List<Integer> groupIndices, List<List<Object>> data, Map<String, Integer> columnNamesMap,
            Map<String, String> aliasMapping);  GroupedDataFrame(List<Integer> groupIndices, List<List<Object>> data, List<String> columns,
            Map<String, String> aliasMapping); private  GroupedDataFrame(List<Integer> groupIndices, Map<List<Object>, List<List<Object>>> groupData,
            Map<String, Integer> columnNamesMap, Map<String, String> aliasMapping); DataFrame select(List<SelectItem> cols); GroupedDataFrame having(HavingClause havingClause);  }