• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

torand / FasterSQL / 17012572989

16 Aug 2025 08:26PM UTC coverage: 68.265% (-0.008%) from 68.273%
17012572989

push

github

torand
refactor: sonar cloud issues

299 of 598 branches covered (50.0%)

Branch coverage included in aggregate %.

33 of 47 new or added lines in 18 files covered. (70.21%)

1 existing line in 1 file now uncovered.

1680 of 2301 relevant lines covered (73.01%)

3.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.87
/src/main/java/io/github/torand/fastersql/statement/SelectStatement.java
1
/*
2
 * Copyright (c) 2024-2025 Tore Eide Andersen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.torand.fastersql.statement;
17

18
import io.github.torand.fastersql.alias.Alias;
19
import io.github.torand.fastersql.alias.ColumnAlias;
20
import io.github.torand.fastersql.dialect.AnsiIsoDialect;
21
import io.github.torand.fastersql.expression.Expression;
22
import io.github.torand.fastersql.function.aggregate.AggregateFunction;
23
import io.github.torand.fastersql.join.Join;
24
import io.github.torand.fastersql.model.Column;
25
import io.github.torand.fastersql.model.Table;
26
import io.github.torand.fastersql.order.Order;
27
import io.github.torand.fastersql.predicate.OptionalPredicate;
28
import io.github.torand.fastersql.predicate.Predicate;
29
import io.github.torand.fastersql.projection.Projection;
30
import io.github.torand.fastersql.relation.Relation;
31
import io.github.torand.fastersql.setoperation.SetOperation;
32
import io.github.torand.fastersql.setoperation.SetOperator;
33
import io.github.torand.fastersql.sql.Context;
34
import io.github.torand.fastersql.subquery.Subquery;
35

36
import java.util.HashSet;
37
import java.util.LinkedList;
38
import java.util.List;
39
import java.util.Optional;
40
import java.util.Set;
41
import java.util.function.Function;
42
import java.util.function.Supplier;
43
import java.util.stream.Stream;
44

45
import static io.github.torand.fastersql.dialect.Capability.LIMIT_OFFSET;
46
import static io.github.torand.fastersql.dialect.Capability.SELECT_FOR_UPDATE;
47
import static io.github.torand.fastersql.sql.Command.SELECT;
48
import static io.github.torand.fastersql.statement.Helpers.unwrapSuppliers;
49
import static io.github.torand.javacommons.collection.CollectionHelper.asList;
50
import static io.github.torand.javacommons.collection.CollectionHelper.concat;
51
import static io.github.torand.javacommons.collection.CollectionHelper.isEmpty;
52
import static io.github.torand.javacommons.collection.CollectionHelper.nonEmpty;
53
import static io.github.torand.javacommons.contract.Requires.require;
54
import static io.github.torand.javacommons.contract.Requires.requireNonEmpty;
55
import static io.github.torand.javacommons.functional.Functions.castTo;
56
import static io.github.torand.javacommons.functional.Optionals.mapSafely;
57
import static io.github.torand.javacommons.functional.Predicates.instanceOf;
58
import static io.github.torand.javacommons.stream.StreamHelper.streamSafely;
59
import static java.util.Collections.emptyList;
60
import static java.util.Objects.nonNull;
61
import static java.util.Objects.requireNonNull;
62
import static java.util.function.Predicate.not;
63
import static java.util.stream.Collectors.joining;
64
import static java.util.stream.Collectors.toSet;
65

66
/**
67
 * Implements a SELECT statement.
68
 */
