|
| 1 | +/* |
| 2 | + * Copyright 2021 4Paradigm |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com._4paradigm.openmldb.batch |
| 18 | + |
| 19 | +import com._4paradigm.openmldb.batch.api.OpenmldbSession |
| 20 | +import org.apache.spark.sql.Row |
| 21 | +import org.apache.spark.sql.types.{ |
| 22 | + IntegerType, |
| 23 | + StringType, |
| 24 | + StructField, |
| 25 | + StructType |
| 26 | +} |
| 27 | +import com._4paradigm.openmldb.batch.utils.SparkUtil |
| 28 | + |
| 29 | +class TestSetOperation extends SparkTestSuite { |
| 30 | + |
| 31 | + test("Test UNION ALL") { |
| 32 | + val spark = getSparkSession |
| 33 | + val sess = new OpenmldbSession(spark) |
| 34 | + |
| 35 | + val schema = StructType( |
| 36 | + List(StructField("id", IntegerType), StructField("user", StringType)) |
| 37 | + ) |
| 38 | + val data1 = Seq(Row(1, "tom"), Row(2, "amy")) |
| 39 | + val df1 = spark.createDataFrame(spark.sparkContext.makeRDD(data1), schema) |
| 40 | + val data2 = Seq(Row(1, "tom")) |
| 41 | + val df2 = spark.createDataFrame(spark.sparkContext.makeRDD(data2), schema) |
| 42 | + |
| 43 | + sess.registerTable("t1", df1) |
| 44 | + sess.registerTable("t2", df2) |
| 45 | + df1.createOrReplaceTempView("t1") |
| 46 | + df2.createOrReplaceTempView("t2") |
| 47 | + |
| 48 | + val sqlText = "SELECT * FROM t1 UNION ALL SELECT * FROM t2" |
| 49 | + |
| 50 | + val outputDf = sess.sql(sqlText) |
| 51 | + outputDf.show() |
| 52 | + val sparksqlOutputDf = sess.sparksql(sqlText) |
| 53 | + sparksqlOutputDf.show() |
| 54 | + assert(outputDf.getSparkDf().count() == 3) |
| 55 | + assert( |
| 56 | + SparkUtil.approximateDfEqual( |
| 57 | + outputDf.getSparkDf(), |
| 58 | + sparksqlOutputDf, |
| 59 | + true |
| 60 | + ) |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + test("Test UNION DISTINCT") { |
| 65 | + val spark = getSparkSession |
| 66 | + val sess = new OpenmldbSession(spark) |
| 67 | + |
| 68 | + val schema = StructType( |
| 69 | + List(StructField("id", IntegerType), StructField("user", StringType)) |
| 70 | + ) |
| 71 | + val data1 = Seq(Row(1, "tom"), Row(2, "amy")) |
| 72 | + val df1 = spark.createDataFrame(spark.sparkContext.makeRDD(data1), schema) |
| 73 | + val data2 = Seq(Row(1, "tom")) |
| 74 | + val df2 = spark.createDataFrame(spark.sparkContext.makeRDD(data2), schema) |
| 75 | + |
| 76 | + sess.registerTable("t1", df1) |
| 77 | + sess.registerTable("t2", df2) |
| 78 | + df1.createOrReplaceTempView("t1") |
| 79 | + df2.createOrReplaceTempView("t2") |
| 80 | + |
| 81 | + val sqlText = "SELECT * FROM t1 UNION DISTINCT SELECT * FROM t2" |
| 82 | + |
| 83 | + val outputDf = sess.sql(sqlText) |
| 84 | + outputDf.show() |
| 85 | + val sparksqlOutputDf = sess.sparksql(sqlText) |
| 86 | + sparksqlOutputDf.show() |
| 87 | + assert(outputDf.getSparkDf().count() == 2) |
| 88 | + assert( |
| 89 | + SparkUtil.approximateDfEqual( |
| 90 | + outputDf.getSparkDf(), |
| 91 | + sparksqlOutputDf, |
| 92 | + true |
| 93 | + ) |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments