From 18e06d2e6722622969a596dbb3be9c20180aafa4 Mon Sep 17 00:00:00 2001 From: kangpinghuang <40422952+kangpinghuang@users.noreply.github.com> Date: Thu, 16 May 2019 17:29:43 +0800 Subject: [PATCH] Fix position seek bug for varchar short key (#1167) --- be/src/olap/field.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/be/src/olap/field.h b/be/src/olap/field.h index 1688a328e5dcb1..1b421efc4e8127 100644 --- a/be/src/olap/field.h +++ b/be/src/olap/field.h @@ -154,11 +154,24 @@ inline int Field::index_cmp(char* left, char* right) const { Slice* l_slice = reinterpret_cast(left + 1); Slice* r_slice = reinterpret_cast(right + 1); - if (r_slice->size + OLAP_STRING_MAX_BYTES > _index_size) { + if (r_slice->size + OLAP_STRING_MAX_BYTES > _index_size + || l_slice->size + OLAP_STRING_MAX_BYTES > _index_size) { // 如果field的实际长度比short key长,则仅比较前缀,确保相同short key的所有block都被扫描, // 否则,可以直接比较short key和field int compare_size = _index_size - OLAP_STRING_MAX_BYTES; + // l_slice size and r_slice size may be less than compare_size + // so calculate the min of the three size as new compare_size + compare_size = std::min(std::min(compare_size, (int)l_slice->size), (int)r_slice->size); res = strncmp(l_slice->data, r_slice->data, compare_size); + if (res == 0) { + if (l_slice->size < r_slice->size) { + res = -1; + } else if (l_slice->size > r_slice->size) { + res = 1; + } else { + res = 0; + } + } } else { res = l_slice->compare(*r_slice); }