ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
135 stars 56 forks source link

[Bug]: Unable to identify error in PostgreSQL DB query results #6132

Open chalindukodikara opened 4 months ago

chalindukodikara commented 4 months ago

Description

public isolated function searchKind() returns error? {

    int port = 8080;

    sql:ParameterizedQuery kindSearchQuery =
            `SELECT * FROM component WHERE (kind #> Array['spec', 'build', 'buildpack', 'port']) @> ${port}`;
    stream<ComponentKindResultStream, sql:Error?> componentKindResultStream = dbClient->query(kindSearchQuery);
    if componentKindResultStream is stream<ComponentKindResultStream> {
        ComponentKindResultStream[] componentKinds = from var x in componentKindResultStream
            let var idValue = x.id, var kindValue = x.kind
            group by idValue, kindValue
            select {
                id: idValue,
                kind: kindValue
            };
        io:println(componentKinds);
    }

}

dbClient->query produces an error but if condition is true. It should not go inside the if condition since there is an error.

Steps to Reproduce

DB is PostgreSQL. 2 columns

Affected Version(s)

No response

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

MaryamZi commented 4 months ago

This works as expected with a stream implementor, so could be an issue with how the stream is created or the value/type creator.

import ballerina/sql;
import ballerina/io;

type ComponentKindResultStream record {
    string id;
    string kind;
};

class StreamImplementor {
    public isolated function next() returns record {|ComponentKindResultStream value;|}|sql:Error {
        return error sql:Error("this is error");
    }
}

public function main() {
    stream<ComponentKindResultStream, sql:Error?> componentKindResultStream = new (new StreamImplementor()); 
    if componentKindResultStream is stream<ComponentKindResultStream> {
        io:println("Is stream<ComponentKindResultStream>");
        ComponentKindResultStream[] _ = from var x in componentKindResultStream
            let var idValue = x.id, var kindValue = x.kind
            group by idValue, kindValue
            select {
                id: idValue,
                kind: kindValue
            };
    } else {
        io:println("Not stream<ComponentKindResultStream>");
    }
}

Prints "Not stream\".