From d43a94991083d175c5519a657b3d3d065d887b75 Mon Sep 17 00:00:00 2001 From: Jianmin Zhao Date: Wed, 23 Oct 2024 20:08:50 -0700 Subject: [PATCH] CBL-6370: Query parser fails if we combine indexed unnest with unindexed unnest Handle the case where fl_each following unnest table. --- LiteCore/Query/QueryParser.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/LiteCore/Query/QueryParser.cc b/LiteCore/Query/QueryParser.cc index a9b244025..c882aeaba 100644 --- a/LiteCore/Query/QueryParser.cc +++ b/LiteCore/Query/QueryParser.cc @@ -1788,13 +1788,7 @@ namespace litecore { if ( !isInColumnList ) { fail("%s", verifyDbAliasError.c_str()); } } - bool isNestedUnnest = fn == kEachFnName && type == kUnnestVirtualTableAlias; - // ex. of nested unnest: "FROM: [{as: 'doc'}, {as: 'student', unnest: ['.doc.students']}, - // {as: 'interest', unnest: ['.student.interests']}]" - // Target SQL: "FROM kv_default AS doc JOIN fl_each(doc.body, 'students') AS student - // JOIN fl_each(student.value, 'interests') AS interest - - if ( !isNestedUnnest && verifyDbAliasError.empty() && type >= kUnnestVirtualTableAlias ) { + if ( fn == kValueFnName && verifyDbAliasError.empty() && type >= kUnnestVirtualTableAlias ) { // The alias is to an UNNEST, but not inside fl_each. This needs to be written specially: // The following function will reject any fn's but kValueFnName writeUnnestPropertyGetter(fn, property, alias, type); @@ -1871,6 +1865,12 @@ namespace litecore { // It's more efficent to get the doc root with fl_root than with fl_value: if ( property.empty() && fn == kValueFnName ) fn = kRootFnName; + bool isNestedUnnest = fn == kEachFnName && type == kUnnestVirtualTableAlias; + // ex. of nested unnest: "FROM: [{as: 'doc'}, {as: 'student', unnest: ['.doc.students']}, + // {as: 'interest', unnest: ['.student.interests']}]" + // Target SQL: "FROM kv_default AS doc JOIN fl_each(doc.body, 'students') AS student + // JOIN fl_each(student.value, 'interests') AS interest + // Write the function call: _sql << fn << "(" << tablePrefix << (isNestedUnnest ? "value" : _bodyColumnName); if ( !property.empty() ) { _sql << ", " << sqlString(string(property)); }