Skip to content

Commit a32b9a3

Browse files
committed
Bug#34852090: Incorrect result with VALUES in a correlated LATERAL subquery
Bug#35087820: VALUES Statement with dependent subquery is wrong When a table value constructor has to be read multiple times during a query, it will only return data the first time it is read. When it's read the second time, it will return EOF immediately. It is caused by a bug in TableValueConstructorIterator::Read() where it returns EOF immediately when the number of examined rows is equal to the number of rows in the table value constructor. Since this counter is intentionally not reset when the iterator is re-initialized, the table value constructor will keep returning EOF forever once every row has been read, and not start again from the beginning when it's re-initialized. Fixed by checking the position of the iterator over the row value list instead of the number of examined rows. Change-Id: I0e828eb0de0360a16cea9f77b578c68205cefbaf
1 parent aa48ffc commit a32b9a3

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

mysql-test/r/table_value_constructor.result

+27
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,30 @@ x
721721
2
722722
3
723723
DROP TABLE t;
724+
#
725+
# Bug#35087820: VALUES Statement with dependent subquery is wrong
726+
#
727+
CREATE TABLE t(
728+
id INTEGER PRIMARY KEY,
729+
a VARCHAR(4),
730+
b VARCHAR(4),
731+
c VARCHAR(3));
732+
INSERT INTO t VALUES (1, 'a1', 'b1', 'c1'), (2, 'a2', 'b2', 'c2');
733+
SELECT
734+
id,
735+
(SELECT MAX(col1) FROM (VALUES ROW(a), ROW(b), ROW(c)) AS x(col1)) AS max
736+
FROM t;
737+
id max
738+
1 c1
739+
2 c2
740+
DROP TABLE t;
741+
#
742+
# Bug#34852090: Incorrect result with VALUES in
743+
# a correlated LATERAL subquery
744+
#
745+
WITH v1(x) AS (VALUES ROW (1), ROW (2), ROW (3))
746+
SELECT * FROM v1, LATERAL (VALUES ROW(v1.x)) AS v2;
747+
x column_0
748+
1 1
749+
2 2
750+
3 3

mysql-test/t/table_value_constructor.test

+27
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,30 @@ let $query =
515515
--sorted_result
516516
SELECT * FROM t;
517517
DROP TABLE t;
518+
519+
--echo #
520+
--echo # Bug#35087820: VALUES Statement with dependent subquery is wrong
521+
--echo #
522+
523+
CREATE TABLE t(
524+
id INTEGER PRIMARY KEY,
525+
a VARCHAR(4),
526+
b VARCHAR(4),
527+
c VARCHAR(3));
528+
INSERT INTO t VALUES (1, 'a1', 'b1', 'c1'), (2, 'a2', 'b2', 'c2');
529+
530+
--sorted_result
531+
SELECT
532+
id,
533+
(SELECT MAX(col1) FROM (VALUES ROW(a), ROW(b), ROW(c)) AS x(col1)) AS max
534+
FROM t;
535+
536+
DROP TABLE t;
537+
538+
--echo #
539+
--echo # Bug#34852090: Incorrect result with VALUES in
540+
--echo # a correlated LATERAL subquery
541+
--echo #
542+
543+
WITH v1(x) AS (VALUES ROW (1), ROW (2), ROW (3))
544+
SELECT * FROM v1, LATERAL (VALUES ROW(v1.x)) AS v2;

sql/iterators/ref_row_iterators.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ bool TableValueConstructorIterator::Init() {
919919
}
920920

921921
int TableValueConstructorIterator::Read() {
922-
if (*m_examined_rows == m_row_value_list.size()) return -1;
922+
if (m_row_it == m_row_value_list.end()) return -1;
923923

924924
// If the TVC has a single row, we don't create Item_values_column reference
925925
// objects during resolving. We will instead use the single row directly from
@@ -937,9 +937,9 @@ int TableValueConstructorIterator::Read() {
937937
// Item_values_column object cannot be const.
938938
ref->set_value(const_cast<Item *>(value));
939939
}
940-
++m_row_it;
941940
}
942941

942+
++m_row_it;
943943
++*m_examined_rows;
944944
return 0;
945945
}

0 commit comments

Comments
 (0)