This is an automated email from the ASF dual-hosted git repository.
etudenhoefner pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-go.git
The following commit(s) were added to refs/heads/main by this push:
new 1df3dbd refactor: some more small refactors (#130)
1df3dbd is described below
commit 1df3dbd0734c9753f5872bf520fbfdd92c37536d
Author: Matt Topol <[email protected]>
AuthorDate: Tue Aug 20 03:04:00 2024 -0400
refactor: some more small refactors (#130)
---
literals.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
manifest.go | 18 ++++++++++++++----
transforms.go | 3 +++
types.go | 8 ++++++++
4 files changed, 81 insertions(+), 6 deletions(-)
diff --git a/literals.go b/literals.go
index d88f4aa..1150524 100644
--- a/literals.go
+++ b/literals.go
@@ -71,6 +71,12 @@ type TypedLiteral[T LiteralType] interface {
Comparator() Comparator[T]
}
+type NumericLiteral interface {
+ Literal
+ Increment() Literal
+ Decrement() Literal
+}
+
// NewLiteral provides a literal based on the type of T
func NewLiteral[T LiteralType](val T) Literal {
switch v := any(val).(type) {
@@ -416,6 +422,22 @@ func (i Int32Literal) Equals(other Literal) bool {
return literalEq(i, other)
}
+func (i Int32Literal) Increment() Literal {
+ if i == math.MaxInt32 {
+ return Int32AboveMaxLiteral()
+ }
+
+ return Int32Literal(i + 1)
+}
+
+func (i Int32Literal) Decrement() Literal {
+ if i == math.MinInt32 {
+ return Int32BelowMinLiteral()
+ }
+
+ return Int32Literal(i - 1)
+}
+
func (i Int32Literal) MarshalBinary() (data []byte, err error) {
// stored as 4 bytes in little endian order
data = make([]byte, 4)
@@ -482,6 +504,22 @@ func (i Int64Literal) Equals(other Literal) bool {
return literalEq(i, other)
}
+func (i Int64Literal) Increment() Literal {
+ if i == math.MaxInt64 {
+ return Int64AboveMaxLiteral()
+ }
+
+ return Int64Literal(i + 1)
+}
+
+func (i Int64Literal) Decrement() Literal {
+ if i == math.MinInt64 {
+ return Int64BelowMinLiteral()
+ }
+
+ return Int64Literal(i - 1)
+}
+
func (i Int64Literal) MarshalBinary() (data []byte, err error) {
// stored as 8 byte little-endian
data = make([]byte, 8)
@@ -598,7 +636,7 @@ func (DateLiteral) Comparator() Comparator[Date] { return
cmp.Compare[Date] }
func (d DateLiteral) Type() Type { return PrimitiveTypes.Date }
func (d DateLiteral) Value() Date { return Date(d) }
func (d DateLiteral) String() string {
- t := time.Unix(0, 0).UTC().AddDate(0, 0, int(d))
+ t := Date(d).ToTime()
return t.Format("2006-01-02")
}
func (d DateLiteral) To(t Type) (Literal, error) {
@@ -612,6 +650,9 @@ func (d DateLiteral) Equals(other Literal) bool {
return literalEq(d, other)
}
+func (d DateLiteral) Increment() Literal { return DateLiteral(d + 1) }
+func (d DateLiteral) Decrement() Literal { return DateLiteral(d - 1) }
+
func (d DateLiteral) MarshalBinary() (data []byte, err error) {
// stored as 4 byte little endian
data = make([]byte, 4)
@@ -673,7 +714,7 @@ func (TimestampLiteral) Comparator() Comparator[Timestamp]
{ return cmp.Compare[
func (t TimestampLiteral) Type() Type { return
PrimitiveTypes.Timestamp }
func (t TimestampLiteral) Value() Timestamp { return
Timestamp(t) }
func (t TimestampLiteral) String() string {
- tm := time.UnixMicro(int64(t)).UTC()
+ tm := Timestamp(t).ToTime()
return tm.Format("2006-01-02 15:04:05.000000")
}
func (t TimestampLiteral) To(typ Type) (Literal, error) {
@@ -691,6 +732,9 @@ func (t TimestampLiteral) Equals(other Literal) bool {
return literalEq(t, other)
}
+func (t TimestampLiteral) Increment() Literal { return TimestampLiteral(t + 1)
}
+func (t TimestampLiteral) Decrement() Literal { return TimestampLiteral(t - 1)
}
+
func (t TimestampLiteral) MarshalBinary() (data []byte, err error) {
// stored as 8 byte little endian
data = make([]byte, 8)
@@ -1073,6 +1117,16 @@ func (d DecimalLiteral) Equals(other Literal) bool {
return d.Val == rescaled
}
+func (d DecimalLiteral) Increment() Literal {
+ d.Val = d.Val.Add(decimal128.FromU64(1))
+ return d
+}
+
+func (d DecimalLiteral) Decrement() Literal {
+ d.Val = d.Val.Sub(decimal128.FromU64(1))
+ return d
+}
+
func (d DecimalLiteral) MarshalBinary() (data []byte, err error) {
// stored as unscaled value in two's compliment big-endian values
// using the minimum number of bytes for the values
diff --git a/manifest.go b/manifest.go
index 5340aa5..57465f1 100644
--- a/manifest.go
+++ b/manifest.go
@@ -385,6 +385,10 @@ func getFieldIDMap(sc avro.Schema) map[string]int {
return result
}
+type hasFieldToIDMap interface {
+ setFieldNameToIDMap(map[string]int)
+}
+
func fetchManifestEntries(m ManifestFile, fs iceio.IO, discardDeleted bool)
([]ManifestEntry, error) {
f, err := fs.Open(m.FilePath())
if err != nil {
@@ -441,7 +445,9 @@ func fetchManifestEntries(m ManifestFile, fs iceio.IO,
discardDeleted bool) ([]M
if !discardDeleted || tmp.Status() != EntryStatusDELETED {
tmp.inheritSeqNum(m)
- tmp.DataFile().setFieldNameToIDMap(fieldNameToID)
+ if fieldToIDMap, ok :=
tmp.DataFile().(hasFieldToIDMap); ok {
+ fieldToIDMap.setFieldNameToIDMap(fieldNameToID)
+ }
results = append(results, tmp)
}
}
@@ -720,9 +726,13 @@ func (d *dataFile) setFieldNameToIDMap(m map[string]int) {
d.fieldNameToID = m }
func (d *dataFile) ContentType() ManifestEntryContent { return d.Content }
func (d *dataFile) FilePath() string { return d.Path }
func (d *dataFile) FileFormat() FileFormat { return d.Format }
-func (d *dataFile) Partition() map[string]any { return d.PartitionData
}
-func (d *dataFile) Count() int64 { return d.RecordCount }
-func (d *dataFile) FileSizeBytes() int64 { return d.FileSize }
+func (d *dataFile) Partition() map[string]any {
+ d.initializeMapData()
+ return d.PartitionData
+}
+
+func (d *dataFile) Count() int64 { return d.RecordCount }
+func (d *dataFile) FileSizeBytes() int64 { return d.FileSize }
func (d *dataFile) ColumnSizes() map[int]int64 {
d.initializeMapData()
diff --git a/transforms.go b/transforms.go
index 58d2011..81e85b0 100644
--- a/transforms.go
+++ b/transforms.go
@@ -22,6 +22,7 @@ import (
"fmt"
"strconv"
"strings"
+ "time"
)
// ParseTransform takes the string representation of a transform as
@@ -128,6 +129,8 @@ func (t TruncateTransform) String() string { return
fmt.Sprintf("truncate[%d]",
func (TruncateTransform) ResultType(t Type) Type { return t }
+var epochTM = time.Unix(0, 0).UTC()
+
// YearTransform transforms a datetime value into a year value.
type YearTransform struct{}
diff --git a/types.go b/types.go
index 5aabdb4..e7a8b4d 100644
--- a/types.go
+++ b/types.go
@@ -506,6 +506,10 @@ func (Float64Type) String() string { return "double" }
type Date int32
+func (d Date) ToTime() time.Time {
+ return epochTM.AddDate(0, 0, int(d))
+}
+
// DateType represents a calendar date without a timezone or time,
// represented as a 32-bit integer denoting the number of days since
// the unix epoch.
@@ -536,6 +540,10 @@ func (TimeType) String() string { return "time" }
type Timestamp int64
+func (t Timestamp) ToTime() time.Time {
+ return time.UnixMicro(int64(t)).UTC()
+}
+
func (t Timestamp) ToDate() Date {
tm := time.UnixMicro(int64(t)).UTC()
return Date(tm.Truncate(24*time.Hour).Unix() / int64((time.Hour *
24).Seconds()))