-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathStatementFeatureVisitor.java
More file actions
899 lines (784 loc) · 33.6 KB
/
Copy pathStatementFeatureVisitor.java
File metadata and controls
899 lines (784 loc) · 33.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement;
import net.sf.jsqlparser.statement.create.publication.CreatePublication;
import net.sf.jsqlparser.statement.alter.AlterPublication;
import net.sf.jsqlparser.statement.create.subscription.CreateSubscription;
import net.sf.jsqlparser.statement.create.subscription.SubscriptionOption;
import net.sf.jsqlparser.statement.alter.AlterSubscription;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterSession;
import net.sf.jsqlparser.statement.alter.AlterSystemStatement;
import net.sf.jsqlparser.statement.alter.RenameTableStatement;
import net.sf.jsqlparser.statement.alter.sequence.AlterSequence;
import net.sf.jsqlparser.statement.analyze.Analyze;
import net.sf.jsqlparser.statement.comment.Comment;
import net.sf.jsqlparser.statement.create.database.CreateDatabase;
import net.sf.jsqlparser.statement.create.index.CreateIndex;
import net.sf.jsqlparser.statement.create.policy.CreatePolicy;
import net.sf.jsqlparser.statement.create.schema.CreateSchema;
import net.sf.jsqlparser.statement.create.sequence.CreateSequence;
import net.sf.jsqlparser.statement.create.synonym.CreateSynonym;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.create.view.AlterView;
import net.sf.jsqlparser.statement.create.view.CreateView;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.export.Export;
import net.sf.jsqlparser.statement.grant.Grant;
import net.sf.jsqlparser.statement.imprt.Import;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.lock.LockStatement;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.FromItemVisitor;
import net.sf.jsqlparser.statement.select.FromItemVisitorAdapter;
import net.sf.jsqlparser.statement.select.PivotVisitor;
import net.sf.jsqlparser.statement.select.PivotVisitorAdapter;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.SelectItemVisitor;
import net.sf.jsqlparser.statement.select.SelectItemVisitorAdapter;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.statement.select.SelectVisitorAdapter;
import net.sf.jsqlparser.statement.select.TableFunction;
import net.sf.jsqlparser.statement.select.TableStatement;
import net.sf.jsqlparser.statement.select.WithItem;
import net.sf.jsqlparser.statement.show.ShowIndexStatement;
import net.sf.jsqlparser.statement.show.ShowTablesStatement;
import net.sf.jsqlparser.statement.truncate.Truncate;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.upsert.Upsert;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.Predicate;
/**
* Derives a {@link StatementFeatures} verdict from a statement tree.
*
* <p>
* Built on the existing adapter chain rather than on a hand-written traversal: only the nodes that
* <em>contribute</em> a feature are overridden, everything else is inherited from
* {@link StatementVisitorAdapter} / {@link SelectVisitorAdapter} / {@link ExpressionVisitorAdapter}
* / {@link FromItemVisitorAdapter}.
*
* <h2>Why a top-level flag and not a flag union</h2>
*
* {@code RETURNS_RESULT_SET} is a property of the statement's own result position, not of whatever
* happens to appear somewhere in the tree:
*
* <pre>
* SELECT * FROM t RETURNS_RESULT_SET, READS_DATA
* INSERT INTO x SELECT * FROM t READS_DATA, MODIFIES_DATA (no result set)
* DELETE FROM t RETURNING * RETURNS_RESULT_SET, MODIFIES_DATA
* WITH c AS (DELETE FROM t RETURNING *) SELECT * FROM c RETURNS_RESULT_SET, MODIFIES_DATA
* INSERT INTO x WITH c AS (DELETE .. RETURNING *) SELECT MODIFIES_DATA (no result set)
* </pre>
*
* The first statement visit claims the top level; every nested statement - reached through a
* data-modifying CTE, a subquery or an IF block - finds it already claimed and can therefore
* contribute {@code MODIFIES_DATA} but never {@code RETURNS_RESULT_SET}.
*
* <h2>Two upstream defects this class works around</h2>
*
* <ol>
* <li>{@code SelectVisitorAdapter#visit(WithItem)} calls {@code withItem.getSelect()}, an unchecked
* cast to {@code ParenthesedSelect}. A data-modifying CTE reached through the <em>select</em> path
* - i.e. from a subquery, via {@code ExpressionVisitorAdapter#visit(Select)} - throws
* ClassCastException. {@link FeatureSelectVisitor} routes through {@code getParenthesedStatement()}
* instead. Worth fixing in the adapter itself.</li>
* <li>{@code StatementVisitorAdapter#visit(Delete)} never calls {@code visitReturningClause},
* unlike its Insert and Update counterparts, so expressions in {@code DELETE .. RETURNING f(x)} are
* not traversed. Walked explicitly here.</li>
* </ol>
*/
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.TooManyMethods", "PMD.ExcessiveImports"})
public class StatementFeatureVisitor extends StatementVisitorAdapter<Void> {
private final Analysis analysis;
// ---- construction -----------------------------------------------------------------------
private StatementFeatureVisitor(Analysis analysis) {
super(wire(analysis));
this.analysis = analysis;
analysis.statements = this;
}
/**
* Builds the mutually recursive visitor chain. The cycle (expression -> select -> from
* item -> expression) is closed after construction through the adapters' setters, because
* neither adapter can be handed a reference to something not yet constructed.
*/
private static SelectVisitorAdapter<Void> wire(Analysis analysis) {
FeatureExpressionVisitor expressions = new FeatureExpressionVisitor(analysis);
FeatureFromItemVisitor fromItems = new FeatureFromItemVisitor(analysis);
FeatureSelectVisitor selects = new FeatureSelectVisitor(analysis, expressions,
new PivotVisitorAdapter<>(expressions),
new SelectItemVisitorAdapter<>(expressions), fromItems);
expressions.setSelectVisitor(selects);
fromItems.setSelectVisitor((SelectVisitor<Void>) selects).setExpressionVisitor(expressions);
analysis.expressions = expressions;
analysis.selects = selects;
return selects;
}
public static StatementFeatures analyse(Statement statement) {
return analyse(statement, name -> false);
}
/**
* @param pureFunctions names (lower case) the caller can prove side-effect free, e.g. from
* {@code pg_proc.provolatile} or a static allow-list. Anything not proven pure leaves
* {@code MODIFIES_DATA} in the <em>possible</em> set.
*/
public static StatementFeatures analyse(Statement statement, Predicate<String> pureFunctions) {
Analysis analysis = new Analysis(pureFunctions);
StatementFeatureVisitor visitor = new StatementFeatureVisitor(analysis);
statement.accept(visitor, null);
analysis.failLoudIfSilent(statement.getClass().getSimpleName());
return new StatementFeatures(analysis.certain, analysis.uncertain, analysis.unresolved);
}
public static StatementFeatures analyse(String sql) throws JSQLParserException {
return analyse(CCJSqlParserUtil.parse(sql));
}
/**
* Aggregate verdict over a whole script.
*
* <p>
* {@link Statements} is an {@code ArrayList<Statement>}, not a {@link Statement}, so it cannot
* go through {@link #analyse(Statement)} and has no {@code getFeatures()} default method -
* hence this overload.
*
* <p>
* <b>Read the result as a union, and only for guard purposes.</b> Each statement in the script
* claims its own result position, so {@code UPDATE ..; SELECT ..;} yields both
* {@code MODIFIES_DATA} and {@code RETURNS_RESULT_SET} - which is the right answer for "may
* this script write?" and a meaningless one for "executeQuery or executeUpdate?", since the
* union says nothing about <em>which</em> statement returns rows. Dispatchers want
* {@link #analyseEach(Statements)}.
*/
public static StatementFeatures analyse(Statements statements) {
return analyse(statements, name -> false);
}
public static StatementFeatures analyse(Statements statements,
Predicate<String> pureFunctions) {
Analysis analysis = new Analysis(pureFunctions);
StatementFeatureVisitor visitor = new StatementFeatureVisitor(analysis);
statements.accept(visitor, null);
analysis.failLoudIfSilent(Statements.class.getSimpleName());
return new StatementFeatures(analysis.certain, analysis.uncertain, analysis.unresolved);
}
/**
* One verdict per statement, in script order. This is what a dispatcher needs: it answers "does
* statement <i>n</i> return rows?" instead of "does anything in here return rows?". Each
* statement is analysed independently, so no feature leaks between them.
*/
public static List<StatementFeatures> analyseEach(Statements statements) {
return analyseEach(statements, name -> false);
}
public static List<StatementFeatures> analyseEach(Statements statements,
Predicate<String> pureFunctions) {
List<StatementFeatures> perStatement = new ArrayList<>(statements.size());
for (Statement statement : statements) {
perStatement.add(analyse(statement, pureFunctions));
}
return perStatement;
}
// ---- shared state -----------------------------------------------------------------------
/**
* The {@code S context} argument cannot carry this state:
* {@code StatementVisitorAdapter#visit(Insert)} passes a hardcoded {@code null} context down to
* the select visitor, so anything threaded through the context is lost at exactly the node that
* matters most.
*/
static final class Analysis {
final EnumSet<StmtFeature> certain = EnumSet.noneOf(StmtFeature.class);
final EnumSet<StmtFeature> uncertain = EnumSet.noneOf(StmtFeature.class);
final Set<String> unresolved = new LinkedHashSet<>();
final Predicate<String> pureFunctions;
StatementVisitor<Void> statements;
ExpressionVisitor<Void> expressions;
SelectVisitor<Void> selects;
private boolean topLevelClaimed;
private int readsSuppressed;
Analysis(Predicate<String> pureFunctions) {
this.pureFunctions = pureFunctions;
}
/** True exactly once per top-level statement; false for everything nested. */
boolean claimTopLevel() {
if (topLevelClaimed) {
return false;
}
topLevelClaimed = true;
return true;
}
void releaseTopLevel() {
topLevelClaimed = false;
}
void certain(StmtFeature... features) {
for (StmtFeature feature : features) {
if (feature == StmtFeature.READS_DATA && readsSuppressed > 0) {
continue;
}
certain.add(feature);
}
}
void possible(StmtFeature... features) {
for (StmtFeature feature : features) {
uncertain.add(feature);
}
}
void unresolved(String reference) {
unresolved.add(reference.toLowerCase(Locale.ROOT));
}
/** A view definition references tables without reading them. */
void suppressReads(Runnable body) {
readsSuppressed++;
try {
body.run();
} finally {
readsSuppressed--;
}
}
void opaque(String reference) {
certain.add(StmtFeature.OPAQUE);
uncertain.addAll(EnumSet.of(StmtFeature.READS_DATA, StmtFeature.RETURNS_RESULT_SET,
StmtFeature.MODIFIES_DATA, StmtFeature.MODIFIES_SCHEMA));
unresolved(reference);
}
/**
* A statement that contributed nothing was not classified - almost certainly a statement
* type added to the grammar after this visitor was written, whose inherited adapter method
* silently returns null. Degrade loudly, never to "harmless". This is what replaces a
* hand-maintained type table; the price is that a genuinely inert statement is also
* reported opaque, which is the right direction to be wrong in.
*/
void failLoudIfSilent(String label) {
if (certain.isEmpty()) {
opaque(label);
}
}
}
// ---- result-producing clauses -----------------------------------------------------------
/**
* RETURNING always ships rows to the client. So does OUTPUT - unless the rows are redirected,
* which the grammar allows in two ways:
*
* <pre>
* OUTPUT DELETED.* -> rows go to the client
* OUTPUT DELETED.id INTO audit -> rows go to a table (getOutputTable())
* OUTPUT DELETED.id INTO @MyTableVar -> rows go to a table variable (getTableVariable())
* </pre>
*
* Both redirections have to be checked. {@code OutputClause()} in the grammar is
* {@code <K_INTO> ( UserVariable() | Table() )}, so the table-variable form leaves
* {@code getOutputTable()} null - testing only that one would report
* {@code OUTPUT .. INTO @MyTableVar} as returning a result set, which it does not.
*/
private void resultClauses(boolean topLevel, ReturningClause returning, OutputClause output) {
if (!topLevel) {
return;
}
if (returning != null && !returning.isEmpty()) {
analysis.certain(StmtFeature.RETURNS_RESULT_SET);
}
if (output != null && output.getSelectItemList() != null
&& output.getOutputTable() == null && output.getTableVariable() == null) {
analysis.certain(StmtFeature.RETURNS_RESULT_SET);
}
if (output != null && (output.getOutputTable() != null
|| output.getTableVariable() != null)) {
// OUTPUT .. INTO <table> writes rows somewhere else
analysis.certain(StmtFeature.MODIFIES_DATA);
}
}
/** Works around defect (2): the adapter does not walk DELETE's RETURNING expressions. */
private void walkReturning(ReturningClause returning) {
if (returning == null) {
return;
}
for (SelectItem<?> item : returning) {
Expression expression = item.getExpression();
if (expression != null) {
expression.accept(analysis.expressions, null);
}
}
}
// ---- DML --------------------------------------------------------------------------------
@Override
public <S> Void visit(Select select, S context) {
if (analysis.claimTopLevel()) {
analysis.certain(StmtFeature.RETURNS_RESULT_SET);
}
// READS_DATA is decided in FeatureSelectVisitor - "SELECT 1" reads nothing.
return super.visit(select, context);
}
@Override
public <S> Void visit(Insert insert, S context) {
boolean topLevel = analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_DATA);
resultClauses(topLevel, insert.getReturningClause(), insert.getOutputClause());
if (insert.getSelect() != null) {
analysis.certain(StmtFeature.READS_DATA);
}
return super.visit(insert, context);
}
@Override
public <S> Void visit(Update update, S context) {
boolean topLevel = analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_DATA);
resultClauses(topLevel, update.getReturningClause(), update.getOutputClause());
return super.visit(update, context);
}
@Override
public <S> Void visit(Delete delete, S context) {
boolean topLevel = analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_DATA);
resultClauses(topLevel, delete.getReturningClause(), delete.getOutputClause());
super.visit(delete, context);
walkReturning(delete.getReturningClause());
return null;
}
@Override
public <S> Void visit(Merge merge, S context) {
boolean topLevel = analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_DATA, StmtFeature.READS_DATA);
resultClauses(topLevel, null, merge.getOutputClause());
return super.visit(merge, context);
}
@Override
public <S> Void visit(Upsert upsert, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_DATA);
if (upsert.getSelect() != null) {
analysis.certain(StmtFeature.READS_DATA);
upsert.getSelect().accept(analysis.selects, context);
}
return null;
}
@Override
public <S> Void visit(Import imprt, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_DATA);
return null;
}
@Override
public <S> Void visit(Export export, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.READS_DATA);
if (export.getSelect() != null) {
export.getSelect().accept(analysis.selects, context);
}
return null;
}
// ---- DDL --------------------------------------------------------------------------------
@Override
public <S> Void visit(CreateTable createTable, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA);
if (createTable.getSelect() != null) {
// CTAS reads; it does not return a result set - the top level is already claimed
analysis.certain(StmtFeature.READS_DATA);
}
return super.visit(createTable, context);
}
@Override
public <S> Void visit(CreateView createView, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA);
if (createView.isMaterialized() && !Boolean.FALSE.equals(createView.getWithData())) {
if (createView.getSelect() != null) {
createView.getSelect().accept(analysis.selects, context);
}
} else {
analysis.suppressReads(() -> {
if (createView.getSelect() != null) {
createView.getSelect().accept(analysis.selects, null);
}
});
}
return null;
}
@Override
public <S> Void visit(AlterView alterView, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA);
analysis.suppressReads(() -> {
if (alterView.getSelect() != null) {
alterView.getSelect().accept(analysis.selects, null);
}
});
return null;
}
@Override
public <S> Void visit(RefreshMaterializedViewStatement materializedView, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA, StmtFeature.MODIFIES_DATA,
StmtFeature.READS_DATA);
return null;
}
@Override
public <S> Void visit(Truncate truncate, S context) {
analysis.claimTopLevel();
// DDL in most engines (implicit commit, no row triggers) but it destroys rows:
// a read-only guard must see MODIFIES_DATA here
analysis.certain(StmtFeature.MODIFIES_SCHEMA, StmtFeature.MODIFIES_DATA);
return null;
}
@Override
public <S> Void visit(Drop drop, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA, StmtFeature.MODIFIES_DATA);
return null;
}
@Override
public <S> Void visit(PurgeStatement purgeStatement, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA, StmtFeature.MODIFIES_DATA);
return null;
}
@Override
public <S> Void visit(Alter alter, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(CreateIndex createIndex, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(CreateSchema createSchema, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(CreateDatabase createDatabase, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(CreateSequence createSequence, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(AlterSequence alterSequence, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(CreateSynonym createSynonym, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(CreatePolicy createPolicy, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(RenameTableStatement renameTableStatement, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(Comment comment, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(Grant grant, S context) {
return schemaOnly();
}
@Override
public <S> Void visit(Analyze analyze, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA, StmtFeature.READS_DATA); // rewrites
// statistics
return null;
}
private Void schemaOnly() {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA);
return null;
}
// ---- opaque -----------------------------------------------------------------------------
@Override
public <S> Void visit(Execute execute, S context) {
analysis.claimTopLevel();
analysis.opaque(String.valueOf(execute.getName()));
return null;
}
@Override
public <S> Void visit(CreateFunctionalStatement createFunctionalStatement, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA);
// the body is an opaque token list - nothing about it is knowable here
analysis.possible(StmtFeature.MODIFIES_DATA, StmtFeature.READS_DATA);
analysis.unresolved(createFunctionalStatement.getClass().getSimpleName());
return null;
}
@Override
public <S> Void visit(UnsupportedStatement unsupportedStatement, S context) {
analysis.claimTopLevel();
analysis.opaque("unsupported");
return null;
}
@Override
public <S> Void visit(ExplainStatement explainStatement, S context) {
if (analysis.claimTopLevel()) {
analysis.certain(StmtFeature.RETURNS_RESULT_SET);
}
// EXPLAIN ANALYZE executes the statement. The option set is dialect-specific, so the
// side effects of the explained statement stay in the possible set.
analysis.possible(StmtFeature.MODIFIES_DATA, StmtFeature.READS_DATA);
analysis.unresolved("explain");
if (explainStatement.getStatement() != null) {
explainStatement.getStatement().accept(this, context);
}
return null;
}
// ---- control flow -----------------------------------------------------------------------
@Override
public <S> Void visit(Statements statements, S context) {
for (Statement statement : statements) {
analysis.releaseTopLevel(); // each statement in a script has its own result position
statement.accept(this, context);
}
analysis.releaseTopLevel();
return null;
}
@Override
public <S> Void visit(Block block, S context) {
analysis.claimTopLevel();
// Statements is itself a List<Statement>; getStatements() is deprecated
Statements inner = block.getStatements();
if (inner != null) {
for (Statement statement : inner) {
statement.accept(this, context);
}
}
return null;
}
@Override
public <S> Void visit(IfElseStatement ifElseStatement, S context) {
analysis.claimTopLevel();
// both branches contribute: this is what the statement *may* do, not what it will do
return super.visit(ifElseStatement, context);
}
// ---- session / transaction --------------------------------------------------------------
@Override
public <S> Void visit(Commit commit, S context) {
return transactionOnly();
}
@Override
public <S> Void visit(RollbackStatement rollbackStatement, S context) {
return transactionOnly();
}
@Override
public <S> Void visit(SavepointStatement savepointStatement, S context) {
return transactionOnly();
}
@Override
public <S> Void visit(LockStatement lock, S context) {
return transactionOnly();
}
private Void transactionOnly() {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_TRANSACTION);
return null;
}
@Override
public <S> Void visit(SetStatement set, S context) {
return sessionOnly();
}
@Override
public <S> Void visit(ResetStatement reset, S context) {
return sessionOnly();
}
@Override
public <S> Void visit(UseStatement use, S context) {
return sessionOnly();
}
@Override
public <S> Void visit(SessionStatement sessionStatement, S context) {
return sessionOnly();
}
@Override
public <S> Void visit(AlterSession alterSession, S context) {
return sessionOnly();
}
@Override
public <S> Void visit(DeclareStatement declareStatement, S context) {
return sessionOnly();
}
@Override
public <S> Void visit(AlterSystemStatement alterSystemStatement, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SESSION);
analysis.possible(StmtFeature.MODIFIES_SCHEMA);
return null;
}
private Void sessionOnly() {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SESSION);
return null;
}
// ---- metadata readers -------------------------------------------------------------------
@Override
public <S> Void visit(ShowStatement showStatement, S context) {
return metadataOnly();
}
@Override
public <S> Void visit(ShowColumnsStatement showColumnsStatement, S context) {
return metadataOnly();
}
@Override
public <S> Void visit(ShowIndexStatement showIndexStatement, S context) {
return metadataOnly();
}
@Override
public <S> Void visit(ShowTablesStatement showTables, S context) {
return metadataOnly();
}
@Override
public <S> Void visit(DescribeStatement describe, S context) {
return metadataOnly();
}
/** Reads the catalogue, not the data. */
private Void metadataOnly() {
if (analysis.claimTopLevel()) {
analysis.certain(StmtFeature.RETURNS_RESULT_SET);
} else {
analysis.certain(StmtFeature.OPAQUE); // keeps failLoudIfSilent honest when nested
}
return null;
}
// ---- nested visitors --------------------------------------------------------------------
static final class FeatureSelectVisitor extends SelectVisitorAdapter<Void> {
private final Analysis analysis;
FeatureSelectVisitor(Analysis analysis, ExpressionVisitor<Void> expressionVisitor,
PivotVisitor<Void> pivotVisitor, SelectItemVisitor<Void> selectItemVisitor,
FromItemVisitor<Void> fromItemVisitor) {
super(expressionVisitor, pivotVisitor, selectItemVisitor, fromItemVisitor);
this.analysis = analysis;
}
@Override
public <S> Void visit(PlainSelect plainSelect, S context) {
if (plainSelect.getFromItem() != null
|| plainSelect.getJoins() != null && !plainSelect.getJoins().isEmpty()) {
analysis.certain(StmtFeature.READS_DATA);
}
// SELECT .. INTO <table> materialises rows instead of returning them, so the
// RETURNS_RESULT_SET claimed by the statement visit has to be withdrawn
boolean into = plainSelect.getIntoTables() != null
&& !plainSelect.getIntoTables().isEmpty()
|| plainSelect.getIntoTempTable() != null;
if (into) {
analysis.certain(StmtFeature.MODIFIES_DATA, StmtFeature.MODIFIES_SCHEMA);
analysis.certain.remove(StmtFeature.RETURNS_RESULT_SET);
}
if (plainSelect.getForMode() != null) {
// FOR UPDATE / FOR SHARE take row locks
analysis.certain(StmtFeature.MODIFIES_TRANSACTION);
}
return super.visit(plainSelect, context);
}
@Override
public <S> Void visit(TableStatement tableStatement, S context) {
analysis.certain(StmtFeature.READS_DATA);
return super.visit(tableStatement, context);
}
/**
* Replaces the inherited implementation, which casts the CTE body to ParenthesedSelect and
* therefore throws on {@code WITH c AS (DELETE .. RETURNING *)}. Routing to the statement
* visitor is also what makes a data-modifying CTE contribute MODIFIES_DATA.
*/
@Override
public <S> Void visit(WithItem<?> withItem, S context) {
ParenthesedStatement body = withItem.getParenthesedStatement();
if (body instanceof Statement) {
((Statement) body).accept(analysis.statements, context);
}
return null;
}
}
static final class FeatureExpressionVisitor extends ExpressionVisitorAdapter<Void> {
private final Analysis analysis;
FeatureExpressionVisitor(Analysis analysis) {
this.analysis = analysis;
}
/**
* Volatility is not a syntactic property. Everything the caller has not proven pure stays
* in the <em>possible</em> set, with the name recorded so it can be resolved against a
* catalogue rather than guessed at here.
*/
@Override
public <S> Void visit(Function function, S context) {
String name = function.getName() == null
? "?"
: function.getName().toLowerCase(Locale.ROOT);
if (!analysis.pureFunctions.test(name)) {
analysis.possible(StmtFeature.MODIFIES_DATA, StmtFeature.MODIFIES_SCHEMA);
analysis.unresolved(name);
}
return super.visit(function, context);
}
}
static final class FeatureFromItemVisitor extends FromItemVisitorAdapter<Void> {
private final Analysis analysis;
FeatureFromItemVisitor(Analysis analysis) {
this.analysis = analysis;
}
/** The inherited implementation is empty, so table functions escape the purity check. */
@Override
public <S> Void visit(TableFunction tableFunction, S context) {
if (tableFunction.getFunction() != null) {
tableFunction.getFunction().accept(analysis.expressions, context);
}
return super.visit(tableFunction, context);
}
}
@Override
public <S> Void visit(CreatePublication statement, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA);
return null;
}
@Override
public <S> Void visit(AlterPublication statement, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA);
return null;
}
@Override
public <S> Void visit(CreateSubscription statement, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA);
if (statement.getOptions().stream()
.noneMatch(option -> (option.getKind() == SubscriptionOption.Kind.CONNECT
|| option.getKind() == SubscriptionOption.Kind.ENABLED)
&& Boolean.FALSE.equals(option.getBooleanValue()))) {
// A subscription may start asynchronous replication; the remote contents are unknown.
analysis.possible(StmtFeature.MODIFIES_DATA);
}
return null;
}
@Override
public <S> Void visit(AlterSubscription statement, S context) {
analysis.claimTopLevel();
analysis.certain(StmtFeature.MODIFIES_SCHEMA);
if (statement.getAction() == AlterSubscription.Action.ENABLE
|| statement.getAction() == AlterSubscription.Action.REFRESH_PUBLICATION
|| statement.getAction() == AlterSubscription.Action.SET_PUBLICATION
|| statement.getAction() == AlterSubscription.Action.ADD_PUBLICATION
|| statement.getAction() == AlterSubscription.Action.DROP_PUBLICATION) {
analysis.possible(StmtFeature.MODIFIES_DATA);
}
return null;
}
}