Skip to content

Commit

Permalink
CBL-6370: Query parser fails if we combine indexed unnest with uninde…
Browse files Browse the repository at this point in the history
…xed unnest (#2172)
  • Loading branch information
jianminzhao authored Oct 24, 2024
1 parent 00a637a commit ead2f9a
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions LiteCore/Query/QueryParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)); }
Expand Down

0 comments on commit ead2f9a

Please sign in to comment.