Skip to content

Commit

Permalink
fix(mc2mc): remove comment consistently (#69)
Browse files Browse the repository at this point in the history
fix: remove comment consistently
  • Loading branch information
deryrahman authored Feb 6, 2025
1 parent c4f5ec2 commit 84fc701
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
21 changes: 11 additions & 10 deletions mc2mc/internal/query/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const (
)

var (
semicolonPattern = regexp.MustCompile(`;(\n+|$)`) // regex to match semicolons
commentPattern = regexp.MustCompile(`(?m)\s*--.*\n?`) // regex to match comments
multiCommentPattern = regexp.MustCompile(`(?s)\s*/\*.*?\*/\s*\n?`) // regex to match multi-line comments
headerPattern = regexp.MustCompile(`(?i)^set`) // regex to match header statements
variablePattern = regexp.MustCompile(`(?i)^@`) // regex to match variable statements
udfPattern = regexp.MustCompile(`(?i)^function\s+`) // regex to match UDF statements
ddlPattern = regexp.MustCompile(`(?i)^CREATE\s+`) // regex to match DDL statements
semicolonPattern = regexp.MustCompile(`;(\n+|$)`) // regex to match semicolons
commentPattern = regexp.MustCompile(`--[^\n]*`) // regex to match comments
multiCommentPattern = regexp.MustCompile(`(?s)/\*.*?\*/`) // regex to match multi-line comments
headerPattern = regexp.MustCompile(`(?i)^set`) // regex to match header statements
variablePattern = regexp.MustCompile(`(?i)^@`) // regex to match variable statements
udfPattern = regexp.MustCompile(`(?i)^function\s+`) // regex to match UDF statements
ddlPattern = regexp.MustCompile(`(?i)^CREATE\s+`) // regex to match DDL statements
)

func SeparateHeadersAndQuery(query string) (string, string) {
Expand All @@ -32,7 +32,7 @@ func SeparateHeadersAndQuery(query string) (string, string) {
continue
}
stmtWithoutComment := commentPattern.ReplaceAllString(stmt, "")
if headerPattern.MatchString(stmtWithoutComment) {
if headerPattern.MatchString(strings.TrimSpace(stmtWithoutComment)) {
headers = append(headers, stmt)
} else {
remainingQueries = append(remainingQueries, stmt)
Expand Down Expand Up @@ -67,7 +67,8 @@ func SeparateVariablesUDFsAndQuery(query string) (string, string) {
continue
}
stmtWithoutComment := commentPattern.ReplaceAllString(stmt, "")
if variablePattern.MatchString(stmtWithoutComment) || udfPattern.MatchString(stmtWithoutComment) {
if variablePattern.MatchString(strings.TrimSpace(stmtWithoutComment)) ||
udfPattern.MatchString(strings.TrimSpace(stmtWithoutComment)) {
variablesAndUDFs = append(variablesAndUDFs, stmt)
} else {
remainingQueries = append(remainingQueries, stmt)
Expand All @@ -92,7 +93,7 @@ func SeparateVariablesUDFsAndQuery(query string) (string, string) {
func RemoveComments(query string) string {
query = commentPattern.ReplaceAllString(query, "")
query = multiCommentPattern.ReplaceAllString(query, "")
return strings.TrimSpace(query)
return query
}

func IsDDL(query string) bool {
Expand Down
18 changes: 14 additions & 4 deletions mc2mc/internal/query/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,28 @@ func TestRemoveComments(t *testing.T) {
q1 := `-- comment here
SELECT * FROM project.dataset.table; -- comment there`
query := query.RemoveComments(q1)
assert.Equal(t, `SELECT * FROM project.dataset.table;`, query)
assert.Equal(t, `
SELECT * FROM project.dataset.table; `, query)

})
t.Run("returns query without multiline comments", func(t *testing.T) {
q1 := `/* comment here
another
*/
*/
SELECT * FROM project.dataset.table; -- comment there
`
query := query.RemoveComments(q1)
assert.Equal(t, `SELECT * FROM project.dataset.table;`, query)
assert.Equal(t, `
SELECT * FROM project.dataset.table;
`, query)
})
t.Run("returns query without comment and no changing query structure", func(t *testing.T) {
q1 := `SELECT * -- comment here
FROM project.dataset.table;`
query := query.RemoveComments(q1)
assert.Equal(t, `SELECT *
FROM project.dataset.table;`, query)
})
}

0 comments on commit 84fc701

Please sign in to comment.