-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add aggregate function count_if for relational table
- Loading branch information
Showing
6 changed files
with
527 additions
and
0 deletions.
There are no files selected for viewing
331 changes: 331 additions & 0 deletions
331
...st/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTableAggregationIT.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...b/db/queryengine/execution/operator/source/relational/aggregation/CountIfAccumulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* 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.file.metadata.statistics.Statistics; | ||
import org.apache.tsfile.utils.RamUsageEstimator; | ||
|
||
public class CountIfAccumulator implements TableAccumulator { | ||
private static final long INSTANCE_SIZE = | ||
RamUsageEstimator.shallowSizeOfInstance(CountIfAccumulator.class); | ||
|
||
private long countState = 0; | ||
|
||
@Override | ||
public long getEstimatedSize() { | ||
return INSTANCE_SIZE; | ||
} | ||
|
||
@Override | ||
public TableAccumulator copy() { | ||
return new CountIfAccumulator(); | ||
} | ||
|
||
@Override | ||
public void addInput(Column[] arguments) { | ||
int count = arguments[0].getPositionCount(); | ||
for (int i = 0; i < count; i++) { | ||
if (!arguments[0].isNull(i) && 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].isNull(i) && arguments[0].getBoolean(i)) { | ||
countState--; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean removable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void addStatistics(Statistics[] statistics) { | ||
throw new UnsupportedOperationException("CountIfAccumulator does not support statistics"); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
countState = 0; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...e/execution/operator/source/relational/aggregation/grouped/GroupedCountIfAccumulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.utils.RamUsageEstimator; | ||
|
||
public class GroupedCountIfAccumulator implements GroupedAccumulator { | ||
private final LongBigArray countValues = new LongBigArray(0L); | ||
|
||
private static final long INSTANCE_SIZE = | ||
RamUsageEstimator.shallowSizeOfInstance(GroupedCountIfAccumulator.class); | ||
|
||
@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].isNull(i) && arguments[0].getBoolean(i)) { | ||
countValues.increment(groupIds[i]); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void addIntermediate(int[] groupIds, Column argument) { | ||
for (int i = 0; i < groupIds.length; i++) { | ||
if (!argument.isNull(i)) { | ||
countValues.add(groupIds[i], argument.getLong(i)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void evaluateIntermediate(int groupId, ColumnBuilder columnBuilder) { | ||
columnBuilder.writeLong(countValues.get(groupId)); | ||
} | ||
|
||
@Override | ||
public void evaluateFinal(int groupId, ColumnBuilder columnBuilder) { | ||
columnBuilder.writeLong(countValues.get(groupId)); | ||
} | ||
|
||
@Override | ||
public void prepareFinal() {} | ||
|
||
@Override | ||
public void reset() { | ||
countValues.reset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters