Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --replace_result #119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 3 additions & 0 deletions r/example.result
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ explain analyze select * from t;
id estRows actRows task access object execution info operator info memory disk
TableReader_5 10000.00 5 root NULL time:<num>, loops:<num>, RU:<num>, cop_task: {num:<num>, max:<num>, proc_keys:<num>, rpc_num:<num>, rpc_time:<num>, copr_cache_hit_ratio:<num>, build_task_duration:<num>, max_distsql_concurrency:<num>} data:TableFullScan_4 <num> Bytes N/A
└─TableFullScan_4 10000.00 5 cop[tikv] table:t tikv_task:{time:<num>, loops:<num>} keep order:false, stats:pseudo N/A N/A
select "My Matched matched changed Changed"
My Matched matched changed Changed
My Changed matched changed Changed
insert into t values (6, 6);
affected rows: 1
info:
Expand Down
25 changes: 25 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ type ReplaceRegex struct {
replace string
}

type ReplaceResult struct {
match string
replace string
}

type tester struct {
mdb *sql.DB
name string
Expand Down Expand Up @@ -146,6 +151,9 @@ type tester struct {

// replace output result through --replace_regex /\.dll/.so/
replaceRegex []*ReplaceRegex

// replace output result through --replace_result from to [from to [...]]
replaceResult []ReplaceResult
}

func newTester(name string) *tester {
Expand Down Expand Up @@ -411,6 +419,7 @@ func (t *tester) Run() error {
t.sortedResult = false
t.replaceColumn = nil
t.replaceRegex = nil
t.replaceResult = nil
case Q_SORTED_RESULT:
t.sortedResult = true
case Q_REPLACE_COLUMN:
Expand Down Expand Up @@ -490,6 +499,18 @@ func (t *tester) Run() error {
return errors.Annotate(err, fmt.Sprintf("Could not parse regex in --replace_regex: line: %d sql:%v", q.Line, q.Query))
}
t.replaceRegex = regex
case Q_REPLACE:
t.replaceResult = nil // Only use the latest one!
// First iteration
// TODO: handle quoted strings, as well as variables
cols := strings.Fields(q.Query)
// Require that col + replacement comes in pairs otherwise skip the last column number
if len(cols)%2 != 0 {
log.WithFields(log.Fields{"command": q.firstWord, "arguments": q.Query, "line": q.Line}).Warn("uneven number of replacement, will skip the last one")
}
for i := 0; i < len(cols)-1; i = i + 2 {
t.replaceResult = append(t.replaceResult, ReplaceResult{cols[i], cols[i+1]})
}
default:
log.WithFields(log.Fields{"command": q.firstWord, "arguments": q.Query, "line": q.Line}).Warn("command not implemented")
}
Expand Down Expand Up @@ -814,6 +835,10 @@ func (t *tester) writeQueryResult(rows *byteRows) error {
} else {
value = string(col)
}
// replace result
for _, replace := range t.replaceResult {
value = strings.ReplaceAll(value, replace.match, replace.replace)
}
t.buf.WriteString(value)
if i < len(row.data)-1 {
t.buf.WriteString("\t")
Expand Down
1 change: 1 addition & 0 deletions src/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const (
Q_COMMENT /* Comments, ignored. */
Q_COMMENT_WITH_COMMAND
Q_EMPTY_LINE
Q_REPLACE_RESULT
)

// ParseQueries parses an array of string into an array of query object.
Expand Down
3 changes: 3 additions & 0 deletions t/example.test
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ explain analyze format='brief' select * from t;
--replace_regex /:[ ]?[.0-9]+.*?,/:<num>,/ /:[ ]?[.0-9]+.*?}/:<num>}/ /[0-9]+ Bytes/<num> Bytes/
explain analyze select * from t;

--replace_result Matched Changed
select "My Matched matched changed Changed"

--enable_info
insert into t values (6, 6);

Expand Down