Skip to content

Add filter option for diff command - #3499

Closed
ksankeerth wants to merge 3 commits into
dolthub:mainfrom
ksankeerth:dev-1430
Closed

ksankeerth wants to merge 3 commits into
dolthub:mainfrom
ksankeerth:dev-1430

Conversation

@ksankeerth

@ksankeerth ksankeerth commented May 28, 2022 •

Copy link
Copy Markdown

This PR closes #1430

Description :

The following assumptions were taken for diff filter.

For docs

  • adding new doc will be considered as "insert"
  • modifying existing file will be considered as "update"
  • removing a file will be considered as "update" and "delete" (Removing lines = update and removing docs table = delete)

For Table

  • create a table = insert
  • adding rows = insert
  • modifying row = update
  • delete row = delete
  • adding a column = update
  • changing data type of a column = update
  • deleting a column = update
  • renaming a column = update

The filter flag is not allowed with --summary

Tests

  • added tests for docs and tables in diff.bats

Please check the changes and let me know your feedback.
Thanks

I have done the following changes.

1. Diff for docs
2. Diff for rows
3. Diff for Table summary
4. Diff schema change and creation
    I have added filter the following functions.

    1. Diff for docs
    2. Diff for rows
    3. Diff for Table summary
    4. Diff schema change and creation
