|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.hive.orc |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.expressions._ |
| 21 | +import org.apache.hadoop.hive.ql.io.sarg.SearchArgument |
| 22 | +import org.apache.hadoop.hive.ql.io.sarg.SearchArgument.Builder |
| 23 | +import org.apache.spark.Logging |
| 24 | + |
| 25 | +private[sql] object OrcFilters extends Logging { |
| 26 | + |
| 27 | + def createFilter(expr: Seq[Expression]): Option[SearchArgument] = { |
| 28 | + if (expr == null || expr.size == 0) return None |
| 29 | + var sarg: Option[Builder] = Some(SearchArgument.FACTORY.newBuilder()) |
| 30 | + sarg.get.startAnd() |
| 31 | + expr.foreach { |
| 32 | + x => { |
| 33 | + sarg match { |
| 34 | + case Some(s1) => sarg = createFilter(x, s1) |
| 35 | + case _ => None |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + sarg match { |
| 40 | + case Some(b) => Some(b.end.build) |
| 41 | + case _ => None |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + def createFilter(expression: Expression, builder: Builder): Option[Builder] = { |
| 46 | + expression match { |
| 47 | + case p@And(left: Expression, right: Expression) => { |
| 48 | + val b1 = builder.startAnd() |
| 49 | + val b2 = createFilter(left, b1) |
| 50 | + b2 match { |
| 51 | + case Some(b) => val b3 = createFilter(right, b) |
| 52 | + if (b3.isDefined) { |
| 53 | + Some(b3.get.end) |
| 54 | + } else { |
| 55 | + None |
| 56 | + } |
| 57 | + case _ => None |
| 58 | + } |
| 59 | + } |
| 60 | + case p@Or(left: Expression, right: Expression) => { |
| 61 | + val b1 = builder.startOr() |
| 62 | + val b2 = createFilter(left, b1) |
| 63 | + b2 match { |
| 64 | + case Some(b) => val b3 = createFilter(right, b) |
| 65 | + if (b3.isDefined) { |
| 66 | + Some(b3.get.end) |
| 67 | + } else { |
| 68 | + None |
| 69 | + } |
| 70 | + case _ => None |
| 71 | + } |
| 72 | + } |
| 73 | + case p@EqualTo(left: Literal, right: NamedExpression) => { |
| 74 | + val b1 = builder.equals(right.name, left.value) |
| 75 | + Some(b1) |
| 76 | + } |
| 77 | + case p@EqualTo(left: NamedExpression, right: Literal) => { |
| 78 | + val b1 = builder.equals(left.name, right.value) |
| 79 | + Some(b1) |
| 80 | + } |
| 81 | + case p@LessThan(left: NamedExpression, right: Literal) => { |
| 82 | + val b1 = builder.lessThan(left.name, right.value) |
| 83 | + Some(b1) |
| 84 | + } |
| 85 | + case p@LessThan(left: Literal, right: NamedExpression) => { |
| 86 | + val b1 = builder.startNot().lessThanEquals(right.name, left.value).end() |
| 87 | + Some(b1) |
| 88 | + } |
| 89 | + case p@LessThanOrEqual(left: NamedExpression, right: Literal) => { |
| 90 | + val b1 = builder.lessThanEquals(left.name, right.value) |
| 91 | + Some(b1) |
| 92 | + } |
| 93 | + case p@LessThanOrEqual(left: Literal, right: NamedExpression) => { |
| 94 | + val b1 = builder.startNot().lessThan(right.name, left.value).end() |
| 95 | + Some(b1) |
| 96 | + } |
| 97 | + case p@GreaterThan(left: NamedExpression, right: Literal) => { |
| 98 | + val b1 = builder.startNot().lessThanEquals(left.name, right.value).end() |
| 99 | + Some(b1) |
| 100 | + } |
| 101 | + case p@GreaterThan(left: Literal, right: NamedExpression) => { |
| 102 | + val b1 = builder.lessThanEquals(right.name, left.value) |
| 103 | + Some(b1) |
| 104 | + } |
| 105 | + case p@GreaterThanOrEqual(left: NamedExpression, right: Literal) => { |
| 106 | + val b1 = builder.startNot().lessThan(left.name, right.value).end() |
| 107 | + Some(b1) |
| 108 | + } |
| 109 | + case p@GreaterThanOrEqual(left: Literal, right: NamedExpression) => { |
| 110 | + val b1 = builder.lessThan(right.name, left.value) |
| 111 | + Some(b1) |
| 112 | + } |
| 113 | + // TODO: test it |
| 114 | + case p@EqualNullSafe(left: NamedExpression, right: NamedExpression) => { |
| 115 | + val b1 = builder.nullSafeEquals(left.name, right.name) |
| 116 | + Some(b1) |
| 117 | + } |
| 118 | + case p@In(left: NamedExpression, list: Seq[Literal]) => { |
| 119 | + val b1 = builder.in(left.name, list.map(_.value).toArray) |
| 120 | + Some(b1) |
| 121 | + } |
| 122 | + case _ => None |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments