Kevin Wang
02/07/2025, 2:41 AMRobert Howell
02/07/2025, 3:00 AMRobert Howell
02/07/2025, 3:06 AMpostgres=# CREATE SCHEMA example;
postgres=# SET search_path TO example;
SET
postgres=# CREATE TABLE df1 ( key text, value INT );
CREATE TABLE
postgres=# CREATE TABLE df2 ( key text, value INT );
CREATE TABLE
postgres=# INSERT INTO df1 VALUES ('A',1),('A',2),('B',1);
INSERT 0 3
postgres=# INSERT INTO df2 VALUES ('A',1),('A',1),('B',1);
INSERT 0 3
I switched the query to join on keys and compare on value.
postgres=# SELECT * FROM df1 WHERE value > (SELECT max(value) FROM df2 WHERE df1.key = df2.key);
key | value
-----+-------
A | 2
(1 row)
postgres=# EXPLAIN SELECT * FROM df1 WHERE value > (SELECT max(value) FROM df2 WHERE df1.key = df2.key);
QUERY PLAN
--------------------------------------------------------------------------
Seq Scan on example.df1 (cost=0.00..32918.88 rows=423 width=36)
Output: df1.key, df1.value
Filter: (df1.value > (SubPlan 1))
SubPlan 1
-> Aggregate (cost=25.89..25.90 rows=1 width=4)
Output: max(df2.value)
-> Seq Scan on example.df2 (cost=0.00..25.88 rows=6 width=4)
Output: df2.key, df2.value
Filter: (df1.key = df2.key)Robert Howell
02/07/2025, 3:22 AMKevin Wang
02/07/2025, 4:29 AMsubquery first before building the full query.
The issue with this is we don't currently have a mechanism for retrieving the logical plan for df1 when building subquery , which means we can't get things like the datatype of df1["value"] during build-time.Kevin Wang
02/07/2025, 4:31 AMdf1["value"] .
The thing is we also have these anonymous col("x") expressions we can also create, which we cannot resolve without the input plan. So essentially we will need to bind tables to columns in multiple places:
1. when getting a column directly from a dataframe like in df["x"]
2. when planning SQL identifiers tbl.x
3. in the builder, for anonymous Python DataFrame columns col("x")Kevin Wang
02/07/2025, 4:34 AMRobert Howell
02/07/2025, 4:49 AMKevin Wang
02/07/2025, 4:55 AMKevin Wang
02/07/2025, 4:57 AMcol("df.x")?Robert Howell
02/07/2025, 4:59 AMKevin Wang
02/07/2025, 5:00 AMRobert Howell
02/07/2025, 5:00 AMKevin Wang
02/07/2025, 5:02 AMKevin Wang
02/07/2025, 5:11 AM