Skip to content

Commit 9403566

Browse files
jiaoqingbopan3793
authored andcommitted
[KYUUBI #2863] Unify the logic of tpch and tpcds to generate golden file
### _Why are the changes needed?_ fix #2863 ### _How was this patch tested?_ - [x] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #2864 from jiaoqingbo/kyuubi-2863. Closes #2863 9d11495 [jiaoqingbo] [KYUUBI #2863] Unify the logic of tpch and tpcds to generate golden file Authored-by: jiaoqingbo <1178404354@qq.com> Signed-off-by: Cheng Pan <chengpan@apache.org>
1 parent c4955a8 commit 9403566

File tree

91 files changed

+881
-127
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+881
-127
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.common
19+
20+
import java.nio.charset.StandardCharsets
21+
import java.nio.file.{Files, Path, Paths}
22+
23+
object GoldenFileUtils {
24+
25+
val LICENSE_HEADER: String =
26+
"""/*
27+
| * Licensed to the Apache Software Foundation (ASF) under one or more
28+
| * contributor license agreements. See the NOTICE file distributed with
29+
| * this work for additional information regarding copyright ownership.
30+
| * The ASF licenses this file to You under the Apache License, Version 2.0
31+
| * (the "License"); you may not use this file except in compliance with
32+
| * the License. You may obtain a copy of the License at
33+
| *
34+
| * http://www.apache.org/licenses/LICENSE-2.0
35+
| *
36+
| * Unless required by applicable law or agreed to in writing, software
37+
| * distributed under the License is distributed on an "AS IS" BASIS,
38+
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39+
| * See the License for the specific language governing permissions and
40+
| * limitations under the License.
41+
| */""".stripMargin + "\n\n"
42+
43+
private val regenerateGoldenFiles: Boolean = sys.env.get("KYUUBI_UPDATE").contains("1")
44+
45+
private val baseResourcePath: Path =
46+
Paths.get("src", "main", "resources")
47+
48+
private def fileToString(file: Path): String = {
49+
new String(Files.readAllBytes(file), StandardCharsets.UTF_8)
50+
}
51+
52+
def generateGoldenFiles(
53+
dirctory: String,
54+
sqlName: String,
55+
schema: String,
56+
sumHash: String): (String, String) = {
57+
val goldenSchemaFile = Paths.get(
58+
baseResourcePath.toFile.getAbsolutePath,
59+
dirctory,
60+
s"${sqlName.stripSuffix(".sql")}.output.schema")
61+
62+
val goldenHashFile = Paths.get(
63+
baseResourcePath.toFile.getAbsolutePath,
64+
dirctory,
65+
s"${sqlName.stripSuffix(".sql")}.output.hash")
66+
if (regenerateGoldenFiles) {
67+
Files.write(goldenSchemaFile, schema.getBytes)
68+
Files.write(goldenHashFile, sumHash.getBytes)
69+
}
70+
val expectedSchema = fileToString(goldenSchemaFile)
71+
val expectedHash = fileToString(goldenHashFile)
72+
(expectedSchema, expectedHash)
73+
}
74+
75+
}

extensions/spark/kyuubi-spark-connector-tpcds/src/test/scala/org/apache/kyuubi/spark/connector/tpcds/TPCDSQuerySuite.scala

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717

1818
package org.apache.kyuubi.spark.connector.tpcds
1919

20-
import java.nio.charset.StandardCharsets
21-
import java.nio.file.{Files, Path, Paths}
22-
2320
import scala.collection.JavaConverters._
2421
import scala.io.{Codec, Source}
2522

2623
import org.apache.spark.SparkConf
2724
import org.apache.spark.sql.SparkSession
2825

2926
import org.apache.kyuubi.KyuubiFunSuite
27+
import org.apache.kyuubi.spark.connector.common.GoldenFileUtils._
3028
import org.apache.kyuubi.spark.connector.common.LocalSparkSession.withSparkSession
3129
import org.apache.kyuubi.spark.connector.common.SparkUtils
3230

@@ -50,37 +48,10 @@ import org.apache.kyuubi.spark.connector.common.SparkUtils
5048

5149
class TPCDSQuerySuite extends KyuubiFunSuite {
5250

53-
private val regenerateGoldenFiles = sys.env.get("KYUUBI_UPDATE").contains("1")
54-
55-
private val licenseHeader =
56-
"""/*
57-
| * Licensed to the Apache Software Foundation (ASF) under one or more
58-
| * contributor license agreements. See the NOTICE file distributed with
59-
| * this work for additional information regarding copyright ownership.
60-
| * The ASF licenses this file to You under the Apache License, Version 2.0
61-
| * (the "License"); you may not use this file except in compliance with
62-
| * the License. You may obtain a copy of the License at
63-
| *
64-
| * http://www.apache.org/licenses/LICENSE-2.0
65-
| *
66-
| * Unless required by applicable law or agreed to in writing, software
67-
| * distributed under the License is distributed on an "AS IS" BASIS,
68-
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
69-
| * See the License for the specific language governing permissions and
70-
| * limitations under the License.
71-
| */""".stripMargin + "\n\n"
72-
73-
val baseResourcePath: Path =
74-
Paths.get("src", "main", "resources")
75-
7651
val queries: Set[String] = (1 to 99).map(i => s"q$i").toSet -
7752
("q14", "q23", "q24", "q39") +
7853
("q14a", "q14b", "q23a", "q23b", "q24a", "q24b", "q39a", "q39b")
7954

80-
private def fileToString(file: Path): String = {
81-
new String(Files.readAllBytes(file), StandardCharsets.UTF_8)
82-
}
83-
8455
test("run query on tiny") {
8556
assume(SparkUtils.isSparkVersionEqualTo("3.2"))
8657
val viewSuffix = "view";
@@ -100,28 +71,13 @@ class TPCDSQuerySuite extends KyuubiFunSuite {
10071
try {
10172
val result = spark.sql(sql).collect()
10273
val schema = spark.sql(sql).schema
103-
val schemaDDL = licenseHeader + schema.toDDL + "\n"
74+
val schemaDDL = LICENSE_HEADER + schema.toDDL + "\n"
10475
spark.createDataFrame(result.toList.asJava, schema).createTempView(s"$name$viewSuffix")
105-
val sumHashResult = licenseHeader + spark.sql(
76+
val sumHashResult = LICENSE_HEADER + spark.sql(
10677
s"select sum(hash(*)) from $name$viewSuffix").collect().head.get(0) + "\n"
107-
108-
val goldenSchemaFile = Paths.get(
109-
baseResourcePath.toFile.getAbsolutePath,
110-
"tpcds_3.2",
111-
s"${name.stripSuffix(".sql")}.output.schema")
112-
113-
val goldenHashFile = Paths.get(
114-
baseResourcePath.toFile.getAbsolutePath,
115-
"tpcds_3.2",
116-
s"${name.stripSuffix(".sql")}.output.hash")
117-
if (regenerateGoldenFiles) {
118-
Files.write(goldenSchemaFile, schemaDDL.getBytes)
119-
Files.write(goldenHashFile, sumHashResult.getBytes)
120-
}
121-
val expectedSchema = fileToString(goldenSchemaFile)
122-
val expectedHash = fileToString(goldenHashFile)
123-
assert(schemaDDL == expectedSchema)
124-
assert(sumHashResult == expectedHash)
78+
val tuple = generateGoldenFiles("tpcds_3.2", name, schemaDDL, sumHashResult)
79+
assert(schemaDDL == tuple._1)
80+
assert(sumHashResult == tuple._2)
12581
} catch {
12682
case cause: Throwable =>
12783
fail(name, cause)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
-2130215201
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
`l_returnflag` STRING,`l_linestatus` STRING,`sum_qty` DOUBLE,`sum_base_price` DOUBLE,`sum_disc_price` DOUBLE,`sum_charge` DOUBLE,`avg_qty` DOUBLE,`avg_price` DOUBLE,`avg_disc` DOUBLE,`count_order` BIGINT NOT NULL
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
-4090660469
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
`c_custkey` BIGINT,`c_name` STRING,`revenue` DOUBLE,`c_acctbal` DOUBLE,`n_name` STRING,`c_address` STRING,`c_phone` STRING,`c_comment` STRING
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
-21773379672
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
`ps_partkey` BIGINT,`value` DOUBLE

0 commit comments

Comments
 (0)