-
Notifications
You must be signed in to change notification settings - Fork 63
Add EXCLUDE
to partiql-eval
#1320
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bc681e
Add EXCLUDE to partiql-eval
alancai98 9e9a040
Move around top-level fns; make EXCLUDE input private
alancai98 50de032
Reduce repeated looping; additional test for EXCLUDE
alancai98 15b846a
[WIP] refactor EXCLUDE repr in plan; move subsumption to earlier plan…
alancai98 ea5baf5
Fix case-sensitive plan typing test
alancai98 3be4a5e
Address RelExclude comments; slight refactor of subsumption + testing…
alancai98 596a429
Remove unnecessary fold in RelExclude
alancai98 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
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
199 changes: 199 additions & 0 deletions
199
partiql-eval/src/main/kotlin/org/partiql/eval/internal/operator/rel/RelExclude.kt
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 |
---|---|---|
@@ -0,0 +1,199 @@ | ||
package org.partiql.eval.internal.operator.rel | ||
|
||
import org.partiql.eval.internal.Record | ||
import org.partiql.eval.internal.operator.Operator | ||
import org.partiql.plan.Rel | ||
import org.partiql.plan.relOpExcludeTypeCollIndex | ||
import org.partiql.plan.relOpExcludeTypeCollWildcard | ||
import org.partiql.plan.relOpExcludeTypeStructKey | ||
import org.partiql.plan.relOpExcludeTypeStructSymbol | ||
import org.partiql.plan.relOpExcludeTypeStructWildcard | ||
import org.partiql.value.BagValue | ||
import org.partiql.value.CollectionValue | ||
import org.partiql.value.ListValue | ||
import org.partiql.value.PartiQLValue | ||
import org.partiql.value.PartiQLValueExperimental | ||
import org.partiql.value.PartiQLValueType | ||
import org.partiql.value.SexpValue | ||
import org.partiql.value.StructValue | ||
import org.partiql.value.bagValue | ||
import org.partiql.value.listValue | ||
import org.partiql.value.sexpValue | ||
import org.partiql.value.structValue | ||
|
||
internal class RelExclude( | ||
private val input: Operator.Relation, | ||
private val exclusions: List<Rel.Op.Exclude.Path> | ||
) : Operator.Relation { | ||
|
||
override fun open() { | ||
input.open() | ||
} | ||
|
||
override fun next(): Record? { | ||
val record = input.next() ?: return null | ||
return exclusions.fold(record) { rec, path -> exclude(rec, path) } | ||
} | ||
|
||
override fun close() { | ||
input.close() | ||
} | ||
|
||
@OptIn(PartiQLValueExperimental::class) | ||
private fun exclude( | ||
record: Record, | ||
path: Rel.Op.Exclude.Path | ||
): Record { | ||
val values = record.values | ||
val value = values.getOrNull(path.root.ref) | ||
val newValues = if (value != null) { | ||
values[path.root.ref] = exclude(value, path.steps) | ||
values | ||
} else { | ||
values | ||
} | ||
return Record(newValues) | ||
} | ||
|
||
@OptIn(PartiQLValueExperimental::class) | ||
private fun exclude( | ||
structValue: StructValue<*>, | ||
exclusions: List<Rel.Op.Exclude.Step> | ||
): PartiQLValue { | ||
val structSymbolsToRemove = mutableSetOf<String>() | ||
val structKeysToRemove = mutableSetOf<String>() // keys stored as lowercase strings | ||
val branches = mutableMapOf<Rel.Op.Exclude.Type, List<Rel.Op.Exclude.Step>>() | ||
exclusions.forEach { exclusion -> | ||
when (exclusion.substeps.isEmpty()) { | ||
true -> { | ||
when (val leafType = exclusion.type) { | ||
is Rel.Op.Exclude.Type.StructWildcard -> { | ||
// struct wildcard at current level. return empty struct | ||
return structValue<PartiQLValue>() | ||
} | ||
is Rel.Op.Exclude.Type.StructSymbol -> structSymbolsToRemove.add(leafType.symbol) | ||
is Rel.Op.Exclude.Type.StructKey -> structKeysToRemove.add(leafType.key.lowercase()) | ||
else -> { /* coll step; do nothing */ } | ||
} | ||
} | ||
false -> { | ||
when (exclusion.type) { | ||
is Rel.Op.Exclude.Type.StructWildcard, is Rel.Op.Exclude.Type.StructSymbol, is Rel.Op.Exclude.Type.StructKey -> branches[exclusion.type] = | ||
exclusion.substeps | ||
else -> { /* coll step; do nothing */ } | ||
} | ||
} | ||
} | ||
} | ||
val finalStruct = structValue.entries.mapNotNull { structField -> | ||
if (structSymbolsToRemove.contains(structField.first) || structKeysToRemove.contains(structField.first.lowercase())) { | ||
// struct attr is to be removed at current level | ||
null | ||
} else { | ||
// deeper level exclusions | ||
val name = structField.first | ||
var value = structField.second | ||
// apply struct key exclusions at deeper levels | ||
val structKey = relOpExcludeTypeStructKey(name) | ||
branches[structKey]?.let { | ||
value = exclude(value, it) | ||
} | ||
// apply struct symbol exclusions at deeper levels | ||
val structSymbol = relOpExcludeTypeStructSymbol(name) | ||
branches[structSymbol]?.let { | ||
value = exclude(value, it) | ||
} | ||
// apply struct wildcard exclusions at deeper levels | ||
val structWildcard = relOpExcludeTypeStructWildcard() | ||
branches[structWildcard]?.let { | ||
value = exclude(value, it) | ||
} | ||
Pair(name, value) | ||
} | ||
} | ||
return structValue(finalStruct) | ||
} | ||
|
||
/** | ||
* Returns a [PartiQLValue] created from an iterable of [coll]. Requires [type] to be a collection type | ||
* (i.e. [PartiQLValueType.LIST], [PartiQLValueType.BAG], or [PartiQLValueType.SEXP]). | ||
*/ | ||
@OptIn(PartiQLValueExperimental::class) | ||
private fun newCollValue(type: PartiQLValueType, coll: Iterable<PartiQLValue>): PartiQLValue { | ||
return when (type) { | ||
PartiQLValueType.LIST -> listValue(coll) | ||
PartiQLValueType.BAG -> bagValue(coll) | ||
PartiQLValueType.SEXP -> sexpValue(coll) | ||
else -> error("Collection type required") | ||
} | ||
} | ||
|
||
@OptIn(PartiQLValueExperimental::class) | ||
private fun exclude( | ||
coll: CollectionValue<*>, | ||
type: PartiQLValueType, | ||
exclusions: List<Rel.Op.Exclude.Step> | ||
): PartiQLValue { | ||
val indexesToRemove = mutableSetOf<Int>() | ||
val branches = mutableMapOf<Rel.Op.Exclude.Type, List<Rel.Op.Exclude.Step>>() | ||
exclusions.forEach { exclusion -> | ||
when (exclusion.substeps.isEmpty()) { | ||
true -> { | ||
when (val leafType = exclusion.type) { | ||
is Rel.Op.Exclude.Type.CollWildcard -> { | ||
// collection wildcard at current level. return empty collection | ||
return newCollValue(type, emptyList()) | ||
} | ||
is Rel.Op.Exclude.Type.CollIndex -> { | ||
indexesToRemove.add(leafType.index) | ||
} | ||
else -> { /* struct step; do nothing */ } | ||
} | ||
} | ||
false -> { | ||
when (exclusion.type) { | ||
is Rel.Op.Exclude.Type.CollWildcard, is Rel.Op.Exclude.Type.CollIndex -> branches[exclusion.type] = | ||
exclusion.substeps | ||
else -> { /* struct step; do nothing */ } | ||
} | ||
} | ||
} | ||
} | ||
val finalColl = coll.mapIndexedNotNull { index, element -> | ||
if (indexesToRemove.contains(index)) { | ||
// coll index is to be removed at current level | ||
null | ||
} else { | ||
// deeper level exclusions | ||
var value = element | ||
if (coll is ListValue || coll is SexpValue) { | ||
// apply collection index exclusions at deeper levels for lists and sexps | ||
val collIndex = relOpExcludeTypeCollIndex(index) | ||
branches[collIndex]?.let { | ||
value = exclude(element, it) | ||
} | ||
} | ||
// apply collection wildcard exclusions at deeper levels for lists, bags, and sexps | ||
val collWildcard = relOpExcludeTypeCollWildcard() | ||
branches[collWildcard]?.let { | ||
value = exclude(value, it) | ||
} | ||
value | ||
} | ||
} | ||
return newCollValue(type, finalColl) | ||
} | ||
|
||
@OptIn(PartiQLValueExperimental::class) | ||
private fun exclude(initialPartiQLValue: PartiQLValue, exclusions: List<Rel.Op.Exclude.Step>): PartiQLValue { | ||
return when (initialPartiQLValue) { | ||
is StructValue<*> -> exclude(initialPartiQLValue, exclusions) | ||
is BagValue<*> -> exclude(initialPartiQLValue, PartiQLValueType.BAG, exclusions) | ||
is ListValue<*> -> exclude(initialPartiQLValue, PartiQLValueType.LIST, exclusions) | ||
is SexpValue<*> -> exclude(initialPartiQLValue, PartiQLValueType.SEXP, exclusions) | ||
else -> { | ||
initialPartiQLValue | ||
} | ||
} | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.