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][QueryExpression]:Looping a stream with error completion to create a concatenated xml fails with a type cast error #39641

Closed
pcnfernando opened this issue Feb 16, 2023 · 3 comments · Fixed by #40041
Assignees
Labels
Area/TypeChecker Type Checker related issues #Compiler Priority/High Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug

Comments

@pcnfernando
Copy link
Member

Description

import ballerina/io;

type Invoice record {|
    string Sony_Item_ID;
    string Item_Model_Number;
    string Serial_Number;
    int Quantity;
|};

public function main() returns error? {
    stream<Invoice, io:Error?> fileReadCsvAsStream = check io:fileReadCsvAsStream("./test.csv");

    xml x = check from var item in fileReadCsvAsStream
        select xml `<Invoice>
                            <Sony_Item_ID>${item.Sony_Item_ID}</Sony_Item_ID>
                            <Item_Model_Number>${item.Item_Model_Number}</Item_Model_Number>
                            <Serial_Number>${item.Serial_Number}</Serial_Number>
                            <Quantity>${item.Quantity}</Quantity>
                        </Invoice>`;
}

Fails with

error: {ballerina}TypeCastError {"message":"incompatible types: 'any[]' cannot be cast to 'error'"}
        at bbb:main(bbb.bal:36)

When type is inferred creates an xml:Element[]

import ballerina/io;

type Invoice record {|
    string Sony_Item_ID;
    string Item_Model_Number;
    string Serial_Number;
    int Quantity;
|};

public function main() returns error? {
    stream<Invoice, io:Error?> fileReadCsvAsStream = check io:fileReadCsvAsStream("./test.csv");

    var x = check from var item in fileReadCsvAsStream
        select xml `<Invoice>
                            <Sony_Item_ID>${item.Sony_Item_ID}</Sony_Item_ID>
                            <Item_Model_Number>${item.Item_Model_Number}</Item_Model_Number>
                            <Serial_Number>${item.Serial_Number}</Serial_Number>
                            <Quantity>${item.Quantity}</Quantity>
                        </Invoice>`;

   io:println(x);  
    // [`<Invoice>
    //     <Sony_Item_ID>P00000000000029401</Sony_Item_ID>
    //     <Item_Model_Number>SEL70200GM</Item_Model_Number>
    //     <Serial_Number>1817535</Serial_Number>
    //     <Quantity>1</Quantity>
    // </Invoice>`,`<Invoice>
    //     <Sony_Item_ID>P00000000000029504</Sony_Item_ID>
    //     <Item_Model_Number>SEL100400GM</Item_Model_Number>
    //     <Serial_Number>1816034</Serial_Number>
    //     <Quantity>1</Quantity>
    // </Invoice>`]
}

When used an array as the collection in the query expression, concatenated xml is created successfully

public function main() returns error? {
    Invoice[] arr = [{Sony_Item_ID: "P00000000000029401", Item_Model_Number: "SEL70200GM", Serial_Number: "1817535", Quantity: 1}, {Sony_Item_ID: "P00000000000029401", Item_Model_Number: "SEL70200GM", Serial_Number: "1817535", Quantity: 1}];

    xml x = from var item in arr
        select xml `<Invoice>
                            <Sony_Item_ID>${item.Sony_Item_ID}</Sony_Item_ID>
                            <Item_Model_Number>${item.Item_Model_Number}</Item_Model_Number>
                            <Serial_Number>${item.Serial_Number}</Serial_Number>
                            <Quantity>${item.Quantity}</Quantity>
                        </Invoice>`;

   io:println(x);  
    // <Invoice>
    //     <Sony_Item_ID>P00000000000029401</Sony_Item_ID>
    //     <Item_Model_Number>SEL70200GM</Item_Model_Number>
    //     <Serial_Number>1817535</Serial_Number>
    //     <Quantity>1</Quantity>
    // </Invoice>`,`<Invoice>
    //     <Sony_Item_ID>P00000000000029504</Sony_Item_ID>
    //     <Item_Model_Number>SEL100400GM</Item_Model_Number>
    //     <Serial_Number>1816034</Serial_Number>
    //     <Quantity>1</Quantity>
    // </Invoice>
}

Steps to Reproduce

No response

Affected Version(s)

2201.4.0

OS, DB, other environment details and versions

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@pcnfernando pcnfernando added Type/Bug Priority/High Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Area/TypeChecker Type Checker related issues #Compiler labels Feb 16, 2023
@SasinduDilshara
Copy link
Contributor

import ballerina/io;

type Invoice record {|
    string Sony_Item_ID;
    string Item_Model_Number;
    string Serial_Number;
    int Quantity;
|};

public function main() returns error? {
    error? err = trap foo();
    io:println(err);
}

 function foo() returns error? {
     stream<Invoice, io:Error?> fileReadCsvAsStream = check io:fileReadCsvAsStream("test.csv");

     xml|error x = from var item in fileReadCsvAsStream
         select xml `<Invoice>
                    </Invoice>`;

     io:println(x); //xml:Element[]
}

Prints an array of xml:Element values.

@SasinduDilshara
Copy link
Contributor

Same issue occured with

import ballerina/io;

type Invoice record {|
    string Sony_Item_ID;
    string Item_Model_Number;
    string Serial_Number;
    int Quantity;
|};
public function main() returns error? {
    stream<Invoice, error?> arr = [{Sony_Item_ID: "P00000000000029401", Item_Model_Number: "SEL70200GM", Serial_Number: "1817535", Quantity: 1}].toStream();

    xml x = check from var item in arr
        select xml `<Invoice>
                            <Sony_Item_ID>${item.Sony_Item_ID}</Sony_Item_ID>
                            <Item_Model_Number>${item.Item_Model_Number}</Item_Model_Number>
                            <Serial_Number>${item.Serial_Number}</Serial_Number>
                            <Quantity>${item.Quantity}</Quantity>
                        </Invoice>`;

   io:println(x); 
}

@github-actions
Copy link

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/TypeChecker Type Checker related issues #Compiler Priority/High Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug
Projects
Archived in project
2 participants