|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.expression.predicate.operator.comparison; |
| 9 | + |
| 10 | +import org.elasticsearch.compute.data.Block; |
| 11 | +import org.elasticsearch.compute.data.BooleanBlock; |
| 12 | +import org.elasticsearch.compute.data.LongBlock; |
| 13 | +import org.elasticsearch.compute.data.LongVector; |
| 14 | +import org.elasticsearch.compute.data.Page; |
| 15 | +import org.elasticsearch.compute.operator.DriverContext; |
| 16 | +import org.elasticsearch.compute.operator.EvalOperator; |
| 17 | +import org.elasticsearch.compute.operator.Warnings; |
| 18 | +import org.elasticsearch.core.Releasable; |
| 19 | +import org.elasticsearch.core.Releasables; |
| 20 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.BitSet; |
| 24 | + |
| 25 | +/** |
| 26 | + * {@link EvalOperator.ExpressionEvaluator} implementation for {@link In}. |
| 27 | + * This class is generated. Edit {@code X-InEvaluator.java.st} instead. |
| 28 | + */ |
| 29 | +public class InMillisNanosEvaluator implements EvalOperator.ExpressionEvaluator { |
| 30 | + private final Source source; |
| 31 | + |
| 32 | + private final EvalOperator.ExpressionEvaluator lhs; |
| 33 | + |
| 34 | + private final EvalOperator.ExpressionEvaluator[] rhs; |
| 35 | + |
| 36 | + private final DriverContext driverContext; |
| 37 | + |
| 38 | + private Warnings warnings; |
| 39 | + |
| 40 | + public InMillisNanosEvaluator( |
| 41 | + Source source, |
| 42 | + EvalOperator.ExpressionEvaluator lhs, |
| 43 | + EvalOperator.ExpressionEvaluator[] rhs, |
| 44 | + DriverContext driverContext |
| 45 | + ) { |
| 46 | + this.source = source; |
| 47 | + this.lhs = lhs; |
| 48 | + this.rhs = rhs; |
| 49 | + this.driverContext = driverContext; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Block eval(Page page) { |
| 54 | + try (LongBlock lhsBlock = (LongBlock) lhs.eval(page)) { |
| 55 | + LongBlock[] rhsBlocks = new LongBlock[rhs.length]; |
| 56 | + try (Releasable rhsRelease = Releasables.wrap(rhsBlocks)) { |
| 57 | + for (int i = 0; i < rhsBlocks.length; i++) { |
| 58 | + rhsBlocks[i] = (LongBlock) rhs[i].eval(page); |
| 59 | + } |
| 60 | + LongVector lhsVector = lhsBlock.asVector(); |
| 61 | + if (lhsVector == null) { |
| 62 | + return eval(page.getPositionCount(), lhsBlock, rhsBlocks); |
| 63 | + } |
| 64 | + LongVector[] rhsVectors = new LongVector[rhs.length]; |
| 65 | + for (int i = 0; i < rhsBlocks.length; i++) { |
| 66 | + rhsVectors[i] = rhsBlocks[i].asVector(); |
| 67 | + if (rhsVectors[i] == null) { |
| 68 | + return eval(page.getPositionCount(), lhsBlock, rhsBlocks); |
| 69 | + } |
| 70 | + } |
| 71 | + return eval(page.getPositionCount(), lhsVector, rhsVectors); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private BooleanBlock eval(int positionCount, LongBlock lhsBlock, LongBlock[] rhsBlocks) { |
| 77 | + try (BooleanBlock.Builder result = driverContext.blockFactory().newBooleanBlockBuilder(positionCount)) { |
| 78 | + long[] rhsValues = new long[rhs.length]; |
| 79 | + BitSet nulls = new BitSet(rhs.length); |
| 80 | + BitSet mvs = new BitSet(rhs.length); |
| 81 | + boolean foundMatch; |
| 82 | + for (int p = 0; p < positionCount; p++) { |
| 83 | + if (lhsBlock.isNull(p)) { |
| 84 | + result.appendNull(); |
| 85 | + continue; |
| 86 | + } |
| 87 | + if (lhsBlock.getValueCount(p) != 1) { |
| 88 | + if (lhsBlock.getValueCount(p) > 1) { |
| 89 | + warnings().registerException(new IllegalArgumentException("single-value function encountered multi-value")); |
| 90 | + } |
| 91 | + result.appendNull(); |
| 92 | + continue; |
| 93 | + } |
| 94 | + // unpack rhsBlocks into rhsValues |
| 95 | + nulls.clear(); |
| 96 | + mvs.clear(); |
| 97 | + for (int i = 0; i < rhsBlocks.length; i++) { |
| 98 | + if (rhsBlocks[i].isNull(p)) { |
| 99 | + nulls.set(i); |
| 100 | + continue; |
| 101 | + } |
| 102 | + if (rhsBlocks[i].getValueCount(p) > 1) { |
| 103 | + mvs.set(i); |
| 104 | + warnings().registerException(new IllegalArgumentException("single-value function encountered multi-value")); |
| 105 | + continue; |
| 106 | + } |
| 107 | + int o = rhsBlocks[i].getFirstValueIndex(p); |
| 108 | + rhsValues[i] = rhsBlocks[i].getLong(o); |
| 109 | + } |
| 110 | + if (nulls.cardinality() == rhsBlocks.length || mvs.cardinality() == rhsBlocks.length) { |
| 111 | + result.appendNull(); |
| 112 | + continue; |
| 113 | + } |
| 114 | + foundMatch = In.process(nulls, mvs, lhsBlock.getLong(lhsBlock.getFirstValueIndex(p)), rhsValues); |
| 115 | + if (foundMatch) { |
| 116 | + result.appendBoolean(true); |
| 117 | + } else { |
| 118 | + if (nulls.cardinality() > 0) { |
| 119 | + result.appendNull(); |
| 120 | + } else { |
| 121 | + result.appendBoolean(false); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return result.build(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private BooleanBlock eval(int positionCount, LongVector lhsVector, LongVector[] rhsVectors) { |
| 130 | + try (BooleanBlock.Builder result = driverContext.blockFactory().newBooleanBlockBuilder(positionCount)) { |
| 131 | + long[] rhsValues = new long[rhs.length]; |
| 132 | + for (int p = 0; p < positionCount; p++) { |
| 133 | + // unpack rhsVectors into rhsValues |
| 134 | + for (int i = 0; i < rhsVectors.length; i++) { |
| 135 | + rhsValues[i] = rhsVectors[i].getLong(p); |
| 136 | + } |
| 137 | + result.appendBoolean(In.processMillisNanos(null, null, lhsVector.getLong(p), rhsValues)); |
| 138 | + } |
| 139 | + return result.build(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String toString() { |
| 145 | + return "InMillisNanosEvaluator[" + "lhs=" + lhs + ", rhs=" + Arrays.toString(rhs) + "]"; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void close() { |
| 150 | + Releasables.closeExpectNoException(lhs, () -> Releasables.close(rhs)); |
| 151 | + } |
| 152 | + |
| 153 | + private Warnings warnings() { |
| 154 | + if (warnings == null) { |
| 155 | + this.warnings = Warnings.createWarnings( |
| 156 | + driverContext.warningsMode(), |
| 157 | + source.source().getLineNumber(), |
| 158 | + source.source().getColumnNumber(), |
| 159 | + source.text() |
| 160 | + ); |
| 161 | + } |
| 162 | + return warnings; |
| 163 | + } |
| 164 | + |
| 165 | + static class Factory implements EvalOperator.ExpressionEvaluator.Factory { |
| 166 | + private final Source source; |
| 167 | + private final EvalOperator.ExpressionEvaluator.Factory lhs; |
| 168 | + private final EvalOperator.ExpressionEvaluator.Factory[] rhs; |
| 169 | + |
| 170 | + Factory(Source source, EvalOperator.ExpressionEvaluator.Factory lhs, EvalOperator.ExpressionEvaluator.Factory[] rhs) { |
| 171 | + this.source = source; |
| 172 | + this.lhs = lhs; |
| 173 | + this.rhs = rhs; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public InMillisNanosEvaluator get(DriverContext context) { |
| 178 | + EvalOperator.ExpressionEvaluator[] rhs = Arrays.stream(this.rhs) |
| 179 | + .map(a -> a.get(context)) |
| 180 | + .toArray(EvalOperator.ExpressionEvaluator[]::new); |
| 181 | + return new InMillisNanosEvaluator(source, lhs.get(context), rhs, context); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public String toString() { |
| 186 | + return "InMillisNanosEvaluator[" + "lhs=" + lhs + ", rhs=" + Arrays.toString(rhs) + "]"; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments