go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.52k stars 118 forks source link

How can I compare Postgres tuples? #248

Closed BranislavLazic closed 1 year ago

BranislavLazic commented 1 year ago

I have a query like this:

SELECT * FROM table WHERE (x, y) < (0, 0);

How can I write the WHERE clause with Jet? WHERE x < 0 AND y < 0 is not the same.

Obviously (x, y) should be grouped into a BoolExpression and (0, 0) too.

go-jet commented 1 year ago

Hi @BranislavLazic ,

You can use ROW constructor to create a tuple, but unfortunately Row expression doesn't support comparison at the moment.

There are two workarounds for now: 1) Use SQL Builder Cast Wrapper to cast into Int or Float expression.

IntExp(ROW(Table.X, Table.Y)).LT(IntExp(ROW(Int(1), Int(2)))) 
// IntExp would not add a cast into SQL query

2) Use Raw expression

RawBool("(x, y) < (#1, #2)", RawArgs{"#1":10, "#2": 20})
BranislavLazic commented 1 year ago

@go-jet Thanks a lot! The first option looks suitable.