Skip to content

Commit

Permalink
added test for sum-if aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
DHRUV6029 committed Nov 25, 2024
1 parent d84d95a commit 3e3bd86
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.aggregation;

import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.block.BlockBuilder;
import com.facebook.presto.common.type.StandardTypes;
import com.google.common.collect.ImmutableList;

import java.util.List;

import static com.facebook.presto.common.type.BooleanType.BOOLEAN;
import static com.facebook.presto.common.type.DoubleType.DOUBLE;

public class TestDoubleSumIfAggregation
extends AbstractTestAggregationFunction
{
@Override
public Block[] getSequenceBlocks(int start, int length)
{
BlockBuilder conditions = BOOLEAN.createBlockBuilder(null, length);
BlockBuilder values = DOUBLE.createBlockBuilder(null, length);
for (int i = start; i < start + length; i++) {
BOOLEAN.writeBoolean(conditions, i % 2 == 0);
DOUBLE.writeDouble(values, i);
}
return new Block[] {conditions.build(), values.build()};
}

@Override
public Number getExpectedValue(int start, int length)
{
double sum = 0.0;
for (int i = start; i < start + length; i++) {
if (i % 2 == 0) {
sum += i;
}
}
return sum;
}

@Override
protected String getFunctionName()
{
return "sum_if";
}

@Override
protected List<String> getFunctionParameterTypes()
{
return ImmutableList.of(StandardTypes.BOOLEAN, StandardTypes.DOUBLE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.aggregation;

import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.block.BlockBuilder;
import com.facebook.presto.common.type.StandardTypes;
import com.google.common.collect.ImmutableList;

import java.util.List;

import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.common.type.BooleanType.BOOLEAN;

public class TestLongSumIfAggregation
extends AbstractTestAggregationFunction
{
@Override
public Block[] getSequenceBlocks(int start, int length)
{
BlockBuilder conditions = BOOLEAN.createBlockBuilder(null, length);
BlockBuilder values = BIGINT.createBlockBuilder(null, length);
for (int i = start; i < start + length; i++) {
BOOLEAN.writeBoolean(conditions, i % 2 == 0);
BIGINT.writeLong(values, i);
}
return new Block[] {conditions.build(), values.build()};
}

@Override
public Number getExpectedValue(int start, int length)
{
long sum = 0L;
for (int i = start; i < start + length; i++) {
if (i % 2 == 0) {
sum += i;
}
}
return sum;
}

@Override
protected String getFunctionName()
{
return "sum_if";
}

@Override
protected List<String> getFunctionParameterTypes()
{
return ImmutableList.of(StandardTypes.BOOLEAN, StandardTypes.BIGINT);
}
}

0 comments on commit 3e3bd86

Please sign in to comment.