Skip to content

Commit 063f1c7

Browse files
committed
Remove commented code and refactor PostgresWindowFunctionGenerator and PostgresToStringVisitor
1 parent 9bf6f1b commit 063f1c7

3 files changed

Lines changed: 12 additions & 50 deletions

File tree

src/sqlancer/postgres/PostgresToStringVisitor.java

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,6 @@ public void visit(PostgresSelect s) {
189189
}
190190
}
191191

192-
// @Override
193-
// public void visit(PostgresOrderByTerm op) {
194-
// visit(op.getExpr());
195-
// sb.append(" ");
196-
// sb.append(op.getOrder());
197-
// }
198-
199192
@Override
200193
public void visit(PostgresOrderByTerm term) {
201194
visit(term.getExpr());
@@ -373,26 +366,25 @@ public void visit(PostgresLikeOperation op) {
373366
}
374367

375368
@Override
369+
@SuppressWarnings("unchecked")
376370
public void visit(PostgresWindowFunction windowFunction) {
377371
sb.append(windowFunction.getFunctionName());
378372
sb.append("(");
379-
// Fix: Use visitList instead of visit for a list of expressions
380-
visitList(windowFunction.getArguments());
373+
visit(windowFunction.getArguments());
381374
sb.append(") OVER (");
382375

383376
WindowSpecification spec = windowFunction.getWindowSpec();
384377
if (!spec.getPartitionBy().isEmpty()) {
385378
sb.append("PARTITION BY ");
386-
visitList(spec.getPartitionBy());
379+
visit(spec.getPartitionBy());
387380
}
388381

389382
if (!spec.getOrderBy().isEmpty()) {
390383
if (!spec.getPartitionBy().isEmpty()) {
391384
sb.append(" ");
392385
}
393386
sb.append("ORDER BY ");
394-
// Fix: Create a method to handle order by terms specifically
395-
visitOrderByList(spec.getOrderBy());
387+
visit((List<PostgresExpression>) (List<?>) spec.getOrderBy());
396388
}
397389

398390
if (spec.getFrame() != null) {
@@ -407,26 +399,4 @@ public void visit(PostgresWindowFunction windowFunction) {
407399

408400
sb.append(")");
409401
}
410-
411-
// this method handles lists of expressions
412-
private void visitList(List<PostgresExpression> expressions) {
413-
int i = 0;
414-
for (PostgresExpression expr : expressions) {
415-
if (i++ != 0) {
416-
sb.append(", ");
417-
}
418-
visit(expr);
419-
}
420-
}
421-
422-
// this method handles lists of order by terms
423-
private void visitOrderByList(List<PostgresOrderByTerm> orderBy) {
424-
int i = 0;
425-
for (PostgresOrderByTerm term : orderBy) {
426-
if (i++ != 0) {
427-
sb.append(", ");
428-
}
429-
visit(term);
430-
}
431-
}
432402
}

src/sqlancer/postgres/gen/PostgresExpressionGenerator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import sqlancer.postgres.ast.PostgresJoin.PostgresJoinType;
5353
import sqlancer.postgres.ast.PostgresLikeOperation;
5454
import sqlancer.postgres.ast.PostgresOrderByTerm;
55-
// import sqlancer.postgres.ast.PostgresOrderByTerm.PostgresOrder;
5655
import sqlancer.postgres.ast.PostgresPOSIXRegularExpression;
5756
import sqlancer.postgres.ast.PostgresPOSIXRegularExpression.POSIXRegex;
5857
import sqlancer.postgres.ast.PostgresPostfixOperation;

src/sqlancer/postgres/gen/PostgresWindowFunctionGenerator.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public final class PostgresWindowFunctionGenerator {
1919
private static final List<String> WINDOW_FUNCTIONS = Arrays.asList("row_number", "rank", "dense_rank",
2020
"percent_rank", "cume_dist", "ntile", "lag", "lead", "first_value", "last_value", "nth_value");
2121

22-
// Private constructor to prevent instantiation
2322
private PostgresWindowFunctionGenerator() {
2423
throw new AssertionError("Utility class should not be instantiated");
2524
}
@@ -72,10 +71,15 @@ private static List<PostgresExpression> generateFunctionArguments(String functio
7271
private static WindowSpecification generateWindowSpecification(PostgresGlobalState globalState,
7372
List<PostgresExpression> availableExpr) {
7473
List<PostgresExpression> partitionBy = generatePartitionByClause(availableExpr);
75-
List<PostgresOrderByTerm> orderBy = generateOrderByClause(availableExpr);
76-
WindowFrame frame = generateWindowFrame(globalState);
74+
PostgresExpressionGenerator exprGen = new PostgresExpressionGenerator(globalState);
75+
List<PostgresExpression> orderBys = exprGen.generateOrderBys();
76+
List<PostgresOrderByTerm> orderByTerms = new ArrayList<>();
77+
for (PostgresExpression expr : orderBys) {
78+
orderByTerms.add(new PostgresOrderByTerm(expr, Randomly.getBoolean()));
79+
}
7780

78-
return new WindowSpecification(partitionBy, orderBy, frame);
81+
WindowFrame frame = generateWindowFrame(globalState);
82+
return new WindowSpecification(partitionBy, orderByTerms, frame);
7983
}
8084

8185
private static List<PostgresExpression> generatePartitionByClause(List<PostgresExpression> availableExpr) {
@@ -89,17 +93,6 @@ private static List<PostgresExpression> generatePartitionByClause(List<PostgresE
8993
return partitionBy;
9094
}
9195

92-
private static List<PostgresOrderByTerm> generateOrderByClause(List<PostgresExpression> availableExpr) {
93-
List<PostgresOrderByTerm> orderBy = new ArrayList<>();
94-
if (Randomly.getBooleanWithRatherLowProbability()) {
95-
int count = Randomly.smallNumber();
96-
for (int i = 0; i < count; i++) {
97-
orderBy.add(new PostgresOrderByTerm(Randomly.fromList(availableExpr), Randomly.getBoolean()));
98-
}
99-
}
100-
return orderBy;
101-
}
102-
10396
private static WindowFrame generateWindowFrame(PostgresGlobalState globalState) {
10497
if (Randomly.getBooleanWithRatherLowProbability()) {
10598
WindowFrame.FrameType frameType = Randomly.fromOptions(WindowFrame.FrameType.values());

0 commit comments

Comments
 (0)