Skip to content
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

[SPARK-48490][CORE] Unescapes any literals for message of MessageWithContext #46824

Closed
wants to merge 2 commits into from

Conversation

panbingkun
Copy link
Contributor

@panbingkun panbingkun commented May 31, 2024

What changes were proposed in this pull request?

The pr aims to unescapes any literals for message of MessageWithContext

Why are the changes needed?

  • For example, before this PR
logInfo("This is a log message\nThis is a new line \t other msg")

It will output:

24/05/31 22:53:27 INFO PatternLoggingSuite: This is a log message
This is a new line 	 other msg

But:

logInfo(log"This is a log message\nThis is a new line \t other msg")

It will output:

24/05/31 22:53:59 ERROR PatternLoggingSuite: This is a log message\nThis is a new line \t other msg

Obviously, the latter is not the result we expected.

Does this PR introduce any user-facing change?

Yes, fix bug.

How was this patch tested?

  • Add new UT.
  • Pass GA.

Was this patch authored or co-authored using generative AI tooling?

No.

@panbingkun panbingkun marked this pull request as ready for review May 31, 2024 15:01
@panbingkun
Copy link
Contributor Author

cc @gengliangwang

@panbingkun
Copy link
Contributor Author

@gengliangwang
In addition, we also encountered this issue during migration: #46822

@gengliangwang
Copy link
Member

Thanks, merging to master

riyaverm-db pushed a commit to riyaverm-db/spark that referenced this pull request Jun 7, 2024
…Context

### What changes were proposed in this pull request?
The pr aims to `unescapes` any literals for `message` of `MessageWithContext`

### Why are the changes needed?
- For example, before this PR
```
logInfo("This is a log message\nThis is a new line \t other msg")
```
It will output:
```
24/05/31 22:53:27 INFO PatternLoggingSuite: This is a log message
This is a new line 	 other msg
```

But:

```
logInfo(log"This is a log message\nThis is a new line \t other msg")
```
It will output:
```
24/05/31 22:53:59 ERROR PatternLoggingSuite: This is a log message\nThis is a new line \t other msg
```

Obviously, the latter is not the result we `expected`.

### Does this PR introduce _any_ user-facing change?
Yes, fix bug.

### How was this patch tested?
- Add new UT.
- Pass GA.

### Was this patch authored or co-authored using generative AI tooling?
No.

Closes apache#46824 from panbingkun/SPARK-48490.

Authored-by: panbingkun <[email protected]>
Signed-off-by: Gengliang Wang <[email protected]>
@@ -99,7 +100,7 @@ case class MessageWithContext(message: String, context: java.util.HashMap[String
* Companion class for lazy evaluation of the MessageWithContext instance.
*/
class LogEntry(messageWithContext: => MessageWithContext) {
def message: String = messageWithContext.message
def message: String = StringEscapeUtils.unescapeJava(messageWithContext.message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @panbingkun and @gengliangwang .

This seems to break SparkR somehow. Could you take a look at this?

-- Error ('test_basic.R:25:3'): create DataFrame from list or data.frame -------
Error in `handleErrors(returnStatus, conn)`: java.lang.IllegalArgumentException: Unable to parse unicode value: serF
	at org.apache.commons.text.translate.UnicodeUnescaper.translate(UnicodeUnescaper.java:55)
	at org.apache.commons.text.translate.AggregateTranslator.translate(AggregateTranslator.java:58)
	at org.apache.commons.text.translate.CharSequenceTranslator.translate(CharSequenceTranslator.java:101)
	at org.apache.commons.text.translate.CharSequenceTranslator.translate(CharSequenceTranslator.java:63)
	at org.apache.commons.text.StringEscapeUtils.unescapeJava(StringEscapeUtils.java:802)
	at org.apache.spark.internal.LogEntry.message(Logging.scala:103)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, Let me investigate it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dongjoon-hyun
I have identified the root cause of this issue and am currently attempting to fix it
#46897 (comment)

yaooqinn pushed a commit that referenced this pull request Jun 21, 2024
### What changes were proposed in this pull request?

Even with the fix in #46824, the escape sequences (`\r`, `\n`, `\t`, etc) are not handled properly. For example, when we use `log"\n"`, the StringContext interprets `\n` as a literal backslash `\` followed by `n` instead of a newline character. As a result, the bytes of `log"\n".message` becomes `[92, 110]`, instead of `[10]`.

This PR is to fix the issue by using the method StringContext.processEscapes in `LogStringContext`.

### Why are the changes needed?

 To ensure that escape sequences are properly processed in Spark logs

### Does this PR introduce _any_ user-facing change?

No
### How was this patch tested?

New UT

### Was this patch authored or co-authored using generative AI tooling?

No

Closes #47050 from gengliangwang/fixEscape.

Authored-by: Gengliang Wang <[email protected]>
Signed-off-by: Kent Yao <[email protected]>
attilapiros pushed a commit to attilapiros/spark that referenced this pull request Oct 4, 2024
### What changes were proposed in this pull request?

Even with the fix in apache#46824, the escape sequences (`\r`, `\n`, `\t`, etc) are not handled properly. For example, when we use `log"\n"`, the StringContext interprets `\n` as a literal backslash `\` followed by `n` instead of a newline character. As a result, the bytes of `log"\n".message` becomes `[92, 110]`, instead of `[10]`.

This PR is to fix the issue by using the method StringContext.processEscapes in `LogStringContext`.

### Why are the changes needed?

 To ensure that escape sequences are properly processed in Spark logs

### Does this PR introduce _any_ user-facing change?

No
### How was this patch tested?

New UT

### Was this patch authored or co-authored using generative AI tooling?

No

Closes apache#47050 from gengliangwang/fixEscape.

Authored-by: Gengliang Wang <[email protected]>
Signed-off-by: Kent Yao <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants