|
| 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.codegen |
| 19 | + |
| 20 | +import org.apache.spark.SparkFunSuite |
| 21 | +import org.apache.spark.sql.catalyst.expressions.codegen.Block._ |
| 22 | +import org.apache.spark.sql.types.{BooleanType, IntegerType} |
| 23 | + |
| 24 | +class CodeBlockSuite extends SparkFunSuite { |
| 25 | + |
| 26 | + test("Block can interpolate string and ExprValue inputs") { |
| 27 | + val isNull = JavaCode.isNullVariable("expr1_isNull") |
| 28 | + val code = code"boolean ${isNull} = ${JavaCode.defaultLiteral(BooleanType)};" |
| 29 | + assert(code.toString == "boolean expr1_isNull = false;") |
| 30 | + } |
| 31 | + |
| 32 | + test("Block.stripMargin") { |
| 33 | + val isNull = JavaCode.isNullVariable("expr1_isNull") |
| 34 | + val value = JavaCode.variable("expr1", IntegerType) |
| 35 | + val code1 = |
| 36 | + code""" |
| 37 | + |boolean $isNull = false; |
| 38 | + |int $value = ${JavaCode.defaultLiteral(IntegerType)};""".stripMargin |
| 39 | + val expected = |
| 40 | + s""" |
| 41 | + |boolean expr1_isNull = false; |
| 42 | + |int expr1 = ${JavaCode.defaultLiteral(IntegerType)};""".stripMargin |
| 43 | + assert(code1.toString == expected) |
| 44 | + |
| 45 | + val code2 = |
| 46 | + code""" |
| 47 | + >boolean $isNull = false; |
| 48 | + >int $value = ${JavaCode.defaultLiteral(IntegerType)};""".stripMargin('>') |
| 49 | + assert(code2.toString == expected) |
| 50 | + } |
| 51 | + |
| 52 | + test("Block can capture input expr values") { |
| 53 | + val isNull = JavaCode.isNullVariable("expr1_isNull") |
| 54 | + val value = JavaCode.variable("expr1", IntegerType) |
| 55 | + val code = |
| 56 | + code""" |
| 57 | + |boolean $isNull = false; |
| 58 | + |int $value = -1; |
| 59 | + """.stripMargin |
| 60 | + val exprValues = code.exprValues |
| 61 | + assert(exprValues.length == 2) |
| 62 | + assert(exprValues(0).isInstanceOf[VariableValue] && exprValues(0).code == "expr1_isNull") |
| 63 | + assert(exprValues(1).isInstanceOf[VariableValue] && exprValues(1).code == "expr1") |
| 64 | + } |
| 65 | + |
| 66 | + test("concatenate blocks") { |
| 67 | + val isNull1 = JavaCode.isNullVariable("expr1_isNull") |
| 68 | + val value1 = JavaCode.variable("expr1", IntegerType) |
| 69 | + val isNull2 = JavaCode.isNullVariable("expr2_isNull") |
| 70 | + val value2 = JavaCode.variable("expr2", IntegerType) |
| 71 | + val literal = JavaCode.literal("100", IntegerType) |
| 72 | + |
| 73 | + val code = |
| 74 | + code""" |
| 75 | + |boolean $isNull1 = false; |
| 76 | + |int $value1 = -1;""".stripMargin + |
| 77 | + code""" |
| 78 | + |boolean $isNull2 = true; |
| 79 | + |int $value2 = $literal;""".stripMargin |
| 80 | + |
| 81 | + val expected = |
| 82 | + """ |
| 83 | + |boolean expr1_isNull = false; |
| 84 | + |int expr1 = -1; |
| 85 | + |boolean expr2_isNull = true; |
| 86 | + |int expr2 = 100;""".stripMargin |
| 87 | + |
| 88 | + assert(code.toString == expected) |
| 89 | + |
| 90 | + val exprValues = code.exprValues |
| 91 | + assert(exprValues.length == 5) |
| 92 | + assert(exprValues(0).isInstanceOf[VariableValue] && exprValues(0).code == "expr1_isNull") |
| 93 | + assert(exprValues(1).isInstanceOf[VariableValue] && exprValues(1).code == "expr1") |
| 94 | + assert(exprValues(2).isInstanceOf[VariableValue] && exprValues(2).code == "expr2_isNull") |
| 95 | + assert(exprValues(3).isInstanceOf[VariableValue] && exprValues(3).code == "expr2") |
| 96 | + assert(exprValues(4).isInstanceOf[LiteralValue] && exprValues(4).code == "100") |
| 97 | + } |
| 98 | +} |
0 commit comments