-
Notifications
You must be signed in to change notification settings - Fork 96
WIP:subquery exists #2932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
WIP:subquery exists #2932
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
hidden format and tests
- Loading branch information
commit 55aa81789d3d0fd00e1cb104d9c0c10342ae3eeb
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ import type { | |
AST, | ||
Condition, | ||
Conjunction, | ||
CorrelatedSubQuery, | ||
CorrelatedSubQueryCondition, | ||
Disjunction, | ||
LiteralValue, | ||
Ordering, | ||
|
@@ -14,6 +16,7 @@ import type { | |
} from '../../../zero-protocol/src/ast.js'; | ||
import type {Row} from '../../../zero-protocol/src/data.js'; | ||
import type {PrimaryKey} from '../../../zero-protocol/src/primary-key.js'; | ||
import {Exists} from '../ivm/exists.js'; | ||
import {FanIn} from '../ivm/fan-in.js'; | ||
import {FanOut} from '../ivm/fan-out.js'; | ||
import {Filter} from '../ivm/filter.js'; | ||
|
@@ -104,15 +107,25 @@ export function bindStaticParameters( | |
}; | ||
|
||
function bindCondition(condition: Condition): Condition { | ||
return condition.type === 'simple' | ||
? { | ||
...condition, | ||
value: bindValue(condition.value), | ||
} | ||
: { | ||
...condition, | ||
conditions: condition.conditions.map(bindCondition), | ||
}; | ||
if (condition.type === 'simple') { | ||
return { | ||
...condition, | ||
value: bindValue(condition.value), | ||
}; | ||
} | ||
if (condition.type === 'subquery') { | ||
return { | ||
...condition, | ||
related: { | ||
...condition.related, | ||
subquery: visit(condition.related.subquery), | ||
}, | ||
}; | ||
} | ||
return { | ||
...condition, | ||
conditions: condition.conditions.map(bindCondition), | ||
}; | ||
} | ||
|
||
const bindValue = (value: ValuePosition): LiteralValue => { | ||
|
@@ -157,8 +170,20 @@ function buildPipelineInternal( | |
end = new Skip(end, ast.start); | ||
} | ||
|
||
const correlatedSubQueryConditions = gatherCorrelatedSubQueryConditions( | ||
ast.where, | ||
); | ||
|
||
for (const csqc of correlatedSubQueryConditions) { | ||
let csq = csqc.related; | ||
if (csqc.condition.type === 'exists') { | ||
csq = {...csq, subquery: {...csq.subquery, limit: EXISTS_LIMIT}}; | ||
} | ||
end = applyCorrelatedSubQuery(csq, delegate, staticQueryParameters, end); | ||
} | ||
|
||
if (ast.where) { | ||
end = applyWhere(end, ast.where, appliedFilters); | ||
end = applyWhere(end, ast.where, appliedFilters, delegate); | ||
} | ||
|
||
if (ast.limit) { | ||
|
@@ -167,28 +192,38 @@ function buildPipelineInternal( | |
|
||
if (ast.related) { | ||
for (const sq of ast.related) { | ||
assert(sq.subquery.alias, 'Subquery must have an alias'); | ||
const child = buildPipelineInternal( | ||
sq.subquery, | ||
delegate, | ||
staticQueryParameters, | ||
sq.correlation.childField, | ||
); | ||
end = new Join({ | ||
parent: end, | ||
child, | ||
storage: delegate.createStorage(), | ||
parentKey: sq.correlation.parentField, | ||
childKey: sq.correlation.childField, | ||
relationshipName: sq.subquery.alias, | ||
hidden: sq.hidden ?? false, | ||
}); | ||
end = applyCorrelatedSubQuery(sq, delegate, staticQueryParameters, end); | ||
} | ||
} | ||
|
||
return end; | ||
} | ||
|
||
function applyCorrelatedSubQuery( | ||
sq: CorrelatedSubQuery, | ||
delegate: BuilderDelegate, | ||
staticQueryParameters: StaticQueryParameters | undefined, | ||
end: Input, | ||
) { | ||
assert(sq.subquery.alias, 'Subquery must have an alias'); | ||
const child = buildPipelineInternal( | ||
sq.subquery, | ||
delegate, | ||
staticQueryParameters, | ||
sq.correlation.childField, | ||
); | ||
end = new Join({ | ||
parent: end, | ||
child, | ||
storage: delegate.createStorage(), | ||
parentKey: sq.correlation.parentField, | ||
childKey: sq.correlation.childField, | ||
relationshipName: sq.subquery.alias, | ||
hidden: sq.hidden ?? false, | ||
}); | ||
return end; | ||
} | ||
|
||
function applyWhere( | ||
input: Input, | ||
condition: Condition, | ||
|
@@ -197,12 +232,15 @@ function applyWhere( | |
// Or we do the union of queries approach and retain this `appliedFilters` and `sourceConnect` behavior. | ||
// Downside of that being unbounded memory usage. | ||
appliedFilters: boolean, | ||
delegate: BuilderDelegate, | ||
): Input { | ||
switch (condition.type) { | ||
case 'and': | ||
return applyAnd(input, condition, appliedFilters); | ||
return applyAnd(input, condition, appliedFilters, delegate); | ||
case 'or': | ||
return applyOr(input, condition, appliedFilters); | ||
return applyOr(input, condition, appliedFilters, delegate); | ||
case 'subquery': | ||
return applyCorrelatedSubqueryCondition(input, condition, delegate); | ||
default: | ||
return applySimpleCondition(input, condition, appliedFilters); | ||
} | ||
|
@@ -212,9 +250,10 @@ function applyAnd( | |
input: Input, | ||
condition: Conjunction, | ||
appliedFilters: boolean, | ||
delegate: BuilderDelegate, | ||
) { | ||
for (const subCondition of condition.conditions) { | ||
input = applyWhere(input, subCondition, appliedFilters); | ||
input = applyWhere(input, subCondition, appliedFilters, delegate); | ||
} | ||
return input; | ||
} | ||
|
@@ -223,11 +262,12 @@ function applyOr( | |
input: Input, | ||
condition: Disjunction, | ||
appliedFilters: boolean, | ||
delegate: BuilderDelegate, | ||
): Input { | ||
const fanOut = new FanOut(input); | ||
const branches: Input[] = []; | ||
for (const subCondition of condition.conditions) { | ||
branches.push(applyWhere(fanOut, subCondition, appliedFilters)); | ||
branches.push(applyWhere(fanOut, subCondition, appliedFilters, delegate)); | ||
} | ||
assert(branches.length > 0, 'Or condition must have at least one branch'); | ||
return new FanIn(fanOut, branches as [Input, ...Input[]]); | ||
|
@@ -264,3 +304,37 @@ export function assertOrderingIncludesPK( | |
); | ||
} | ||
} | ||
function applyCorrelatedSubqueryCondition( | ||
input: Input, | ||
condition: CorrelatedSubQueryCondition, | ||
delegate: BuilderDelegate, | ||
): Input { | ||
assert(condition.condition.type === 'exists'); | ||
return new Exists( | ||
input, | ||
delegate.createStorage(), | ||
must(condition.related.subquery.alias), | ||
); | ||
} | ||
|
||
function gatherCorrelatedSubQueryConditions(condition: Condition | undefined) { | ||
const correlatedSubQueryConditions: CorrelatedSubQueryCondition[] = []; | ||
const gather = (condition: Condition) => { | ||
if (condition.type === 'subquery') { | ||
correlatedSubQueryConditions.push(condition); | ||
return; | ||
} | ||
if (condition.type === 'and' || condition.type === 'or') { | ||
for (const c of condition.conditions) { | ||
gather(c); | ||
} | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: seems like a random return. |
||
} | ||
}; | ||
if (condition) { | ||
gather(condition); | ||
} | ||
return correlatedSubQueryConditions; | ||
} | ||
|
||
const EXISTS_LIMIT = 5; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ export class ArrayView<V extends View> implements Output, TypedView<V> { | |
|
||
constructor( | ||
input: Input, | ||
format: Format = {singular: false, relationships: {}}, | ||
format: Format = {singular: false, hidden: false, relationships: {}}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yay! hidden should have been a format param. I think it predated format though. |
||
) { | ||
this.#input = input; | ||
this.#schema = input.getSchema(); | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
correlatedSubqueryConditionConditionSchema
or
correlatedSubqueryConditionOperatorSchema
for EXISTS and NOT EXISTS there are no other operands other than the subquery, but for other subquery conditions there may be additional operands.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be an
operator
. It is more symmetrical withSimpleCondition
that way.