JackieTien97 commented on code in PR #14511:
URL: https://github.com/apache/iotdb/pull/14511#discussion_r1894728712
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/metadata/TableMetadataImpl.java:
##########
@@ -567,6 +567,7 @@ && isIntegerNumber(argumentTypes.get(2)))) {
case SqlConstant.MIN:
case SqlConstant.MAX:
case SqlConstant.MODE:
+ case SqlConstant.COUNT_IF:
Review Comment:
the type of argument should be boolean?
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/CountIfAccumulator.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.
+ */
+
+package
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.file.metadata.statistics.Statistics;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.util.Collections;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.tsfile.enums.TSDataType.BOOLEAN;
+
+public class CountIfAccumulator implements TableAccumulator {
+ private static final long INSTANCE_SIZE =
+ RamUsageEstimator.shallowSizeOfInstance(CountIfAccumulator.class);
+
+ private long countState = 0;
+
+ public CountIfAccumulator(List<TSDataType> seriesDataType) {
+ checkArgument(
+ seriesDataType.size() == 1 && seriesDataType.get(0) == BOOLEAN,
+ "argument of count_if should be one boolean expression");
+ }
+
+ @Override
+ public long getEstimatedSize() {
+ return INSTANCE_SIZE;
+ }
+
+ @Override
+ public TableAccumulator copy() {
+ return new CountIfAccumulator(Collections.singletonList(BOOLEAN));
+ }
+
+ @Override
+ public void addInput(Column[] arguments) {
+ int count = arguments[0].getPositionCount();
+ for (int i = 0; i < count; i++) {
+ if (arguments[0].getBoolean(i)) {
+ countState++;
+ }
+ }
+ }
+
+ @Override
+ public void addIntermediate(Column argument) {
+ for (int i = 0; i < argument.getPositionCount(); i++) {
+ if (argument.isNull(i)) {
+ continue;
+ }
+ countState += argument.getLong(i);
+ }
+ }
+
+ @Override
+ public void evaluateIntermediate(ColumnBuilder columnBuilder) {
+ columnBuilder.writeLong(countState);
+ }
+
+ @Override
+ public void evaluateFinal(ColumnBuilder columnBuilder) {
+ columnBuilder.writeLong(countState);
+ }
+
+ @Override
+ public boolean hasFinalResult() {
+ return false;
+ }
+
+ @Override
+ public void removeInput(Column[] arguments) {
+ for (int i = 0; i < arguments[0].getPositionCount(); i++) {
+ if (arguments[0].getBoolean(i)) {
Review Comment:
```suggestion
if (!arguments[0].isNull(i) && arguments[0].getBoolean(i)) {
```
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/AccumulatorFactory.java:
##########
@@ -149,6 +150,8 @@ private static GroupedAccumulator
createBuiltinGroupedAccumulator(
switch (aggregationType) {
case COUNT:
return new GroupedCountAccumulator();
+ case COUNT_IF:
+ return new GroupedCountIfAccumulator(inputDataTypes);
Review Comment:
```suggestion
return new GroupedCountIfAccumulator();
```
##########
integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTableAggregationIT.java:
##########
@@ -433,6 +433,327 @@ public void countTest() {
tableResultSetEqualTest("select count(*) from table1", expectedHeader,
retArray, DATABASE_NAME);
}
+ @Test
+ public void countIfTest() {
Review Comment:
also need to add some extra unexpected exception test, like:
1. input expression is not boolean type
2. number of input expressions are not only 1
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/grouped/GroupedCountIfAccumulator.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.
+ */
+
+package
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped;
+
+import
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.array.LongBigArray;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.tsfile.enums.TSDataType.BOOLEAN;
+
+public class GroupedCountIfAccumulator implements GroupedAccumulator {
+ private final LongBigArray countValues = new LongBigArray(0L);
+
+ private static final long INSTANCE_SIZE =
+ RamUsageEstimator.shallowSizeOfInstance(GroupedCountIfAccumulator.class);
+
+ public GroupedCountIfAccumulator(List<TSDataType> seriesDataType) {
+ checkArgument(
+ seriesDataType.size() == 1 && seriesDataType.get(0) == BOOLEAN,
+ "argument of count_if should be one boolean expression");
+ }
+
+ @Override
+ public long getEstimatedSize() {
+ return INSTANCE_SIZE + countValues.sizeOf();
+ }
+
+ @Override
+ public void setGroupCount(long groupCount) {
+ countValues.ensureCapacity(groupCount);
+ }
+
+ @Override
+ public void addInput(int[] groupIds, Column[] arguments) {
+ for (int i = 0; i < groupIds.length; i++) {
+ if (arguments[0].getBoolean(i)) {
Review Comment:
firstly should check the nullability of arguments by if
(arguments[0].isNull(i))
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/grouped/GroupedCountIfAccumulator.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.
+ */
+
+package
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped;
+
+import
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.array.LongBigArray;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.tsfile.enums.TSDataType.BOOLEAN;
+
+public class GroupedCountIfAccumulator implements GroupedAccumulator {
+ private final LongBigArray countValues = new LongBigArray(0L);
+
+ private static final long INSTANCE_SIZE =
+ RamUsageEstimator.shallowSizeOfInstance(GroupedCountIfAccumulator.class);
+
+ public GroupedCountIfAccumulator(List<TSDataType> seriesDataType) {
Review Comment:
don't need to pass `seriesDataType` here, you should check data type in the
analyze phase.
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/CountIfAccumulator.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.
+ */
+
+package
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.file.metadata.statistics.Statistics;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.util.Collections;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.tsfile.enums.TSDataType.BOOLEAN;
+
+public class CountIfAccumulator implements TableAccumulator {
+ private static final long INSTANCE_SIZE =
+ RamUsageEstimator.shallowSizeOfInstance(CountIfAccumulator.class);
+
+ private long countState = 0;
+
+ public CountIfAccumulator(List<TSDataType> seriesDataType) {
+ checkArgument(
+ seriesDataType.size() == 1 && seriesDataType.get(0) == BOOLEAN,
+ "argument of count_if should be one boolean expression");
+ }
+
+ @Override
+ public long getEstimatedSize() {
+ return INSTANCE_SIZE;
+ }
+
+ @Override
+ public TableAccumulator copy() {
+ return new CountIfAccumulator(Collections.singletonList(BOOLEAN));
+ }
+
+ @Override
+ public void addInput(Column[] arguments) {
+ int count = arguments[0].getPositionCount();
+ for (int i = 0; i < count; i++) {
+ if (arguments[0].getBoolean(i)) {
Review Comment:
```suggestion
if (!arguments[0].isNull(i) && arguments[0].getBoolean(i)) {
```
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/CountIfAccumulator.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.
+ */
+
+package
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.file.metadata.statistics.Statistics;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.util.Collections;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.tsfile.enums.TSDataType.BOOLEAN;
+
+public class CountIfAccumulator implements TableAccumulator {
+ private static final long INSTANCE_SIZE =
+ RamUsageEstimator.shallowSizeOfInstance(CountIfAccumulator.class);
+
+ private long countState = 0;
+
+ public CountIfAccumulator(List<TSDataType> seriesDataType) {
+ checkArgument(
+ seriesDataType.size() == 1 && seriesDataType.get(0) == BOOLEAN,
+ "argument of count_if should be one boolean expression");
+ }
+
+ @Override
+ public long getEstimatedSize() {
+ return INSTANCE_SIZE;
+ }
+
+ @Override
+ public TableAccumulator copy() {
+ return new CountIfAccumulator(Collections.singletonList(BOOLEAN));
+ }
+
+ @Override
+ public void addInput(Column[] arguments) {
+ int count = arguments[0].getPositionCount();
+ for (int i = 0; i < count; i++) {
+ if (arguments[0].getBoolean(i)) {
+ countState++;
+ }
+ }
+ }
+
+ @Override
+ public void addIntermediate(Column argument) {
+ for (int i = 0; i < argument.getPositionCount(); i++) {
+ if (argument.isNull(i)) {
+ continue;
+ }
+ countState += argument.getLong(i);
+ }
+ }
+
+ @Override
+ public void evaluateIntermediate(ColumnBuilder columnBuilder) {
+ columnBuilder.writeLong(countState);
+ }
+
+ @Override
+ public void evaluateFinal(ColumnBuilder columnBuilder) {
+ columnBuilder.writeLong(countState);
+ }
+
+ @Override
+ public boolean hasFinalResult() {
+ return false;
+ }
+
+ @Override
+ public void removeInput(Column[] arguments) {
+ for (int i = 0; i < arguments[0].getPositionCount(); i++) {
+ if (arguments[0].getBoolean(i)) {
+ countState--;
+ }
+ }
+ }
+
+ @Override
+ public void addStatistics(Statistics[] statistics) {}
Review Comment:
better to throw unsupported exception if we don't plan to support push down
count_if into scan
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/CountIfAccumulator.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.
+ */
+
+package
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.file.metadata.statistics.Statistics;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.util.Collections;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.tsfile.enums.TSDataType.BOOLEAN;
+
+public class CountIfAccumulator implements TableAccumulator {
+ private static final long INSTANCE_SIZE =
+ RamUsageEstimator.shallowSizeOfInstance(CountIfAccumulator.class);
+
+ private long countState = 0;
+
+ public CountIfAccumulator(List<TSDataType> seriesDataType) {
Review Comment:
don't need to pass `seriesDataType` here, you should check data type in the
analyze phase.
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/grouped/GroupedCountIfAccumulator.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.
+ */
+
+package
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped;
+
+import
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation.grouped.array.LongBigArray;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.tsfile.enums.TSDataType.BOOLEAN;
+
+public class GroupedCountIfAccumulator implements GroupedAccumulator {
+ private final LongBigArray countValues = new LongBigArray(0L);
+
+ private static final long INSTANCE_SIZE =
+ RamUsageEstimator.shallowSizeOfInstance(GroupedCountIfAccumulator.class);
+
+ public GroupedCountIfAccumulator(List<TSDataType> seriesDataType) {
+ checkArgument(
+ seriesDataType.size() == 1 && seriesDataType.get(0) == BOOLEAN,
+ "argument of count_if should be one boolean expression");
+ }
+
+ @Override
+ public long getEstimatedSize() {
+ return INSTANCE_SIZE + countValues.sizeOf();
+ }
+
+ @Override
+ public void setGroupCount(long groupCount) {
+ countValues.ensureCapacity(groupCount);
+ }
+
+ @Override
+ public void addInput(int[] groupIds, Column[] arguments) {
+ for (int i = 0; i < groupIds.length; i++) {
+ if (arguments[0].getBoolean(i)) {
Review Comment:
```suggestion
if (!arguments[0].isNull(i) && arguments[0].getBoolean(i)) {
```
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/CountIfAccumulator.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.
+ */
+
+package
org.apache.iotdb.db.queryengine.execution.operator.source.relational.aggregation;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.file.metadata.statistics.Statistics;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.util.Collections;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.tsfile.enums.TSDataType.BOOLEAN;
+
+public class CountIfAccumulator implements TableAccumulator {
+ private static final long INSTANCE_SIZE =
+ RamUsageEstimator.shallowSizeOfInstance(CountIfAccumulator.class);
+
+ private long countState = 0;
+
+ public CountIfAccumulator(List<TSDataType> seriesDataType) {
+ checkArgument(
+ seriesDataType.size() == 1 && seriesDataType.get(0) == BOOLEAN,
+ "argument of count_if should be one boolean expression");
+ }
+
+ @Override
+ public long getEstimatedSize() {
+ return INSTANCE_SIZE;
+ }
+
+ @Override
+ public TableAccumulator copy() {
+ return new CountIfAccumulator(Collections.singletonList(BOOLEAN));
+ }
+
+ @Override
+ public void addInput(Column[] arguments) {
+ int count = arguments[0].getPositionCount();
+ for (int i = 0; i < count; i++) {
+ if (arguments[0].getBoolean(i)) {
+ countState++;
+ }
+ }
+ }
+
+ @Override
+ public void addIntermediate(Column argument) {
+ for (int i = 0; i < argument.getPositionCount(); i++) {
+ if (argument.isNull(i)) {
+ continue;
+ }
+ countState += argument.getLong(i);
+ }
+ }
+
+ @Override
+ public void evaluateIntermediate(ColumnBuilder columnBuilder) {
+ columnBuilder.writeLong(countState);
+ }
+
+ @Override
+ public void evaluateFinal(ColumnBuilder columnBuilder) {
+ columnBuilder.writeLong(countState);
+ }
+
+ @Override
+ public boolean hasFinalResult() {
+ return false;
+ }
+
+ @Override
+ public void removeInput(Column[] arguments) {
Review Comment:
you should also override `removable` method to return true.
--
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]