|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.flint.spark.skipping.bloomfilter |
| 7 | + |
| 8 | +import java.io.ByteArrayInputStream |
| 9 | + |
| 10 | +import org.opensearch.flint.core.field.bloomfilter.classic.ClassicBloomFilter |
| 11 | + |
| 12 | +import org.apache.spark.sql.Column |
| 13 | +import org.apache.spark.sql.catalyst.InternalRow |
| 14 | +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult |
| 15 | +import org.apache.spark.sql.catalyst.expressions.{BinaryComparison, Expression} |
| 16 | +import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode} |
| 17 | +import org.apache.spark.sql.catalyst.expressions.codegen.Block.BlockHelper |
| 18 | +import org.apache.spark.sql.functions.{col, lit} |
| 19 | +import org.apache.spark.sql.types._ |
| 20 | + |
| 21 | +/** |
| 22 | + * Bloom filter function that returns the membership check result for values of `valueExpression` |
| 23 | + * in the bloom filter represented by `bloomFilterExpression`. |
| 24 | + * |
| 25 | + * @param bloomFilterExpression |
| 26 | + * binary expression that represents bloom filter data |
| 27 | + * @param valueExpression |
| 28 | + * Long value expression to be tested |
| 29 | + */ |
| 30 | +case class BloomFilterMightContain(bloomFilterExpression: Expression, valueExpression: Expression) |
| 31 | + extends BinaryComparison { |
| 32 | + |
| 33 | + override def nullable: Boolean = true |
| 34 | + |
| 35 | + override def left: Expression = bloomFilterExpression |
| 36 | + |
| 37 | + override def right: Expression = valueExpression |
| 38 | + |
| 39 | + override def prettyName: String = "bloom_filter_might_contain" |
| 40 | + |
| 41 | + override def dataType: DataType = BooleanType |
| 42 | + |
| 43 | + override def symbol: String = "BLOOM_FILTER_MIGHT_CONTAIN" |
| 44 | + |
| 45 | + override def checkInputDataTypes(): TypeCheckResult = { |
| 46 | + (left.dataType, right.dataType) match { |
| 47 | + case (BinaryType, NullType) | (NullType, LongType) | (NullType, NullType) | |
| 48 | + (BinaryType, LongType) => |
| 49 | + TypeCheckResult.TypeCheckSuccess |
| 50 | + case _ => |
| 51 | + TypeCheckResult.TypeCheckFailure(s""" |
| 52 | + | Input to function $prettyName should be Binary expression followed by a Long value, |
| 53 | + | but it's [${left.dataType.catalogString}, ${right.dataType.catalogString}]. |
| 54 | + | """.stripMargin) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + override protected def withNewChildrenInternal( |
| 59 | + newBloomFilterExpression: Expression, |
| 60 | + newValueExpression: Expression): BloomFilterMightContain = |
| 61 | + copy(bloomFilterExpression = newBloomFilterExpression, valueExpression = newValueExpression) |
| 62 | + |
| 63 | + override def eval(input: InternalRow): Any = { |
| 64 | + val value = valueExpression.eval(input) |
| 65 | + if (value == null) { |
| 66 | + null |
| 67 | + } else { |
| 68 | + val bytes = bloomFilterExpression.eval(input).asInstanceOf[Array[Byte]] |
| 69 | + val bloomFilter = ClassicBloomFilter.readFrom(new ByteArrayInputStream(bytes)) |
| 70 | + bloomFilter.mightContain(value.asInstanceOf[Long]) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Generate expression code for Spark codegen execution. Sample result code: |
| 76 | + * ``` |
| 77 | + * boolean filter_isNull_0 = true; |
| 78 | + * boolean filter_value_0 = false; |
| 79 | + * if (!right_isNull) { |
| 80 | + * filter_isNull_0 = false; |
| 81 | + * filter_value_0 = |
| 82 | + * org.opensearch.flint.core.field.bloomfilter.classic.ClassicBloomFilter.readFrom( |
| 83 | + * new java.io.ByteArrayInputStream(left_value) |
| 84 | + * ).mightContain(right_value); |
| 85 | + * } |
| 86 | + * ``` |
| 87 | + */ |
| 88 | + override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { |
| 89 | + val leftGen = left.genCode(ctx) |
| 90 | + val rightGen = right.genCode(ctx) |
| 91 | + val bloomFilterEncoder = classOf[ClassicBloomFilter].getCanonicalName.stripSuffix("$") |
| 92 | + val bf = s"$bloomFilterEncoder.readFrom(new java.io.ByteArrayInputStream(${leftGen.value}))" |
| 93 | + val result = s"$bf.mightContain(${rightGen.value})" |
| 94 | + val resultCode = |
| 95 | + s""" |
| 96 | + |if (!(${rightGen.isNull})) { |
| 97 | + | ${leftGen.code} |
| 98 | + | ${ev.isNull} = false; |
| 99 | + | ${ev.value} = $result; |
| 100 | + |} |
| 101 | + """.stripMargin |
| 102 | + ev.copy(code = code""" |
| 103 | + ${rightGen.code} |
| 104 | + boolean ${ev.isNull} = true; |
| 105 | + boolean ${ev.value} = false; |
| 106 | + $resultCode""") |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +object BloomFilterMightContain { |
| 111 | + |
| 112 | + /** |
| 113 | + * Generate bloom filter might contain function given the bloom filter column and value. |
| 114 | + * |
| 115 | + * @param colName |
| 116 | + * column name |
| 117 | + * @param value |
| 118 | + * value |
| 119 | + * @return |
| 120 | + * bloom filter might contain expression |
| 121 | + */ |
| 122 | + def bloom_filter_might_contain(colName: String, value: Any): Column = { |
| 123 | + new Column(BloomFilterMightContain(col(colName).expr, lit(value).expr)) |
| 124 | + } |
| 125 | +} |
0 commit comments