vmware-archive / sql-to-dbsp-compiler

Compiler translating SQL view definitions into DBSP circuits (https://github.com/vmware/database-stream-processor)
Other
28 stars 5 forks source link

Generates jit code with invalid binary operand types #135

Closed Kixiron closed 1 year ago

Kixiron commented 1 year ago

This sql

CREATE TABLE bid (
    auction BIGINT,
    bidder BIGINT,
    price BIGINT,
    channel VARCHAR,
    url VARCHAR,
    dateTime TIMESTAMP(3),
    extra VARCHAR
);

CREATE VIEW q2 AS
SELECT
    auction,
    bidder,
    CAST(0.908 AS FLOAT) * price as price, -- convert dollar to euro
    dateTime,
    extra
FROM bid;

Produces this json

{
    "nodes": {
        "90": {
            "Source": {
                "layout": 1
            }
        },
        "104": {
            "Map": {
                "input": 90,
                "map_fn": {
                    "args": [
                        {
                            "id": 1,
                            "layout": 1,
                            "flags": "input"
                        },
                        {
                            "id": 2,
                            "layout": 2,
                            "flags": "output"
                        }
                    ],
                    "ret": "Unit",
                    "entry_block": 1,
                    "blocks": {
                        "1": {
                            "id": 1,
                            "body": [
                                [
                                    3,
                                    {
                                        "Load": {
                                            "source": 1,
                                            "source_layout": 3,
                                            "column": 0,
                                            "column_type": "I64"
                                        }
                                    }
                                ],
                                [
                                    4,
                                    {
                                        "IsNull": {
                                            "target": 1,
                                            "target_layout": 3,
                                            "column": 0
                                        }
                                    }
                                ],
                                [
                                    5,
                                    {
                                        "Store": {
                                            "target": 2,
                                            "target_layout": 2,
                                            "column": 0,
                                            "value": {
                                                "Expr": 3
                                            },
                                            "value_type": "I64"
                                        }
                                    }
                                ],
                                [
                                    6,
                                    {
                                        "SetNull": {
                                            "target": 2,
                                            "target_layout": 2,
                                            "column": 0,
                                            "is_null": {
                                                "Expr": 4
                                            }
                                        }
                                    }
                                ],
                                [
                                    7,
                                    {
                                        "Load": {
                                            "source": 1,
                                            "source_layout": 3,
                                            "column": 1,
                                            "column_type": "I64"
                                        }
                                    }
                                ],
                                [
                                    8,
                                    {
                                        "IsNull": {
                                            "target": 1,
                                            "target_layout": 3,
                                            "column": 1
                                        }
                                    }
                                ],
                                [
                                    9,
                                    {
                                        "Store": {
                                            "target": 2,
                                            "target_layout": 2,
                                            "column": 1,
                                            "value": {
                                                "Expr": 7
                                            },
                                            "value_type": "I64"
                                        }
                                    }
                                ],
                                [
                                    10,
                                    {
                                        "SetNull": {
                                            "target": 2,
                                            "target_layout": 2,
                                            "column": 1,
                                            "is_null": {
                                                "Expr": 8
                                            }
                                        }
                                    }
                                ],
                                [
                                    11,
                                    {
                                        "Constant": {
                                            "F64": 0.908
                                        }
                                    }
                                ],
                                [
                                    12,
                                    {
                                        "Load": {
                                            "source": 1,
                                            "source_layout": 3,
                                            "column": 2,
                                            "column_type": "I64"
                                        }
                                    }
                                ],
                                [
                                    13,
                                    {
                                        "IsNull": {
                                            "target": 1,
                                            "target_layout": 3,
                                            "column": 2
                                        }
                                    }
                                ],
                                [
                                    14,
                                    {
                                        "Cast": {
                                            "value": 12,
                                            "from": "I64",
                                            "to": "F32"
                                        }
                                    }
                                ],
                                [
                                    15,
                                    {
                                        "CopyVal": {
                                            "value": 13,
                                            "value_ty": "Bool"
                                        }
                                    }
                                ],
                                [
                                    16,
                                    {
                                        "Constant": {
                                            "Bool": false
                                        }
                                    }
                                ],
                                [
                                    17,
                                    {
                                        "BinOp": {
                                            "lhs": 11,
                                            "rhs": 14,
                                            "kind": "Mul",
                                            "operand_ty": "F32"
                                        }
                                    }
                                ],
                                [
                                    18,
                                    {
                                        "BinOp": {
                                            "lhs": 16,
                                            "rhs": 15,
                                            "kind": "Or",
                                            "operand_ty": "Bool"
                                        }
                                    }
                                ],
                                [
                                    19,
                                    {
                                        "Store": {
                                            "target": 2,
                                            "target_layout": 2,
                                            "column": 2,
                                            "value": {
                                                "Expr": 17
                                            },
                                            "value_type": "F32"
                                        }
                                    }
                                ],
                                [
                                    20,
                                    {
                                        "SetNull": {
                                            "target": 2,
                                            "target_layout": 2,
                                            "column": 2,
                                            "is_null": {
                                                "Expr": 18
                                            }
                                        }
                                    }
                                ],
                                [
                                    21,
                                    {
                                        "Load": {
                                            "source": 1,
                                            "source_layout": 3,
                                            "column": 5,
                                            "column_type": "Timestamp"
                                        }
                                    }
                                ],
                                [
                                    22,
                                    {
                                        "IsNull": {
                                            "target": 1,
                                            "target_layout": 3,
                                            "column": 5
                                        }
                                    }
                                ],
                                [
                                    23,
                                    {
                                        "Store": {
                                            "target": 2,
                                            "target_layout": 2,
                                            "column": 3,
                                            "value": {
                                                "Expr": 21
                                            },
                                            "value_type": "Timestamp"
                                        }
                                    }
                                ],
                                [
                                    24,
                                    {
                                        "SetNull": {
                                            "target": 2,
                                            "target_layout": 2,
                                            "column": 3,
                                            "is_null": {
                                                "Expr": 22
                                            }
                                        }
                                    }
                                ],
                                [
                                    25,
                                    {
                                        "Load": {
                                            "source": 1,
                                            "source_layout": 3,
                                            "column": 6,
                                            "column_type": "String"
                                        }
                                    }
                                ],
                                [
                                    26,
                                    {
                                        "IsNull": {
                                            "target": 1,
                                            "target_layout": 3,
                                            "column": 6
                                        }
                                    }
                                ],
                                [
                                    27,
                                    {
                                        "Store": {
                                            "target": 2,
                                            "target_layout": 2,
                                            "column": 4,
                                            "value": {
                                                "Expr": 25
                                            },
                                            "value_type": "String"
                                        }
                                    }
                                ],
                                [
                                    28,
                                    {
                                        "SetNull": {
                                            "target": 2,
                                            "target_layout": 2,
                                            "column": 4,
                                            "is_null": {
                                                "Expr": 26
                                            }
                                        }
                                    }
                                ]
                            ],
                            "terminator": {
                                "Return": {
                                    "value": {
                                        "Imm": "Unit"
                                    }
                                }
                            }
                        }
                    }
                },
                "layout": 2
            }
        },
        "106": {
            "Sink": {
                "input": 104
            }
        }
    },
    "layouts": {
        "2": {
            "columns": [
                {
                    "nullable": true,
                    "ty": "I64"
                },
                {
                    "nullable": true,
                    "ty": "I64"
                },
                {
                    "nullable": true,
                    "ty": "F32"
                },
                {
                    "nullable": true,
                    "ty": "Timestamp"
                },
                {
                    "nullable": true,
                    "ty": "String"
                }
            ]
        },
        "3": {
            "columns": [
                {
                    "nullable": true,
                    "ty": "I64"
                },
                {
                    "nullable": true,
                    "ty": "I64"
                },
                {
                    "nullable": true,
                    "ty": "I64"
                },
                {
                    "nullable": true,
                    "ty": "String"
                },
                {
                    "nullable": true,
                    "ty": "String"
                },
                {
                    "nullable": true,
                    "ty": "Timestamp"
                },
                {
                    "nullable": true,
                    "ty": "String"
                }
            ]
        },
        "1": {
            "columns": [
                {
                    "nullable": true,
                    "ty": "I64"
                },
                {
                    "nullable": true,
                    "ty": "I64"
                },
                {
                    "nullable": true,
                    "ty": "I64"
                },
                {
                    "nullable": true,
                    "ty": "String"
                },
                {
                    "nullable": true,
                    "ty": "String"
                },
                {
                    "nullable": true,
                    "ty": "Timestamp"
                },
                {
                    "nullable": true,
                    "ty": "String"
                }
            ]
        }
    }
}

Which has this binary op in it

[
    17,
    {
        "BinOp": {
            "lhs": 11,
            "rhs": 14,
            "kind": "Mul",
            "operand_ty": "F32"
        }
    }
],

For which the lhs is a f32 but the rhs is a f64, generating invalid code

Kixiron commented 1 year ago

It seems the problem is that the float literal gets made a f64 regardless of the cast or anything else

mihaibudiu commented 1 year ago

The bug is here:

public boolean preorder(DBSPFloatLiteral expression) {
        return this.createJsonLiteral(expression, "F64",  //  <<<< should be F32
                expression.value == null ? null : new FloatNode(expression.value));
    }
mihaibudiu commented 1 year ago

The next PR for the jit branch will contain the fix, but trying to bundle some other stuff too.

mihaibudiu commented 1 year ago

I think this is fixed.