Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mc2mc/mc2mc.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func mc2mc(envs []string) error {
query.WithCostAttributionLabel(cfg.CostAttributionTeam),
query.WithMethod(query.MERGE),
query.WithDryRun(cfg.DryRun),
query.WithDisableMultiQueryGeneration(cfg.DisableMultiQueryGeneration),
).Build()
if err != nil {
return errors.WithStack(err)
Expand Down
11 changes: 6 additions & 5 deletions mc2mc/pkg/query/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
disableMultiQueryGeneration bool
}

// NewBuilder creates a new query builder with the given options
Expand Down Expand Up @@ -208,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.enableDryRun && len(queries) == 1 {

@sravankorumilli sravankorumilli Oct 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

for dryrun can you check again, was thinking it would be good if same queries are executed in dry run & in prod

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

will be covered in a separate MR

// 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))
}
Expand Down
71 changes: 69 additions & 2 deletions mc2mc/pkg/query/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;`
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -992,6 +1002,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.WithDisableMultiQueryGeneration(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.WithDisableMultiQueryGeneration(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) {
Expand Down
6 changes: 6 additions & 0 deletions mc2mc/pkg/query/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ func WithDryRun(enable bool) Option {
b.enableDryRun = enable
}
}

func WithDisableMultiQueryGeneration(disable bool) Option {
return func(b *Builder) {
b.disableMultiQueryGeneration = disable
}
}
Loading