diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/ConditionalFilter.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/ConditionalFilter.java new file mode 100644 index 0000000..d410d78 --- /dev/null +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/ConditionalFilter.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hive.ql.exec.vector.expressions; + + +import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector; +import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch; + + + +/** + *For conditional expressions, the{@code ConditionalFilter} class updated + * the selected array of batch parameter after the conditional expression is executed. + * Then the remaining expression will only do the selected rows instead of all. + */ +public class ConditionalFilter { + + /* + *For If(expr1,expr2,expr3) expression, + * Firstly, save the previous selected vector,size and selectedInUse value of batch. + * Secondly evaluate the conditional expression and update the selected array of batch based + * on the result of conditional expression(1 denote done, 0 denote not done) + * Then evaluate the expr2 based on the updated selected. + * After the expr2 is executed, remove the indexes which have done in expr2. + * Last, evaluate the expr3 based on the updated selected. + */ + public static void evaluateIfChildren(VectorizedRowBatch batch, VectorExpression[] childExpressions, int[] indexes) { + //Save the previous selected vector,size and selectedInUse value of batch. + int[] prevSelected = new int[VectorizedRowBatch.DEFAULT_SIZE]; + int prevSize; + boolean prevSelectInUse; + + prevSize = batch.size; + prevSelectInUse = batch.selectedInUse; + if (batch.selectedInUse == false) { + for (int i = 0; i < prevSize; i++) { + prevSelected[i] = i; + } + System.arraycopy(prevSelected, 0, batch.selected, 0, prevSize); + } else { + System.arraycopy(batch.selected, 0, prevSelected, 0, batch.selected.length); + } + + if (childExpressions != null) { + int length = childExpressions.length; + //evaluate the conditional expression. + evaluateConditionalExpression(batch, childExpressions[0], prevSize, prevSelectInUse); + if (length == 2) { + //If the length is 2, it has two situations:If(expr1,expr2,null) or If(expr1,null,expr3) distinguished by the indexes. + if (childExpressions[1].getOutputColumn() == indexes[1]) { + //evaluate the expr2 expression. + childExpressions[1].evaluate(batch); + } else { + //update the selected array of batch to remove the index of being done. + evaluateSelectedArray(batch, indexes[0], prevSelected, prevSize); + //If(expr1,null,expr3), if the expr1 is false, expr3 will be evaluated. + childExpressions[1].evaluate(batch); + } + } else if (length == 3) { + //IF(expr1,expr2,expr3). expr1,expr2,expr3 are all the expression. + //evaluate the expr2 expression. + childExpressions[1].evaluate(batch); + //update the selected array of batch to remove the index of being done. + evaluateSelectedArray(batch, indexes[0], prevSelected, prevSize); + //evaluate the expr3 expression. + childExpressions[2].evaluate(batch); + } + //When evaluate all the expressions, restore the previous selected vector,size and selectedInUse value of batch. + batch.size = prevSize; + batch.selectedInUse = prevSelectInUse; + batch.selected = prevSelected; + } + } + + /* + * update the selected array of batch based on the conditional expression result, remove the index of being done. + */ + private static void evaluateSelectedArray(VectorizedRowBatch batch, int num, int[] prevSelected, int size) { + // get the result of conditional expression. + LongColumnVector outputColVector = (LongColumnVector) batch.cols[num]; + long[] flag = outputColVector.vector; + int newSize = 0; + //update the selected array of batch + for (int j = 0; j < size; j++) { + if (flag[prevSelected[j]] == 0) { + batch.selected[newSize++] = prevSelected[j]; + } + } + batch.size = newSize; + batch.selectedInUse = true; + } + + /* + * evaluate the conditional expression and update the selected array of batch based on the result of conditional expression. + */ + private static void evaluateConditionalExpression(VectorizedRowBatch batch, VectorExpression ve, int prevSize, boolean prevSelectInUse) { + batch.size = prevSize; + batch.selectedInUse = prevSelectInUse; + int colNum = ve.getOutputColumn(); + // evaluate the conditional expression. + ve.evaluate(batch); + LongColumnVector outputColVector = (LongColumnVector) batch.cols[colNum]; + long[] flag = outputColVector.vector; + int[] sel = batch.selected; + int n = batch.size; + int newSize = 0; + //update the selected array of the batch based on the conditional expression. + for (int j = 0; j < n; j++) { + int k = sel[j]; + if (flag[k] == 1) { + sel[newSize++] = k; + } + } + if(newSize < n ) { + batch.size = newSize; + batch.selectedInUse = true; + } + } +} + diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprColumnNull.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprColumnNull.java index 8cae274..7133136 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprColumnNull.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprColumnNull.java @@ -40,9 +40,9 @@ public IfExprColumnNull(int arg1Column, int arg2Column, int outputColumn) { public void evaluate(VectorizedRowBatch batch) { if (childExpressions != null) { - super.evaluateChildren(batch); + int[] indexes = {this.arg1Column, this.arg2Column}; + ConditionalFilter.evaluateIfChildren(batch, childExpressions, indexes); } - final LongColumnVector arg1ColVector = (LongColumnVector) batch.cols[arg1Column]; final ColumnVector arg2ColVector = batch.cols[arg2Column]; final ColumnVector outputColVector = batch.cols[outputColumn]; diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprDoubleColumnDoubleColumn.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprDoubleColumnDoubleColumn.java index 514b453..be41304 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprDoubleColumnDoubleColumn.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprDoubleColumnDoubleColumn.java @@ -48,7 +48,8 @@ public IfExprDoubleColumnDoubleColumn() { public void evaluate(VectorizedRowBatch batch) { if (childExpressions != null) { - super.evaluateChildren(batch); + int[] indexes = {this.arg1Column, this.arg2Column, this.arg3Column}; + ConditionalFilter.evaluateIfChildren(batch, childExpressions, indexes); } LongColumnVector arg1ColVector = (LongColumnVector) batch.cols[arg1Column]; diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprIntervalDayTimeColumnColumn.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprIntervalDayTimeColumnColumn.java index 98fa29e..c7efa6e 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprIntervalDayTimeColumnColumn.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprIntervalDayTimeColumnColumn.java @@ -48,7 +48,8 @@ public IfExprIntervalDayTimeColumnColumn() { public void evaluate(VectorizedRowBatch batch) { if (childExpressions != null) { - super.evaluateChildren(batch); + int[] indexes = {this.arg1Column, this.arg2Column, this.arg3Column}; + ConditionalFilter.evaluateIfChildren(batch, childExpressions, indexes); } LongColumnVector arg1ColVector = (LongColumnVector) batch.cols[arg1Column]; diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprLongColumnLongColumn.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprLongColumnLongColumn.java index 4c6015e..109ba9a 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprLongColumnLongColumn.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprLongColumnLongColumn.java @@ -47,7 +47,8 @@ public IfExprLongColumnLongColumn() { public void evaluate(VectorizedRowBatch batch) { if (childExpressions != null) { - super.evaluateChildren(batch); + int[] indexes = {this.arg1Column, this.arg2Column, this.arg3Column}; + ConditionalFilter.evaluateIfChildren(batch, childExpressions, indexes); } LongColumnVector arg1ColVector = (LongColumnVector) batch.cols[arg1Column]; diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprNullColumn.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprNullColumn.java index 156fcc4..e8c027f 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprNullColumn.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprNullColumn.java @@ -40,7 +40,8 @@ public IfExprNullColumn(int arg1Column, int arg2Column, int outputColumn) { public void evaluate(VectorizedRowBatch batch) { if (childExpressions != null) { - super.evaluateChildren(batch); + int[] indexes = {this.arg1Column, this.arg2Column}; + ConditionalFilter.evaluateIfChildren(batch, childExpressions, indexes); } final LongColumnVector arg1ColVector = (LongColumnVector) batch.cols[arg1Column]; diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprStringGroupColumnStringGroupColumn.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprStringGroupColumnStringGroupColumn.java index c8367c6..f45e661 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprStringGroupColumnStringGroupColumn.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprStringGroupColumnStringGroupColumn.java @@ -51,7 +51,8 @@ public IfExprStringGroupColumnStringGroupColumn() { public void evaluate(VectorizedRowBatch batch) { if (childExpressions != null) { - super.evaluateChildren(batch); + int[] indexes = {this.arg1Column, this.arg2Column, this.arg3Column}; + ConditionalFilter.evaluateIfChildren(batch, childExpressions, indexes); } LongColumnVector arg1ColVector = (LongColumnVector) batch.cols[arg1Column]; diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprTimestampColumnColumnBase.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprTimestampColumnColumnBase.java index 8219b3c..718e21b 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprTimestampColumnColumnBase.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/IfExprTimestampColumnColumnBase.java @@ -47,7 +47,8 @@ public IfExprTimestampColumnColumnBase() { public void evaluate(VectorizedRowBatch batch) { if (childExpressions != null) { - super.evaluateChildren(batch); + int[] indexes = {this.arg1Column, this.arg2Column, this.arg3Column}; + ConditionalFilter.evaluateIfChildren(batch, childExpressions, indexes); } LongColumnVector arg1ColVector = (LongColumnVector) batch.cols[arg1Column];