Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BEAM-13202] Add Coder to CountIfFn.Accum #16856

Merged
merged 3 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/
package org.apache.beam.sdk.extensions.sql.impl.transform.agg;

import java.io.Serializable;
import org.apache.beam.sdk.coders.CannotProvideCoderException;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.coders.CoderRegistry;
import org.apache.beam.sdk.transforms.Combine;
import org.apache.beam.sdk.transforms.Count;

/**
* Returns the count of TRUE values for expression. Returns 0 if there are zero input rows, or if
Expand All @@ -27,49 +30,41 @@
public class CountIf {
private CountIf() {}

public static CountIfFn combineFn() {
return new CountIf.CountIfFn();
public static Combine.CombineFn<Boolean, ?, Long> combineFn() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose technically this is a breaking change. But of course everything implementing SQL is not intended for users. Is sql/impl marked @Internal? (this change still LGTM because it is not actually intended as a user API)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I hesitated but this is internal and this class is only instantiated by SQL when it registers the built-in aggregators so we should be good. sql/impl is not explicitly marked as Internal but I agree that it should.

return new CountIfFn();
}

public static class CountIfFn extends Combine.CombineFn<Boolean, CountIfFn.Accum, Long> {
public static class CountIfFn extends Combine.CombineFn<Boolean, long[], Long> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup! At this point, would it be worthwhile to let Count.CountFn be public, so we could just inherit it? I don't really have a strong opinion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO using it via composition like this is preferable anyhow. You can see it is more flexible, since you can achieve all the same things without needing it to be public.

private final Combine.CombineFn<Boolean, long[], Long> countFn =
(Combine.CombineFn<Boolean, long[], Long>) Count.<Boolean>combineFn();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast makes me think that Count.combineFn() worked too hard to hide the accumulator type. It should just expose it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, I hesitated too make it public and make the CountIfFn inherit it and change just to override the addInput method, what let me puzzled was how to make CompineFn composable so I could just 'filter' and then apply CountFn


public static class Accum implements Serializable {
boolean isExpressionFalse = true;
long countIfResult = 0L;
@Override
public long[] createAccumulator() {
return countFn.createAccumulator();
}

@Override
public Accum createAccumulator() {
return new Accum();
public long[] addInput(long[] accumulator, Boolean input) {
if (Boolean.TRUE.equals(input)) {
countFn.addInput(accumulator, input);
}
return accumulator;
}

@Override
public Accum addInput(Accum accum, Boolean input) {
if (input) {
accum.isExpressionFalse = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never looked at this code before. Now that I see it... why was this field ever needed? Seems like the 0L result contains all the useful info. Makes me worried there is something tricky that I am not noticing...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was the same conclusion we arrived too with Ryan, that's why I went into the simplification road. I added the tests to try to find issues and fixed the ones I saw.

accum.countIfResult += 1;
}
return accum;
public long[] mergeAccumulators(Iterable<long[]> accumulators) {
return countFn.mergeAccumulators(accumulators);
}

@Override
public Accum mergeAccumulators(Iterable<Accum> accums) {
CountIfFn.Accum merged = createAccumulator();
for (CountIfFn.Accum accum : accums) {
if (!accum.isExpressionFalse) {
merged.isExpressionFalse = false;
merged.countIfResult += accum.countIfResult;
}
}
return merged;
public Long extractOutput(long[] accumulator) {
return countFn.extractOutput(accumulator);
}

@Override
public Long extractOutput(Accum accum) {
if (!accum.isExpressionFalse) {
return accum.countIfResult;
}
return 0L;
public Coder<long[]> getAccumulatorCoder(CoderRegistry registry, Coder<Boolean> inputCoder)
throws CannotProvideCoderException {
return countFn.getAccumulatorCoder(registry, inputCoder);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically changing coders here would break in-place update. But SQL really just cannot be relied on for that, since the optimizer might change. So I am just noting that I explicitly say it is OK to break in-place update here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically we did not have a specified Coder so it was breaking when running on a distributed system as the JIRA ticket reported so backwards compatibility seems less of an issue ;)

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.beam.sdk.extensions.sql.impl.transform.agg;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Arrays;
import java.util.List;
import org.apache.beam.sdk.coders.BooleanCoder;
import org.apache.beam.sdk.coders.CannotProvideCoderException;
import org.apache.beam.sdk.coders.CoderRegistry;
import org.apache.beam.sdk.transforms.Combine;
import org.junit.Test;

/** Unit tests for {@link CountIf}. */
public class CountIfTest {

@Test
public void testCreatesEmptyAccumulator() {
long[] accumulator = (long[]) CountIf.combineFn().createAccumulator();

assertEquals(0, accumulator[0]);
}

@Test
public void testReturnsAccumulatorUnchangedForNullInput() {
Combine.CombineFn countIfFn = CountIf.combineFn();
long[] accumulator = (long[]) countIfFn.addInput(countIfFn.createAccumulator(), null);

assertEquals(0L, accumulator[0]);
}

@Test
public void testAddsInputToAccumulator() {
Combine.CombineFn countIfFn = CountIf.combineFn();
long[] accumulator = (long[]) countIfFn.addInput(countIfFn.createAccumulator(), Boolean.TRUE);

assertEquals(1L, accumulator[0]);
}

@Test
public void testCreatesAccumulatorCoder() throws CannotProvideCoderException {
assertNotNull(
CountIf.combineFn().getAccumulatorCoder(CoderRegistry.createDefault(), BooleanCoder.of()));
}

@Test
public void testMergeAccumulators() {
Combine.CombineFn countIfFn = CountIf.combineFn();
List<long[]> accums = Arrays.asList(new long[] {2}, new long[] {2});
long[] accumulator = (long[]) countIfFn.mergeAccumulators(accums);

assertEquals(4L, accumulator[0]);
}

@Test
public void testExtractsOutput() {
Combine.CombineFn countIfFn = CountIf.combineFn();

assertEquals(0L, countIfFn.extractOutput(countIfFn.createAccumulator()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ public void testAddsInputToAccumulator() {
}

@Test
public void testCeatesAccumulatorCoder() {
public void testCreatesAccumulatorCoder() {
assertNotNull(varianceFn.getAccumulatorCoder(CoderRegistry.createDefault(), VarIntCoder.of()));
}

@Test
public void testReturnsOutput() {
public void testExtractsOutput() {
assertEquals(expectedExtractedResult, varianceFn.extractOutput(testAccumulatorInput));
}
}