Skip to content

Instantly share code, notes, and snippets.

@frsyuki
Created September 26, 2014 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frsyuki/54262f6631232f941145 to your computer and use it in GitHub Desktop.
Save frsyuki/54262f6631232f941145 to your computer and use it in GitHub Desktop.
/*
* Licensed 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 com.facebook.presto.operator.window;
import java.util.List;
import com.facebook.presto.operator.PagesIndex;
import com.facebook.presto.spi.block.BlockBuilder;
import com.facebook.presto.spi.type.Type;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
public class SumFunction
implements WindowFunction
{
private final int argumentChannel;
private int partitionStartPosition;
private long sum;
protected SumFunction(List<Integer> argumentChannels)
{
this.argumentChannel = getOnlyElement(argumentChannels);
}
@Override
public Type getType()
{
return BIGINT;
}
@Override
public void reset(int partitionRowCount, PagesIndex pagesIndex)
{
int nextPartitionStartPosition = partitionStartPosition + partitionRowCount;
long sum = 0;
for (int i = partitionStartPosition; i < nextPartitionStartPosition; i++) {
sum += pagesIndex.getLong(argumentChannel, i);
}
partitionStartPosition = nextPartitionStartPosition;
}
@Override
public void processRow(BlockBuilder output, boolean newPeerGroup, int peerGroupCount)
{
BIGINT.writeLong(output, sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment