Skip to content

Better statement splitting - #816

Open
icedtoast wants to merge 4 commits into
grate-devs:mainfrom
icedtoast:better-statement-splitting
Open

Better statement splitting#816
icedtoast wants to merge 4 commits into
grate-devs:mainfrom
icedtoast:better-statement-splitting

Conversation

@icedtoast

@icedtoast icedtoast commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

This is my PR chucknorris/roundhouse#82 to grate.

Briefly, it does cause minor behavior changes around whitespace at the beginning and ending of batches, however I don't think that will have any material affect.

In terms of the commits themselves I added the new failing tests seperate from the tokenizer implementations, to make it a bit clearer what the new cases that are covered are. Please let me know if I should split out the commits differently etc.

Thanks in advance for reviewing.

@wokket

wokket commented Aug 16, 2026

Copy link
Copy Markdown
Collaborator

Hi,

Thanks for taking an interest in this. Two quick thoughts while you're working:

  • If you wanted to submit a separate PR to resolve the SSH issues that would be welcome. It's affecting a couple of the other PRs and dependabot doesn't seem to have caught up yet
  • There's an old unmerged PR (Refine Oracle statement batch splitting #329) around Oracle statement splitting... I'm not an oracle guy and I'm not sure it was never merged, but may be relevant

Help make the SQL Server Batch Splitting more robust by adding test
cases which don't work well with the current regex-based replacement.
These are taken from chucknorris/roundhouse#82
@icedtoast
icedtoast force-pushed the better-statement-splitting branch from c19099f to 12422f1 Compare August 18, 2026 02:31
@icedtoast
icedtoast marked this pull request as ready for review August 18, 2026 06:42
@icedtoast

Copy link
Copy Markdown
Contributor Author

I've updated the description and I believe this is ready for review and merging (hopefully :-) ).

@icedtoast
icedtoast marked this pull request as draft August 18, 2026 06:54
@icedtoast

Copy link
Copy Markdown
Contributor Author

Looks like there is an issue with the oracle tests, looks like I need to split on ';' as well. whoops

@icedtoast
icedtoast force-pushed the better-statement-splitting branch 2 times, most recently from b02ce3b to 4ae2040 Compare August 18, 2026 20:57
@icedtoast
icedtoast marked this pull request as ready for review August 18, 2026 21:01
@icedtoast

Copy link
Copy Markdown
Contributor Author

I've changed this PR to be just SQL Server and the general refactoring, as the Oracle cases were becoming more complicated and need some more work.

@icedtoast

Copy link
Copy Markdown
Contributor Author

@wokket - this is ready for review :-)

@wokket

wokket commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

LGTM. Anyone else have feedback?

@wokket wokket added the enhancement New feature or request label Aug 21, 2026
";
_testOutput.WriteLine(sql_to_match);
var result = Splitter.Split(sql_to_match).ToList();
Assert.Equal(["--\n"], result);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This test (and the equiv in Oracle) are failing under windows because it receives \r\n... update with Environment.NewLine?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've updated these tests to be parameterized with the different line endings.

public class should_replace_on
{
private ITestOutputHelper _testOutput;
private SqlServerStatementSplitter Splitter;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: can this be _splitter for consistency please?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done :-)

icedtoast added a commit to icedtoast/grate that referenced this pull request Aug 24, 2026
Hide the implementation details that it is implemented by regular
expressions to allow easier implementation of different techniques in
the future. Also a note on the terminology, it is probably better named
batch splitting, but I wanted to keep the implementation as close to how
it was originally; hence the same naming.

Furthermore, dependent code doesn't care *how* the statements are split,
just that they are - so this further encapsulates that decision and is
a better abstraction point.

Notably some tests have changed from using raw string literals, instead
being parameterized to accept the line endings in different environments
(i.e. Windows and everything else). This helps the tests be more
consistent. (Thanks to @wokket for pointing this issue out:
grate-devs#816 (comment)
)
icedtoast added a commit to icedtoast/grate that referenced this pull request Aug 24, 2026
Although the Splitter name was inherited from the BatchSplitterReplacer
tests, we want to more to more consistent naming used in the rest of
grate.

Thanks @wokket for the comment:
grate-devs#816 (comment)
@icedtoast
icedtoast force-pushed the better-statement-splitting branch from 4ae2040 to 2194995 Compare August 24, 2026 22:12
Hide the implementation details that it is implemented by regular
expressions to allow easier implementation of different techniques in
the future. Also a note on the terminology, it is probably better named
batch splitting, but I wanted to keep the implementation as close to how
it was originally; hence the same naming.

Furthermore, dependent code doesn't care *how* the statements are split,
just that they are - so this further encapsulates that decision and is
a better abstraction point.

Notably some tests have changed from using raw string literals, instead
being parameterized to accept the line endings in different environments
(i.e. Windows and everything else). This helps the tests be more
consistent. (Thanks to @wokket for pointing this issue out:
grate-devs#816 (comment)
)
Although the Splitter name was inherited from the BatchSplitterReplacer
tests, we want to more to more consistent naming used in the rest of
grate.

Thanks @wokket for the comment:
grate-devs#816 (comment)
This handles nested comments and strings more reliably that a solely
regex-based approach. It is also clearer.

Regular expressions used to tokenize the sql statement, which is then
parsed to ensure that it is only split when outside comments and string
literals.

A small behavior change is that tabs will longer be a batch on their own
, i.e. whitespace only batches are dropped.
@icedtoast
icedtoast force-pushed the better-statement-splitting branch from 2194995 to 7e077e5 Compare August 24, 2026 23:07
@icedtoast

Copy link
Copy Markdown
Contributor Author

Updated based on your comments @wokket

@icedtoast
icedtoast requested a review from wokket August 24, 2026 23:25
@icedtoast

Copy link
Copy Markdown
Contributor Author

Note: It looks like the test failure is a connection timeout, unfortunately I can't rerun the tests on github.

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants