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

[Task]: Update on conflict clause BBE after fixing #38991 #4062

Open
rdulmina opened this issue Dec 8, 2022 · 0 comments
Open

[Task]: Update on conflict clause BBE after fixing #38991 #4062

rdulmina opened this issue Dec 8, 2022 · 0 comments

Comments

@rdulmina
Copy link
Contributor

rdulmina commented Dec 8, 2022

Description

Due to ballerina-platform/ballerina-lang#38991 usage of frame variables inside on conflict clause has been removed from the on conflict clause BBE. Need to update it with the below sample after fixing ballerina-platform/ballerina-lang#38991.

import ballerina/io;

type Student record {|
    readonly int id;
    string name;
    int score;
|};

public function main() {
    table<Student> students = table [
            {id: 1, name: "John", score: 100},
            {id: 2, name: "Jane", score: 150},
            {id: 1, name: "John", score: 200}
        ];

    // Result of the following will be an error since key `John` is duplicated
    // and `isSafeReplace()` function returns error for the name `John`
    map<int>|error studentScores = map from var {name, score} in students
        select [name, score]
        on conflict isSafeReplace(name);

    io:println(studentScores);

    // Result of the following will be an error since key `1` is duplicated
    // and `isSafeReplace()` function returns error for the name `John`
    table<Student> key(id)|error studentScoresTable = table key(id) from var student in students
        select student
        on conflict isSafeReplace(student.name);

    io:println(studentScoresTable);

    table<Student> students2 = table [
            {id: 1, name: "John", score: 100},
            {id: 2, name: "Mike", score: 150},
            {id: 2, name: "Mike", score: 200}
        ];

    // Value `100` of the key `Mike` will be replaced by the second occurrence value `200`
    // since `isSafeReplace()` function returns nill for the name 'Mike`
    map<int>|error lastRoundScore = map from var student in students2
        select [student.name, student.score]
        on conflict isSafeReplace(student.name);

    io:println(lastRoundScore);

    // Return value will be just the construct type if the `on conflict` clause
    // always returns `()`
    map<int> lastRoundScore2 = map from var student in students2
        select [student.name, student.score]
        on conflict ();

    io:println(lastRoundScore2);

}

function isSafeReplace(string name) returns error? {
    if name == "John" {
        return error("Key Conflict", message = "record with same key exists.");
    }
}

Describe your task(s)

No response

Related area

-> Website

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@rdulmina rdulmina changed the title [Task]: [Task]: Update on conflict clause BBE after fixing #38991 Dec 8, 2022
@ballerina-bot ballerina-bot transferred this issue from ballerina-platform/ballerina-lang Dec 8, 2022
@praneesha praneesha assigned praneesha and rdulmina and unassigned praneesha Dec 9, 2022
@anupama-pathirage anupama-pathirage transferred this issue from ballerina-platform/ballerina-dev-website Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants