Mapepire-IBMi / mapepire-server

Server-side support for Code for IBM i
GNU General Public License v3.0
24 stars 8 forks source link

Response JSON and duplicate column names #66

Closed onlysumitg closed 1 month ago

onlysumitg commented 1 month ago

Based on my previous experience with converting database result sets to JSON:

I encountered an issue where duplicate column names in the query would lead to the first column's value being overwritten by the second one in the final JSON output.

There are several scenarios where a query might produce duplicate column names, such as:

Self-joins Joins between tables that have columns with the same names Duplicate aliases etc

In the code snippet:

BlockRetrievableRequest.DataBlockFetchResult() ==> mapRowData.put(column, cellDataForResponse);

When duplicate column names are present, the second occurrence overwrites the value in the map from the first put operation.

I encountered this problem while building a JDBC-based SQL client. To resolve it, I prefixed the column names in the 'data' array with their index to ensure uniqueness. (And also included this prefixed value as part of the 'columns' array. So the client can map it)

This observation is based solely on reviewing the code, and I haven't tested it on a running instance of the Mapepire server. If this issue has already been fixed, that's great!

Here is a sample JSON produced using my code. `

[
    {
        "Heading": "Result",
        "CurrentSql": {
            "ID": "d4a6ad79-f3a8-4e09-9795-36ec75273be8",
            "Sql": "select * from  t3 join t4  on t3.name = t4.name",
            "RunningNow": "select * from t3 join t4  on t3.name = t4.name",
            "StatementType": "SELECT",
            "LoadMore": false,
            "ScrollTo": 1,
            "ResultSetSize": 10,
            "LimitRecods": true,
            "Heading": "",
            "Error": "",
            "SessionID": "J070eQR5NXPSZw9bdBaYwlBHtRv1i07ne5cbvPdXWhM_eaa18a50-66ab-4bd5-9883-e6f22341c8b0"
        },
        "Rows": [
            {
                "0_T3.NAME": "SUMIT     ",
                "1_T3.DEPT": "D1        ",
                "2_T4.NAME": "SUMIT     ",
                "3_T4.ADDR": "ADDRESS1  "
            }
        ],
        "Columns": [
            {
                "IndexName": "0_T3.NAME",
                "Name": "T3.NAME",
                "HasNullable": false,
                "HasLength": false,
                "HasPrecisionScale": false,
                "Nullable": false,
                "Length": 0,
                "DatabaseType": "",
                "Precision": 0,
                "Scale": 0,
                "IsLink": false
            },
            {
                "IndexName": "1_T3.DEPT",
                "Name": "T3.DEPT",
                "HasNullable": false,
                "HasLength": false,
                "HasPrecisionScale": false,
                "Nullable": false,
                "Length": 0,
                "DatabaseType": "",
                "Precision": 0,
                "Scale": 0,
                "IsLink": false
            },
            {
                "IndexName": "2_T4.NAME",
                "Name": "T4.NAME",
                "HasNullable": false,
                "HasLength": false,
                "HasPrecisionScale": false,
                "Nullable": false,
                "Length": 0,
                "DatabaseType": "",
                "Precision": 0,
                "Scale": 0,
                "IsLink": false
            },
            {
                "IndexName": "3_T4.ADDR",
                "Name": "T4.ADDR",
                "HasNullable": false,
                "HasLength": false,
                "HasPrecisionScale": false,
                "Nullable": false,
                "Length": 0,
                "DatabaseType": "",
                "Precision": 0,
                "Scale": 0,
                "IsLink": false
            }
        ],
        "FlashMessage": "",
        "ErrorMessage": ""
    }
]

`

worksofliam commented 1 month ago

Thanks for the note! I will see about writing a test case tomorrow to check. Nonetheless, since this is not really an issue as you mentioned, i will close this for now.

onlysumitg commented 1 month ago

Finally, I was able to load the server.

"column_count": 4,

    "data": [
        {
            "NAME": "SUMIT",
            "DEPT": "D1",
            "ADDR": "ADDRESS1"
        }
    ],
{
    "id": "query3",
    "has_results": "True",
    "update_count": -1,
    "metadata": {
        "column_count": 4,
        "job": "***************************",
        "columns": [
            {
                "name": "NAME",
                "type": "CHAR",
                "display_size": 10,
                "label": "NAME"
            },
            {
                "name": "DEPT",
                "type": "CHAR",
                "display_size": 10,
                "label": "DEPT"
            },
            {
                "name": "NAME",
                "type": "CHAR",
                "display_size": 10,
                "label": "NAME"
            },
            {
                "name": "ADDR",
                "type": "CHAR",
                "display_size": 10,
                "label": "ADDR"
            }
        ]
    },
    "data": [
        {
            "NAME": "SUMIT",
            "DEPT": "D1",
            "ADDR": "ADDRESS1"
        }
    ],
    "is_done": "True",
    "success": "True"
}