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

torand / FasterSQL / 17073502945

19 Aug 2025 02:56PM UTC coverage: 68.2%. Remained the same
17073502945

push

github

torand
chore: adjust sonar cloud config to avoid noise

299 of 598 branches covered (50.0%)

Branch coverage included in aggregate %.

1629 of 2229 relevant lines covered (73.08%)

3.97 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.sql.Sql;
35
import io.github.torand.fastersql.subquery.Subquery;
36

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

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

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

83
    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✔
84
        this.projections = asList(projections);
4✔
85
        this.relations = asList(relations);
4✔
86
        this.joins = asList(joins);
4✔
87
        this.wherePredicates = asList(wherePredicates);
4✔
88
        this.groups = asList(groups);
4✔
89
        this.havingPredicates = asList(havingPredicates);
4✔
90
        this.orders = asList(orders);
4✔
91
        this.distinct = distinct;
3✔
92
        this.limit = limit;
3✔
93
        this.offset = offset;
3✔
94
        this.forUpdate = forUpdate;
3✔
95
    }
1✔
96

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

359
        validate(context);
3✔
360

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

468
        return innerSql;
×
469
    }
470

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

592
    @Override
593
    public String toString() {
594
        return toString(new AnsiIsoDialect());
×
595
    }
596
}
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