Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1524221 handle nested arrays #1176

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,16 @@ func buildStructuredArray(ctx context.Context, fieldMetadata fieldMetadata, srcV
return buildStructuredArrayRecursive[[]byte](ctx, fieldMetadata.Fields[0], srcValue, params)
case "date", "time", "timestamp_ltz", "timestamp_ntz", "timestamp_tz":
return buildStructuredArrayRecursive[time.Time](ctx, fieldMetadata.Fields[0], srcValue, params)
case "array":
arr := make([]any, len(srcValue))
for i, v := range srcValue {
structuredArray, err := buildStructuredArray(ctx, fieldMetadata.Fields[0], v.([]any), params)
if err != nil {
return nil, err
}
arr[i] = structuredArray
}
return arr, nil
}
}
return srcValue, nil
Expand Down
24 changes: 24 additions & 0 deletions converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,30 @@ func TestStringToValue(t *testing.T) {
} else if ts.UnixNano() != 1549491451123456789 {
t.Errorf("expected unix timestamp: 1549491451123456789, got %v", ts.UnixNano())
}

rowType = &execResponseRowType{Type: "array", Fields: []fieldMetadata{{Type: "array", Fields: []fieldMetadata{{Type: "fixed"}}}}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a E2E test as well (in structured_types_read.go)? How does it relate to TestArrayOfArrays?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, added a case in TestArrayOfArrays for a 3-dimensional array. Currently native arrow fails it, I'm working on that as well

src = "[[3]]"
if err = stringToValue(context.Background(), &dest, *rowType, &src, nil, nil); err != nil {
t.Errorf("unexpected error: %v", err)
} else if arr, ok := dest.([][]int64); !ok {
t.Errorf("expected type: '[][]int64', got '%v'", reflect.TypeOf(dest))
} else if arr[0][0] != 3 {
t.Errorf("expected value: 3, got '%v'", arr[0][0])
}

rowType = &execResponseRowType{Type: "array", Fields: []fieldMetadata{{Type: "array", Fields: []fieldMetadata{{Type: "array", Fields: []fieldMetadata{{Type: "array", Fields: []fieldMetadata{{Type: "fixed"}}}}}}}}}
src = "[[[[3]]]]"
if err = stringToValue(context.Background(), &dest, *rowType, &src, nil, nil); err != nil {
t.Errorf("unexpected error: %v", err)
} else if arr, ok := dest.([]any); !ok {
t.Errorf("expected type: '[]any', got '%v'", reflect.TypeOf(dest))
} else if arr1, ok := arr[0].([]any); !ok {
t.Errorf("expected type: '[]any', got '%v'", reflect.TypeOf(arr[0]))
} else if arr2, ok := arr1[0].([][]int64); !ok {
t.Errorf("expected type: '[][]int64', got '%v'", reflect.TypeOf(arr1[0]))
} else if arr2[0][0] != 3 {
t.Errorf("expected value: 3, got '%v'", arr2[0][0])
}
}

type tcArrayToString struct {
Expand Down
6 changes: 6 additions & 0 deletions structured_type_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,12 @@ func TestArrayOfArrays(t *testing.T) {
actual: make([][]time.Time, 2),
expected: [][]time.Time{{time.Date(2024, time.January, 5, 11, 22, 33, 0, warsawTz)}, {time.Date(2001, time.November, 12, 11, 22, 33, 0, warsawTz)}},
},
{
name: "3D_bool",
query: "SELECT ARRAY_CONSTRUCT(ARRAY_CONSTRUCT(ARRAY_CONSTRUCT(true, false), ARRAY_CONSTRUCT(false)), ARRAY_CONSTRUCT(ARRAY_CONSTRUCT(false, true), ARRAY_CONSTRUCT(true, true)))::ARRAY(ARRAY(ARRAY(BOOLEAN)))",
actual: make([][][]bool, 2),
expected: []any{[][]bool{{true, false}, {false}}, [][]bool{{false, true}, {true, true}}},
},
}
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec("ALTER SESSION SET TIMEZONE = 'Europe/Warsaw'")
Expand Down
Loading