Skip to content

[SPARK-49538][SQL][TESTS] Detect unused error message parameters#48026

Closed
MaxGekk wants to merge 13 commits into
apache:masterfrom
MaxGekk:detect-unused-error-params
Closed

[SPARK-49538][SQL][TESTS] Detect unused error message parameters#48026
MaxGekk wants to merge 13 commits into
apache:masterfrom
MaxGekk:detect-unused-error-params

Conversation

@MaxGekk

@MaxGekk MaxGekk commented Sep 8, 2024

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

In the PR, I propose to detect unused error message parameters while substituting placeholders in error message formats, and raise an internal error in tests when the number of parameters is greater than the number of placeholders. That might indicate presence of unused message parameters.

Why are the changes needed?

Might happens that the passed error message parameters and placeholders in message format are not matched, and contain extra items. From the code maintainability perspective, it would be nice to detect such cases while running tests.

For example, the error message format could look like:

"CANNOT_UP_CAST_DATATYPE" : {
  "message" : [
    "Cannot up cast <expression> from <sourceType> to <targetType>.",
    "<details>"
    ],
  "sqlState" : "42846"
},

but the passed message parameters have extra parameter:

messageParameters = Map(
  "expression" -> "CAST('aaa' AS LONG)",
  "sourceType" -> "STRING",
  "targetType" -> "LONG",
  "op" -> "CAST", // unused parameter
  "details" -> "implicit cast"
))

This can happen because:

  • tech editor/dev forgot to mention some parameters in error message format,
  • wrong usage of error conditions/sub-conditions
  • source code became outdated
  • typos in error message formats

Does this PR introduce any user-facing change?

No.

How was this patch tested?

By running the added test via:

$ build/mvn -Dtest=none -DwildcardSuites=org.apache.spark.SparkThrowableSuite test

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

No.

@github-actions github-actions Bot added the CORE label Sep 8, 2024
"<leftPlan>",
"and",
"rightPlan",
"<rightPlan>",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The purpose of this PR is to detect mistakes like this.

@MaxGekk MaxGekk changed the title [WIP][SPARK-49538][SQL][TESTS] Detect unused error message parameters [SPARK-49538][SQL][TESTS] Detect unused error message parameters Sep 8, 2024
@MaxGekk MaxGekk marked this pull request as ready for review September 8, 2024 16:31
Comment on lines +4274 to +4275
"Query [id = <id>, runId = <runId>, startOffset = <startOffset>, endOffset = <endOffset>] terminated with exception: <message>",
"<queryDebugString>"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@HeartSaVioR Could you look at the changes, please.

@HeartSaVioR HeartSaVioR Sep 9, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems to be slightly messed up -

https://github.com/apache/spark/blob/master/common/utils/src/main/scala/org/apache/spark/sql/streaming/StreamingQueryException.scala

This class overrides getMessage and handles queryDebugString separately. So it's expected to not have queryDebugString here.

What I'm confused is why we have a case where queryDebugString is passed over message parameters, since we have constructors which directly receive queryDebugString.

@MaxGekk MaxGekk Sep 9, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So it's expected to not have queryDebugString here.

@HeartSaVioR ok, let me remove it from message parameters and from the error format. How about other 2 unused parameters: startOffset and endOffset. Does queryDebugString include the info, it seems?

  private def toDebugString(includeLogicalPlan: Boolean): String = {
    val debugString =
      s"""|=== Streaming Query ===
          |Identifier: $prettyIdString
          |Current Committed Offsets: ${getLatestExecutionContext().startOffsets}
          |Current Available Offsets: ${getLatestExecutionContext().endOffsets}

@MaxGekk

MaxGekk commented Sep 8, 2024

Copy link
Copy Markdown
Member Author

@srielau Please, have a look at the PR.

Comment on lines -373 to -379
"queryDebugString" -> toDebugString(includeLogicalPlan = isInitialized),
"startOffset" -> getLatestExecutionContext().startOffsets.toOffsetSeq(
sources.toSeq, getLatestExecutionContext().offsetSeqMetadata).toString,
"endOffset" -> getLatestExecutionContext().endOffsets.toOffsetSeq(
sources.toSeq, getLatestExecutionContext().offsetSeqMetadata).toString

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As @HeartSaVioR mentioned in a comment, the parameters are included in message in some circumstances.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah not sure why this passed through params as we have params in constructor. Thanks for fixing this!

@@ -7827,7 +7827,7 @@
},
"_LEGACY_ERROR_TEMP_3055" : {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: If we're already touching this _LEGACY, could we just name it? (is this an internal error??)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Comment thread common/utils/src/main/resources/error/error-conditions.json
@MaxGekk

MaxGekk commented Sep 9, 2024

Copy link
Copy Markdown
Member Author

@HeartSaVioR Do you have any objections for the changes?

"NON_STRING_TYPE" : {
"message" : [
"all arguments must be strings."
"all arguments of the function <funcName> must be strings."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch!

@MaxGekk

MaxGekk commented Sep 10, 2024

Copy link
Copy Markdown
Member Author

Merging to master. Thank you, @HeartSaVioR @srielau @allisonwang-db @HyukjinKwon for review.

@MaxGekk MaxGekk closed this in 5f2d839 Sep 10, 2024
MaxGekk added a commit that referenced this pull request Sep 10, 2024
…Class` in `SparkThrowableSuite` and in `DateTimeFormatterHelperSuite`

### What changes were proposed in this pull request?
In the PR, I propose to use `condition` instead of `errorClass` in two test suites:
- SparkThrowableSuite
- DateTimeFormatterHelperSuite

### Why are the changes needed?
Because the changes from the PR #48027 conflict to #48058 and #48026, and tests in #48027 were passed earlier than the last PRs were merged to master.

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

### How was this patch tested?
By compiling and running the following tests locally:
```
$ build/sbt "test:testOnly *org.apache.spark.sql.catalyst.util.DateTimeFormatterHelperSuite"
$ build/sbt "test:testOnly *SparkThrowableSuite"
```

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

Closes #48061 from MaxGekk/fix-missing-errorClass.

Authored-by: Max Gekk <max.gekk@gmail.com>
Signed-off-by: Max Gekk <max.gekk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants