-
Notifications
You must be signed in to change notification settings - Fork 211
[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
Open
xuzifu666
wants to merge
7
commits into
apache:master
Choose a base branch
from
xuzifu666:instr_function
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+524
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a876716
[AURON #2067] Implement native function of instr
xuzifu666 9a207be
[AURON #2067] Implement native function of instr
xuzifu666 9a42e0d
fix tests
xuzifu666 5eeaf06
fix tests
xuzifu666 a0da01d
fix styles
xuzifu666 8fa388c
fix styles
xuzifu666 094d824
Fix
xuzifu666 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
auron-spark-tests/spark33/src/test/scala/org/apache/spark/sql/AuronInstrSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| * 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) | ||
| } | ||
|
|
||
| test("test instr function - in where clause") { | ||
| val data = Seq( | ||
| ("hello world", "world"), | ||
| ("hello", "world"), | ||
| ("testing", "test"), | ||
| ("abc", "def") | ||
| ) | ||
|
|
||
| val df = spark.createDataFrame(data).toDF("str", "substr") | ||
| val result = df | ||
| .filter("instr(str, substr) = 1") | ||
| .select("str") | ||
| .collect() | ||
| .map(_.getString(0)) | ||
|
|
||
| assert(result.length >= 1) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 test is titled "in group by" but it never evaluates
instr(it only groups bysubstrand counts). As written, it doesn't validate thatinstrworks correctly in the presence of aggregations / grouping. Consider incorporatinginstr(...)either in the grouping key, an aggregate expression, or a post-aggregation filter so the test actually exercises the new function in a group-by context.