-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-15243][ML][SQL][PYSPARK] Param methods should use basestring for type checking #13036
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
878fc5f
check for basestring in param methods
sethah b04ac41
replacing sql isinstance(obj, str)
sethah e6d9c19
Merge branch 'master' of https://github.com/apache/spark into SPARK-1…
sethah 3babfd3
merging master
sethah 48f0557
revert
sethah c6a8828
test for sampleby
sethah 976d682
revert doc test
sethah 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
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
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
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
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 |
|---|---|---|
|
|
@@ -799,25 +799,30 @@ def test_first_last_ignorenulls(self): | |
|
|
||
| def test_approxQuantile(self): | ||
| df = self.sc.parallelize([Row(a=i) for i in range(10)]).toDF() | ||
| aq = df.stat.approxQuantile("a", [0.1, 0.5, 0.9], 0.1) | ||
| aq = df.stat.approxQuantile(u"a", [0.1, 0.5, 0.9], 0.1) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically in these tests the field names are all ascii characters. Is it possibly to add tests using non-ascii characters so we can make sure it works? |
||
| self.assertTrue(isinstance(aq, list)) | ||
| self.assertEqual(len(aq), 3) | ||
| self.assertTrue(all(isinstance(q, float) for q in aq)) | ||
|
|
||
| def test_corr(self): | ||
| import math | ||
| df = self.sc.parallelize([Row(a=i, b=math.sqrt(i)) for i in range(10)]).toDF() | ||
| corr = df.stat.corr("a", "b") | ||
| corr = df.stat.corr(u"a", "b") | ||
| self.assertTrue(abs(corr - 0.95734012) < 1e-6) | ||
|
|
||
| def test_sampleby(self): | ||
| df = self.sc.parallelize([Row(a=i, b=(i % 3)) for i in range(10)]).toDF() | ||
| corr = df.stat.sampleBy(u"b", fractions={0: 0.5, 1: 0.5}, seed=0) | ||
| self.assertTrue(corr.count() == 3) | ||
|
|
||
| def test_cov(self): | ||
| df = self.sc.parallelize([Row(a=i, b=2 * i) for i in range(10)]).toDF() | ||
| cov = df.stat.cov("a", "b") | ||
| cov = df.stat.cov(u"a", "b") | ||
| self.assertTrue(abs(cov - 55.0 / 3) < 1e-6) | ||
|
|
||
| def test_crosstab(self): | ||
| df = self.sc.parallelize([Row(a=i % 3, b=i % 2) for i in range(1, 7)]).toDF() | ||
| ct = df.stat.crosstab("a", "b").collect() | ||
| ct = df.stat.crosstab(u"a", "b").collect() | ||
| ct = sorted(ct, key=lambda x: x[0]) | ||
| for i, row in enumerate(ct): | ||
| self.assertEqual(row[0], str(i)) | ||
|
|
@@ -883,9 +888,9 @@ def test_between_function(self): | |
|
|
||
| def test_struct_type(self): | ||
| from pyspark.sql.types import StructType, StringType, StructField | ||
| struct1 = StructType().add("f1", StringType(), True).add("f2", StringType(), True, None) | ||
| struct1 = StructType().add(u"f1", StringType(), True).add("f2", StringType(), True, None) | ||
| struct2 = StructType([StructField("f1", StringType(), True), | ||
| StructField("f2", StringType(), True, None)]) | ||
| StructField(u"f2", StringType(), True, None)]) | ||
| self.assertEqual(struct1, struct2) | ||
|
|
||
| struct1 = StructType().add("f1", StringType(), True).add("f2", StringType(), True, None) | ||
|
|
@@ -916,6 +921,7 @@ def test_struct_type(self): | |
|
|
||
| struct1 = StructType().add("f1", StringType(), True).add("f2", StringType(), True, None) | ||
| self.assertIs(struct1["f1"], struct1.fields[0]) | ||
| self.assertIs(struct1[u"f1"], struct1.fields[0]) | ||
| self.assertIs(struct1[0], struct1.fields[0]) | ||
| self.assertEqual(struct1[0:1], StructType(struct1.fields[0:1])) | ||
| with self.assertRaises(KeyError): | ||
|
|
||
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
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.
I am not sure if this change is needed. Because I think in SQL the column name is only allowed with alphabet, digit and underline, so it is a question why users will use unicode string as column in particular.
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.
According to f958f27, it seems to be possible to use Non-ascii characters in column name.
I think there are use cases which want to use non-ascii character in column name.
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.
ah, got it. I just mean from SQL parser.
Similarly, as the unicode column name will be encoded by
name.encode('utf-8'), it is now astrinstance. In other words, the schema still stores column names asstr. However, this change is allowing unicode input ascol. I think there will be mismatching.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.
So I think we don't need to do this.
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.
Thank you for answering. I understood why
isinstance(col, basestring)is not needed here.Although column name is basically stored as
str, it is stored asunicodein a certain case.See SPARK-15244 for details.
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.
Is there some harm in allowing unicode here though? If my column is
'a'and I callsampleBy(u'a')it will work after this change, otherwise it will throw an error. I think it's better to treat'a'andu'a'as equivalent...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.
I agree with you. There is no problem caused by allowing unicode here.
As you mentioned, it's better to handle
'a'andu'a'because there are few cases that unicode is passed. (e.g. when__future__.unicode_literalsis imported in Python 2.)