69
public class SelectStatement implements PreparableStatement {
70
    private final List<Projection> projections;
71
    private final List<Relation> relations;
72
    private final List<Join> joins;
73
    private final List<Predicate> wherePredicates;
74
    private final List<Column> groups;
75
    private final List<Predicate> havingPredicates;
76
    private final List<Order> orders;
77
    private final boolean distinct;
78
    private final Long limit;
79
    private final Long offset;
80
    private final boolean forUpdate;
81

82
    SelectStatement(List<Projection> projections, List<Relation> relations, List<Join> joins, List<Predicate> wherePredicates, List<Column> groups, List<Predicate> havingPredicates, List<Order> orders, boolean distinct, Long limit, Long offset, boolean forUpdate) {
2✔
83
        this.projections = asList(projections);
4✔
84
        this.relations = asList(relations);
4✔
85
        this.joins = asList(joins);
4✔
86
        this.wherePredicates = asList(wherePredicates);
4✔
87
        this.groups = asList(groups);
4✔
88
        this.havingPredicates = asList(havingPredicates);
4✔
89
        this.orders = asList(orders);
4✔
90
        this.distinct = distinct;
3✔
91
        this.limit = limit;
3✔
92
        this.offset = offset;
3✔
93
        this.forUpdate = forUpdate;
3✔
94
    }
1✔
95

96
    /**
97
     * Adds one or more JOIN clauses.
98
     * @param joins the JOIN clauses.
99
     * @return the modified statement.
100
     */
101
    public SelectStatement join(Join... joins) {
102
        requireNonEmpty(joins, "No joins specified");
6✔
103
        require(() -> streamSafely(relations).noneMatch(instanceOf(Subquery.class)), "Can't combine a subquery FROM clause with joins");
13✔
104

105
        List<Join> concatenated = concat(this.joins, joins);
5✔
106
        return new SelectStatement(projections, relations, concatenated, wherePredicates, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
25✔
107
    }
108

109
    /**
110
     * Adds a LEFT OUTER JOIN clause.
111
     * @param join the JOIN clause.
112
     * @return the modified statement.
113
     */
114
    public SelectStatement leftOuterJoin(Join join) {
115
        requireNonNull(join, "No join specified");
×
116
        return join(join.leftOuter());
×
117
    }
118

119
    /**
120
     * Adds a RIGHT OUTER JOIN clause.
121
     * @param join the JOIN clause.
122
     * @return the modified statement.
123
     */
124
    public SelectStatement rightOuterJoin(Join join) {
125
        requireNonNull(join, "No join specified");
4✔
126
        return join(join.rightOuter());
10✔
127
    }
128

129
    /**
130
     * Adds a FULL OUTER JOIN clause.
131
     * @param join the JOIN clause.
132
     * @return the modified statement.
133
     */
134
    public SelectStatement fullOuterJoin(Join join) {
135
        requireNonNull(join, "No join specified");
4✔
136
        return join(join.fullOuter());
10✔
137
    }
138

139
    /**
140
     * Adds one or more JOIN clauses, if the condition is true.
141
     * @param condition the condition.
142
     * @param joinSuppliers the suppliers of JOIN clauses.
143
     * @return the modified statement.
144
     */
145
    @SafeVarargs
146
    public final SelectStatement joinIf(boolean condition, Supplier<Join>... joinSuppliers) {
147
        requireNonEmpty(joinSuppliers, "No join suppliers specified");
×
148
        require(() -> streamSafely(relations).noneMatch(instanceOf(Subquery.class)), "Can't combine a subquery FROM clause with joins");
×
149
        if (condition) {
×
150
            List<Join> concatenated = concat(this.joins, unwrapSuppliers(joinSuppliers));
×
151
            return new SelectStatement(projections, relations, concatenated, wherePredicates, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
×
152
        } else {
153
            return this;
×
154
        }
155
    }
156

157
    /**
158
     * Adds one or more predicates to the WHERE clause.
159
     * @param predicates the predicates.
160
     * @return the modified statement.
161
     */
162
    public SelectStatement where(Predicate... predicates) {
163
        requireNonEmpty(predicates, "No WHERE predicates specified");
6✔
164

165
        List<Predicate> concatenated = concat(this.wherePredicates, predicates);
5✔
166
        return new SelectStatement(projections, relations, joins, concatenated, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
25✔
167
    }
168

169
    /**
170
     * Adds optional predicates to the WHERE clause if the wrapped predicates are present.
171
     * @param maybePredicates the optional predicates.
172
     * @return the modified statement.
173
     */
174
    @SafeVarargs
175
    public final SelectStatement where(OptionalPredicate... maybePredicates) {
176
        requireNonEmpty(maybePredicates, "No optional WHERE predicates specified");
6✔
177

178
        List<Predicate> concatenated = concat(this.wherePredicates, OptionalPredicate.unwrap(maybePredicates));
6✔
179
        return new SelectStatement(projections, relations, joins, concatenated, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
25✔
180
    }
181

182
    /**
183
     * Adds supplied predicates to the WHERE clause, if the condition is true.
184
     * @param condition the condition.
185
     * @param predicateSuppliers the suppliers providing predicates
186
     * @return the modified statement.
187
     */
188
    @SafeVarargs
189
    public final SelectStatement whereIf(boolean condition, Supplier<Predicate>... predicateSuppliers) {
190
        requireNonEmpty(predicateSuppliers, "No WHERE predicate suppliers specified");
×
191
        if (condition) {
×
192
            List<Predicate> concatenated = concat(this.wherePredicates, unwrapSuppliers(predicateSuppliers));
×
193
            return new SelectStatement(projections, relations, joins, concatenated, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
×
194
        } else {
195
            return this;
×
196
        }
197
    }
198

199
    /**
200
     * Adds one or more columns as groups to the GROUP BY clause.
201
     * @param groups the groups
202
     * @return the modified statement.
203
     */
204
    public SelectStatement groupBy(Column... groups) {
205
        requireNonEmpty(groups, "No groups specified");
6✔
206

207
        List<Column> concatenated = concat(this.groups, groups);
5✔
208
        return new SelectStatement(projections, relations, joins, wherePredicates, concatenated, havingPredicates, orders, distinct, limit, offset, forUpdate);
25✔
209
    }
210

211
    /**
212
     * Adds one or more predicates to the HAVING clause.
213
     * @param predicates the predicates.
214
     * @return the modified statement.
215
     */
216
    public SelectStatement having(Predicate... predicates) {
217
        requireNonEmpty(predicates, "No HAVING predicates specified");
6✔
218

219
        List<Predicate> concatenated = concat(this.havingPredicates, predicates);
5✔
220
        return new SelectStatement(projections, relations, joins, wherePredicates, groups, concatenated, orders, distinct, limit, offset, forUpdate);
25✔
221
    }
222

223
    /**
224
     * Adds optional predicates to the HAVING clause if the wrapped predicates are present.
225
     * @param maybePredicates the optional predicates.
226
     * @return the modified statement.
227
     */
228
    @SafeVarargs
229
    public final SelectStatement having(OptionalPredicate... maybePredicates) {
230
        requireNonEmpty(maybePredicates, "No optional HAVING predicates specified");
×
231

232
        List<Predicate> concatenated = concat(this.havingPredicates, OptionalPredicate.unwrap(maybePredicates));
×
233
        return new SelectStatement(projections, relations, joins, wherePredicates, groups, concatenated, orders, distinct, limit, offset, forUpdate);
×
234
    }
235

236
    /**
237
     * Adds one or more predicates to the HAVING clause, if the condition is true.
238
     * @param condition the condition.
239
     * @param predicateSuppliers the suppliers providing predicates.
240
     * @return the modified statement.
241
     */
242
    @SafeVarargs
243
    public final SelectStatement havingIf(boolean condition, Supplier<Predicate>... predicateSuppliers) {
244
        requireNonEmpty(predicateSuppliers, "No HAVING predicate suppliers specified");
×
245
        if (condition) {
×
246
            List<Predicate> concatenated = concat(this.havingPredicates, unwrapSuppliers(predicateSuppliers));
×
247
            return new SelectStatement(projections, relations, joins, wherePredicates, groups, concatenated, orders, distinct, limit, offset, forUpdate);
×
248
        } else {
249
            return this;
×
250
        }
251
    }
252

253
    /**
254
     * Adds one or more ORDER clauses.
255
     * @param orders the ORDER clauses.
256
     * @return the modified statement.
257
     */
258
    public SelectStatement orderBy(Order... orders) {
259
        requireNonEmpty(orders, "No orders specified");
6✔
260
        List<Order> concatenated = concat(this.orders, orders);
5✔
261
        return new SelectStatement(projections, relations, joins, wherePredicates, groups, havingPredicates, concatenated, distinct, limit, offset, forUpdate);
25✔
262
    }
263

264
    /**
265
     * Adds a LIMIT clause.
266
     * @param limit the limit.
267
     * @return the modified statement.
268
     */
269
    public SelectStatement limit(long limit) {
270
        return new SelectStatement(projections, relations, joins, wherePredicates, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
26✔
271
    }
272

273
    /**
274
     * Adds a OFFSET clause.
275
     * @param offset the offset.
276
     * @return the modified statement.
277
     */
278
    public SelectStatement offset(long offset) {
279
        return new SelectStatement(projections, relations, joins, wherePredicates, groups, havingPredicates, orders, distinct, limit, offset, forUpdate);
26✔
280
    }
281

282
    /**
283
     * Adds a FOR UPDATE clause.
284
     * @return the modified statement.
285
     */
286
    public SelectStatement forUpdate() {
287
        return new SelectStatement(projections, relations, joins, wherePredicates, groups, havingPredicates, orders, distinct, limit, offset, true);
25✔
288
    }
289

290
    /**
291
     * Creates a UNION set operation between this and the specified statement.
292
     * @param other the other SELECT statement
293
     * @return the SELECT set operation statement.
294
     */
295
    public SelectSetOpStatement union(SelectStatement other) {
296
        SetOperation setOperation = new SetOperation(other, SetOperator.UNION);
×
297
        return new SelectSetOpStatement(this, asList(setOperation), emptyList());
×
298
    }
299

300
    /**
301
     * Creates a UNION ALL set operation between this and the specified statement.
302
     * @param other the other SELECT statement
303
     * @return the SELECT set operation statement.
304
     */
305
    public SelectSetOpStatement unionAll(SelectStatement other) {
306
        SetOperation setOperation = new SetOperation(other, SetOperator.UNION).all();
×
307
        return new SelectSetOpStatement(this, asList(setOperation), emptyList());
×
308
    }
309

310
    /**
311
     * Creates an INTERSECT set operation between this and the specified statement.
312
     * Note that INTERSECT has precedence over UNION and EXCEPT.
313
     * @param other the other SELECT statement
314
     * @return the SELECT set operation statement.
315
     */
316
    public SelectSetOpStatement intersect(SelectStatement other) {
317
        SetOperation setOperation = new SetOperation(other, SetOperator.INTERSECT);
6✔
318
        return new SelectSetOpStatement(this, asList(setOperation), emptyList());
13✔
319
    }
320

321
    /**
322
     * Creates an INTERSECT ALL set operation between this and the specified statement.
323
     * Note that INTERSECT has precedence over UNION and EXCEPT.
324
     * @param other the other SELECT statement
325
     * @return the SELECT set operation statement.
326
     */
327
    public SelectSetOpStatement intersectAll(SelectStatement other) {
328
        SetOperation setOperation = new SetOperation(other, SetOperator.INTERSECT).all();
×
329
        return new SelectSetOpStatement(this, asList(setOperation), emptyList());
×
330
    }
331

332
    /**
333
     * Creates an EXCEPT set operation between this and the specified statement.
334
     * @param other the other SELECT statement
335
     * @return the SELECT set operation statement.
336
     */
337
    public SelectSetOpStatement except(SelectStatement other) {
338
        SetOperation setOperation = new SetOperation(other, SetOperator.EXCEPT);
×
339
        return new SelectSetOpStatement(this, asList(setOperation), emptyList());
×
340
    }
341

342
    /**
343
     * Creates an EXCEPT ALL set operation between this and the specified statement.
344
     * @param other the other SELECT statement
345
     * @return the SELECT set operation statement.
346
     */
347
    public SelectSetOpStatement exceptAll(SelectStatement other) {
348
        SetOperation setOperation = new SetOperation(other, SetOperator.EXCEPT).all();
×
349
        return new SelectSetOpStatement(this, asList(setOperation), emptyList());
×
350
    }
351

352
    @Override
353
    public String sql(Context context) {
354
        final Context localContext = context
2✔
355
            .withCommand(SELECT)
2✔
356
            .withOuterStatement(this);
2✔
357

358
        validate(context);
3✔
359

360
        StringBuilder sb = new StringBuilder();
4✔
361
        sb.append("select ");
4✔
362
        if (distinct) {
3✔
363
            sb.append("distinct ");
4✔
364
        }
365

366
        sb.append(streamSafely(projections)
8✔
367
            .map(p -> p.sql(localContext) + (p.alias().isEmpty() ? "" : " " + p.alias().map(a -> a.sql(localContext)).get()))
25✔
368
            .collect(joining(", ")));
3✔
369

370
        sb.append(" from ");
4✔
371

372
        // Tables that are joined with should not be specified in the FROM clause
373
        Set<Table> joinedTables = streamSafely(joins).map(Join::joined).collect(toSet());
9✔
374

375
        sb.append(streamSafely(relations)
8✔
376
            .filter(not(joinedTables::contains))
7✔
377
            .map(t -> t.sql(localContext))
6✔
378
            .collect(joining(", ")));
3✔
379

380
        if (nonEmpty(joins)) {
4✔
381
            sb.append(" ");
4✔
382
            sb.append(streamSafely(joins)
8✔
383
                .map(j -> j.sql(localContext))
6✔
384
                .collect(joining(" ")));
3✔
385
        }
386

387
        if (nonEmpty(wherePredicates)) {
4✔
388
            sb.append(" where ");
4✔
389
            sb.append(streamSafely(wherePredicates)
8✔
390
                .map(p -> p.sql(localContext))
6✔
391
                .collect(joining(" and ")));
3✔
392
        }
393

394
        if (nonEmpty(groups)) {
4✔
395
            sb.append(" group by ");
4✔
396
            sb.append(streamSafely(groups)
8✔
397
                .map(g -> g.sql(localContext))
6✔
398
                .collect(joining(", ")));
3✔
399
        }
400

401
        if (nonEmpty(havingPredicates)) {
4✔
402
            sb.append(" having ");
4✔
403
            sb.append(streamSafely(havingPredicates)
8✔
404
                .map(p -> p.sql(localContext))
6✔
405
                .collect(joining(" and ")));
3✔
406
        }
407

408
        if (nonEmpty(orders)) {
4✔
409
            sb.append(" order by ");
4✔
410
            sb.append(streamSafely(orders)
8✔
411
                .map(o -> o.sql(localContext))
6✔
412
                .collect(joining(", ")));
3✔
413
        }
414

415
        if (nonNull(offset) || nonNull(limit)) {
8!
416
            if (context.getDialect().supports(LIMIT_OFFSET)) {
5✔
417
                String offsetClause = nonNull(offset) ? " " + localContext.getDialect().formatRowOffsetClause().orElseThrow(() -> new RuntimeException("Dialect " + localContext.getDialect().getProductName() + " has no row offset clause")) : "";
14!
418
                String limitClause = nonNull(limit) ? " " + localContext.getDialect().formatRowLimitClause().orElseThrow(() -> new RuntimeException("Dialect " + localContext.getDialect().getProductName() + " has no row limit clause")) : "";
14!
419

420
                if (localContext.getDialect().offsetBeforeLimit()) {
4✔
421
                    sb.append(offsetClause).append(limitClause);
7✔
422
                } else {
423
                    sb.append(limitClause).append(offsetClause);
6✔
424
                }
425
            } else {
1✔
426
                sb = addLimitOffsetFallback(localContext, sb, rowFrom(), rowTo());
9✔
427
            }
428
        }
429

430
        if (forUpdate) {
3✔
431
            sb.append(" for update");
4✔
432
        }
433

434
        return sb.toString();
3✔
435
    }
436

437
    Stream<Projection> projections() {
438
        return streamSafely(projections);
4✔
439
    }
440

441
    private Long rowFrom() {
442
        return mapSafely(offset, o -> o + 1);
12✔
443
    }
444

445
    private Long rowTo() {
446
        return mapSafely(limit, l -> (nonNull(offset) ? offset : 0) + l);
20!
447
    }
448

449
    private StringBuilder addLimitOffsetFallback(Context context, StringBuilder innerSql, Long rowFrom, Long rowTo) {
450
        final String ROWNUM = "{ROWNUM}";
2✔
451

452
        String rowNum = context.getDialect().formatRowNumLiteral()
5✔
453
            .orElseThrow(() -> new RuntimeException("Dialect " + context.getDialect().getProductName() + " has no row number literal"));
3✔
454

455
        if (nonNull(rowFrom) && nonNull(rowTo)) {
6!
456
            String limitSql = "select ORIGINAL.*, " + ROWNUM + " ROW_NO from ( " + innerSql.toString() + " ) ORIGINAL where " + ROWNUM + " <= ?";
4✔
457
            String offsetSql = "select * from ( " + limitSql + " ) where ROW_NO >= ?";
3✔
458
            return new StringBuilder(offsetSql.replace(ROWNUM, rowNum));
8✔
459
        } else if (nonNull(rowFrom)) {
×
NEW
460
            String offsetSql = "select * from ( " + innerSql.toString() + " ) where " + ROWNUM + " >= ?";
×
NEW
461
            return new StringBuilder(offsetSql.replace(ROWNUM, rowNum));
×
462
        } else if (nonNull(rowTo)) {
×
NEW
463
            String limitSql = "select * from ( " + innerSql.toString() + " ) where " + ROWNUM + " <= ?";
×
NEW
464
            return new StringBuilder(limitSql.replace(ROWNUM, rowNum));
×
465
        }
466

467
        return innerSql;
×
468
    }
469

470
    @Override
471
    public Stream<Object> params(Context context) {
472
        List<Object> params = new LinkedList<>();
4✔
473

474
        streamSafely(projections).flatMap(p -> p.params(context)).forEach(params::add);
16✔
475

476
        streamSafely(relations).flatMap(r -> r.params(context)).forEach(params::add);
16✔
477

478
        streamSafely(wherePredicates).flatMap(p -> p.params(context)).forEach(params::add);
16✔
479

480
        streamSafely(havingPredicates).flatMap(p -> p.params(context)).forEach(params::add);
16✔
481

482
        if (context.getDialect().supports(LIMIT_OFFSET)) {
5✔
483
            if (context.getDialect().offsetBeforeLimit()) {
4✔
484
                if (nonNull(offset)) {
4✔
485
                    params.add(offset);
5✔
486
                }
487
                if (nonNull(limit)) {
4✔
488
                    params.add(limit);
6✔
489
                }
490
            } else {
491
                if (nonNull(limit)) {
4✔
492
                    params.add(limit);
5✔
493
                }
494
                if (nonNull(offset)) {
4✔
495
                    params.add(offset);
6✔
496
                }
497
            }
498
        } else {
499
            if (nonNull(limit)) {
4!
500
                params.add(rowTo());
5✔
501
            }
502
            if (nonNull(offset)) {
4!
503
                params.add(rowFrom());
5✔
504
            }
505
        }
506

507
        return params.stream();
3✔
508
    }
509

510
    private void validate(Context context) {
511
        if (isEmpty(relations)) {
4!
512
            throw new IllegalStateException("No FROM clause specified");
×
513
        }
514

515
        Stream<Column> projectedColumns = streamSafely(projections)
4✔
516
            .filter(instanceOf(Expression.class))
3✔
517
            .map(castTo(Expression.class))
3✔
518
            .flatMap(Expression::columnRefs);
2✔
519
        validateColumnTableRelations(context, projectedColumns);
4✔
520

521
        if (nonEmpty(joins)) {
4✔
522
            validateColumnTableRelations(context, streamSafely(joins).flatMap(Join::columnRefs));
8✔
523
        }
524

525
        if (nonEmpty(orders)) {
4✔
526
            Set<String> orderableAliases = streamSafely(projections)
4✔
527
                .map(Projection::alias)
2✔
528
                .flatMap(Optional::stream)
2✔
529
                .map(Alias::name)
1✔
530
                .collect(toSet());
4✔
531

532
            streamSafely(orders)
4✔
533
                .flatMap(o -> o.aliasRefs())
5✔
534
                .map(ColumnAlias::name)
3✔
535
                .filter(a -> !orderableAliases.contains(a))
7!
536
                .findFirst()
2✔
537
                .ifPresent(a -> {
1✔
538
                    throw new IllegalStateException("ORDER BY column alias " + a + " is not specified in the SELECT clause");
×
539
                });
540
        }
541

542
        validateColumnTableRelations(context, streamSafely(wherePredicates).flatMap(Predicate::columnRefs));
8✔
543
        validateColumnTableRelations(context, streamSafely(groups));
6✔
544
        validateColumnTableRelations(context, streamSafely(havingPredicates).flatMap(Predicate::columnRefs));
8✔
545
        validateColumnTableRelations(context, streamSafely(orders).flatMap(Order::columnRefs));
8✔
546

547
        if (forUpdate) {
3✔
548
            if (!context.getDialect().supports(SELECT_FOR_UPDATE)) {
5!
549
                throw new UnsupportedOperationException("%s does not support the SELECT ... FOR UPDATE clause".formatted(context.getDialect().getProductName()));
×
550
            }
551

552
            if (distinct || nonEmpty(groups) || streamSafely(projections).anyMatch(instanceOf(AggregateFunction.class))) {
14!
553
                throw new IllegalStateException("SELECT ... FOR UPDATE can't be used with DISTINCT, GROUP BY or aggregates");
×
554
            }
555

556
            List<String> projectedTables = streamSafely(projections)
4✔
557
                .filter(instanceOf(Expression.class))
3✔
558
                .map(castTo(Expression.class))
3✔
559
                .flatMap(Expression::columnRefs)
2✔
560
                .map(cr -> cr.table().name())
5✔
561
                .distinct()
1✔
562
                .toList();
2✔
563

564
            if (projectedTables.size() != 1) {
4!
565
                throw new IllegalStateException("SELECT ... FOR UPDATE can be used for a single projected table only. Projected tables in this statement are: %s".formatted(projectedTables));
×
566
            }
567
        }
568
    }
1✔
569

570
    private void validateColumnTableRelations(Context context, Stream<Column> columns) {
571
        Function<Relation, Stream<Table>> filterTables = r -> r instanceof Table table ? Stream.of(table) : Stream.empty();
13✔
572

573
        Set<String> outerTableNames = new HashSet<>();
4✔
574
        if (nonEmpty(context.getOuterStatements())) {
4✔
575
            context.getOuterStatements().forEach(os -> streamSafely(os.relations).flatMap(filterTables).map(Table::name).forEach(outerTableNames::add));
20✔
576
            context.getOuterStatements().forEach(os -> streamSafely(os.joins).map(Join::joined).map(Table::name).forEach(outerTableNames::add));
19✔
577
        }
578

579
        Set<String> tableNames = Stream.concat(streamSafely(relations).flatMap(filterTables), streamSafely(joins).map(Join::joined))
12✔
580
            .map(Table::name)
1✔
581
            .collect(toSet());
4✔
582

583
        columns
4✔
584
            .filter(c -> !outerTableNames.contains(c.table().name()) && !tableNames.contains(c.table().name()))
15!
585
            .findFirst()
2✔
586
            .ifPresent(c -> {
1✔
587
                throw new IllegalStateException("Column " + c.name() + " belongs to table " + c.table().name() + ", but is not specified in a FROM or JOIN clause");
×
588
            });
589
    }
1✔
590

591
    @Override
592
    public String toString() {
593
        return toString(new AnsiIsoDialect());
×
594
    }
595
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc