You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by at...@apache.org on 2026/08/07 15:55:49 UTC

(iceberg-go) branch main updated: fix(substrait): convert nanosecond timestamp literals (#1590)

This is an automated email from the ASF dual-hosted git repository.

laskoviymishka 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 069e3a38 fix(substrait): convert nanosecond timestamp literals (#1590)
069e3a38 is described below

commit 069e3a38c80a94bdfff50c29fbd9bdefa05f6697
Author: Minh Vu <vu...@gmail.com>
AuthorDate: Fri Aug 7 17:55:44 2026 +0200

    fix(substrait): convert nanosecond timestamp literals (#1590)
    
    * fix(substrait): convert nanosecond timestamp literals
    
    Signed-off-by: Minh Vu <vu...@gmail.com>
    
    * test(substrait): cover nanosecond range literals
    
    * test(substrait): assert nanosecond set literals
    
    ---------
    
    Signed-off-by: Minh Vu <vu...@gmail.com>
---
 table/substrait/substrait.go      | 15 ++++++++++
 table/substrait/substrait_test.go | 58 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)

diff --git a/table/substrait/substrait.go b/table/substrait/substrait.go
index 26b03ed4..d51c4ba4 100644
--- a/table/substrait/substrait.go
+++ b/table/substrait/substrait.go
@@ -21,6 +21,7 @@ import (
 	_ "embed"
 	"encoding/binary"
 	"fmt"
+	"sort"
 	"strings"
 	"unsafe"
 
@@ -314,6 +315,16 @@ func toSubstraitLiteral(typ iceberg.Type, lit iceberg.Literal) expr.Literal {
 		}
 
 		return toPrimitiveSubstraitLiteral(types.Timestamp(lit))
+	case iceberg.TimestampNsLiteral:
+		if typ.Equals(iceberg.PrimitiveTypes.TimestampTzNs) {
+			return expr.NewPrecisionTimestampTzLiteral(
+				int64(lit), types.PrecisionNanoSeconds, types.NullabilityRequired,
+			)
+		}
+
+		return expr.NewPrecisionTimestampLiteral(
+			int64(lit), types.PrecisionNanoSeconds, types.NullabilityRequired,
+		)
 	case iceberg.DateLiteral:
 		return toPrimitiveSubstraitLiteral(types.Date(lit))
 	case iceberg.TimeLiteral:
@@ -335,6 +346,10 @@ func toSubstraitLiteralSet(typ iceberg.Type, lits []iceberg.Literal) expr.ListLi
 		return nil
 	}
 
+	sort.Slice(lits, func(i, j int) bool {
+		return lits[i].String() < lits[j].String()
+	})
+
 	out := make([]expr.Literal, len(lits))
 	for i, l := range lits {
 		out[i] = toSubstraitLiteral(typ, l)
diff --git a/table/substrait/substrait_test.go b/table/substrait/substrait_test.go
index 5c4f4ade..1a4f673b 100644
--- a/table/substrait/substrait_test.go
+++ b/table/substrait/substrait_test.go
@@ -191,6 +191,64 @@ func TestExprs(t *testing.T) {
 	}
 }
 
+func TestNanosecondTimestampLiterals(t *testing.T) {
+	tests := []struct {
+		name      string
+		fieldType iceberg.Type
+		predicate iceberg.BooleanExpression
+		wantExpr  string
+	}{
+		{
+			name:      "timestamp equality",
+			fieldType: iceberg.PrimitiveTypes.TimestampNs,
+			predicate: iceberg.EqualTo(iceberg.Reference("ts"), iceberg.TimestampNano(123456789)),
+			wantExpr:  "equal(.field(0) => precision_timestamp?<9>, precision_timestamp<9>(1970-01-01 00:00:00.123456789)) => boolean?",
+		},
+		{
+			name:      "timestamp with timezone equality before epoch",
+			fieldType: iceberg.PrimitiveTypes.TimestampTzNs,
+			predicate: iceberg.EqualTo(iceberg.Reference("ts"), iceberg.TimestampNano(-123456789)),
+			wantExpr:  "equal(.field(0) => precision_timestamp_tz?<9>, precision_timestamp_tz<9>(1969-12-31T23:59:59.876543211Z)) => boolean?",
+		},
+		{
+			name:      "timestamp in",
+			fieldType: iceberg.PrimitiveTypes.TimestampNs,
+			predicate: iceberg.IsIn(iceberg.Reference("ts"), iceberg.TimestampNano(1), iceberg.TimestampNano(1001)),
+			wantExpr:  "is_in(.field(0) => precision_timestamp?<9>, list<precision_timestamp<9>>([precision_timestamp<9>(1970-01-01 00:00:00.000000001) precision_timestamp<9>(1970-01-01 00:00:00.000001001)])) => boolean?",
+		},
+		{
+			name:      "timestamp with timezone not in",
+			fieldType: iceberg.PrimitiveTypes.TimestampTzNs,
+			predicate: iceberg.NotIn(iceberg.Reference("ts"), iceberg.TimestampNano(-1), iceberg.TimestampNano(1001)),
+			wantExpr:  "not(is_in(.field(0) => precision_timestamp_tz?<9>, list<precision_timestamp_tz<9>>([precision_timestamp_tz<9>(1969-12-31T23:59:59.999999999Z) precision_timestamp_tz<9>(1970-01-01T00:00:00.000001001Z)])) => boolean?) => boolean?",
+		},
+		{
+			name:      "timestamp greater than",
+			fieldType: iceberg.PrimitiveTypes.TimestampNs,
+			predicate: iceberg.GreaterThan(iceberg.Reference("ts"), iceberg.TimestampNano(42)),
+			wantExpr:  "gt(.field(0) => precision_timestamp?<9>, precision_timestamp<9>(1970-01-01 00:00:00.000000042)) => boolean?",
+		},
+		{
+			name:      "timestamp with timezone less than",
+			fieldType: iceberg.PrimitiveTypes.TimestampTzNs,
+			predicate: iceberg.LessThan(iceberg.Reference("ts"), iceberg.TimestampNano(-42)),
+			wantExpr:  "lt(.field(0) => precision_timestamp_tz?<9>, precision_timestamp_tz<9>(1969-12-31T23:59:59.999999958Z)) => boolean?",
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			sc := iceberg.NewSchema(1, iceberg.NestedField{ID: 1, Name: "ts", Type: tt.fieldType})
+			bound, err := iceberg.BindExpr(sc, tt.predicate, true)
+			require.NoError(t, err)
+
+			_, converted, err := substrait.ConvertExpr(sc, bound, true)
+			require.NoError(t, err)
+			assert.Equal(t, tt.wantExpr, converted.String())
+		})
+	}
+}
+
 func TestVariantSchemaConversionDoesNotPanic(t *testing.T) {
 	sc := iceberg.NewSchema(1,
 		iceberg.NestedField{ID: 1, Name: "id", Type: iceberg.PrimitiveTypes.Int64, Required: true},