-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,11 @@ | |
*/ | ||
package org.apache.beam.sdk.extensions.sql.impl.transform.agg; | ||
|
||
import com.google.auto.value.AutoValue; | ||
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.coders.SerializableCoder; | ||
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 | ||
|
@@ -31,62 +30,41 @@ | |
public class CountIf { | ||
private CountIf() {} | ||
|
||
public static CountIfFn combineFn() { | ||
return new CountIf.CountIfFn(); | ||
public static Combine.CombineFn<Boolean, ?, Long> combineFn() { | ||
return new CountIfFn(); | ||
} | ||
|
||
public static class CountIfFn extends Combine.CombineFn<Boolean, CountIfFn.Accum, Long> { | ||
|
||
@AutoValue | ||
public abstract static class Accum implements Serializable { | ||
abstract boolean isExpressionFalse(); | ||
|
||
abstract long countIfResult(); | ||
|
||
static Accum empty() { | ||
return of(true, 0L); | ||
} | ||
|
||
static Accum of(boolean isExpressionFalse, long countIfResult) { | ||
return new AutoValue_CountIf_CountIfFn_Accum(isExpressionFalse, countIfResult); | ||
} | ||
} | ||
public static class CountIfFn extends Combine.CombineFn<Boolean, long[], Long> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cast makes me think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, I hesitated too make it public and make the |
||
|
||
@Override | ||
public Accum createAccumulator() { | ||
return Accum.empty(); | ||
public long[] createAccumulator() { | ||
return countFn.createAccumulator(); | ||
} | ||
|
||
@Override | ||
public Accum addInput(Accum accum, Boolean input) { | ||
public long[] addInput(long[] accumulator, Boolean input) { | ||
if (Boolean.TRUE.equals(input)) { | ||
return Accum.of(false, accum.countIfResult() + 1); | ||
countFn.addInput(accumulator, input); | ||
} | ||
return accum; | ||
return accumulator; | ||
} | ||
|
||
@Override | ||
public Accum mergeAccumulators(Iterable<Accum> accums) { | ||
CountIfFn.Accum merged = createAccumulator(); | ||
for (CountIfFn.Accum accum : accums) { | ||
if (!accum.isExpressionFalse()) { | ||
merged = Accum.of(false, merged.countIfResult() + accum.countIfResult()); | ||
} | ||
} | ||
return merged; | ||
public long[] mergeAccumulators(Iterable<long[]> accumulators) { | ||
return countFn.mergeAccumulators(accumulators); | ||
} | ||
|
||
@Override | ||
public Coder<Accum> getAccumulatorCoder(CoderRegistry registry, Coder<Boolean> inputCoder) { | ||
return SerializableCoder.of(Accum.class); | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically we did not have a specified |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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.