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

Fix left-join for multiple matches #402

Merged
merged 1 commit into from
Nov 9, 2024
Merged
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
57 changes: 37 additions & 20 deletions evaldo/builtins_spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1605,15 +1605,13 @@ func LeftJoin(ps *env.ProgramState, s1 env.Spreadsheet, s2 env.Spreadsheet, col1
if err != nil {
return MakeError(ps, fmt.Sprintf("Couldn't retrieve value at row %d (%s)", i, err))
}
newRow := make([]any, len(combinedCols))

// the row id of the second spreadsheet that matches the current row
s2RowId := -1
// the row ids of the second spreadsheet which match the values in the current first spreadsheet row
var s2RowIds []int
// use index if available
if ix, ok := s2.Indexes[col2]; ok {
if rowIds, ok := ix[val1]; ok {
// if there are multiple rows with the same value (ie. joining on non-unique column), just use the first one
s2RowId = rowIds[0]
s2RowIds = rowIds
}
} else {
for j, row2 := range s2.GetRows() {
Expand All @@ -1626,39 +1624,58 @@ func LeftJoin(ps *env.ProgramState, s1 env.Spreadsheet, s2 env.Spreadsheet, col1
val2o, ok1 := val2.(env.Object)
if ok1 {
if val1o.Equal(val2o) {
s2RowId = j
break
s2RowIds = append(s2RowIds, j)
continue
}
} else {
if env.RyeToRaw(val1o, ps.Idx) == val2 {
s2RowId = j
break
s2RowIds = append(s2RowIds, j)
continue
}
}
}
val1s, ok := val1.(string)
if ok {
if val1s == val2.(string) {
s2RowId = j
break
s2RowIds = append(s2RowIds, j)
continue
}
}
}
}
if innerJoin && s2RowId == -1 {
if innerJoin && len(s2RowIds) == 0 {
continue
}
copy(newRow, row1.Values)
if s2RowId > -1 {
for i, v := range s2.GetRow(ps, s2RowId).Values {
newRow[i+len(s1.Cols)] = v
buildCombinedRow := func(row1Values []any, row2Values []any) []any {
newRow := make([]any, len(combinedCols))
copy(newRow, row1Values)

if row2Values != nil {
// copy values from second spreadsheet
for i, v := range row2Values {
newRow[i+len(s1.Cols)] = v
}
} else {
// fill with Void{} values when no match
for i := len(s1.Cols); i < len(combinedCols); i++ {
newRow[i] = env.Void{}
}
}
} else {
for k := range s2.Cols {
newRow[k+len(s1.Cols)] = env.Void{}
return newRow
}

if len(s2RowIds) > 0 {
// add a row for each matching record from s2
for _, s2RowId := range s2RowIds {
row2Values := s2.GetRow(ps, s2RowId).Values
combinedValues := buildCombinedRow(row1.Values, row2Values)
nspr.AddRow(*env.NewSpreadsheetRow(combinedValues, nspr))
}
} else {
// add a single row with Void values for s2 columns
combinedValues := buildCombinedRow(row1.Values, nil)
nspr.AddRow(*env.NewSpreadsheetRow(combinedValues, nspr))
}
nspr.AddRow(*env.NewSpreadsheetRow(newRow, nspr))
}
return *nspr
}
Expand Down
26 changes: 26 additions & 0 deletions tests/structures.rye
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,18 @@ section "Spreadsheet related functions"
houses .add-indexes! [ 'id ] ::houses ,
names .left-join houses 'id 'id
} spreadsheet { "id" "name" "id_2" "house" } { 1 "Paul" 1 "Atreides" 2 "Chani" _ _ 3 "Vladimir" 3 "Harkonnen" }

equal { a:: spreadsheet { "a" "b" } { 1 "one" 2 "two" } ,
c:: spreadsheet { "a" "c" } { 1 "ena" 1 "eden" 1 "eno" } ,
a .left-join c "a" "a"
} spreadsheet { "a" "b" "a_2" "c" } { 1 "one" 1 "ena" 1 "one" 1 "eden" 1 "one" 1 "eno" 2 "two" _ _ }

; joining with an index on the second spreadsheet
equal { a:: spreadsheet { "a" "b" } { 1 "one" 2 "two" } ,
c:: spreadsheet { "a" "c" } { 1 "ena" 1 "eden" 1 "eno" } ,
c .add-indexes! [ 'a ] ::c ,
a .left-join c "a" "a"
} spreadsheet { "a" "b" "a_2" "c" } { 1 "one" 1 "ena" 1 "one" 1 "eden" 1 "one" 1 "eno" 2 "two" _ _ }
}

group "inner join"
Expand All @@ -668,6 +680,20 @@ section "Spreadsheet related functions"
houses .add-indexes! [ 'id ] ::houses ,
names .inner-join houses 'id 'id
} spreadsheet { "id" "name" "id_2" "house" } { 1 "Paul" 1 "Atreides" 3 "Vladimir" 3 "Harkonnen" }


equal { a:: spreadsheet { "a" "b" } { 1 "one" 2 "two" } ,
c:: spreadsheet { "a" "c" } { 1 "ena" 1 "eden" 1 "eno" } ,
a .inner-join c "a" "a"
} spreadsheet { "a" "b" "a_2" "c" } { 1 "one" 1 "ena" 1 "one" 1 "eden" 1 "one" 1 "eno" }

; joining with an index on the second spreadsheet
equal { a:: spreadsheet { "a" "b" } { 1 "one" 2 "two" } ,
c:: spreadsheet { "a" "c" } { 1 "ena" 1 "eden" 1 "eno" } ,
c .add-indexes! [ 'a ] ::c ,
a .inner-join c "a" "a"
} spreadsheet { "a" "b" "a_2" "c" } { 1 "one" 1 "ena" 1 "one" 1 "eden" 1 "one" 1 "eno" }

}

group "group by"
Expand Down
Loading