|
| 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.kyuubi.spark.connector.tpch |
| 19 | + |
| 20 | +import org.apache.spark.SPARK_VERSION |
| 21 | + |
| 22 | +object SparkUtils { |
| 23 | + |
| 24 | + /** |
| 25 | + * Given a Kyuubi/Spark/Hive version string, |
| 26 | + * return the (major version number, minor version number). |
| 27 | + * E.g., for 2.0.1-SNAPSHOT, return (2, 0). |
| 28 | + */ |
| 29 | + def majorMinorVersion(version: String): (Int, Int) = { |
| 30 | + """^(\d+)\.(\d+)(\..*)?$""".r.findFirstMatchIn(version) match { |
| 31 | + case Some(m) => |
| 32 | + (m.group(1).toInt, m.group(2).toInt) |
| 33 | + case None => |
| 34 | + throw new IllegalArgumentException(s"Tried to parse '$version' as a project" + |
| 35 | + s" version string, but it could not find the major and minor version numbers.") |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Given a Kyuubi/Spark/Hive version string, return the major version number. |
| 41 | + * E.g., for 2.0.1-SNAPSHOT, return 2. |
| 42 | + */ |
| 43 | + def majorVersion(version: String): Int = majorMinorVersion(version)._1 |
| 44 | + |
| 45 | + /** |
| 46 | + * Given a Kyuubi/Spark/Hive version string, return the minor version number. |
| 47 | + * E.g., for 2.0.1-SNAPSHOT, return 0. |
| 48 | + */ |
| 49 | + def minorVersion(version: String): Int = majorMinorVersion(version)._2 |
| 50 | + |
| 51 | + def isSparkVersionAtMost(ver: String): Boolean = { |
| 52 | + val runtimeMajor = majorVersion(SPARK_VERSION) |
| 53 | + val targetMajor = majorVersion(ver) |
| 54 | + (runtimeMajor < targetMajor) || { |
| 55 | + val runtimeMinor = minorVersion(SPARK_VERSION) |
| 56 | + val targetMinor = minorVersion(ver) |
| 57 | + runtimeMajor == targetMajor && runtimeMinor <= targetMinor |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + def isSparkVersionAtLeast(ver: String): Boolean = { |
| 62 | + val runtimeMajor = majorVersion(SPARK_VERSION) |
| 63 | + val targetMajor = majorVersion(ver) |
| 64 | + (runtimeMajor > targetMajor) || { |
| 65 | + val runtimeMinor = minorVersion(SPARK_VERSION) |
| 66 | + val targetMinor = minorVersion(ver) |
| 67 | + runtimeMajor == targetMajor && runtimeMinor >= targetMinor |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + def isSparkVersionEqualTo(ver: String): Boolean = { |
| 72 | + val runtimeMajor = majorVersion(SPARK_VERSION) |
| 73 | + val targetMajor = majorVersion(ver) |
| 74 | + val runtimeMinor = minorVersion(SPARK_VERSION) |
| 75 | + val targetMinor = minorVersion(ver) |
| 76 | + runtimeMajor == targetMajor && runtimeMinor == targetMinor |
| 77 | + } |
| 78 | +} |
0 commit comments