|
| 1 | +package org.partiql.eval.internal.operator.rel |
| 2 | + |
| 3 | +import org.partiql.eval.internal.Record |
| 4 | +import org.partiql.eval.internal.operator.Operator |
| 5 | +import org.partiql.plan.Rel |
| 6 | +import org.partiql.plan.relOpExcludeTypeCollIndex |
| 7 | +import org.partiql.plan.relOpExcludeTypeCollWildcard |
| 8 | +import org.partiql.plan.relOpExcludeTypeStructKey |
| 9 | +import org.partiql.plan.relOpExcludeTypeStructSymbol |
| 10 | +import org.partiql.plan.relOpExcludeTypeStructWildcard |
| 11 | +import org.partiql.value.BagValue |
| 12 | +import org.partiql.value.CollectionValue |
| 13 | +import org.partiql.value.ListValue |
| 14 | +import org.partiql.value.PartiQLValue |
| 15 | +import org.partiql.value.PartiQLValueExperimental |
| 16 | +import org.partiql.value.PartiQLValueType |
| 17 | +import org.partiql.value.SexpValue |
| 18 | +import org.partiql.value.StructValue |
| 19 | +import org.partiql.value.bagValue |
| 20 | +import org.partiql.value.listValue |
| 21 | +import org.partiql.value.sexpValue |
| 22 | +import org.partiql.value.structValue |
| 23 | + |
| 24 | +internal class RelExclude( |
| 25 | + private val input: Operator.Relation, |
| 26 | + private val exclusions: List<Rel.Op.Exclude.Path> |
| 27 | +) : Operator.Relation { |
| 28 | + |
| 29 | + override fun open() { |
| 30 | + input.open() |
| 31 | + } |
| 32 | + |
| 33 | + @OptIn(PartiQLValueExperimental::class) |
| 34 | + override fun next(): Record? { |
| 35 | + val record = input.next() ?: return null |
| 36 | + exclusions.forEach { path -> |
| 37 | + val root = path.root.ref |
| 38 | + val value = record.values[root] |
| 39 | + record.values[root] = exclude(value, path.steps) |
| 40 | + } |
| 41 | + return record |
| 42 | + } |
| 43 | + |
| 44 | + override fun close() { |
| 45 | + input.close() |
| 46 | + } |
| 47 | + |
| 48 | + @OptIn(PartiQLValueExperimental::class) |
| 49 | + private fun exclude( |
| 50 | + structValue: StructValue<*>, |
| 51 | + exclusions: List<Rel.Op.Exclude.Step> |
| 52 | + ): PartiQLValue { |
| 53 | + val structSymbolsToRemove = mutableSetOf<String>() |
| 54 | + val structKeysToRemove = mutableSetOf<String>() // keys stored as lowercase strings |
| 55 | + val branches = mutableMapOf<Rel.Op.Exclude.Type, List<Rel.Op.Exclude.Step>>() |
| 56 | + exclusions.forEach { exclusion -> |
| 57 | + when (exclusion.substeps.isEmpty()) { |
| 58 | + true -> { |
| 59 | + when (val leafType = exclusion.type) { |
| 60 | + is Rel.Op.Exclude.Type.StructWildcard -> { |
| 61 | + // struct wildcard at current level. return empty struct |
| 62 | + return structValue<PartiQLValue>() |
| 63 | + } |
| 64 | + is Rel.Op.Exclude.Type.StructSymbol -> structSymbolsToRemove.add(leafType.symbol) |
| 65 | + is Rel.Op.Exclude.Type.StructKey -> structKeysToRemove.add(leafType.key.lowercase()) |
| 66 | + else -> { /* coll step; do nothing */ } |
| 67 | + } |
| 68 | + } |
| 69 | + false -> { |
| 70 | + when (exclusion.type) { |
| 71 | + is Rel.Op.Exclude.Type.StructWildcard, is Rel.Op.Exclude.Type.StructSymbol, is Rel.Op.Exclude.Type.StructKey -> branches[exclusion.type] = |
| 72 | + exclusion.substeps |
| 73 | + else -> { /* coll step; do nothing */ } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + val finalStruct = structValue.entries.mapNotNull { structField -> |
| 79 | + if (structSymbolsToRemove.contains(structField.first) || structKeysToRemove.contains(structField.first.lowercase())) { |
| 80 | + // struct attr is to be removed at current level |
| 81 | + null |
| 82 | + } else { |
| 83 | + // deeper level exclusions |
| 84 | + val name = structField.first |
| 85 | + var value = structField.second |
| 86 | + // apply struct key exclusions at deeper levels |
| 87 | + val structKey = relOpExcludeTypeStructKey(name) |
| 88 | + branches[structKey]?.let { |
| 89 | + value = exclude(value, it) |
| 90 | + } |
| 91 | + // apply struct symbol exclusions at deeper levels |
| 92 | + val structSymbol = relOpExcludeTypeStructSymbol(name) |
| 93 | + branches[structSymbol]?.let { |
| 94 | + value = exclude(value, it) |
| 95 | + } |
| 96 | + // apply struct wildcard exclusions at deeper levels |
| 97 | + val structWildcard = relOpExcludeTypeStructWildcard() |
| 98 | + branches[structWildcard]?.let { |
| 99 | + value = exclude(value, it) |
| 100 | + } |
| 101 | + Pair(name, value) |
| 102 | + } |
| 103 | + } |
| 104 | + return structValue(finalStruct) |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns a [PartiQLValue] created from an iterable of [coll]. Requires [type] to be a collection type |
| 109 | + * (i.e. [PartiQLValueType.LIST], [PartiQLValueType.BAG], or [PartiQLValueType.SEXP]). |
| 110 | + */ |
| 111 | + @OptIn(PartiQLValueExperimental::class) |
| 112 | + private fun newCollValue(type: PartiQLValueType, coll: Iterable<PartiQLValue>): PartiQLValue { |
| 113 | + return when (type) { |
| 114 | + PartiQLValueType.LIST -> listValue(coll) |
| 115 | + PartiQLValueType.BAG -> bagValue(coll) |
| 116 | + PartiQLValueType.SEXP -> sexpValue(coll) |
| 117 | + else -> error("Collection type required") |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @OptIn(PartiQLValueExperimental::class) |
| 122 | + private fun exclude( |
| 123 | + coll: CollectionValue<*>, |
| 124 | + type: PartiQLValueType, |
| 125 | + exclusions: List<Rel.Op.Exclude.Step> |
| 126 | + ): PartiQLValue { |
| 127 | + val indexesToRemove = mutableSetOf<Int>() |
| 128 | + val branches = mutableMapOf<Rel.Op.Exclude.Type, List<Rel.Op.Exclude.Step>>() |
| 129 | + exclusions.forEach { exclusion -> |
| 130 | + when (exclusion.substeps.isEmpty()) { |
| 131 | + true -> { |
| 132 | + when (val leafType = exclusion.type) { |
| 133 | + is Rel.Op.Exclude.Type.CollWildcard -> { |
| 134 | + // collection wildcard at current level. return empty collection |
| 135 | + return newCollValue(type, emptyList()) |
| 136 | + } |
| 137 | + is Rel.Op.Exclude.Type.CollIndex -> { |
| 138 | + indexesToRemove.add(leafType.index) |
| 139 | + } |
| 140 | + else -> { /* struct step; do nothing */ } |
| 141 | + } |
| 142 | + } |
| 143 | + false -> { |
| 144 | + when (exclusion.type) { |
| 145 | + is Rel.Op.Exclude.Type.CollWildcard, is Rel.Op.Exclude.Type.CollIndex -> branches[exclusion.type] = |
| 146 | + exclusion.substeps |
| 147 | + else -> { /* struct step; do nothing */ } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + val finalColl = coll.mapIndexedNotNull { index, element -> |
| 153 | + if (indexesToRemove.contains(index)) { |
| 154 | + // coll index is to be removed at current level |
| 155 | + null |
| 156 | + } else { |
| 157 | + // deeper level exclusions |
| 158 | + var value = element |
| 159 | + if (coll is ListValue || coll is SexpValue) { |
| 160 | + // apply collection index exclusions at deeper levels for lists and sexps |
| 161 | + val collIndex = relOpExcludeTypeCollIndex(index) |
| 162 | + branches[collIndex]?.let { |
| 163 | + value = exclude(element, it) |
| 164 | + } |
| 165 | + } |
| 166 | + // apply collection wildcard exclusions at deeper levels for lists, bags, and sexps |
| 167 | + val collWildcard = relOpExcludeTypeCollWildcard() |
| 168 | + branches[collWildcard]?.let { |
| 169 | + value = exclude(value, it) |
| 170 | + } |
| 171 | + value |
| 172 | + } |
| 173 | + } |
| 174 | + return newCollValue(type, finalColl) |
| 175 | + } |
| 176 | + |
| 177 | + @OptIn(PartiQLValueExperimental::class) |
| 178 | + private fun exclude(initialPartiQLValue: PartiQLValue, exclusions: List<Rel.Op.Exclude.Step>): PartiQLValue { |
| 179 | + return when (initialPartiQLValue) { |
| 180 | + is StructValue<*> -> exclude(initialPartiQLValue, exclusions) |
| 181 | + is BagValue<*> -> exclude(initialPartiQLValue, PartiQLValueType.BAG, exclusions) |
| 182 | + is ListValue<*> -> exclude(initialPartiQLValue, PartiQLValueType.LIST, exclusions) |
| 183 | + is SexpValue<*> -> exclude(initialPartiQLValue, PartiQLValueType.SEXP, exclusions) |
| 184 | + else -> { |
| 185 | + initialPartiQLValue |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments