Source:
<code>public RelBuilder sortLimit(int offset, int fetch, Iterable<? extends RexNode> nodes) { if (SortRemoveRule.shouldRemoveSortBasedOnRowCount( <START> peek().getCluster().getMetadataQuery(), peek(), fetch, offset)) { <END> return this; } final Registrar registrar = new Registrar(fields()); final List<RelFieldCollation> fieldCollations = registrar.registerFieldCollations(nodes); final RexNode offsetNode = offset <= 0 ? null : literal(offset); final RexNode fetchNode = fetch < 0 ? null : literal(fetch); if (offsetNode == null && fetch == 0) { return empty(); } if (offsetNode == null && fetchNode == null && fieldCollations.isEmpty()) { return this; } if (fieldCollations.isEmpty()) { assert registrar.addedFieldCount() == 0; RelNode top = peek(); if (top instanceof Sort) { final Sort sort2 = (Sort) top; if (sort2.offset == null && sort2.fetch == null) { replaceTop(sort2.getInput()); final RelNode sort = struct.sortFactory.createSort(peek(), sort2.collation, offsetNode, fetchNode); replaceTop(sort); return this; } } if (top instanceof Project) { final Project project = (Project) top; if (project.getInput() instanceof Sort) { final Sort sort2 = (Sort) project.getInput(); if (sort2.offset == null && sort2.fetch == null) { final RelNode sort = struct.sortFactory.createSort(sort2.getInput(), sort2.collation, offsetNode, fetchNode); replaceTop( struct.projectFactory.createProject(sort, project.getHints(), project.getProjects(), Pair.right(project.getNamedProjects()))); return this; } } } } if (registrar.addedFieldCount() > 0) { project(registrar.extraNodes); } final RelNode sort = struct.sortFactory.createSort(peek(), RelCollations.of(fieldCollations), offsetNode, fetchNode); replaceTop(sort); if (registrar.addedFieldCount() > 0) { project(registrar.originalExtraNodes); } return this; }</code><technical_language>I recommend consider changing order of offset/fetch in sync this methods order</technical_language>

Target:
public RelBuilder sortLimit(int offset, int fetch, Iterable<? extends RexNode> nodes) { Double maxRowCount = peek().getCluster().getMetadataQuery().getMaxRowCount(peek()); if (SortRemoveRule.shouldRemoveSortBasedOnRowCount(maxRowCount, offset, fetch)) { return this; } final Registrar registrar = new Registrar(fields()); final List<RelFieldCollation> fieldCollations = registrar.registerFieldCollations(nodes); final RexNode offsetNode = offset <= 0 ? null : literal(offset); final RexNode fetchNode = fetch < 0 ? null : literal(fetch); if (offsetNode == null && fetch == 0) { return empty(); } if (offsetNode == null && fetchNode == null && fieldCollations.isEmpty()) { return this; } if (fieldCollations.isEmpty()) { assert registrar.addedFieldCount() == 0; RelNode top = peek(); if (top instanceof Sort) { final Sort sort2 = (Sort) top; if (sort2.offset == null && sort2.fetch == null) { replaceTop(sort2.getInput()); final RelNode sort = struct.sortFactory.createSort(peek(), sort2.collation, offsetNode, fetchNode); replaceTop(sort); return this; } } if (top instanceof Project) { final Project project = (Project) top; if (project.getInput() instanceof Sort) { final Sort sort2 = (Sort) project.getInput(); if (sort2.offset == null && sort2.fetch == null) { final RelNode sort = struct.sortFactory.createSort(sort2.getInput(), sort2.collation, offsetNode, fetchNode); replaceTop( struct.projectFactory.createProject(sort, project.getHints(), project.getProjects(), Pair.right(project.getNamedProjects()))); return this; } } } } if (registrar.addedFieldCount() > 0) { project(registrar.extraNodes); } final RelNode sort = struct.sortFactory.createSort(peek(), RelCollations.of(fieldCollations), offsetNode, fetchNode); replaceTop(sort); if (registrar.addedFieldCount() > 0) { project(registrar.originalExtraNodes); } return this; }