From 5d5c99ebee62c15d38c8f384d5ffc23f0cd3334c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vavru=C5=A1a?= Date: Thu, 4 Oct 2018 15:55:40 -0700 Subject: [PATCH] Formats/CapnProtoRowInputStream: fix column mismatch in list of structures The fields are lexicographically sorted to make traversal easier, but their order must be preserved when collecting fields from structures. For example, a list with a structure like `{b @0 :Text, a @1 :Text}` would read `a` first despite being second, which would cause a mismatch. --- dbms/src/Formats/CapnProtoRowInputStream.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dbms/src/Formats/CapnProtoRowInputStream.cpp b/dbms/src/Formats/CapnProtoRowInputStream.cpp index c30aa2c446a..710ad2f08ac 100644 --- a/dbms/src/Formats/CapnProtoRowInputStream.cpp +++ b/dbms/src/Formats/CapnProtoRowInputStream.cpp @@ -144,7 +144,12 @@ void CapnProtoRowInputStream::createActions(const NestedFieldList & sortedFields { // The field list hereĀ flattens Nested elements into multiple arrays // In order to map Nested types in Cap'nProto back, they need to be collected - actions.back().columns.push_back(field.pos); + // Since the field names are sorted, the order of field positions must be preserved + // For example, if the fields are { b @0 :Text, a @1 :Text }, the `a` would come first + // even though it's position is second. + auto & columns = actions.back().columns; + auto it = std::upper_bound(columns.cbegin(), columns.cend(), field.pos); + columns.insert(it, field.pos); } else {