[
https://issues.apache.org/jira/browse/BEAM-14347?focusedWorklogId=767704&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-767704
]
ASF GitHub Bot logged work on BEAM-14347:
-----------------------------------------
Author: ASF GitHub Bot
Created on: 08/May/22 18:52
Start Date: 08/May/22 18:52
Worklog Time Spent: 10m
Work Description: lostluck commented on code in PR #17574:
URL: https://github.com/apache/beam/pull/17574#discussion_r867527758
##########
sdks/go/pkg/beam/registration/emitter.go:
##########
@@ -0,0 +1,173 @@
+// 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 registration
+
+import (
+ "context"
+ "reflect"
+
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/exec"
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/typex"
+)
+
+type emit1[T any] struct {
+ n exec.ElementProcessor
+
+ ctx context.Context
+ ws []typex.Window
+ et typex.EventTime
+ value exec.FullValue
+}
+
+func (e *emit1[T]) Init(ctx context.Context, ws []typex.Window, et
typex.EventTime) error {
+ e.ctx = ctx
+ e.ws = ws
+ e.et = et
+ return nil
+}
+
+func (e *emit1[T]) Value() interface{} {
+ return e.invoke
+}
+
+func (e *emit1[T]) invoke(val T) {
+ e.value = exec.FullValue{Windows: e.ws, Timestamp: e.et, Elm: val}
+ if err := e.n.ProcessElement(e.ctx, &e.value); err != nil {
+ panic(err)
+ }
+}
+
+type emit2[T1, T2 any] struct {
+ n exec.ElementProcessor
+
+ ctx context.Context
+ ws []typex.Window
+ et typex.EventTime
+ value exec.FullValue
+}
+
+func (e *emit2[T1, T2]) Init(ctx context.Context, ws []typex.Window, et
typex.EventTime) error {
+ e.ctx = ctx
+ e.ws = ws
+ e.et = et
+ return nil
+}
+
+func (e *emit2[T1, T2]) Value() interface{} {
+ return e.invoke
+}
+
+func (e *emit2[T1, T2]) invoke(key T1, val T2) {
+ e.value = exec.FullValue{Windows: e.ws, Timestamp: e.et, Elm: key,
Elm2: val}
+ if err := e.n.ProcessElement(e.ctx, &e.value); err != nil {
+ panic(err)
+ }
+}
+
+type emit1WithTimestamp[T any] struct {
+ n exec.ElementProcessor
+
+ ctx context.Context
+ ws []typex.Window
+ et typex.EventTime
+ value exec.FullValue
+}
+
+func (e *emit1WithTimestamp[T]) Init(ctx context.Context, ws []typex.Window,
et typex.EventTime) error {
+ e.ctx = ctx
+ e.ws = ws
+ e.et = et
+ return nil
+}
+
+func (e *emit1WithTimestamp[T]) Value() interface{} {
+ return e.invoke
+}
+
+func (e *emit1WithTimestamp[T]) invoke(et typex.EventTime, val T) {
+ e.value = exec.FullValue{Windows: e.ws, Timestamp: et, Elm: val}
+ if err := e.n.ProcessElement(e.ctx, &e.value); err != nil {
+ panic(err)
+ }
+}
+
+type emit2WithTimestamp[T1, T2 any] struct {
+ n exec.ElementProcessor
+
+ ctx context.Context
+ ws []typex.Window
+ et typex.EventTime
+ value exec.FullValue
+}
+
+func (e *emit2WithTimestamp[T1, T2]) Init(ctx context.Context, ws
[]typex.Window, et typex.EventTime) error {
+ e.ctx = ctx
+ e.ws = ws
+ e.et = et
+ return nil
+}
+
+func (e *emit2WithTimestamp[T1, T2]) Value() interface{} {
+ return e.invoke
+}
+
+func (e *emit2WithTimestamp[T1, T2]) invoke(et typex.EventTime, key T1, val
T2) {
+ e.value = exec.FullValue{Windows: e.ws, Timestamp: et, Elm: key, Elm2:
val}
+ if err := e.n.ProcessElement(e.ctx, &e.value); err != nil {
+ panic(err)
+ }
+}
+
+// Emitter1 registers parameters from your DoFn with a
+// signature func(T) and optimizes their execution.
+// This must be done by passing in type parameters of all inputs as
constraints,
+// aka: registration.Emitter1[T]()
+func Emitter1[T1 any]() {
+ e := (*func(T1))(nil)
+ registerFunc := func(n exec.ElementProcessor) exec.ReusableEmitter {
+ return &emit1[T1]{n: n}
+ }
+ exec.RegisterEmitter(reflect.TypeOf(e).Elem(), registerFunc)
+}
+
+// Emitter2 registers parameters from your DoFn with a
+// signature func(T1, T2) and optimizes their execution.
+// This must be done by passing in type parameters of all inputs as
constraints,
+// aka: registration.Emitter2[T1, T2]()
+func Emitter2[T1, T2 any]() {
+ e := (*func(T1, T2))(nil)
+ registerFunc := func(n exec.ElementProcessor) exec.ReusableEmitter {
+ return &emit2[T1, T2]{n: n}
+ }
+ if reflect.TypeOf(e).Elem().In(0) == typex.EventTimeType {
+ registerFunc = func(n exec.ElementProcessor)
exec.ReusableEmitter {
+ return &emit1WithTimestamp[T2]{n: n}
+ }
+ }
+ exec.RegisterEmitter(reflect.TypeOf(e).Elem(), registerFunc)
+}
+
+// Emitter3 registers parameters from your DoFn with a
+// signature func(T1, T2, T3) and optimizes their execution.
+// This must be done by passing in type parameters of all inputs as
constraints,
+// aka: registration.Emitter3[T1, T2, T3]()
+func Emitter3[T1, T2, T3 any]() {
Review Comment:
As written this is going to end up confusing for users: We always assume
it's a timestamp and it should always be a timestamp, but as written it's
implied it could be any 3 types. Either make this actually be an Emitter3, or
make it an EventTimeEmitter2. Either way the documentation should be updated to
be explicit about the timestamp restriction here.
It may be worthwhile to simply just go with EmitterV emitter KV in these
cases instead for the user API, to reduce how much the user would need to think
about these cases. Then any EventTime specializations we do can bit EmitterETV
and EmitterETKV.
Similarly for IterV and IterKV
Issue Time Tracking
-------------------
Worklog Id: (was: 767704)
Time Spent: 9.5h (was: 9h 20m)
> [Go SDK] Allow users to optimize DoFns with a single generic registration
> function
> ----------------------------------------------------------------------------------
>
> Key: BEAM-14347
> URL: https://issues.apache.org/jira/browse/BEAM-14347
> Project: Beam
> Issue Type: New Feature
> Components: sdk-go
> Reporter: Danny McCormick
> Assignee: Danny McCormick
> Priority: P2
> Time Spent: 9.5h
> Remaining Estimate: 0h
>
> Right now, to optimize DoFn execution, users have to use the code generator.
> This updates to allow them to use generics instead.
--
This message was sent by Atlassian Jira
(v8.20.7#820007)