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

[Bug]: Data conversion using foreach seems to cause data loss #41819

Closed
janihur opened this issue Dec 4, 2023 · 1 comment · Fixed by #41722
Closed

[Bug]: Data conversion using foreach seems to cause data loss #41819

janihur opened this issue Dec 4, 2023 · 1 comment · Fixed by #41722
Assignees
Labels
Reason/EngineeringMistake The issue occurred due to a mistake made in the past. Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug userCategory/Compilation
Milestone

Comments

@janihur
Copy link

janihur commented Dec 4, 2023

Description

I'm converting data from list of records to table of records with foreach and add(). When printing the source and target data structures it looks like data is lost during the conversion. E.g:

Source data:

{"number":739,"coords":[{"x":0,"y":41},{"x":1,"y":41},{"x":2,"y":41}]}

Actual target data:

{"coord":{"x":0,"y":41},"number":739}
{"coord":{"x":1,"y":41},"number":739}

Expected target data:

{"coord":{"x":0,"y":41},"number":739}
{"coord":{"x":1,"y":41},"number":739}
{"coord":{"x":2,"y":41},"number":739}

This is just one example. You'll see other examples when checking "y":41.
The compiler reports no errors/warnings.

Steps to Reproduce

$ bal run main2.bal > output
Compiling source
        main2.bal

Running executable

$ ack 739 output
{"number":739,"coords":[{"x":0,"y":41},{"x":1,"y":41},{"x":2,"y":41}]}
processing 739
processing 739 coordinate: {"x":0,"y":41}
processing 739 coordinate: {"x":1,"y":41}
processing 739 coordinate: {"x":2,"y":41}
{"coord":{"x":0,"y":41},"number":739}
{"coord":{"x":1,"y":41},"number":739}

The conversion code:

public function main() {
    EngineSchematic engineSchematic = data();

    foreach PartNumber partNumber in engineSchematic.partNumbers {
        io:println(partNumber);
    }

    // 739 is just one example of lost data
    // e.g. check also other coordinates where y = 41
    table<PartNumberTable> key(coord) partNumbers = table key(coord) [];
    foreach PartNumber partNumber in engineSchematic.partNumbers {
        if partNumber.number == 739 {
            io:println("processing 739");
        }
        foreach Coord coord in partNumber.coords {
            if partNumber.number == 739 {
                io:println("processing 739 coordinate: ", coord);
            }
            partNumbers.add({
                coord: coord.cloneReadOnly(),
                number: partNumber.number
            });
        }
    }

    foreach PartNumberTable partNumber in partNumbers {
        io:println(partNumber);
    }
}

The complete source code and output attached.
bug.zip

Affected Version(s)

$ bal version
Ballerina 2201.8.3 (Swan Lake Update 8)
Language specification 2023R1
Update Tool 1.4.2

OS, DB, other environment details and versions

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:        22.04
Codename:       jammy

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@ballerina-bot ballerina-bot added needTriage The issue has to be inspected and labeled manually userCategory/Compilation labels Dec 4, 2023
@MaryamZi MaryamZi added Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime and removed needTriage The issue has to be inspected and labeled manually labels Dec 4, 2023
@nipunayf
Copy link
Contributor

nipunayf commented Dec 4, 2023

The above issue is produced since the hash-collided keys are not handled in the TableIterator, and the bug is fixed with: #41722

@nipunayf nipunayf self-assigned this Dec 4, 2023
@nipunayf nipunayf added the Reason/EngineeringMistake The issue occurred due to a mistake made in the past. label Dec 10, 2023
@nipunayf nipunayf added this to the 2201.9.0 milestone Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Reason/EngineeringMistake The issue occurred due to a mistake made in the past. Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug userCategory/Compilation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants