Skip to content

Commit ca7cdb8

Browse files
authored
feat: Allow homogeneous data types to be configured (#1533)
For OpenSearch (and presumably ElasticSearch) arrays (even in JSON objects) are limited to the same data types. This adds a configuration option to the writer test suite to only use the same data type in the JSON example data - allowing the test suite to be used for the OpenSearch plugin. Currently JSON objects in the ElasticSearch and OpenSearch plugins are mapped to strings.
1 parent 4b475bb commit ca7cdb8

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

plugin/testing_write.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ type WriterTestSuite struct {
2424
// Destination setups that don't support nulls in lists should set this to true.
2525
ignoreNullsInLists bool
2626

27+
// useHomogeneousTypes use the same type for all items within an array - useful for destinations that don't support mixed types.
28+
useHomogeneousTypes bool
29+
2730
// genDataOptions define how to generate test data and which data types to skip
2831
genDatOptions schema.TestSourceOptions
2932

@@ -92,6 +95,12 @@ func WithRandomSeed(seed int64) func(o *WriterTestSuite) {
9295
}
9396
}
9497

98+
func WithHomogeneousTypes() func(o *WriterTestSuite) {
99+
return func(o *WriterTestSuite) {
100+
o.useHomogeneousTypes = true
101+
}
102+
}
103+
95104
func TestWriterSuiteRunner(t *testing.T, p *Plugin, tests WriterTestSuiteTests, opts ...func(o *WriterTestSuite)) {
96105
suite := &WriterTestSuite{
97106
tests: tests,

plugin/testing_write_delete.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ func (s *WriterTestSuite) testDeleteStaleAll(ctx context.Context, t *testing.T)
9393

9494
tg := schema.NewTestDataGenerator(0)
9595
normalRecord := tg.Generate(table, schema.GenTestDataOptions{
96-
MaxRows: rowsPerRecord,
97-
TimePrecision: s.genDatOptions.TimePrecision,
98-
SourceName: "test",
99-
SyncTime: syncTime, // Generate call may truncate the value further based on the options
96+
MaxRows: rowsPerRecord,
97+
TimePrecision: s.genDatOptions.TimePrecision,
98+
SourceName: "test",
99+
SyncTime: syncTime, // Generate call may truncate the value further based on the options
100+
UseHomogeneousType: s.useHomogeneousTypes,
100101
})
101102
r.NoErrorf(s.plugin.writeOne(ctx, &message.WriteInsert{Record: normalRecord}), "failed to insert record")
102103
normalRecord = s.handleNulls(normalRecord) // we process nulls after writing
@@ -118,11 +119,12 @@ func (s *WriterTestSuite) testDeleteStaleAll(ctx context.Context, t *testing.T)
118119
// https://github.com/golang/go/issues/41087
119120
syncTime = time.Now().UTC().Truncate(time.Microsecond)
120121
nullRecord := tg.Generate(table, schema.GenTestDataOptions{
121-
MaxRows: rowsPerRecord,
122-
TimePrecision: s.genDatOptions.TimePrecision,
123-
NullRows: true,
124-
SourceName: "test",
125-
SyncTime: syncTime, // Generate call may truncate the value further based on the options
122+
MaxRows: rowsPerRecord,
123+
TimePrecision: s.genDatOptions.TimePrecision,
124+
NullRows: true,
125+
SourceName: "test",
126+
SyncTime: syncTime, // Generate call may truncate the value further based on the options
127+
UseHomogeneousType: s.useHomogeneousTypes,
126128
})
127129
r.NoErrorf(s.plugin.writeOne(ctx, &message.WriteInsert{Record: nullRecord}), "failed to insert record second time")
128130
nullRecord = s.handleNulls(nullRecord) // we process nulls after writing

plugin/testing_write_insert.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ func (s *WriterTestSuite) testInsertAll(ctx context.Context) error {
9191
}
9292
tg := schema.NewTestDataGenerator(0)
9393
normalRecord := tg.Generate(table, schema.GenTestDataOptions{
94-
MaxRows: rowsPerRecord,
95-
TimePrecision: s.genDatOptions.TimePrecision,
94+
MaxRows: rowsPerRecord,
95+
TimePrecision: s.genDatOptions.TimePrecision,
96+
UseHomogeneousType: s.useHomogeneousTypes,
9697
})
9798
if err := s.plugin.writeOne(ctx, &message.WriteInsert{
9899
Record: normalRecord,

plugin/testing_write_migrate.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ func (s *WriterTestSuite) migrate(ctx context.Context, target *schema.Table, sou
3838
sourceName := target.Name
3939
syncTime := time.Now().UTC().Round(1 * time.Second)
4040
opts := schema.GenTestDataOptions{
41-
SourceName: sourceName,
42-
SyncTime: syncTime,
43-
MaxRows: rowsPerRecord,
44-
TimePrecision: s.genDatOptions.TimePrecision,
41+
SourceName: sourceName,
42+
SyncTime: syncTime,
43+
MaxRows: rowsPerRecord,
44+
TimePrecision: s.genDatOptions.TimePrecision,
45+
UseHomogeneousType: s.useHomogeneousTypes,
4546
}
4647
// Test Generator should be initialized with the current number of items in the destination
4748
// this allows us to have multi-pass tests that ensure the migrations are stable

plugin/testing_write_upsert.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ func (s *WriterTestSuite) testUpsertAll(ctx context.Context) error {
8080

8181
tg := schema.NewTestDataGenerator(0)
8282
normalRecord := tg.Generate(table, schema.GenTestDataOptions{
83-
MaxRows: rowsPerRecord,
84-
TimePrecision: s.genDatOptions.TimePrecision,
83+
MaxRows: rowsPerRecord,
84+
TimePrecision: s.genDatOptions.TimePrecision,
85+
UseHomogeneousType: s.useHomogeneousTypes,
8586
})
8687
if err := s.plugin.writeOne(ctx, &message.WriteInsert{
8788
Record: normalRecord,

schema/testdata.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ type GenTestDataOptions struct {
196196
TimePrecision time.Duration
197197
// NullRows indicates whether to generate rows with all null values.
198198
NullRows bool
199+
// UseHomogeneousType indicates whether to use a single type for JSON arrays.
200+
UseHomogeneousType bool
199201
}
200202

201203
type TestDataGenerator struct {
@@ -297,8 +299,14 @@ func (tg TestDataGenerator) getExampleJSON(colName string, dataType arrow.DataTy
297299
}
298300
if arrow.TypeEqual(dataType, types.ExtensionTypes.JSON) {
299301
if strings.HasSuffix(colName, "_array") {
302+
if opts.UseHomogeneousType {
303+
return `[{"test1":"test1"},{"test2":"test2"},{"test3":"test3"}]`
304+
}
300305
return `[{"test":"test"},123,{"test_number":456}]`
301306
}
307+
if opts.UseHomogeneousType {
308+
return `{"test":["a", "b", "c"]}`
309+
}
302310
return `{"test":["a","b",3]}`
303311
}
304312
if arrow.TypeEqual(dataType, types.ExtensionTypes.Inet) {

0 commit comments

Comments
 (0)