From a77144eb86f130ac9642c9364bdfa5a946db252b Mon Sep 17 00:00:00 2001 From: Dery Rahman Ahaddienata Date: Thu, 13 Feb 2025 16:05:30 +0700 Subject: [PATCH] fix: omit last comment in execution --- mc2mc/internal/query/builder_test.go | 3 --- mc2mc/internal/query/helper.go | 17 +++++++---------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/mc2mc/internal/query/builder_test.go b/mc2mc/internal/query/builder_test.go index edd5984..f58b886 100644 --- a/mc2mc/internal/query/builder_test.go +++ b/mc2mc/internal/query/builder_test.go @@ -446,7 +446,6 @@ select * from project.playground.table SELECT col1, col2, _partitiontime FROM ( SELECT col1, col2, TIMESTAMP('2021-01-01') as _partitiontime FROM ( select * from project.playground.table --- this is comment ) ) ;`, queryToExecute) @@ -657,7 +656,6 @@ END function my_add(@a BIGINT) as @a + 1 ; @src := SELECT my_add(1) id --- this is comment ; INSERT OVERWRITE TABLE append_test VALUES(0),(1) ; @@ -676,7 +674,6 @@ END function my_add(@a BIGINT) as @a + 1 ; @src := SELECT my_add(1) id --- this is comment ; MERGE INTO append_test USING (SELECT castStringToBoolean(id) FROM @src) source diff --git a/mc2mc/internal/query/helper.go b/mc2mc/internal/query/helper.go index 653b43f..83e742f 100644 --- a/mc2mc/internal/query/helper.go +++ b/mc2mc/internal/query/helper.go @@ -36,12 +36,11 @@ func SeparateHeadersAndQuery(query string) (string, string) { stmtWithoutComment := RemoveComments(stmt) if headerPattern.MatchString(strings.TrimSpace(stmtWithoutComment)) { headers = append(headers, stmt) + } else if strings.TrimSpace(stmtWithoutComment) == "" { + // if the statement is empty, it's a comment, then omit it + // since it doesn't make sense to execute this statement + continue } else { - // if the statement is empty, it's a comment, then add it to the previous query - if strings.TrimSpace(stmtWithoutComment) == "" { - remainingQueries[len(remainingQueries)-1] += fmt.Sprintf("\n%s\n", stmt) - continue - } remainingQueries = append(remainingQueries, stmt) } } @@ -77,12 +76,10 @@ func SeparateVariablesUDFsAndQuery(query string) (string, string) { if variablePattern.MatchString(strings.TrimSpace(stmtWithoutComment)) || udfPattern.MatchString(strings.TrimSpace(stmtWithoutComment)) { variablesAndUDFs = append(variablesAndUDFs, stmt) + } else if strings.TrimSpace(stmtWithoutComment) == "" { + // if the statement is empty, it's a comment, then omit it + // since it doesn't make sense to execute this statement } else { - // if the statement is empty, it's a comment, then add it to the previous query - if strings.TrimSpace(stmtWithoutComment) == "" { - remainingQueries[len(remainingQueries)-1] += fmt.Sprintf("\n%s\n", stmt) - continue - } remainingQueries = append(remainingQueries, stmt) } }