JackieTien97 commented on code in PR #9342: URL: https://github.com/apache/iotdb/pull/9342#discussion_r1138092678
########## server/src/main/codegen/templates/ModeAccumulator.ftl: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +<@pp.dropOutputFile /> + +<#list allDataTypes.types as type> + + <#assign className = "${type.dataType?cap_first}ModeAccumulator"> + <@pp.changeOutputFile name="/org/apache/iotdb/db/mpp/aggregation/${className}.java" /> + +package org.apache.iotdb.db.mpp.aggregation; + +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics; +import org.apache.iotdb.tsfile.read.common.block.column.Column; +import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder; +import org.apache.iotdb.tsfile.utils.Binary; +import org.apache.iotdb.tsfile.utils.BitMap; +import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkArgument; + +/* +* This class is generated using freemarker and the ${.template_name} template. +*/ +@SuppressWarnings("unused") +public class ${className} implements Accumulator { + private final Map<${type.javaBoxName}, Long> countMap = new HashMap<>(); + + <#if type.dataType != "boolean"> + private final int MAP_SIZE_THRESHOLD = 10000; + + </#if> + @Override + public void addInput(Column[] column, BitMap bitMap, int lastIndex) { + for (int i = 0; i <= lastIndex; i++) { + if (bitMap != null && !bitMap.isMarked(i)) { + continue; + } + if (!column[1].isNull(i)) { + countMap.compute(column[1].get${type.dataType?cap_first}(i), (k, v) -> v == null ? 1 : v + 1); + <#if type.dataType != "boolean"> + + if (countMap.size() > MAP_SIZE_THRESHOLD) { + throw new RuntimeException( + String.format( + "distinct values has exceeded the threshold %s when calculate Mode", + MAP_SIZE_THRESHOLD)); + } + </#if> + } + } + } + + @Override + public void addIntermediate(Column[] partialResult) { + checkArgument(partialResult.length == 1, "partialResult of Mode should be 1"); + checkArgument(!partialResult[0].isNull(0), "partialResult of Mode should not be null"); + deserializeAndMergeCountMap(partialResult[0].getBinary(0)); + } + + @Override + public void addStatistics(Statistics statistics) { + throw new UnsupportedOperationException(getClass().getName()); + } + + @Override + public void setFinal(Column finalResult) { + if (finalResult.isNull(0)) { + return; + } + + // Step of ModeAccumulator is STATIC, + // countMap only need to record one entry which key is finalResult + countMap.put(finalResult.get${type.dataType?cap_first}(0), 0L); Review Comment: ```suggestion maxCountKey = finalResult.get${type.dataType?cap_first}(0); ``` ########## server/src/main/codegen/templates/ModeAccumulator.ftl: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +<@pp.dropOutputFile /> + +<#list allDataTypes.types as type> + + <#assign className = "${type.dataType?cap_first}ModeAccumulator"> + <@pp.changeOutputFile name="/org/apache/iotdb/db/mpp/aggregation/${className}.java" /> + +package org.apache.iotdb.db.mpp.aggregation; + +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics; +import org.apache.iotdb.tsfile.read.common.block.column.Column; +import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder; +import org.apache.iotdb.tsfile.utils.Binary; +import org.apache.iotdb.tsfile.utils.BitMap; +import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkArgument; + +/* +* This class is generated using freemarker and the ${.template_name} template. +*/ +@SuppressWarnings("unused") +public class ${className} implements Accumulator { + private final Map<${type.javaBoxName}, Long> countMap = new HashMap<>(); + + <#if type.dataType != "boolean"> + private final int MAP_SIZE_THRESHOLD = 10000; + + </#if> + @Override + public void addInput(Column[] column, BitMap bitMap, int lastIndex) { + for (int i = 0; i <= lastIndex; i++) { + if (bitMap != null && !bitMap.isMarked(i)) { + continue; + } + if (!column[1].isNull(i)) { + countMap.compute(column[1].get${type.dataType?cap_first}(i), (k, v) -> v == null ? 1 : v + 1); + <#if type.dataType != "boolean"> + + if (countMap.size() > MAP_SIZE_THRESHOLD) { + throw new RuntimeException( + String.format( + "distinct values has exceeded the threshold %s when calculate Mode", + MAP_SIZE_THRESHOLD)); + } + </#if> + } + } + } + + @Override + public void addIntermediate(Column[] partialResult) { + checkArgument(partialResult.length == 1, "partialResult of Mode should be 1"); + checkArgument(!partialResult[0].isNull(0), "partialResult of Mode should not be null"); + deserializeAndMergeCountMap(partialResult[0].getBinary(0)); + } + + @Override + public void addStatistics(Statistics statistics) { + throw new UnsupportedOperationException(getClass().getName()); + } + + @Override + public void setFinal(Column finalResult) { + if (finalResult.isNull(0)) { + return; + } + + // Step of ModeAccumulator is STATIC, + // countMap only need to record one entry which key is finalResult + countMap.put(finalResult.get${type.dataType?cap_first}(0), 0L); + } + + @Override + public void outputIntermediate(ColumnBuilder[] tsBlockBuilder) { + tsBlockBuilder[0].writeBinary(serializeCountMap()); + } + + @Override + public void outputFinal(ColumnBuilder tsBlockBuilder) { + if (countMap.isEmpty()) { + tsBlockBuilder.appendNull(); + } else { + tsBlockBuilder.write${type.dataType?cap_first}( + Collections.max(countMap.entrySet(), Map.Entry.comparingByValue()).getKey()); + } + } + + @Override + public void reset() { + countMap.clear(); + } + + @Override + public boolean hasFinalResult() { + return false; + } + + @Override + public TSDataType[] getIntermediateType() { + return new TSDataType[] {TSDataType.TEXT}; + } + + @Override + public TSDataType getFinalType() { + return ${type.tsDataType}; + } + + private Binary serializeCountMap() { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + try { + ReadWriteIOUtils.write(countMap.size(), stream); + for (Map.Entry<${type.javaBoxName}, Long> entry : countMap.entrySet()) { + ReadWriteIOUtils.write(entry.getKey(), stream); + ReadWriteIOUtils.write(entry.getValue(), stream); + } + } catch (IOException e) { + // Totally memory operation. This case won't happen. + } + return new Binary(stream.toByteArray()); + } + + private void deserializeAndMergeCountMap(Binary partialResult) { Review Comment: in this method, we need to update `maxCountKey` and `maxCount` ########## server/src/main/codegen/templates/ModeAccumulator.ftl: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +<@pp.dropOutputFile /> + +<#list allDataTypes.types as type> + + <#assign className = "${type.dataType?cap_first}ModeAccumulator"> + <@pp.changeOutputFile name="/org/apache/iotdb/db/mpp/aggregation/${className}.java" /> + +package org.apache.iotdb.db.mpp.aggregation; + +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics; +import org.apache.iotdb.tsfile.read.common.block.column.Column; +import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder; +import org.apache.iotdb.tsfile.utils.Binary; +import org.apache.iotdb.tsfile.utils.BitMap; +import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkArgument; + +/* +* This class is generated using freemarker and the ${.template_name} template. +*/ +@SuppressWarnings("unused") +public class ${className} implements Accumulator { + private final Map<${type.javaBoxName}, Long> countMap = new HashMap<>(); + + <#if type.dataType != "boolean"> + private final int MAP_SIZE_THRESHOLD = 10000; + + </#if> + @Override + public void addInput(Column[] column, BitMap bitMap, int lastIndex) { + for (int i = 0; i <= lastIndex; i++) { + if (bitMap != null && !bitMap.isMarked(i)) { + continue; + } + if (!column[1].isNull(i)) { + countMap.compute(column[1].get${type.dataType?cap_first}(i), (k, v) -> v == null ? 1 : v + 1); Review Comment: record the max count key and its count during the process, update those two fields and then we don't need to compare again in outputFinal method. ########## server/src/main/codegen/templates/ModeAccumulator.ftl: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +<@pp.dropOutputFile /> + +<#list allDataTypes.types as type> + + <#assign className = "${type.dataType?cap_first}ModeAccumulator"> + <@pp.changeOutputFile name="/org/apache/iotdb/db/mpp/aggregation/${className}.java" /> + +package org.apache.iotdb.db.mpp.aggregation; + +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics; +import org.apache.iotdb.tsfile.read.common.block.column.Column; +import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder; +import org.apache.iotdb.tsfile.utils.Binary; +import org.apache.iotdb.tsfile.utils.BitMap; +import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkArgument; + +/* +* This class is generated using freemarker and the ${.template_name} template. +*/ +@SuppressWarnings("unused") +public class ${className} implements Accumulator { + private final Map<${type.javaBoxName}, Long> countMap = new HashMap<>(); + + <#if type.dataType != "boolean"> + private final int MAP_SIZE_THRESHOLD = 10000; Review Comment: make it as a configuration in iotdb-common.properties ########## server/src/main/codegen/templates/ModeAccumulator.ftl: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +<@pp.dropOutputFile /> + +<#list allDataTypes.types as type> + + <#assign className = "${type.dataType?cap_first}ModeAccumulator"> + <@pp.changeOutputFile name="/org/apache/iotdb/db/mpp/aggregation/${className}.java" /> + +package org.apache.iotdb.db.mpp.aggregation; + +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics; +import org.apache.iotdb.tsfile.read.common.block.column.Column; +import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder; +import org.apache.iotdb.tsfile.utils.Binary; +import org.apache.iotdb.tsfile.utils.BitMap; +import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkArgument; + +/* +* This class is generated using freemarker and the ${.template_name} template. +*/ +@SuppressWarnings("unused") +public class ${className} implements Accumulator { + private final Map<${type.javaBoxName}, Long> countMap = new HashMap<>(); + + <#if type.dataType != "boolean"> + private final int MAP_SIZE_THRESHOLD = 10000; + + </#if> + @Override + public void addInput(Column[] column, BitMap bitMap, int lastIndex) { + for (int i = 0; i <= lastIndex; i++) { + if (bitMap != null && !bitMap.isMarked(i)) { + continue; + } + if (!column[1].isNull(i)) { + countMap.compute(column[1].get${type.dataType?cap_first}(i), (k, v) -> v == null ? 1 : v + 1); + <#if type.dataType != "boolean"> + + if (countMap.size() > MAP_SIZE_THRESHOLD) { + throw new RuntimeException( + String.format( + "distinct values has exceeded the threshold %s when calculate Mode", + MAP_SIZE_THRESHOLD)); + } + </#if> + } + } + } + + @Override + public void addIntermediate(Column[] partialResult) { + checkArgument(partialResult.length == 1, "partialResult of Mode should be 1"); + checkArgument(!partialResult[0].isNull(0), "partialResult of Mode should not be null"); + deserializeAndMergeCountMap(partialResult[0].getBinary(0)); + } + + @Override + public void addStatistics(Statistics statistics) { + throw new UnsupportedOperationException(getClass().getName()); + } + + @Override + public void setFinal(Column finalResult) { + if (finalResult.isNull(0)) { + return; + } + + // Step of ModeAccumulator is STATIC, + // countMap only need to record one entry which key is finalResult + countMap.put(finalResult.get${type.dataType?cap_first}(0), 0L); + } + + @Override + public void outputIntermediate(ColumnBuilder[] tsBlockBuilder) { + tsBlockBuilder[0].writeBinary(serializeCountMap()); + } + + @Override + public void outputFinal(ColumnBuilder tsBlockBuilder) { + if (countMap.isEmpty()) { + tsBlockBuilder.appendNull(); + } else { + tsBlockBuilder.write${type.dataType?cap_first}( + Collections.max(countMap.entrySet(), Map.Entry.comparingByValue()).getKey()); + } + } + + @Override + public void reset() { + countMap.clear(); Review Comment: ```suggestion countMap.clear(); maxCountKey = null; maxCount = 0; ``` ########## server/src/main/codegen/templates/ModeAccumulator.ftl: ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +<@pp.dropOutputFile /> + +<#list allDataTypes.types as type> + + <#assign className = "${type.dataType?cap_first}ModeAccumulator"> + <@pp.changeOutputFile name="/org/apache/iotdb/db/mpp/aggregation/${className}.java" /> + +package org.apache.iotdb.db.mpp.aggregation; + +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics; +import org.apache.iotdb.tsfile.read.common.block.column.Column; +import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder; +import org.apache.iotdb.tsfile.utils.Binary; +import org.apache.iotdb.tsfile.utils.BitMap; +import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkArgument; + +/* +* This class is generated using freemarker and the ${.template_name} template. +*/ +@SuppressWarnings("unused") +public class ${className} implements Accumulator { + private final Map<${type.javaBoxName}, Long> countMap = new HashMap<>(); + + <#if type.dataType != "boolean"> + private final int MAP_SIZE_THRESHOLD = 10000; + + </#if> + @Override + public void addInput(Column[] column, BitMap bitMap, int lastIndex) { + for (int i = 0; i <= lastIndex; i++) { + if (bitMap != null && !bitMap.isMarked(i)) { + continue; + } + if (!column[1].isNull(i)) { + countMap.compute(column[1].get${type.dataType?cap_first}(i), (k, v) -> v == null ? 1 : v + 1); + <#if type.dataType != "boolean"> + + if (countMap.size() > MAP_SIZE_THRESHOLD) { + throw new RuntimeException( + String.format( + "distinct values has exceeded the threshold %s when calculate Mode", + MAP_SIZE_THRESHOLD)); + } + </#if> + } + } + } + + @Override + public void addIntermediate(Column[] partialResult) { + checkArgument(partialResult.length == 1, "partialResult of Mode should be 1"); + checkArgument(!partialResult[0].isNull(0), "partialResult of Mode should not be null"); + deserializeAndMergeCountMap(partialResult[0].getBinary(0)); + } + + @Override + public void addStatistics(Statistics statistics) { + throw new UnsupportedOperationException(getClass().getName()); + } + + @Override + public void setFinal(Column finalResult) { + if (finalResult.isNull(0)) { + return; + } + + // Step of ModeAccumulator is STATIC, + // countMap only need to record one entry which key is finalResult + countMap.put(finalResult.get${type.dataType?cap_first}(0), 0L); + } + + @Override + public void outputIntermediate(ColumnBuilder[] tsBlockBuilder) { + tsBlockBuilder[0].writeBinary(serializeCountMap()); + } + + @Override + public void outputFinal(ColumnBuilder tsBlockBuilder) { + if (countMap.isEmpty()) { + tsBlockBuilder.appendNull(); + } else { + tsBlockBuilder.write${type.dataType?cap_first}( + Collections.max(countMap.entrySet(), Map.Entry.comparingByValue()).getKey()); + } Review Comment: ```suggestion if (maxCountKey == null) { tsBlockBuilder.appendNull(); } else { tsBlockBuilder.write${type.dataType?cap_first}(maxCountKey); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
