@Test public void testBuildEmptyDataTable() throws IOException { QueryContext queryContext = QueryContextConverterUtils.getQueryContextFromPQL("SELECT * FROM table WHERE foo = 'bar'"); DataTable dataTable = DataTableUtils.buildEmptyDataTable(queryContext); DataSchema dataSchema = dataTable.getDataSchema(); assertEquals(dataSchema.getColumnNames(), new String[]{"*"}); assertEquals(dataSchema.getColumnDataTypes(), new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.STRING}); assertEquals(dataTable.getNumberOfRows(), 0); queryContext = QueryContextConverterUtils .getQueryContextFromPQL("SELECT COUNT(*), SUM(a), MAX(b) FROM table WHERE foo = 'bar'"); dataTable = DataTableUtils.buildEmptyDataTable(queryContext); dataSchema = dataTable.getDataSchema(); assertEquals(dataSchema.getColumnNames(), new String[]{"count_star", "sum_a", "max_b"}); assertEquals(dataSchema.getColumnDataTypes(), new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.LONG, DataSchema.ColumnDataType.DOUBLE, DataSchema.ColumnDataType.DOUBLE}); assertEquals(dataTable.getNumberOfRows(), 1); assertEquals(dataTable.getLong(0, 0), 0L); assertEquals(dataTable.getDouble(0, 1), 0.0); assertEquals(dataTable.getDouble(0, 2), Double.NEGATIVE_INFINITY); queryContext = QueryContextConverterUtils .getQueryContextFromPQL("SELECT COUNT(*), SUM(a), MAX(b) FROM table WHERE foo = 'bar' GROUP BY c, d"); dataTable = DataTableUtils.buildEmptyDataTable(queryContext); dataSchema = dataTable.getDataSchema(); assertEquals(dataSchema.getColumnNames(), new String[]{"functionName", "GroupByResultMap"}); assertEquals(dataSchema.getColumnDataTypes(), new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.STRING, DataSchema.ColumnDataType.OBJECT}); assertEquals(dataTable.getNumberOfRows(), 3); assertEquals(dataTable.getString(0, 0), "count_star"); assertEquals(dataTable.getObject(0, 1), Collections.emptyMap()); assertEquals(dataTable.getString(1, 0), "sum_a"); assertEquals(dataTable.getObject(1, 1), Collections.emptyMap()); assertEquals(dataTable.getString(2, 0), "max_b"); assertEquals(dataTable.getObject(2, 1), Collections.emptyMap()); queryContext = QueryContextConverterUtils.getQueryContextFromPQL( "SELECT c, d, COUNT(*), SUM(a), MAX(b) FROM table WHERE foo = 'bar' GROUP BY c, d OPTION(groupByMode=sql)"); dataTable = DataTableUtils.buildEmptyDataTable(queryContext); dataSchema = dataTable.getDataSchema(); assertEquals(dataSchema.getColumnNames(), new String[]{"c", "d", "count(*)", "sum(a)", "max(b)"}); assertEquals(dataSchema.getColumnDataTypes(), new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.STRING, DataSchema.ColumnDataType.STRING, DataSchema.ColumnDataType.LONG, DataSchema.ColumnDataType.DOUBLE, DataSchema.ColumnDataType.DOUBLE}); assertEquals(dataTable.getNumberOfRows(), 0); }
public static DataTable buildEmptyDataTable(QueryContext queryContext) throws IOException { AggregationFunction[] aggregationFunctions = queryContext.getAggregationFunctions(); if (aggregationFunctions == null) { List<ExpressionContext> selectExpressions = queryContext.getSelectExpressions(); int numSelectExpressions = selectExpressions.size(); String[] columnNames = new String[numSelectExpressions]; for (int i = 0; i < numSelectExpressions; i++) { columnNames[i] = selectExpressions.get(i).toString(); } DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numSelectExpressions]; Arrays.fill(columnDataTypes, DataSchema.ColumnDataType.STRING); DataSchema dataSchema = new DataSchema(columnNames, columnDataTypes); return new DataTableBuilder(dataSchema).build(); } int numAggregations = aggregationFunctions.length; List<ExpressionContext> groupByExpressions = queryContext.getGroupByExpressions(); if (groupByExpressions != null) { if (new QueryOptions(queryContext.getQueryOptions()).isGroupByModeSQL()) { int numColumns = groupByExpressions.size() + numAggregations; String[] columnNames = new String[numColumns]; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numColumns]; int index = 0; for (ExpressionContext groupByExpression : groupByExpressions) { columnNames[index] = groupByExpression.toString(); columnDataTypes[index] = DataSchema.ColumnDataType.STRING; index++; } for (AggregationFunction aggregationFunction : aggregationFunctions) { columnNames[index] = aggregationFunction.getResultColumnName(); columnDataTypes[index] = aggregationFunction.getIntermediateResultColumnType(); index++; } return new DataTableBuilder(new DataSchema(columnNames, columnDataTypes)).build(); } else { String[] columnNames = new String[]{"functionName", "GroupByResultMap"}; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.STRING, DataSchema.ColumnDataType.OBJECT}; DataTableBuilder dataTableBuilder = new DataTableBuilder(new DataSchema(columnNames, columnDataTypes)); for (AggregationFunction aggregationFunction : aggregationFunctions) { dataTableBuilder.startRow(); dataTableBuilder.setColumn(0, aggregationFunction.getColumnName()); dataTableBuilder.setColumn(1, Collections.emptyMap()); dataTableBuilder.finishRow(); } return dataTableBuilder.build(); } } else { String[] aggregationColumnNames = new String[numAggregations]; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numAggregations]; Object[] aggregationResults = new Object[numAggregations]; for (int i = 0; i < numAggregations; i++) { AggregationFunction aggregationFunction = aggregationFunctions[i]; aggregationColumnNames[i] = aggregationFunction.getColumnName(); columnDataTypes[i] = aggregationFunction.getIntermediateResultColumnType(); aggregationResults[i] = aggregationFunction.extractAggregationResult(aggregationFunction.createAggregationResultHolder()); } DataTableBuilder dataTableBuilder = new DataTableBuilder(new DataSchema(aggregationColumnNames, columnDataTypes)); dataTableBuilder.startRow(); for (int i = 0; i < numAggregations; i++) { switch (columnDataTypes[i]) { case LONG: dataTableBuilder.setColumn(i, ((Number) aggregationResults[i]).longValue()); break; case DOUBLE: dataTableBuilder.setColumn(i, ((Double) aggregationResults[i]).doubleValue()); break; case OBJECT: dataTableBuilder.setColumn(i, aggregationResults[i]); break; default: throw new UnsupportedOperationException( "Unsupported aggregation column data type: " + columnDataTypes[i] + " for column: " + aggregationColumnNames[i]); } } dataTableBuilder.finishRow(); return dataTableBuilder.build(); } }
DataTableUtils { public static DataTable buildEmptyDataTable(QueryContext queryContext) throws IOException { AggregationFunction[] aggregationFunctions = queryContext.getAggregationFunctions(); if (aggregationFunctions == null) { List<ExpressionContext> selectExpressions = queryContext.getSelectExpressions(); int numSelectExpressions = selectExpressions.size(); String[] columnNames = new String[numSelectExpressions]; for (int i = 0; i < numSelectExpressions; i++) { columnNames[i] = selectExpressions.get(i).toString(); } DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numSelectExpressions]; Arrays.fill(columnDataTypes, DataSchema.ColumnDataType.STRING); DataSchema dataSchema = new DataSchema(columnNames, columnDataTypes); return new DataTableBuilder(dataSchema).build(); } int numAggregations = aggregationFunctions.length; List<ExpressionContext> groupByExpressions = queryContext.getGroupByExpressions(); if (groupByExpressions != null) { if (new QueryOptions(queryContext.getQueryOptions()).isGroupByModeSQL()) { int numColumns = groupByExpressions.size() + numAggregations; String[] columnNames = new String[numColumns]; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numColumns]; int index = 0; for (ExpressionContext groupByExpression : groupByExpressions) { columnNames[index] = groupByExpression.toString(); columnDataTypes[index] = DataSchema.ColumnDataType.STRING; index++; } for (AggregationFunction aggregationFunction : aggregationFunctions) { columnNames[index] = aggregationFunction.getResultColumnName(); columnDataTypes[index] = aggregationFunction.getIntermediateResultColumnType(); index++; } return new DataTableBuilder(new DataSchema(columnNames, columnDataTypes)).build(); } else { String[] columnNames = new String[]{"functionName", "GroupByResultMap"}; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.STRING, DataSchema.ColumnDataType.OBJECT}; DataTableBuilder dataTableBuilder = new DataTableBuilder(new DataSchema(columnNames, columnDataTypes)); for (AggregationFunction aggregationFunction : aggregationFunctions) { dataTableBuilder.startRow(); dataTableBuilder.setColumn(0, aggregationFunction.getColumnName()); dataTableBuilder.setColumn(1, Collections.emptyMap()); dataTableBuilder.finishRow(); } return dataTableBuilder.build(); } } else { String[] aggregationColumnNames = new String[numAggregations]; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numAggregations]; Object[] aggregationResults = new Object[numAggregations]; for (int i = 0; i < numAggregations; i++) { AggregationFunction aggregationFunction = aggregationFunctions[i]; aggregationColumnNames[i] = aggregationFunction.getColumnName(); columnDataTypes[i] = aggregationFunction.getIntermediateResultColumnType(); aggregationResults[i] = aggregationFunction.extractAggregationResult(aggregationFunction.createAggregationResultHolder()); } DataTableBuilder dataTableBuilder = new DataTableBuilder(new DataSchema(aggregationColumnNames, columnDataTypes)); dataTableBuilder.startRow(); for (int i = 0; i < numAggregations; i++) { switch (columnDataTypes[i]) { case LONG: dataTableBuilder.setColumn(i, ((Number) aggregationResults[i]).longValue()); break; case DOUBLE: dataTableBuilder.setColumn(i, ((Double) aggregationResults[i]).doubleValue()); break; case OBJECT: dataTableBuilder.setColumn(i, aggregationResults[i]); break; default: throw new UnsupportedOperationException( "Unsupported aggregation column data type: " + columnDataTypes[i] + " for column: " + aggregationColumnNames[i]); } } dataTableBuilder.finishRow(); return dataTableBuilder.build(); } } }
DataTableUtils { public static DataTable buildEmptyDataTable(QueryContext queryContext) throws IOException { AggregationFunction[] aggregationFunctions = queryContext.getAggregationFunctions(); if (aggregationFunctions == null) { List<ExpressionContext> selectExpressions = queryContext.getSelectExpressions(); int numSelectExpressions = selectExpressions.size(); String[] columnNames = new String[numSelectExpressions]; for (int i = 0; i < numSelectExpressions; i++) { columnNames[i] = selectExpressions.get(i).toString(); } DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numSelectExpressions]; Arrays.fill(columnDataTypes, DataSchema.ColumnDataType.STRING); DataSchema dataSchema = new DataSchema(columnNames, columnDataTypes); return new DataTableBuilder(dataSchema).build(); } int numAggregations = aggregationFunctions.length; List<ExpressionContext> groupByExpressions = queryContext.getGroupByExpressions(); if (groupByExpressions != null) { if (new QueryOptions(queryContext.getQueryOptions()).isGroupByModeSQL()) { int numColumns = groupByExpressions.size() + numAggregations; String[] columnNames = new String[numColumns]; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numColumns]; int index = 0; for (ExpressionContext groupByExpression : groupByExpressions) { columnNames[index] = groupByExpression.toString(); columnDataTypes[index] = DataSchema.ColumnDataType.STRING; index++; } for (AggregationFunction aggregationFunction : aggregationFunctions) { columnNames[index] = aggregationFunction.getResultColumnName(); columnDataTypes[index] = aggregationFunction.getIntermediateResultColumnType(); index++; } return new DataTableBuilder(new DataSchema(columnNames, columnDataTypes)).build(); } else { String[] columnNames = new String[]{"functionName", "GroupByResultMap"}; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.STRING, DataSchema.ColumnDataType.OBJECT}; DataTableBuilder dataTableBuilder = new DataTableBuilder(new DataSchema(columnNames, columnDataTypes)); for (AggregationFunction aggregationFunction : aggregationFunctions) { dataTableBuilder.startRow(); dataTableBuilder.setColumn(0, aggregationFunction.getColumnName()); dataTableBuilder.setColumn(1, Collections.emptyMap()); dataTableBuilder.finishRow(); } return dataTableBuilder.build(); } } else { String[] aggregationColumnNames = new String[numAggregations]; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numAggregations]; Object[] aggregationResults = new Object[numAggregations]; for (int i = 0; i < numAggregations; i++) { AggregationFunction aggregationFunction = aggregationFunctions[i]; aggregationColumnNames[i] = aggregationFunction.getColumnName(); columnDataTypes[i] = aggregationFunction.getIntermediateResultColumnType(); aggregationResults[i] = aggregationFunction.extractAggregationResult(aggregationFunction.createAggregationResultHolder()); } DataTableBuilder dataTableBuilder = new DataTableBuilder(new DataSchema(aggregationColumnNames, columnDataTypes)); dataTableBuilder.startRow(); for (int i = 0; i < numAggregations; i++) { switch (columnDataTypes[i]) { case LONG: dataTableBuilder.setColumn(i, ((Number) aggregationResults[i]).longValue()); break; case DOUBLE: dataTableBuilder.setColumn(i, ((Double) aggregationResults[i]).doubleValue()); break; case OBJECT: dataTableBuilder.setColumn(i, aggregationResults[i]); break; default: throw new UnsupportedOperationException( "Unsupported aggregation column data type: " + columnDataTypes[i] + " for column: " + aggregationColumnNames[i]); } } dataTableBuilder.finishRow(); return dataTableBuilder.build(); } } private  DataTableUtils(); }
DataTableUtils { public static DataTable buildEmptyDataTable(QueryContext queryContext) throws IOException { AggregationFunction[] aggregationFunctions = queryContext.getAggregationFunctions(); if (aggregationFunctions == null) { List<ExpressionContext> selectExpressions = queryContext.getSelectExpressions(); int numSelectExpressions = selectExpressions.size(); String[] columnNames = new String[numSelectExpressions]; for (int i = 0; i < numSelectExpressions; i++) { columnNames[i] = selectExpressions.get(i).toString(); } DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numSelectExpressions]; Arrays.fill(columnDataTypes, DataSchema.ColumnDataType.STRING); DataSchema dataSchema = new DataSchema(columnNames, columnDataTypes); return new DataTableBuilder(dataSchema).build(); } int numAggregations = aggregationFunctions.length; List<ExpressionContext> groupByExpressions = queryContext.getGroupByExpressions(); if (groupByExpressions != null) { if (new QueryOptions(queryContext.getQueryOptions()).isGroupByModeSQL()) { int numColumns = groupByExpressions.size() + numAggregations; String[] columnNames = new String[numColumns]; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numColumns]; int index = 0; for (ExpressionContext groupByExpression : groupByExpressions) { columnNames[index] = groupByExpression.toString(); columnDataTypes[index] = DataSchema.ColumnDataType.STRING; index++; } for (AggregationFunction aggregationFunction : aggregationFunctions) { columnNames[index] = aggregationFunction.getResultColumnName(); columnDataTypes[index] = aggregationFunction.getIntermediateResultColumnType(); index++; } return new DataTableBuilder(new DataSchema(columnNames, columnDataTypes)).build(); } else { String[] columnNames = new String[]{"functionName", "GroupByResultMap"}; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.STRING, DataSchema.ColumnDataType.OBJECT}; DataTableBuilder dataTableBuilder = new DataTableBuilder(new DataSchema(columnNames, columnDataTypes)); for (AggregationFunction aggregationFunction : aggregationFunctions) { dataTableBuilder.startRow(); dataTableBuilder.setColumn(0, aggregationFunction.getColumnName()); dataTableBuilder.setColumn(1, Collections.emptyMap()); dataTableBuilder.finishRow(); } return dataTableBuilder.build(); } } else { String[] aggregationColumnNames = new String[numAggregations]; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numAggregations]; Object[] aggregationResults = new Object[numAggregations]; for (int i = 0; i < numAggregations; i++) { AggregationFunction aggregationFunction = aggregationFunctions[i]; aggregationColumnNames[i] = aggregationFunction.getColumnName(); columnDataTypes[i] = aggregationFunction.getIntermediateResultColumnType(); aggregationResults[i] = aggregationFunction.extractAggregationResult(aggregationFunction.createAggregationResultHolder()); } DataTableBuilder dataTableBuilder = new DataTableBuilder(new DataSchema(aggregationColumnNames, columnDataTypes)); dataTableBuilder.startRow(); for (int i = 0; i < numAggregations; i++) { switch (columnDataTypes[i]) { case LONG: dataTableBuilder.setColumn(i, ((Number) aggregationResults[i]).longValue()); break; case DOUBLE: dataTableBuilder.setColumn(i, ((Double) aggregationResults[i]).doubleValue()); break; case OBJECT: dataTableBuilder.setColumn(i, aggregationResults[i]); break; default: throw new UnsupportedOperationException( "Unsupported aggregation column data type: " + columnDataTypes[i] + " for column: " + aggregationColumnNames[i]); } } dataTableBuilder.finishRow(); return dataTableBuilder.build(); } } private  DataTableUtils(); static int computeColumnOffsets(DataSchema dataSchema, int[] columnOffsets); static DataTable buildEmptyDataTable(QueryContext queryContext); }
DataTableUtils { public static DataTable buildEmptyDataTable(QueryContext queryContext) throws IOException { AggregationFunction[] aggregationFunctions = queryContext.getAggregationFunctions(); if (aggregationFunctions == null) { List<ExpressionContext> selectExpressions = queryContext.getSelectExpressions(); int numSelectExpressions = selectExpressions.size(); String[] columnNames = new String[numSelectExpressions]; for (int i = 0; i < numSelectExpressions; i++) { columnNames[i] = selectExpressions.get(i).toString(); } DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numSelectExpressions]; Arrays.fill(columnDataTypes, DataSchema.ColumnDataType.STRING); DataSchema dataSchema = new DataSchema(columnNames, columnDataTypes); return new DataTableBuilder(dataSchema).build(); } int numAggregations = aggregationFunctions.length; List<ExpressionContext> groupByExpressions = queryContext.getGroupByExpressions(); if (groupByExpressions != null) { if (new QueryOptions(queryContext.getQueryOptions()).isGroupByModeSQL()) { int numColumns = groupByExpressions.size() + numAggregations; String[] columnNames = new String[numColumns]; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numColumns]; int index = 0; for (ExpressionContext groupByExpression : groupByExpressions) { columnNames[index] = groupByExpression.toString(); columnDataTypes[index] = DataSchema.ColumnDataType.STRING; index++; } for (AggregationFunction aggregationFunction : aggregationFunctions) { columnNames[index] = aggregationFunction.getResultColumnName(); columnDataTypes[index] = aggregationFunction.getIntermediateResultColumnType(); index++; } return new DataTableBuilder(new DataSchema(columnNames, columnDataTypes)).build(); } else { String[] columnNames = new String[]{"functionName", "GroupByResultMap"}; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[]{DataSchema.ColumnDataType.STRING, DataSchema.ColumnDataType.OBJECT}; DataTableBuilder dataTableBuilder = new DataTableBuilder(new DataSchema(columnNames, columnDataTypes)); for (AggregationFunction aggregationFunction : aggregationFunctions) { dataTableBuilder.startRow(); dataTableBuilder.setColumn(0, aggregationFunction.getColumnName()); dataTableBuilder.setColumn(1, Collections.emptyMap()); dataTableBuilder.finishRow(); } return dataTableBuilder.build(); } } else { String[] aggregationColumnNames = new String[numAggregations]; DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numAggregations]; Object[] aggregationResults = new Object[numAggregations]; for (int i = 0; i < numAggregations; i++) { AggregationFunction aggregationFunction = aggregationFunctions[i]; aggregationColumnNames[i] = aggregationFunction.getColumnName(); columnDataTypes[i] = aggregationFunction.getIntermediateResultColumnType(); aggregationResults[i] = aggregationFunction.extractAggregationResult(aggregationFunction.createAggregationResultHolder()); } DataTableBuilder dataTableBuilder = new DataTableBuilder(new DataSchema(aggregationColumnNames, columnDataTypes)); dataTableBuilder.startRow(); for (int i = 0; i < numAggregations; i++) { switch (columnDataTypes[i]) { case LONG: dataTableBuilder.setColumn(i, ((Number) aggregationResults[i]).longValue()); break; case DOUBLE: dataTableBuilder.setColumn(i, ((Double) aggregationResults[i]).doubleValue()); break; case OBJECT: dataTableBuilder.setColumn(i, aggregationResults[i]); break; default: throw new UnsupportedOperationException( "Unsupported aggregation column data type: " + columnDataTypes[i] + " for column: " + aggregationColumnNames[i]); } } dataTableBuilder.finishRow(); return dataTableBuilder.build(); } } private  DataTableUtils(); static int computeColumnOffsets(DataSchema dataSchema, int[] columnOffsets); static DataTable buildEmptyDataTable(QueryContext queryContext);  }