@@ -711,3 +711,64 @@ OFFSET 3 LIMIT 2;
711711
712712statement ok
713713drop table ordered_table;
714+
715+ # Test issue: https://github.com/apache/datafusion/issues/14204
716+ # Test limit pushdown with subquery
717+ statement ok
718+ create table testSubQueryLimit (a int, b int) as values (1,2), (2,3), (3,4);
719+
720+ query IIII
721+ select * from testSubQueryLimit as t1 join (select * from testSubQueryLimit limit 1) limit 10;
722+ ----
723+ 1 2 1 2
724+ 2 3 1 2
725+ 3 4 1 2
726+
727+ query TT
728+ explain select * from testSubQueryLimit as t1 join (select * from testSubQueryLimit limit 1) limit 10;
729+ ----
730+ logical_plan
731+ 01)Limit: skip=0, fetch=10
732+ 02)--Cross Join:
733+ 03)----SubqueryAlias: t1
734+ 04)------Limit: skip=0, fetch=10
735+ 05)--------TableScan: testsubquerylimit projection=[a, b], fetch=10
736+ 06)----Limit: skip=0, fetch=1
737+ 07)------TableScan: testsubquerylimit projection=[a, b], fetch=1
738+ physical_plan
739+ 01)ProjectionExec: expr=[a@2 as a, b@3 as b, a@0 as a, b@1 as b]
740+ 02)--GlobalLimitExec: skip=0, fetch=10
741+ 03)----CrossJoinExec
742+ 04)------GlobalLimitExec: skip=0, fetch=1
743+ 05)--------MemoryExec: partitions=1, partition_sizes=[1]
744+ 06)------GlobalLimitExec: skip=0, fetch=10
745+ 07)--------MemoryExec: partitions=1, partition_sizes=[1]
746+
747+
748+ query IIII
749+ select * from testSubQueryLimit as t1 join (select * from testSubQueryLimit limit 10) limit 2;
750+ ----
751+ 1 2 1 2
752+ 1 2 2 3
753+
754+ query TT
755+ explain select * from testSubQueryLimit as t1 join (select * from testSubQueryLimit limit 10) limit 2;
756+ ----
757+ logical_plan
758+ 01)Limit: skip=0, fetch=2
759+ 02)--Cross Join:
760+ 03)----SubqueryAlias: t1
761+ 04)------Limit: skip=0, fetch=2
762+ 05)--------TableScan: testsubquerylimit projection=[a, b], fetch=2
763+ 06)----Limit: skip=0, fetch=2
764+ 07)------TableScan: testsubquerylimit projection=[a, b], fetch=2
765+ physical_plan
766+ 01)GlobalLimitExec: skip=0, fetch=2
767+ 02)--CrossJoinExec
768+ 03)----GlobalLimitExec: skip=0, fetch=2
769+ 04)------MemoryExec: partitions=1, partition_sizes=[1]
770+ 05)----GlobalLimitExec: skip=0, fetch=2
771+ 06)------MemoryExec: partitions=1, partition_sizes=[1]
772+
773+ statement ok
774+ drop table testSubQueryLimit;
0 commit comments