From 936de7f37bcd9ae30eca422909cdf8230d914510 Mon Sep 17 00:00:00 2001 From: "Ahmad N. F" Date: Thu, 22 Oct 2026 09:37:02 +0700 Subject: [PATCH 1/3] disable merge split query --- mc2mc/mc2mc.go | 7 +++- mc2mc/pkg/query/builder.go | 21 ++++++++++-- mc2mc/pkg/query/builder_test.go | 57 +++++++++++++++++++++++++++++++++ mc2mc/pkg/query/option.go | 6 ++++ 4 files changed, 87 insertions(+), 4 deletions(-) diff --git a/mc2mc/mc2mc.go b/mc2mc/mc2mc.go index ed4101b..85f515d 100644 --- a/mc2mc/mc2mc.go +++ b/mc2mc/mc2mc.go @@ -159,11 +159,16 @@ func mc2mc(envs []string) error { query.WithCostAttributionLabel(cfg.CostAttributionTeam), query.WithMethod(query.MERGE), query.WithDryRun(cfg.DryRun), + query.WithDisableSplitMergeQuery(cfg.DisableMultiQueryGeneration), ).Build() if err != nil { return errors.WithStack(err) } - queriesToExecute = append(queriesToExecute, strings.Split(queryToExecute, query.BREAK_MARKER)...) + if cfg.DisableMultiQueryGeneration { + queriesToExecute = append(queriesToExecute, queryToExecute) + } else { + queriesToExecute = append(queriesToExecute, strings.Split(queryToExecute, query.BREAK_MARKER)...) + } default: return errors.Errorf("not supported load method: %s", cfg.LoadMethod) } diff --git a/mc2mc/pkg/query/builder.go b/mc2mc/pkg/query/builder.go index 06710e9..dc4585a 100644 --- a/mc2mc/pkg/query/builder.go +++ b/mc2mc/pkg/query/builder.go @@ -28,9 +28,10 @@ type Builder struct { orderedColumns []string overridedValues map[string]string - enableAutoPartition bool - enablePartitionValue bool - enableDryRun bool + enableAutoPartition bool + enablePartitionValue bool + enableDryRun bool + disableSplitMergeQuery bool } // NewBuilder creates a new query builder with the given options @@ -67,6 +68,13 @@ func (b *Builder) Build() (string, error) { } if b.method == MERGE { + if b.disableSplitMergeQuery { + query := b.query + if b.costAttributionTeam != "" { + query = fmt.Sprintf("%s\n%s", query, getCostAttributionComment(b.costAttributionTeam)) + } + return query, nil + } // split query components hrs, vars, queries := SplitQueryComponents(b.query) query := b.constructMergeQuery(hrs, vars, queries) @@ -208,6 +216,13 @@ func (b *Builder) constructOverridedValues(query string) (string, error) { // constructMergeQueries constructs merge queries with headers and variables func (b *Builder) constructMergeQuery(hrs, vars, queries []string) string { + if b.disableSplitMergeQuery { + if b.costAttributionTeam != "" { + return fmt.Sprintf("%s\n%s", b.query, getCostAttributionComment(b.costAttributionTeam)) + } + return b.query + } + if !b.enableDryRun && len(queries) == 1 { // maintaining existing logic for single query if b.costAttributionTeam != "" { diff --git a/mc2mc/pkg/query/builder_test.go b/mc2mc/pkg/query/builder_test.go index 64292df..bbe0df0 100644 --- a/mc2mc/pkg/query/builder_test.go +++ b/mc2mc/pkg/query/builder_test.go @@ -992,6 +992,63 @@ WHEN MATCHED THEN UPDATE SET append_test.id = 2 ;`, query) }) + + t.Run("returns query for merge load method with multiple dml and ddl when disable split merge query is true", func(t *testing.T) { + queryToExecute := `SET odps.table.append2.enable=true; + +CREATE TABLE IF NOT EXISTS append_test (id bigint) +TBLPROPERTIES('table.format.version'='2'); + +INSERT OVERWRITE TABLE append_test VALUES(0),(1); + +@src := SELECT 1 id; + +MERGE INTO append_test +USING (SELECT * FROM @src) source +on append_test.id = source.id +WHEN MATCHED THEN UPDATE +SET append_test.id = 2;` + odspClient := &mockOdpsClient{} + + query, err := query.NewBuilder( + logger.NewDefaultLogger(), + odspClient, + query.WithQuery(queryToExecute), + query.WithMethod(query.MERGE), + query.WithDisableSplitMergeQuery(true), + ).Build() + assert.NoError(t, err) + assert.Equal(t, queryToExecute, query) + }) + + t.Run("returns query for merge load method with cost attribution label when disable split merge query is true", func(t *testing.T) { + queryToExecute := `SET odps.table.append2.enable=true; + +CREATE TABLE IF NOT EXISTS append_test (id bigint) +TBLPROPERTIES('table.format.version'='2'); + +INSERT OVERWRITE TABLE append_test VALUES(0),(1); + +@src := SELECT 1 id; + +MERGE INTO append_test +USING (SELECT * FROM @src) source +on append_test.id = source.id +WHEN MATCHED THEN UPDATE +SET append_test.id = 2;` + odspClient := &mockOdpsClient{} + + query, err := query.NewBuilder( + logger.NewDefaultLogger(), + odspClient, + query.WithQuery(queryToExecute), + query.WithMethod(query.MERGE), + query.WithDisableSplitMergeQuery(true), + query.WithCostAttributionLabel("test-team"), + ).Build() + assert.NoError(t, err) + assert.Equal(t, queryToExecute+"\n--cost_attribution_team=test-team\n", query) + }) } func TestBuilder_BuildWithDryRun(t *testing.T) { diff --git a/mc2mc/pkg/query/option.go b/mc2mc/pkg/query/option.go index f1f5ab8..b696ad0 100644 --- a/mc2mc/pkg/query/option.go +++ b/mc2mc/pkg/query/option.go @@ -58,3 +58,9 @@ func WithDryRun(enable bool) Option { b.enableDryRun = enable } } + +func WithDisableSplitMergeQuery(enable bool) Option { + return func(b *Builder) { + b.disableSplitMergeQuery = enable + } +} From 93093fac6d59e0716982256038f2189e6abacee3 Mon Sep 17 00:00:00 2001 From: "Ahmad N. F" Date: Thu, 22 Oct 2026 15:56:11 +0700 Subject: [PATCH 2/3] fix --- mc2mc/mc2mc.go | 8 ++------ mc2mc/pkg/query/builder.go | 24 +++++------------------- mc2mc/pkg/query/builder_test.go | 18 ++++++++++++++---- mc2mc/pkg/query/option.go | 4 ++-- 4 files changed, 23 insertions(+), 31 deletions(-) diff --git a/mc2mc/mc2mc.go b/mc2mc/mc2mc.go index 85f515d..8aafd81 100644 --- a/mc2mc/mc2mc.go +++ b/mc2mc/mc2mc.go @@ -159,16 +159,12 @@ func mc2mc(envs []string) error { query.WithCostAttributionLabel(cfg.CostAttributionTeam), query.WithMethod(query.MERGE), query.WithDryRun(cfg.DryRun), - query.WithDisableSplitMergeQuery(cfg.DisableMultiQueryGeneration), + query.WithDisableMultiQueryGeneration(cfg.DisableMultiQueryGeneration), ).Build() if err != nil { return errors.WithStack(err) } - if cfg.DisableMultiQueryGeneration { - queriesToExecute = append(queriesToExecute, queryToExecute) - } else { - queriesToExecute = append(queriesToExecute, strings.Split(queryToExecute, query.BREAK_MARKER)...) - } + queriesToExecute = append(queriesToExecute, strings.Split(queryToExecute, query.BREAK_MARKER)...) default: return errors.Errorf("not supported load method: %s", cfg.LoadMethod) } diff --git a/mc2mc/pkg/query/builder.go b/mc2mc/pkg/query/builder.go index dc4585a..1cdc33d 100644 --- a/mc2mc/pkg/query/builder.go +++ b/mc2mc/pkg/query/builder.go @@ -28,10 +28,10 @@ type Builder struct { orderedColumns []string overridedValues map[string]string - enableAutoPartition bool - enablePartitionValue bool - enableDryRun bool - disableSplitMergeQuery bool + enableAutoPartition bool + enablePartitionValue bool + enableDryRun bool + disableMultiQueryGeneration bool } // NewBuilder creates a new query builder with the given options @@ -68,13 +68,6 @@ func (b *Builder) Build() (string, error) { } if b.method == MERGE { - if b.disableSplitMergeQuery { - query := b.query - if b.costAttributionTeam != "" { - query = fmt.Sprintf("%s\n%s", query, getCostAttributionComment(b.costAttributionTeam)) - } - return query, nil - } // split query components hrs, vars, queries := SplitQueryComponents(b.query) query := b.constructMergeQuery(hrs, vars, queries) @@ -216,14 +209,7 @@ func (b *Builder) constructOverridedValues(query string) (string, error) { // constructMergeQueries constructs merge queries with headers and variables func (b *Builder) constructMergeQuery(hrs, vars, queries []string) string { - if b.disableSplitMergeQuery { - if b.costAttributionTeam != "" { - return fmt.Sprintf("%s\n%s", b.query, getCostAttributionComment(b.costAttributionTeam)) - } - return b.query - } - - if !b.enableDryRun && len(queries) == 1 { + if b.disableMultiQueryGeneration { // maintaining existing logic for single query if b.costAttributionTeam != "" { return fmt.Sprintf("%s\n%s", b.query, getCostAttributionComment(b.costAttributionTeam)) diff --git a/mc2mc/pkg/query/builder_test.go b/mc2mc/pkg/query/builder_test.go index bbe0df0..b9c9bbc 100644 --- a/mc2mc/pkg/query/builder_test.go +++ b/mc2mc/pkg/query/builder_test.go @@ -31,7 +31,8 @@ func TestBuilder_Build(t *testing.T) { query.WithMethod(query.MERGE), ).Build() assert.NoError(t, err) - assert.Equal(t, queryToExecute, query) + assert.Equal(t, `select * from project.playground.table +;`, query) }) t.Run("returns error for append load method when destination table is not specify", func(t *testing.T) { queryToExecute := `select * from project.playground.table;` @@ -546,7 +547,16 @@ SET append_test.id = 2;` query.WithMethod(query.MERGE), ).Build() assert.NoError(t, err) - assert.Equal(t, queryToExecute, query) + assert.Equal(t, `SET odps.table.append2.enable=true +; +@src := SELECT 1 id +; +MERGE INTO append_test +USING (SELECT * FROM @src) source +on append_test.id = source.id +WHEN MATCHED THEN UPDATE +SET append_test.id = 2 +;`, query) }) t.Run("returns query for merge load method with drop and create table", func(t *testing.T) { queryToExecute := `SET odps.table.append2.enable=true; @@ -1015,7 +1025,7 @@ SET append_test.id = 2;` odspClient, query.WithQuery(queryToExecute), query.WithMethod(query.MERGE), - query.WithDisableSplitMergeQuery(true), + query.WithDisableMultiQueryGeneration(true), ).Build() assert.NoError(t, err) assert.Equal(t, queryToExecute, query) @@ -1043,7 +1053,7 @@ SET append_test.id = 2;` odspClient, query.WithQuery(queryToExecute), query.WithMethod(query.MERGE), - query.WithDisableSplitMergeQuery(true), + query.WithDisableMultiQueryGeneration(true), query.WithCostAttributionLabel("test-team"), ).Build() assert.NoError(t, err) diff --git a/mc2mc/pkg/query/option.go b/mc2mc/pkg/query/option.go index b696ad0..69b60c8 100644 --- a/mc2mc/pkg/query/option.go +++ b/mc2mc/pkg/query/option.go @@ -59,8 +59,8 @@ func WithDryRun(enable bool) Option { } } -func WithDisableSplitMergeQuery(enable bool) Option { +func WithDisableMultiQueryGeneration(disable bool) Option { return func(b *Builder) { - b.disableSplitMergeQuery = enable + b.disableMultiQueryGeneration = disable } } From e8b90a2feb5a596d8aee7a13fa388d57dfb3e5f4 Mon Sep 17 00:00:00 2001 From: "Ahmad N. F" Date: Thu, 22 Oct 2026 16:20:34 +0700 Subject: [PATCH 3/3] enable data --- mc2mc/pkg/query/builder.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mc2mc/pkg/query/builder.go b/mc2mc/pkg/query/builder.go index 1cdc33d..e2003e6 100644 --- a/mc2mc/pkg/query/builder.go +++ b/mc2mc/pkg/query/builder.go @@ -209,8 +209,8 @@ func (b *Builder) constructOverridedValues(query string) (string, error) { // constructMergeQueries constructs merge queries with headers and variables func (b *Builder) constructMergeQuery(hrs, vars, queries []string) string { - if b.disableMultiQueryGeneration { - // maintaining existing logic for single query + // for dry run purposes, multiquery generation will still be generated + if !b.enableDryRun && b.disableMultiQueryGeneration { if b.costAttributionTeam != "" { return fmt.Sprintf("%s\n%s", b.query, getCostAttributionComment(b.costAttributionTeam)) }