Comment thread go/cmd/dolt/commands/diff.go Outdated
ap.SupportsString(whereParam, "", "column", "filters columns based on values in the diff. See {{.EmphasisLeft}}dolt diff --help{{.EmphasisRight}} for details.")
ap.SupportsInt(limitParam, "", "record_count", "limits to the first N diffs.")
ap.SupportsFlag(CachedFlag, "c", "Show only the unstaged data changes.")
ap.SupportsString(filterParam, "", "diff_type", "filters(inserts|updates|deletes) based on type of modifications.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For consistency, how about using the existing diff_type strings that the Dolt diff tables use? Something like:

filters results based on the type of modification (added, modified, removed).

@timsehn – since you specifically mentioned insert/update/delete in the original issue, let us know if you still prefer those. Otherwise, added/modified/removed seems more consistent with the other diff_type uses in Dolt and maps to schema changes better.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm fine with "added/modified/removed".

Comment thread go/cmd/dolt/commands/diff.go Outdated

if fromCreateStmt != toCreateStmt {
cli.Println(textdiff.LineDiff(fromCreateStmt, toCreateStmt))
if fromCreateStmt == "" && toSch.GetAllCols().Size() > fromSch.GetAllCols().Size() {

@fulghum fulghum May 31, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we go with diff_types of added, modified, removed... this could probably be simplified a bit...

You could use td.IsAdd() && (filter == FilterAdded || filter == NoFilter) to identify added tables, td.Drop() to identify dropped tables, and just rely on everything else being a table modification (e.g. renames, column adds/deletes, etc). Slightly different than the insert/add/delete mapping you proposed, but it would match up better with the existing Dolt diff_types.

Also... it might be worth creating a small private struct in this file for the filter. Then you could clean up some of the filter checking logic a bit, make the code read a little smoother, and not have to pass around a string as the filter. For example, I'm imagining usage like:

if (td.Add() && filter.includeAdds()) {

That would give a little more typing/structure and also removes the need to check for NoFilter directly in many places. It also potentially sets us up to more easily support multiple filters in the future (e.g. including all deletes and all modifications).

Comment thread go/cmd/dolt/commands/diff.go Outdated

if td.IsDrop() {
cli.Println(sqlfmt.DropTableStmt(td.FromName))
if filter == NoFilter || filter == FilterDeletes {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(minor) If you go with the diffTypeFilter struct idea above, this could be simple enough to roll into the existing if statement above; something like if td.IsDrop() && diffTypeFilter.includeDrops() {

_, _ = bold.Printf("diff --dolt a/%s b/%s\n", td.ToName, td.ToName)
_, _ = bold.Println("added table")
} else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(minor) This extra newline can be trimmed out please.

Comment thread go/cmd/dolt/commands/diff_filter.go Outdated
newColsCount int
}

func NewDiffFilterTrans(filterBy string, newColsCount int) *DiffFilterTransform {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(minor) Might as well add the "form" at the end just to be complete and consistent with other method naming. Four extra chars isn't too big of a deal anyway.

Comment thread go/cmd/dolt/commands/diff_filter.go Outdated

v, ok := joinedRowAsMap[0]
if v != nil && ok {
if df.newColsCount < len(joinedRowAsMap) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should also simplify nicely if we look at diff_type as added/modified/removed and treat any schema change as modified, table create as add, and table delete as remove. I suspect this might be easier/cleaner if you do this check when you have a TableDelta object (instead of in this pipeline step).

}


@test "diff: filter table diffs" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice BATS tests! 👏

@fulghum

fulghum commented May 31, 2022

Copy link
Copy Markdown
Contributor

Awesome contribution @ksankeerth! My biggest feedback is switching the diff_type to be consistent with the existing Dolt diff types (added/removed/modified). I know that's a new change from what the original tracking issue specified, so thanks for understanding as we think through the details. I think that'll ultimately be more consistent with existing Dolt semantics and will also simplify some of your code, too.

Thanks a ton for this contribution and for including awesome tests, too. 😄 Let us know if you have questions about any of the feedback, and of course, feel free to hit us up on Discord, too.

@ksankeerth

Copy link
Copy Markdown
Author

@fulghum Thanks for the feedback and review. I'll go through them and push the changes. Yeah, If We consider added/modified/removed instead of inserts/updates/deletes, it going to be very simple

I have done the following changes.

1. Made changes as per the suggestions.
2. Added/Modified/Removed instead of inserts/updates/deletes
3. td.IsDrop() and td.IsAdd() were used to identity the diff types
4. removed  transform which was added in the previous commit since the filter type can be identified early with the new approach(3)
5. added struct to manage the filter
@ksankeerth

Copy link
Copy Markdown
Author

Hi @fulghum,

Could you please have look at the latest commit when you have time. Let me know your feedback.

Previously, I filtered each table row based on inserts/updates/deletes/. Now I use td.IsAdd() and td.IsDrop() to identity the diff type, so it seems that not necessary to have the transform.

@timsehn

timsehn commented Jun 10, 2022

Copy link
Copy Markdown
Contributor

Jason is on vacation today so likely, he'll get to this Monday.

@fulghum fulghum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice work with these changes @ksankeerth. It's definitely getting closer and the code is looking cleaner.

I left a few small comments; I think this is missing some code in the diffRows function that will help the filter work for row-level adds/drops/modifies, in addition to table-level adds/drops/modifies.

Thanks for all the effort on this one! It's getting closer.

return errhand.VerboseErrorFromError(err)
}
}
td.IsDrop()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(minor) looks like this can be removed.

}

doesFilterMatchDiff := false
if (td.IsAdd() && dArgs.filter.includeAddsOrAll()) || (td.IsDrop() && dArgs.filter.includeDropsOrAll()) || (!td.IsAdd() && !td.IsDrop() && dArgs.filter.includeModificationsOrAll()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is looking very close and will work for table adds and table drops, but not yet for table modifications or row adds/drops/modifications.

For table modifications, you can use something like tableModified := tableDelta.HasSchemaChanged(ctx) || tableDelta.IsRename().

For row adds/drops/modifications, you'll need something in the diffRows method, where row contents are diffed. You might be able to revive some code from the earlier PR version, but I think something will need to be in this function in order to detect row data changes.

fromSch = toSch
}
if !doesFilterMatchDiff {
return nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

fyi... these return nils will prevent other TableDeltas from being looked at in the for loop, so I don't think this is correct. Take a closer look at these two return nils and see if there's a cleaner way to control the flow through this block. Happy to provide more suggestions if you can't come up with anything.

}



Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(minor) would be good to clean up this extra whitespace for consistency with the existing codebase.

@timsehn

timsehn commented Jul 7, 2022

Copy link
Copy Markdown
Contributor

Any chance you'll get to this @ksankeerth ?

@timsehn

timsehn commented Jul 20, 2022

Copy link
Copy Markdown
Contributor

I'm going to close this. Reopen if you ever revisit the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dolt diff should support --filter option

3 participants