-
Notifications
You must be signed in to change notification settings - Fork 11
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
Added a new detector for catching unsupported COPY command structure #2106
Conversation
48f6f7c
to
c0f2b77
Compare
return nil // Not a COPY statement, nothing to detect | ||
} | ||
|
||
// Check for COPY FROM ... WHERE clause |
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.
Simplified code from what I was discussing in the morning:
I tried running your tests and they all passed, but do take a deeper look!
cc: @sanyamsinghal @priyanshi-yb
protoMsg := msg.Interface().(proto.Message)
copyStmt := protoMsg.(*pg_query.CopyStmt)
if copyStmt.IsFrom && copyStmt.WhereClause != nil {
d.copyFromWhereConstructDetected = true
}
for _, option := range copyStmt.Options {
defElemNode := option.GetDefElem()
if defElemNode != nil {
if defElemNode.Defname == "on_error" {
d.copyOnErrorConstructDetected = true
break
}
}
}
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 think yes this makes sense. It reduces the number of lines and readable.
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.
Lets stick to protomsg for this codepath, reason being the required info is not directly availabe in the struct and extraction of defElem can be generic and reused multiple times.
It can be simplified like this using existing helper functions:
FromField := GetMessageField(msg, "is_from")
whereField := GetMessageField(msg, "where_clause")
if FromField != nil && whereField != nil {
d.copyFromWhereConstructDetected = true
}
defNames := TraverseAndExtractDefNamesFromDefElem(msg)
if slices.Contains(defNames, "on_error") {
d.copyOnErrorConstructDetected = true
}
Refer my PR for TraverseAndExtractDefNamesFromDefElem
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.
just to be on the same page, there are two different issues here.
For the first one, isFrom and whereClause are directly available in CopyStmt
struct, so would prefer dealing with the struct directly for that issue. But GetMessageField(msg, "is_from"
isn't too bad either from a readability standpoint. I'll leave it up to you.
For the DefElem one, we can use the new function that Sanyam is introducing, but then you'll have to wait until that PR is merged.
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 had to add a new function GetBoolField() for the is_from clause. I have used the GetMessageField and GetBoolField approach rn for the first one.
For the second one, I have used Sanyam's function in my PR.
788e744
to
2967e80
Compare
return nil // Not a COPY statement, nothing to detect | ||
} | ||
|
||
// Check for COPY FROM ... WHERE clause |
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.
Lets stick to protomsg for this codepath, reason being the required info is not directly availabe in the struct and extraction of defElem can be generic and reused multiple times.
It can be simplified like this using existing helper functions:
FromField := GetMessageField(msg, "is_from")
whereField := GetMessageField(msg, "where_clause")
if FromField != nil && whereField != nil {
d.copyFromWhereConstructDetected = true
}
defNames := TraverseAndExtractDefNamesFromDefElem(msg)
if slices.Contains(defNames, "on_error") {
d.copyOnErrorConstructDetected = true
}
Refer my PR for TraverseAndExtractDefNamesFromDefElem
Also please add some simple end to end tests, just to cover - final assessment report is getting populated with the required info. |
937711e
to
8ce2110
Compare
…ERROR is still commented
9ccf2c6
to
3a6915f
Compare
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.
LGTM.
If you are planning to add end-to-end tests later, then do file a ticket for that.
@@ -649,3 +649,42 @@ func TestRegexFunctionsIssue(t *testing.T) { | |||
} | |||
|
|||
} | |||
|
|||
func TestCopyUnsupportedConstructIssuesDetected(t *testing.T) { |
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.
see if the tests for this can be at one place only either in detectors.go or parse_issue_detector_test.go
Currently we have the tests distributed among these two files, but since they are essentially testing the same thing, we can just have it one place.
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.
Yeah for now I have added the tests but commented out the sql of CREATE ... ON_ERROR since it is supported only on PG17. Have added a ticket to do the same:
#2119
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.
Removed the tests from detectors_test.go. The tests are same anyways. They were helpful in development.
Describe the changes in this pull request
Reporting two unsupported query constructs related to COPY command:
COPY FROM ... WHERE
COPY ... ON_ERROR
Report unsupported COPY command syntax #1991
Added a new detector
CopyCommandUnsupportedConstructsDetector
indetectors.go
to report both these constructsCurrently the PR also contains changes related to pg_query_go v6 upgrades. Will rebase the PR to main once I merge that PR
Describe if there are any user-facing changes
The assessment report now reports these constructs under UQC:
How was this pull request tested?
Added new unit tests for these copy constructs in:
parser_issue_detector_test.go
issue_dml_test.go
detectors_test.go
An end to end test for the same construct needs to be added
Does your PR have changes that can cause upgrade issues?