From a Go struct slice, the way a real ingestion job would
Realistically, your rows start life as Go values — decoded from an API payload, read off a queue, whatever. The right move is to hand Spark an Arrow record batch built from those structs, so the rows cross the wire as typed columnar data rather than spliced into SQL text. Build every column at the type the table already declares — order_ts in microseconds, amount as a real Decimal128(10,2) — and the frame matches the table exactly, so appending it is one call and there is no SQL in the write path at all:
import("github.com/apache/arrow-go/v18/arrow""github.com/apache/arrow-go/v18/arrow/array""github.com/apache/arrow-go/v18/arrow/decimal128""github.com/apache/arrow-go/v18/arrow/memory""github.com/apache/spark-connect-go/v40/spark/sql")typeOrderstruct{OrderIDint64CustomerIDint64OrderTStime.TimeAmountfloat64StatusstringCountrystring}// NOTE: order_ts MUST be Microsecond — see the trap below.// amount is built as Decimal128 with the table's exact precision/scale,// so no float64 ever carries a currency value and no CAST is needed.varorderArrowSchema=arrow.NewSchema([]arrow.Field{{Name:"order_id",Type:arrow.PrimitiveTypes.Int64},{Name:"customer_id",Type:arrow.PrimitiveTypes.Int64},{Name:"order_ts",Type:&arrow.TimestampType{Unit:arrow.Microsecond}},{Name:"amount",Type:&arrow.Decimal128Type{Precision:10,Scale:2}},{Name:"status",Type:arrow.BinaryTypes.String},{Name:"country",Type:arrow.BinaryTypes.String},},nil)funcinsertOrders(ctxcontext.Context,sparksql.SparkSession,tablestring,rows[]Order){b:=array.NewRecordBuilder(memory.NewGoAllocator(),orderArrowSchema)deferb.Release()for_,r:=rangerows{ts,err:=arrow.TimestampFromTime(r.OrderTS,arrow.Microsecond)iferr!=nil{log.Fatalf("timestamp: %v",err)}b.Field(0).(*array.Int64Builder).Append(r.OrderID)b.Field(1).(*array.Int64Builder).Append(r.CustomerID)b.Field(2).(*array.TimestampBuilder).Append(ts)amt,err:=decimal128.FromString(fmt.Sprintf("%.2f",r.Amount),10,2)iferr!=nil{log.Fatalf("amount: %v",err)}b.Field(3).(*array.Decimal128Builder).Append(amt)b.Field(4).(*array.StringBuilder).Append(r.Status)b.Field(5).(*array.StringBuilder).Append(r.Country)}rec:=b.NewRecord()deferrec.Release()tbl:=array.NewTableFromRecords(orderArrowSchema,[]arrow.Record{rec})defertbl.Release()df,err:=spark.CreateDataFrameFromArrow(ctx,tbl)iferr!=nil{log.Fatalf("createDataFrameFromArrow: %v",err)}iferr:=df.WriteTo(table).Append(ctx);err!=nil{log.Fatalf("append: %v",err)}}// in main():insertOrders(ctx,spark,"hms.db.orders",[]Order{{1005,504,time.Date(2026,1,17,10,0,0,0,time.UTC),89.00,"COMPLETED","US"},{1006,501,time.Date(2026,1,17,11,30,0,0,time.UTC),12.00,"CANCELLED","US"},})
INSERT INTO ... SELECT, the way a real ETL step reads from one table into another
The shape nearly every production write actually takes: INSERT INTO … SELECT from a real relation, not literal VALUES. Rows arrive as something Spark can read in parallel across executors — a table, a path, a Kafka batch — and the interesting logic lives in the SELECT where the planner can push it down. Here the source is a tiny staging table, and the dedup is a distributed anti-join rather than row-at-a-time client code:
run(ctx,spark,`
CREATE TABLE hms.db.orders_staging (
order_id BIGINT, customer_id BIGINT, order_ts TIMESTAMP,
amount DECIMAL(10,2), status STRING, country STRING
) USING iceberg`)run(ctx,spark,`
INSERT INTO hms.db.orders_staging VALUES
(1007, 505, TIMESTAMP '2026-01-18 06:15:00', 42.00, 'COMPLETED', 'DE')`)run(ctx,spark,`
INSERT INTO hms.db.orders
SELECT * FROM hms.db.orders_staging
WHERE order_id NOT IN (SELECT order_id FROM hms.db.orders)`)
Seven rows in (four + two + one), across three separate commits, from three different code paths. That's already enough for the Partitioning and data skipping part's partitioning to have produced multiple partition directories — confirm it:
bash
$ find warehouse/db.db/orders/data -type d
warehouse/db.db/orders/data/order_ts_day=2026-01-15
warehouse/db.db/orders/data/order_ts_day=2026-01-16
warehouse/db.db/orders/data/order_ts_day=2026-01-17
warehouse/db.db/orders/data/order_ts_day=2026-01-18
Nobody wrote order_ts_day anywhere in the schema or the insert statements — Iceberg derived it from days(order_ts) at write time and only surfaces it as a physical directory name, exactly as the Partitioning and data skipping part described. The column your queries filter on (order_ts) and the column the filesystem is organized by (order_ts_day) are not the same identifier on purpose.