You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "Eliaaazzz (via GitHub)" <gi...@apache.org> on 2026/09/17 13:19:39 UTC
[PR] [Go SDK] Add portable logical type registration and standard date and micros_instant types [beam]
Eliaaazzz opened a new pull request, #40154:
URL: https://github.com/apache/beam/pull/40154
Part of #39684. Depends on #40153 (the first two commits); review the last commit.
The Go SDK could only support a portable logical type by registering it in three places (`schema.RegisterLogicalType`, `coder.RegisterSchemaProviders`, and the `beam.SchemaProvider` glue) with hand written encoders, and none of the standard logical types from schema.proto were registered. A schema from another SDK with a `beam:logical_type:date:v1` or `beam:logical_type:micros_instant:v1` field failed with `unknown logical type`.
Registration API, in package `schema`:
- `RegisterLogicalTypeConversion[GoT, StorageT](urn, toStorage, fromStorage)` registers the URN, the Go type and the storage type, and builds the row coder for `GoT` from the row field coder of `StorageT` plus the two conversion functions.
- `RegisterParameterizedLogicalTypeConversion` is the same for a logical type with an argument; each argument value gets its own Go type.
- `RegisterLogicalTypeCoder[GoT, StorageT](urn, enc, dec)` is for logical types whose wire encoding differs from the row encoding of the storage type, such as `millis_instant` and `decimal` (following PRs).
- `coder.RowFieldEncoderForType` and `coder.RowFieldDecoderForType` expose the row field encoding of a single type, which the registration builds on.
Standard types, registered by default:
- `schema.Date` (`beam:logical_type:date:v1`), a calendar date stored as days since the epoch.
- `schema.MicrosInstant` (`beam:logical_type:micros_instant:v1`), a `time.Time` stored as a row of seconds and microseconds. Encoding a value with sub microsecond precision returns an error, as in the Java SDK.
`time.Time` keeps its current Go specific encoding; the standard types are separate Go types, so existing pipelines are unaffected. Because the Go row coder is built from the Go type alone, each URN maps to one named Go type.
Regression test changes in `sdks/go/test/regression/coders/fromyaml`:
- The expected row of a `beam:coder:row:v1` case is built from the schema in the coder payload through `schema.ToType`, so logical type fields get their registered Go types. This replaces the name based type table.
- Coder payloads are decoded from the yaml string the same way the examples are, so payload bytes above 0x7f (used by the `timestamp:v1` cases) are read correctly.
- Encoding mismatches now count as failures; before, they were only logged.
- The `micros_instant` case runs. The remaining logical type cases are skipped by URN with a reason pointing at #39684.
Tests: schema conversion and byte level row encoding for `Date` and `MicrosInstant` (the bytes are the `micros_instant` example of standard_coders.yaml), date conversion at the epoch boundaries, the sub microsecond error, `RegisterLogicalTypeConversion` end to end with a user type, and `RowFieldEncoderForType`/`RowFieldDecoderForType` for atomic, slice and struct types.
------------------------
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [x] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
- [x] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://github.com/apache/beam/blob/master/CONTRIBUTING.md#make-the-reviewers-job-easier).
To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI or the [workflows README](https://github.com/apache/beam/blob/master/.github/workflows/README.md) to see a list of phrases to trigger workflows.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Go SDK] Add portable logical type registration and standard date and micros_instant types [beam]
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #40154:
URL: https://github.com/apache/beam/pull/40154#issuecomment-6050156237
## [Codecov](https://app.codecov.io/gh/apache/beam/pull/40154?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `85.07853%` with `57 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 28.65%. Comparing base ([`69bdf5f`](https://app.codecov.io/gh/apache/beam/commit/69bdf5f29174adebe19006e1397acd6681ffce9d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`db75cf6`](https://app.codecov.io/gh/apache/beam/commit/db75cf6e4a0269c53f4cd90763d8e4cf2583e95f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
:warning: Report is 44 commits behind head on master.
| [Files with missing lines](https://app.codecov.io/gh/apache/beam/pull/40154?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [...s/go/pkg/beam/core/runtime/graphx/schema/schema.go](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&filepath=sdks%2Fgo%2Fpkg%2Fbeam%2Fcore%2Fruntime%2Fgraphx%2Fschema%2Fschema.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9nby9wa2cvYmVhbS9jb3JlL3J1bnRpbWUvZ3JhcGh4L3NjaGVtYS9zY2hlbWEuZ28=) | 71.42% | [17 Missing and 7 partials :warning: ](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [sdks/go/pkg/beam/core/graph/coder/row\_decoder.go](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&filepath=sdks%2Fgo%2Fpkg%2Fbeam%2Fcore%2Fgraph%2Fcoder%2Frow_decoder.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9nby9wa2cvYmVhbS9jb3JlL2dyYXBoL2NvZGVyL3Jvd19kZWNvZGVyLmdv) | 71.42% | [6 Missing and 6 partials :warning: ](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [.../pkg/beam/core/runtime/graphx/schema/fieldvalue.go](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&filepath=sdks%2Fgo%2Fpkg%2Fbeam%2Fcore%2Fruntime%2Fgraphx%2Fschema%2Ffieldvalue.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9nby9wa2cvYmVhbS9jb3JlL3J1bnRpbWUvZ3JhcGh4L3NjaGVtYS9maWVsZHZhbHVlLmdv) | 93.00% | [5 Missing and 2 partials :warning: ](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...kg/beam/core/runtime/graphx/schema/logicaltypes.go](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&filepath=sdks%2Fgo%2Fpkg%2Fbeam%2Fcore%2Fruntime%2Fgraphx%2Fschema%2Flogicaltypes.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9nby9wa2cvYmVhbS9jb3JlL3J1bnRpbWUvZ3JhcGh4L3NjaGVtYS9sb2dpY2FsdHlwZXMuZ28=) | 92.00% | [3 Missing and 3 partials :warning: ](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [sdks/go/pkg/beam/core/graph/coder/int.go](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&filepath=sdks%2Fgo%2Fpkg%2Fbeam%2Fcore%2Fgraph%2Fcoder%2Fint.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9nby9wa2cvYmVhbS9jb3JlL2dyYXBoL2NvZGVyL2ludC5nbw==) | 76.47% | [2 Missing and 2 partials :warning: ](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [sdks/go/pkg/beam/core/graph/coder/row\_encoder.go](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&filepath=sdks%2Fgo%2Fpkg%2Fbeam%2Fcore%2Fgraph%2Fcoder%2Frow_encoder.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9nby9wa2cvYmVhbS9jb3JlL2dyYXBoL2NvZGVyL3Jvd19lbmNvZGVyLmdv) | 82.60% | [3 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/beam/pull/40154?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #40154 +/- ##
=============================================
- Coverage 59.04% 28.65% -30.40%
=============================================
Files 2797 381 -2416
Lines 280750 71057 -209693
Branches 12488 0 -12488
=============================================
- Hits 165764 20359 -145405
+ Misses 108540 48854 -59686
+ Partials 6446 1844 -4602
```
| [Flag](https://app.codecov.io/gh/apache/beam/pull/40154/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [go](https://app.codecov.io/gh/apache/beam/pull/40154/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `28.65% <85.07%> (-0.49%)` | :arrow_down: |
| [java](https://app.codecov.io/gh/apache/beam/pull/40154/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [python](https://app.codecov.io/gh/apache/beam/pull/40154/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/beam/pull/40154?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Go SDK] Add portable logical type registration and standard date and micros_instant types [beam]
Posted by "Eliaaazzz (via GitHub)" <gi...@apache.org>.
Eliaaazzz commented on PR #40154:
URL: https://github.com/apache/beam/pull/40154#issuecomment-6135327116
Rebased onto master now that #40152 and #40153 are merged. I also added a commit that replaces the deprecated `reflect.PtrTo` in row_test.go, as suggested on #40153.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Go SDK] Add portable logical type registration and standard date and micros_instant types [beam]
Posted by "jrmccluskey (via GitHub)" <gi...@apache.org>.
jrmccluskey commented on PR #40154:
URL: https://github.com/apache/beam/pull/40154#issuecomment-6128065609
Hey there! I'm starting a review pass, if you would go ahead and rebase this PR now that the two diffbase PRs are merged that would make this a little bit smoother as far as reviewing and merging. Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Go SDK] Add portable logical type registration and standard date and micros_instant types [beam]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #40154:
URL: https://github.com/apache/beam/pull/40154#issuecomment-5715884211
Assigning reviewers:
R: @jrmccluskey for label go.
This pull request likely touches a core component ("core" label). Please review with scrutiny.
Note: If you would like to opt out of this review, comment `assign to next reviewer`.
Available commands:
- `stop reviewer notifications` - opt out of the automated review tooling
- `remind me after tests pass` - tag the comment author after tests pass
- `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
The PR bot will only process comments in the main thread (not review comments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Go SDK] Add portable logical type registration and standard date and micros_instant types [beam]
Posted by "Eliaaazzz (via GitHub)" <gi...@apache.org>.
Eliaaazzz commented on PR #40154:
URL: https://github.com/apache/beam/pull/40154#issuecomment-6244580262
Thanks for the notes. I pushed e7c4f6c with the three changes and merged master to clear the CHANGES.md conflict.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Go SDK] Add portable logical type registration and standard date and micros_instant types [beam]
Posted by "jrmccluskey (via GitHub)" <gi...@apache.org>.
jrmccluskey commented on code in PR #40154:
URL: https://github.com/apache/beam/pull/40154#discussion_r4262089834
##########
sdks/go/pkg/beam/core/runtime/graphx/schema/logicaltypes.go:
##########
Review Comment:
Now that users will be interacting with this code more directly, it's probably a good idea to include a duplication check here (I am okay with this being a follow-up.)
##########
sdks/go/pkg/beam/core/runtime/graphx/schema/standard_logical_types.go:
##########
@@ -0,0 +1,136 @@
+// 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 schema
+
+import (
+ "time"
+
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime"
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/reflectx"
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
+)
+
+// URNs of the standard logical types defined in schema.proto.
+const (
+ // URNDate identifies the Date logical type.
+ URNDate = "beam:logical_type:date:v1"
+ // URNMicrosInstant identifies the MicrosInstant logical type.
+ URNMicrosInstant = "beam:logical_type:micros_instant:v1"
+)
+
+// Date is a calendar date without a time zone or time of day, the
+// beam:logical_type:date:v1 logical type. It is stored as the number of days
+// since 1970-01-01 in an INT64 field. Time and String normalize out of range
+// fields as time.Date does. Encoding fails unless the fields form a valid
+// date with a year from -999999999 to 999999999, and decoding fails for a day
+// count outside that range.
+type Date struct {
+ Year int
+ Month time.Month
+ Day int
+}
Review Comment:
Date doesn't have a valid zero value (e.g. `Date{}` won't encode and will be rejected.) I would suggest treating `Date{}` as the epoch for clarity.
##########
sdks/go/pkg/beam/core/runtime/graphx/schema/standard_logical_types.go:
##########
@@ -0,0 +1,136 @@
+// 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 schema
+
+import (
+ "time"
+
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime"
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/reflectx"
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
+)
+
+// URNs of the standard logical types defined in schema.proto.
+const (
+ // URNDate identifies the Date logical type.
+ URNDate = "beam:logical_type:date:v1"
+ // URNMicrosInstant identifies the MicrosInstant logical type.
+ URNMicrosInstant = "beam:logical_type:micros_instant:v1"
+)
+
+// Date is a calendar date without a time zone or time of day, the
+// beam:logical_type:date:v1 logical type. It is stored as the number of days
+// since 1970-01-01 in an INT64 field. Time and String normalize out of range
+// fields as time.Date does. Encoding fails unless the fields form a valid
+// date with a year from -999999999 to 999999999, and decoding fails for a day
+// count outside that range.
+type Date struct {
+ Year int
+ Month time.Month
+ Day int
+}
+
+// DateOf returns the Date of t in t's location.
+func DateOf(t time.Time) Date {
+ y, m, d := t.Date()
+ return Date{Year: y, Month: m, Day: d}
+}
+
+// Time returns the start of the date in UTC.
+func (d Date) Time() time.Time {
+ return time.Date(d.Year, d.Month, d.Day, 0, 0, 0, 0, time.UTC)
+}
+
+// String returns the date in ISO 8601 format, such as 2006-01-02.
+func (d Date) String() string {
+ return d.Time().Format("2006-01-02")
+}
+
+const secondsPerDay = 24 * 60 * 60
+
+// minDateDays and maxDateDays bound the supported days since the epoch.
+var (
+ minDateDays = time.Date(-999999999, time.January, 1, 0, 0, 0, 0, time.UTC).Unix() / secondsPerDay
+ maxDateDays = time.Date(999999999, time.December, 31, 0, 0, 0, 0, time.UTC).Unix() / secondsPerDay
+)
+
+func dateToStorage(d Date) (int64, error) {
+ t := d.Time()
+ // Out of range fields can overflow in time.Date, so only a date that
+ // round trips is encoded.
+ if DateOf(t) != d {
+ return 0, errors.Errorf("%d-%02d-%02d is not a valid date", d.Year, d.Month, d.Day)
+ }
+ days := t.Unix() / secondsPerDay
+ if days < minDateDays || days > maxDateDays {
+ return 0, errors.Errorf("date %v is out of range", d)
+ }
+ return days, nil
+}
+
+func dateFromStorage(days int64) (Date, error) {
+ if days < minDateDays || days > maxDateDays {
+ return Date{}, errors.Errorf("%v days since the epoch is out of the supported date range", days)
+ }
+ return DateOf(time.Unix(days*secondsPerDay, 0).UTC()), nil
+}
+
+// MicrosInstant is a timestamp with microsecond precision, the
+// beam:logical_type:micros_instant:v1 logical type. It is stored as a row of
+// the seconds and the microseconds since the epoch. Encoding a value with
+// sub microsecond precision fails; truncate such values with time.Time.Truncate
+// first.
+type MicrosInstant time.Time
Review Comment:
Consider implementing String() and Equals() methods on this type for ease of use.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Go SDK] Add portable logical type registration and standard date and micros_instant types [beam]
Posted by "Eliaaazzz (via GitHub)" <gi...@apache.org>.
Eliaaazzz commented on code in PR #40154:
URL: https://github.com/apache/beam/pull/40154#discussion_r4305374679
##########
sdks/go/pkg/beam/core/runtime/graphx/schema/standard_logical_types.go:
##########
@@ -0,0 +1,136 @@
+// 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 schema
+
+import (
+ "time"
+
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime"
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/reflectx"
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
+)
+
+// URNs of the standard logical types defined in schema.proto.
+const (
+ // URNDate identifies the Date logical type.
+ URNDate = "beam:logical_type:date:v1"
+ // URNMicrosInstant identifies the MicrosInstant logical type.
+ URNMicrosInstant = "beam:logical_type:micros_instant:v1"
+)
+
+// Date is a calendar date without a time zone or time of day, the
+// beam:logical_type:date:v1 logical type. It is stored as the number of days
+// since 1970-01-01 in an INT64 field. Time and String normalize out of range
+// fields as time.Date does. Encoding fails unless the fields form a valid
+// date with a year from -999999999 to 999999999, and decoding fails for a day
+// count outside that range.
+type Date struct {
+ Year int
+ Month time.Month
+ Day int
+}
Review Comment:
Done in e7c4f6c. The zero Date now stands for the epoch, so Date{} encodes as day 0 and prints as 1970-01-01. Decoding day 0 still returns the explicit fields.
##########
sdks/go/pkg/beam/core/runtime/graphx/schema/logicaltypes.go:
##########
Review Comment:
Added in e7c4f6c. RegisterLogicalType now panics on conflicting registrations for a Go type or an identifier and argument. Registering the same logical type again has no effect.
##########
sdks/go/pkg/beam/core/runtime/graphx/schema/standard_logical_types.go:
##########
@@ -0,0 +1,136 @@
+// 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 schema
+
+import (
+ "time"
+
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime"
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/reflectx"
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
+)
+
+// URNs of the standard logical types defined in schema.proto.
+const (
+ // URNDate identifies the Date logical type.
+ URNDate = "beam:logical_type:date:v1"
+ // URNMicrosInstant identifies the MicrosInstant logical type.
+ URNMicrosInstant = "beam:logical_type:micros_instant:v1"
+)
+
+// Date is a calendar date without a time zone or time of day, the
+// beam:logical_type:date:v1 logical type. It is stored as the number of days
+// since 1970-01-01 in an INT64 field. Time and String normalize out of range
+// fields as time.Date does. Encoding fails unless the fields form a valid
+// date with a year from -999999999 to 999999999, and decoding fails for a day
+// count outside that range.
+type Date struct {
+ Year int
+ Month time.Month
+ Day int
+}
+
+// DateOf returns the Date of t in t's location.
+func DateOf(t time.Time) Date {
+ y, m, d := t.Date()
+ return Date{Year: y, Month: m, Day: d}
+}
+
+// Time returns the start of the date in UTC.
+func (d Date) Time() time.Time {
+ return time.Date(d.Year, d.Month, d.Day, 0, 0, 0, 0, time.UTC)
+}
+
+// String returns the date in ISO 8601 format, such as 2006-01-02.
+func (d Date) String() string {
+ return d.Time().Format("2006-01-02")
+}
+
+const secondsPerDay = 24 * 60 * 60
+
+// minDateDays and maxDateDays bound the supported days since the epoch.
+var (
+ minDateDays = time.Date(-999999999, time.January, 1, 0, 0, 0, 0, time.UTC).Unix() / secondsPerDay
+ maxDateDays = time.Date(999999999, time.December, 31, 0, 0, 0, 0, time.UTC).Unix() / secondsPerDay
+)
+
+func dateToStorage(d Date) (int64, error) {
+ t := d.Time()
+ // Out of range fields can overflow in time.Date, so only a date that
+ // round trips is encoded.
+ if DateOf(t) != d {
+ return 0, errors.Errorf("%d-%02d-%02d is not a valid date", d.Year, d.Month, d.Day)
+ }
+ days := t.Unix() / secondsPerDay
+ if days < minDateDays || days > maxDateDays {
+ return 0, errors.Errorf("date %v is out of range", d)
+ }
+ return days, nil
+}
+
+func dateFromStorage(days int64) (Date, error) {
+ if days < minDateDays || days > maxDateDays {
+ return Date{}, errors.Errorf("%v days since the epoch is out of the supported date range", days)
+ }
+ return DateOf(time.Unix(days*secondsPerDay, 0).UTC()), nil
+}
+
+// MicrosInstant is a timestamp with microsecond precision, the
+// beam:logical_type:micros_instant:v1 logical type. It is stored as a row of
+// the seconds and the microseconds since the epoch. Encoding a value with
+// sub microsecond precision fails; truncate such values with time.Time.Truncate
+// first.
+type MicrosInstant time.Time
Review Comment:
Added in e7c4f6c. String prints RFC 3339 with microseconds and Equals wraps time.Time.Equal.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Go SDK] Add portable logical type registration and standard date and micros_instant types [beam]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #40154:
URL: https://github.com/apache/beam/pull/40154#issuecomment-5832412240
Reminder, please take a look at this pr: @jrmccluskey
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Go SDK] Add portable logical type registration and standard date and micros_instant types [beam]
Posted by "jrmccluskey (via GitHub)" <gi...@apache.org>.
jrmccluskey merged PR #40154:
URL: https://github.com/apache/beam/pull/40154
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org