|
| 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.catalyst.expressions |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult |
| 21 | +import org.apache.spark.sql.catalyst.expressions.codegen._ |
| 22 | +import org.apache.spark.sql.catalyst.util.TypeUtils |
| 23 | +import org.apache.spark.sql.types._ |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * A function that calculates bitwise and(&) of two numbers. |
| 28 | + */ |
| 29 | +case class BitwiseAnd(left: Expression, right: Expression) extends BinaryArithmetic { |
| 30 | + override def symbol: String = "&" |
| 31 | + |
| 32 | + protected def checkTypesInternal(t: DataType) = |
| 33 | + TypeUtils.checkForBitwiseExpr(t, "operator " + symbol) |
| 34 | + |
| 35 | + private lazy val and: (Any, Any) => Any = dataType match { |
| 36 | + case ByteType => |
| 37 | + ((evalE1: Byte, evalE2: Byte) => (evalE1 & evalE2).toByte).asInstanceOf[(Any, Any) => Any] |
| 38 | + case ShortType => |
| 39 | + ((evalE1: Short, evalE2: Short) => (evalE1 & evalE2).toShort).asInstanceOf[(Any, Any) => Any] |
| 40 | + case IntegerType => |
| 41 | + ((evalE1: Int, evalE2: Int) => evalE1 & evalE2).asInstanceOf[(Any, Any) => Any] |
| 42 | + case LongType => |
| 43 | + ((evalE1: Long, evalE2: Long) => evalE1 & evalE2).asInstanceOf[(Any, Any) => Any] |
| 44 | + } |
| 45 | + |
| 46 | + protected override def evalInternal(evalE1: Any, evalE2: Any) = and(evalE1, evalE2) |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * A function that calculates bitwise or(|) of two numbers. |
| 51 | + */ |
| 52 | +case class BitwiseOr(left: Expression, right: Expression) extends BinaryArithmetic { |
| 53 | + override def symbol: String = "|" |
| 54 | + |
| 55 | + protected def checkTypesInternal(t: DataType) = |
| 56 | + TypeUtils.checkForBitwiseExpr(t, "operator " + symbol) |
| 57 | + |
| 58 | + private lazy val or: (Any, Any) => Any = dataType match { |
| 59 | + case ByteType => |
| 60 | + ((evalE1: Byte, evalE2: Byte) => (evalE1 | evalE2).toByte).asInstanceOf[(Any, Any) => Any] |
| 61 | + case ShortType => |
| 62 | + ((evalE1: Short, evalE2: Short) => (evalE1 | evalE2).toShort).asInstanceOf[(Any, Any) => Any] |
| 63 | + case IntegerType => |
| 64 | + ((evalE1: Int, evalE2: Int) => evalE1 | evalE2).asInstanceOf[(Any, Any) => Any] |
| 65 | + case LongType => |
| 66 | + ((evalE1: Long, evalE2: Long) => evalE1 | evalE2).asInstanceOf[(Any, Any) => Any] |
| 67 | + } |
| 68 | + |
| 69 | + protected override def evalInternal(evalE1: Any, evalE2: Any) = or(evalE1, evalE2) |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * A function that calculates bitwise xor of two numbers. |
| 74 | + */ |
| 75 | +case class BitwiseXor(left: Expression, right: Expression) extends BinaryArithmetic { |
| 76 | + override def symbol: String = "^" |
| 77 | + |
| 78 | + protected def checkTypesInternal(t: DataType) = |
| 79 | + TypeUtils.checkForBitwiseExpr(t, "operator " + symbol) |
| 80 | + |
| 81 | + private lazy val xor: (Any, Any) => Any = dataType match { |
| 82 | + case ByteType => |
| 83 | + ((evalE1: Byte, evalE2: Byte) => (evalE1 ^ evalE2).toByte).asInstanceOf[(Any, Any) => Any] |
| 84 | + case ShortType => |
| 85 | + ((evalE1: Short, evalE2: Short) => (evalE1 ^ evalE2).toShort).asInstanceOf[(Any, Any) => Any] |
| 86 | + case IntegerType => |
| 87 | + ((evalE1: Int, evalE2: Int) => evalE1 ^ evalE2).asInstanceOf[(Any, Any) => Any] |
| 88 | + case LongType => |
| 89 | + ((evalE1: Long, evalE2: Long) => evalE1 ^ evalE2).asInstanceOf[(Any, Any) => Any] |
| 90 | + } |
| 91 | + |
| 92 | + protected override def evalInternal(evalE1: Any, evalE2: Any): Any = xor(evalE1, evalE2) |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * A function that calculates bitwise not(~) of a number. |
| 97 | + */ |
| 98 | +case class BitwiseNot(child: Expression) extends UnaryArithmetic { |
| 99 | + override def toString: String = s"~$child" |
| 100 | + |
| 101 | + override def checkInputDataTypes(): TypeCheckResult = |
| 102 | + TypeUtils.checkForBitwiseExpr(child.dataType, "operator ~") |
| 103 | + |
| 104 | + private lazy val not: (Any) => Any = dataType match { |
| 105 | + case ByteType => |
| 106 | + ((evalE: Byte) => (~evalE).toByte).asInstanceOf[(Any) => Any] |
| 107 | + case ShortType => |
| 108 | + ((evalE: Short) => (~evalE).toShort).asInstanceOf[(Any) => Any] |
| 109 | + case IntegerType => |
| 110 | + ((evalE: Int) => ~evalE).asInstanceOf[(Any) => Any] |
| 111 | + case LongType => |
| 112 | + ((evalE: Long) => ~evalE).asInstanceOf[(Any) => Any] |
| 113 | + } |
| 114 | + |
| 115 | + override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): Code = { |
| 116 | + defineCodeGen(ctx, ev, c => s"(${ctx.javaType(dataType)})~($c)") |
| 117 | + } |
| 118 | + |
| 119 | + protected override def evalInternal(evalE: Any) = not(evalE) |
| 120 | +} |
0 commit comments