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
3 changes: 1 addition & 2 deletions mc2mc/internal/client/odps.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func NewODPSClient(logger *slog.Logger, client *odps.Odps) *odpsClient {
// when context is cancelled.
func (c *odpsClient) ExecSQL(ctx context.Context, query string, additionalHints map[string]string) error {
if c.isDryRun {
c.logger.Info("dry run mode, skipping execution")
return nil
c.logger.Info("[DRY-RUN] running query in dry-run mode with EXPLAIN.")
}

hints := addHints(additionalHints, query)
Expand Down
5 changes: 4 additions & 1 deletion mc2mc/mc2mc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/goto/transformers/mc2mc/internal/client"
"github.com/goto/transformers/mc2mc/internal/config"
"github.com/goto/transformers/mc2mc/internal/logger"
"github.com/goto/transformers/mc2mc/internal/query"
"github.com/goto/transformers/mc2mc/pkg/query"
)

func mc2mc(envs []string) error {
Expand Down Expand Up @@ -84,6 +84,7 @@ func mc2mc(envs []string) error {
query.WithPartitionValue(cfg.DevEnablePartitionValue == "true"),
query.WithCostAttributionLabel(cfg.CostAttributionTeam),
query.WithColumnOrder(),
query.WithDryRun(cfg.DryRun),
).Build()
if err != nil {
return errors.WithStack(err)
Expand All @@ -100,6 +101,7 @@ func mc2mc(envs []string) error {
query.WithPartitionValue(cfg.DevEnablePartitionValue == "true"),
query.WithCostAttributionLabel(cfg.CostAttributionTeam),
query.WithColumnOrder(),
query.WithDryRun(cfg.DryRun),
)

// -- TODO(START): refactor this part --
Expand Down Expand Up @@ -155,6 +157,7 @@ func mc2mc(envs []string) error {
query.WithQuery(string(raw)),
query.WithCostAttributionLabel(cfg.CostAttributionTeam),
query.WithMethod(query.MERGE),
query.WithDryRun(cfg.DryRun),
).Build()
if err != nil {
return errors.WithStack(err)
Expand Down
37 changes: 28 additions & 9 deletions mc2mc/internal/query/builder.go → mc2mc/pkg/query/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Builder struct {

enableAutoPartition bool
enablePartitionValue bool
enableDryRun bool
}

// NewBuilder creates a new query builder with the given options
Expand Down Expand Up @@ -68,12 +69,6 @@ func (b *Builder) Build() (string, error) {
if b.method == MERGE {
// split query components
hrs, vars, queries := SplitQueryComponents(b.query)
if len(queries) <= 1 {
if b.costAttributionTeam != "" {
return fmt.Sprintf("%s\n%s", b.query, getCostAttributionComment(b.costAttributionTeam)), nil
}
return b.query, nil
}
query := b.constructMergeQuery(hrs, vars, queries)
return query, nil
}
Expand Down Expand Up @@ -141,18 +136,30 @@ func (b *Builder) Build() (string, error) {
}
}

if b.enableDryRun {
// append explain to the query for dry run
query = fmt.Sprintf("EXPLAIN\n%s", query)
}

// construct final query with headers, drops, variables and udfs
if hr != "" {
hr += "\n"
}
if drops != "" {
drops += "\n"
dropsStr := ""
if len(drops) > 0 {
if b.enableDryRun {
for i, drop := range drops {
drops[i] = fmt.Sprintf("EXPLAIN\n%s", drop)
}
}
dropsStr = strings.Join(drops, "\n;\n")
dropsStr += "\n;\n"
}
if varsAndUDFs != "" {
varsAndUDFs += "\n"
}

query = fmt.Sprintf("%s%s%s%s", hr, drops, varsAndUDFs, query)
query = fmt.Sprintf("%s%s%s%s", hr, dropsStr, varsAndUDFs, query)
if b.costAttributionTeam != "" {
query = fmt.Sprintf("%s\n%s\n", query, getCostAttributionComment(b.costAttributionTeam))
}
Expand Down Expand Up @@ -201,6 +208,14 @@ 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 {
// maintaining existing logic for single query
if b.costAttributionTeam != "" {
return fmt.Sprintf("%s\n%s", b.query, getCostAttributionComment(b.costAttributionTeam))
}
return b.query
}

builder := strings.Builder{}
for i, q := range queries {
q = strings.TrimSpace(q)
Expand All @@ -215,6 +230,10 @@ func (b *Builder) constructMergeQuery(hrs, vars, queries []string) string {
if variables != "" && !IsDDL(q) { // skip variables if it's ddl
builder.WriteString(fmt.Sprintf("%s\n", variables))
}
if b.enableDryRun {
// append explain to the DDL query part
builder.WriteString("EXPLAIN\n")
}
builder.WriteString(fmt.Sprintf("%s\n;", q))
if b.costAttributionTeam != "" {
builder.WriteString(fmt.Sprintf("\n%s\n", getCostAttributionComment(b.costAttributionTeam)))
Expand Down
Loading
Loading