-
Notifications
You must be signed in to change notification settings - Fork 226
[AURON #2067] Implement native function of instr #2085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a876716
9a207be
9a42e0d
5eeaf06
a0da01d
8fa388c
094d824
8b8cc4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,113 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * contributor license agreements. See the NOTICE file distributed with | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * this work for additional information regarding copyright ownership. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * (the "License"); you may not use this file except in compliance with | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * the License. You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * limitations under the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| package org.apache.spark.sql | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| class AuronInstrSuite extends QueryTest with SparkQueryTestsBase { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| test("test instr function - basic functionality") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| val data = Seq( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ("hello world", "world"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ("hello world", "hello"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ("hello world", "o"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ("hello world", "z"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| (null, "test"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ("test", null)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| val df = spark.createDataFrame(data).toDF("str", "substr") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| val rows = df.selectExpr("instr(str, substr)").collect() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check non-null results | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(rows(0).getInt(0) == 7, "instr('hello world', 'world') should return 7") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(rows(1).getInt(0) == 1, "instr('hello world', 'hello') should return 1") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(rows(2).getInt(0) == 5, "instr('hello world', 'o') should return 5") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(rows(3).getInt(0) == 0, "instr('hello world', 'z') should return 0") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Check null results | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(rows(4).isNullAt(0), "instr(null, 'test') should return null") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(rows(5).isNullAt(0), "instr('test', null) should return null") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| test("test instr function - multiple occurrences") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| val data = Seq(("banana", "a"), ("testtesttest", "test"), ("abcabcabc", "abc")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| val df = spark.createDataFrame(data).toDF("str", "substr") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| val result = df.selectExpr("instr(str, substr)").collect().map(_.getInt(0)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result(0) == 2, "instr('banana', 'a') should return 2") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result(1) == 1, "instr('testtesttest', 'test') should return 1") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result(2) == 1, "instr('abcabcabc', 'abc') should return 1") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| test("test instr function - case sensitive") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| val data = Seq(("Hello", "hello"), ("HELLO", "hello"), ("Hello", "Hello"), ("hElLo", "hello")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| val df = spark.createDataFrame(data).toDF("str", "substr") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| val result = df.selectExpr("instr(str, substr)").collect().map(_.getInt(0)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result(0) == 0, "instr('Hello', 'hello') should return 0 (case sensitive)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result(1) == 0, "instr('HELLO', 'hello') should return 0 (case sensitive)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result(2) == 1, "instr('Hello', 'Hello') should return 1") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result(3) == 0, "instr('hElLo', 'hello') should return 0 (case sensitive)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| test("test instr function - with filter") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| val data = Seq( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ("hello world", "world", 1), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ("hello", "world", 0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ("hello", "hello", 1), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ("test", "abc", 0)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| val df = spark.createDataFrame(data).toDF("str", "substr", "expected") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| val result = df | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter("instr(str, substr) > 0") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .select("str") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .collect() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map(_.getString(0)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result.length == 2, "Should find 2 matching strings") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result.contains("hello world")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result.contains("hello")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| test("test instr function - in group by") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| val data = Seq(("test1", "test"), ("test2", "test"), ("hello", "world"), ("testing", "test")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| val df = spark.createDataFrame(data).toDF("str", "substr") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| val result = df | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .groupBy("substr") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .count() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter("count > 0") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .orderBy("substr") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .collect() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert(result.length >= 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+90
to
+97
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| val result = df | |
| .groupBy("substr") | |
| .count() | |
| .filter("count > 0") | |
| .orderBy("substr") | |
| .collect() | |
| assert(result.length >= 1) | |
| df.createOrReplaceTempView("instr_group_test") | |
| val result = spark.sql( | |
| """ | |
| |SELECT | |
| | substr, | |
| | SUM(CASE WHEN instr(str, substr) > 0 THEN 1 ELSE 0 END) AS match_count | |
| |FROM instr_group_test | |
| |GROUP BY substr | |
| |HAVING match_count > 0 | |
| |ORDER BY substr | |
| |""".stripMargin) | |
| .collect() | |
| // For the input data, only "test" appears within "str" values, and it occurs 3 times. | |
| assert(result.length == 1) | |
| assert(result(0).getString(0) == "test") | |
| assert(result(0).getLong(1) == 3L) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suite looks like it may not contribute any coverage today, and I want to flag the mechanism since it reads as if its asserts run.
AuronInstrSuiteextendsQueryTest with SparkQueryTestsBase.SparkTestsBaseoverridestest()to route tosuper.ignore()whenevershouldRun(testName)is false (SparkTestsBase.scala:98-105), andshouldRundelegates toSparkTestSettings.shouldRun(...), which returns false for any suite not registered viaenableSuite[...]inAuronSparkTestSettings(SparkTestSettings.scala:45-47). This suite isn't registered there (grep -rn AuronInstrSuite auron-spark-tests/finds nothing) — thatenableSuiteharness is for running Spark's own upstream suites (e.g.AuronStringFunctionsSuite extends StringFunctionsSuite). The effect is that all sixtest()blocks becomeignore(), so the hard-coded asserts likerows(0).getInt(0) == 7never execute. Is that the intent, or were these meant to run?Two of the tests are also weak even if they did run: "in group by" (
:86-98) asserts onlyresult.length >= 1and the query never callsinstrat all, and "in where clause" (:100-112) asserts onlyresult.length >= 1, which is near-tautological. Separately, the manual asserts only check the answer, not that the native operator actually ran — whereasAuronFunctionSuiteusescheckSparkAnswerAndOperator, which verifies both Spark parity and native-operator execution (the repo's canonical convention). Given all that, would folding theinstrcoverage intoAuronFunctionSuiteand dropping this suite be the cleaner path? That would also make the empty-substring behavior I asked about onspark_instr.rsland on a test that genuinely runs.