parts = new ArrayList<>();
+ parts.add(identifierText(functionName.colid()));
+ for (var element : functionName.indirection().indirection_el()) {
+ if (element.attr_name() != null) {
+ parts.add(identifierText(element.attr_name()));
+ } else {
+ break;
+ }
+ }
+ return parts;
+ }
+
+ private static String identifierText(ParserRuleContext identifier) {
+ String raw = identifier.getText();
+ if (raw.length() >= 2 && raw.charAt(0) == '"' && raw.charAt(raw.length() - 1) == '"') {
+ return raw.substring(1, raw.length() - 1).replace("\"\"", "\"");
+ }
+ return asciiLowercase(raw);
+ }
+
+ static String asciiLowercase(String value) {
+ var folded = new StringBuilder(value.length());
+ value
+ .codePoints()
+ .map(codePoint -> codePoint >= 'A' && codePoint <= 'Z' ? codePoint + 32 : codePoint)
+ .forEach(folded::appendCodePoint);
+ return folded.toString();
+ }
+}
diff --git a/src/main/java/ai/singlr/postgresql/ColumnReference.java b/src/main/java/ai/singlr/postgresql/ColumnReference.java
new file mode 100644
index 0000000..c4fc4f6
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/ColumnReference.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import java.util.Objects;
+
+/**
+ * A syntactic reference to a column.
+ *
+ * Unquoted identifiers are reported case-folded to lowercase, matching PostgreSQL semantics;
+ * quoted identifiers are reported exactly. Star usage is reported with {@code "*"} as the name.
+ * Ownership of unqualified columns is not resolved — a bare column name may belong to any relation
+ * in scope.
+ *
+ * @param qualifier dotted qualifier preceding the column name, or null when unqualified
+ * @param name the column name, or {@code "*"} for star usage
+ */
+public record ColumnReference(String qualifier, String name) {
+
+ public ColumnReference {
+ Objects.requireNonNull(name, "name must not be null");
+ }
+
+ /** True when this reference is a star projection ({@code *} or {@code alias.*}). */
+ public boolean star() {
+ return "*".equals(name);
+ }
+}
diff --git a/src/main/java/ai/singlr/postgresql/FunctionReference.java b/src/main/java/ai/singlr/postgresql/FunctionReference.java
new file mode 100644
index 0000000..3914470
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/FunctionReference.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import java.util.Objects;
+
+/**
+ * A syntactic reference to a function call.
+ *
+ *
Unquoted identifiers are reported case-folded to lowercase, matching PostgreSQL semantics;
+ * quoted identifiers are reported exactly. Special-form functions ({@code CAST}, {@code COALESCE},
+ * {@code EXTRACT}, {@code CURRENT_DATE}, and similar) are reported by their lowercase keyword with
+ * a null schema.
+ *
+ * @param schema dotted qualifier preceding the function name, or null when unqualified
+ * @param name the function name
+ * @param line 1-based line of the call site
+ * @param column 0-based column of the call site
+ */
+public record FunctionReference(String schema, String name, int line, int column) {
+
+ public FunctionReference {
+ Objects.requireNonNull(name, "name must not be null");
+ }
+}
diff --git a/src/main/java/ai/singlr/postgresql/PostgresQueryAnalyzer.java b/src/main/java/ai/singlr/postgresql/PostgresQueryAnalyzer.java
new file mode 100644
index 0000000..4bed1ce
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/PostgresQueryAnalyzer.java
@@ -0,0 +1,217 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import ai.singlr.postgresql.parser.PostgreSQLLexer;
+import ai.singlr.postgresql.parser.PostgreSQLParser;
+import java.nio.charset.StandardCharsets;
+import java.util.Set;
+import org.antlr.v4.runtime.BailErrorStrategy;
+import org.antlr.v4.runtime.BaseErrorListener;
+import org.antlr.v4.runtime.CharStreams;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.RecognitionException;
+import org.antlr.v4.runtime.Recognizer;
+import org.antlr.v4.runtime.Token;
+import org.antlr.v4.runtime.atn.PredictionMode;
+import org.antlr.v4.runtime.misc.ParseCancellationException;
+
+/**
+ * Parses complete PostgreSQL statements and reports structural facts.
+ *
+ *
{@link #analyze(String)} parses the whole input through EOF — trailing garbage is a syntax
+ * error — and returns an immutable {@link QueryAnalysis}. Prohibited but syntactically valid
+ * statements (DDL, COPY, multiple statements, and so on) analyze successfully so callers can
+ * produce precise policy errors; only malformed or unsafe input is rejected with a {@link
+ * QueryAnalysisException}.
+ *
+ *
Input is bounded to {@value #MAX_LENGTH} characters, {@value #MAX_TOKENS} tokens, and {@value
+ * #MAX_NESTING_DEPTH} levels of bracket nesting, which together bound parse time and memory on
+ * hostile input. SQL text and literal content never appear in exception messages and are never
+ * logged.
+ *
+ *
Three lexically valid PostgreSQL forms are rejected outright because they cannot be analyzed
+ * faithfully: psql meta-commands (backslash commands), which are not SQL and could smuggle
+ * executable commands past analysis, Unicode-escaped identifiers ({@code U&"..."}), whose effective
+ * name differs from their spelling and could evade name-based policies, and identifiers longer than
+ * {@value #MAX_IDENTIFIER_BYTES} UTF-8 bytes, which PostgreSQL silently truncates so the reported
+ * name would differ from the one the server resolves.
+ */
+public final class PostgresQueryAnalyzer {
+
+ /** Maximum accepted input length in characters. */
+ public static final int MAX_LENGTH = 200_000;
+
+ /** Maximum accepted number of lexed tokens. */
+ public static final int MAX_TOKENS = 50_000;
+
+ /** Maximum accepted parenthesis and bracket nesting depth. */
+ public static final int MAX_NESTING_DEPTH = 128;
+
+ /** Maximum accepted identifier length in UTF-8 bytes, matching a default NAMEDATALEN build. */
+ public static final int MAX_IDENTIFIER_BYTES = 63;
+
+ private static final Set VERBATIM_TOKEN_TYPES =
+ Set.of(
+ PostgreSQLLexer.QuotedIdentifier,
+ PostgreSQLLexer.StringConstant,
+ PostgreSQLLexer.UnicodeEscapeStringConstant,
+ PostgreSQLLexer.EscapeStringConstant,
+ PostgreSQLLexer.BinaryStringConstant,
+ PostgreSQLLexer.HexadecimalStringConstant,
+ PostgreSQLLexer.BeginDollarStringConstant,
+ PostgreSQLLexer.DollarText,
+ PostgreSQLLexer.EndDollarStringConstant,
+ PostgreSQLLexer.PLSQLVARIABLENAME,
+ PostgreSQLLexer.PLSQLIDENTIFIER,
+ PostgreSQLLexer.PARAM);
+
+ private PostgresQueryAnalyzer() {}
+
+ /**
+ * Analyzes a complete PostgreSQL SQL string.
+ *
+ * @param sql one or more complete SQL statements
+ * @return the structural analysis
+ * @throws QueryAnalysisException on null, blank, or oversized input, unrecognized tokens, syntax
+ * errors, excessive nesting, or when no statement is present
+ */
+ public static QueryAnalysis analyze(String sql) {
+ if (sql == null || sql.isBlank()) {
+ throw new QueryAnalysisException("sql must not be null or blank", -1, -1);
+ }
+ if (sql.length() > MAX_LENGTH) {
+ throw new QueryAnalysisException("sql exceeds " + MAX_LENGTH + " characters", -1, -1);
+ }
+ try {
+ var tokens = lex(sql);
+ var root = parse(tokens);
+ return new AnalysisCollector().collect(root, normalize(tokens));
+ } catch (StackOverflowError e) {
+ throw new QueryAnalysisException("sql nesting exceeds parser capacity", -1, -1);
+ }
+ }
+
+ private static CommonTokenStream lex(String sql) {
+ var lexer = new PostgreSQLLexer(CharStreams.fromString(sql));
+ lexer.removeErrorListeners();
+ lexer.addErrorListener(
+ new BaseErrorListener() {
+ @Override
+ public void syntaxError(
+ Recognizer, ?> recognizer,
+ Object offendingSymbol,
+ int line,
+ int charPositionInLine,
+ String msg,
+ RecognitionException e) {
+ throw new QueryAnalysisException("unrecognized token", line, charPositionInLine);
+ }
+ });
+ var tokens = new CommonTokenStream(lexer);
+ tokens.fill();
+ checkBounds(tokens);
+ return tokens;
+ }
+
+ private static void checkBounds(CommonTokenStream tokens) {
+ if (tokens.size() > MAX_TOKENS) {
+ throw new QueryAnalysisException("sql exceeds " + MAX_TOKENS + " tokens", -1, -1);
+ }
+ int depth = 0;
+ for (Token token : tokens.getTokens()) {
+ int type = token.getType();
+ if (type == PostgreSQLLexer.UnicodeQuotedIdentifier) {
+ throw new QueryAnalysisException(
+ "unicode escaped identifiers are not supported",
+ token.getLine(),
+ token.getCharPositionInLine());
+ }
+ checkIdentifierLength(token, type);
+ if (type == PostgreSQLLexer.OPEN_PAREN || type == PostgreSQLLexer.OPEN_BRACKET) {
+ depth++;
+ if (depth > MAX_NESTING_DEPTH) {
+ throw new QueryAnalysisException(
+ "sql exceeds nesting depth of " + MAX_NESTING_DEPTH,
+ token.getLine(),
+ token.getCharPositionInLine());
+ }
+ } else if (type == PostgreSQLLexer.CLOSE_PAREN || type == PostgreSQLLexer.CLOSE_BRACKET) {
+ depth = Math.max(0, depth - 1);
+ }
+ }
+ }
+
+ private static void checkIdentifierLength(Token token, int type) {
+ if (type != PostgreSQLLexer.Identifier && type != PostgreSQLLexer.QuotedIdentifier) {
+ return;
+ }
+ String identifier = token.getText();
+ if (type == PostgreSQLLexer.QuotedIdentifier) {
+ identifier = identifier.substring(1, identifier.length() - 1).replace("\"\"", "\"");
+ }
+ if (identifier.getBytes(StandardCharsets.UTF_8).length > MAX_IDENTIFIER_BYTES) {
+ throw new QueryAnalysisException(
+ "identifiers longer than " + MAX_IDENTIFIER_BYTES + " bytes are not supported",
+ token.getLine(),
+ token.getCharPositionInLine());
+ }
+ }
+
+ private static PostgreSQLParser.RootContext parse(CommonTokenStream tokens) {
+ var parser = new PostgreSQLParser(tokens);
+ parser.removeErrorListeners();
+ parser.setErrorHandler(new BailErrorStrategy());
+ parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
+ try {
+ return parser.root();
+ } catch (ParseCancellationException sllFailure) {
+ tokens.seek(0);
+ parser.reset();
+ parser.getInterpreter().setPredictionMode(PredictionMode.LL);
+ try {
+ return parser.root();
+ } catch (ParseCancellationException llFailure) {
+ throw syntaxError(llFailure);
+ }
+ }
+ }
+
+ private static QueryAnalysisException syntaxError(ParseCancellationException failure) {
+ if (failure.getCause() instanceof RecognitionException recognition
+ && recognition.getOffendingToken() != null) {
+ var token = recognition.getOffendingToken();
+ return new QueryAnalysisException(
+ "invalid sql syntax", token.getLine(), token.getCharPositionInLine());
+ }
+ return new QueryAnalysisException("invalid sql syntax", -1, -1);
+ }
+
+ private static String normalize(CommonTokenStream tokens) {
+ var normalized = new StringBuilder();
+ int previousType = Token.INVALID_TYPE;
+ for (Token token : tokens.getTokens()) {
+ int type = token.getType();
+ if (type == Token.EOF || token.getChannel() != Token.DEFAULT_CHANNEL) {
+ continue;
+ }
+ if (!normalized.isEmpty() && !insideDollarString(previousType, type)) {
+ normalized.append(' ');
+ }
+ var text = token.getText();
+ normalized.append(
+ VERBATIM_TOKEN_TYPES.contains(type) ? text : AnalysisCollector.asciiLowercase(text));
+ previousType = type;
+ }
+ return normalized.toString();
+ }
+
+ private static boolean insideDollarString(int previousType, int type) {
+ return (previousType == PostgreSQLLexer.BeginDollarStringConstant
+ || previousType == PostgreSQLLexer.DollarText)
+ && (type == PostgreSQLLexer.DollarText || type == PostgreSQLLexer.EndDollarStringConstant);
+ }
+}
diff --git a/src/main/java/ai/singlr/postgresql/QueryAnalysis.java b/src/main/java/ai/singlr/postgresql/QueryAnalysis.java
new file mode 100644
index 0000000..952f506
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/QueryAnalysis.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Immutable structural description of an analyzed SQL string.
+ *
+ * When the input holds multiple statements, {@link #statementKind()} describes the first
+ * statement, {@link QueryFeature#MULTIPLE_STATEMENTS} is set, and references and features are
+ * aggregated across all statements.
+ *
+ * @param statementKind classification of the first statement
+ * @param statementCount number of statements; semicolons inside strings, dollar-quoted values, and
+ * comments do not create statements
+ * @param relations every syntactically reachable relation reference, in source order
+ * @param columns every syntactically reachable column reference, including star usage
+ * @param functions every syntactically reachable function call, in source order
+ * @param parameters deduplicated named-parameter names, without the leading colon, exactly as
+ * written
+ * @param features policy-relevant syntactic constructs detected at any depth
+ * @param normalizedSql deterministic single-line form, stable across whitespace, comments, and
+ * keyword or unquoted-identifier case, suitable for hashing and audit comparison
+ */
+public record QueryAnalysis(
+ StatementKind statementKind,
+ int statementCount,
+ List relations,
+ List columns,
+ List functions,
+ Set parameters,
+ Set features,
+ String normalizedSql) {
+
+ public QueryAnalysis {
+ Objects.requireNonNull(statementKind, "statementKind must not be null");
+ Objects.requireNonNull(normalizedSql, "normalizedSql must not be null");
+ if (statementCount < 1) {
+ throw new IllegalArgumentException("statementCount must be at least 1");
+ }
+ relations = List.copyOf(relations);
+ columns = List.copyOf(columns);
+ functions = List.copyOf(functions);
+ parameters = Collections.unmodifiableSet(new LinkedHashSet<>(parameters));
+ features =
+ features.isEmpty() ? Set.of() : Collections.unmodifiableSet(EnumSet.copyOf(features));
+ }
+}
diff --git a/src/main/java/ai/singlr/postgresql/QueryAnalysisException.java b/src/main/java/ai/singlr/postgresql/QueryAnalysisException.java
new file mode 100644
index 0000000..f61deaa
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/QueryAnalysisException.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+/**
+ * Thrown when SQL cannot be analyzed: null, blank, or oversized input, unrecognized tokens, syntax
+ * errors, excessive nesting, or an empty statement list.
+ *
+ * The message carries only a stable reason and, when available, a line and column. It never
+ * contains SQL text, literals, or parser internals, so it is safe to log and to return to callers.
+ */
+public class QueryAnalysisException extends RuntimeException {
+
+ private final String reason;
+ private final int line;
+ private final int column;
+
+ public QueryAnalysisException(String reason, int line, int column) {
+ super(line >= 1 ? "%s at line %d, column %d".formatted(reason, line, column) : reason);
+ this.reason = reason;
+ this.line = line;
+ this.column = column;
+ }
+
+ /** Stable, content-free description of the failure. */
+ public String reason() {
+ return reason;
+ }
+
+ /** 1-based line of the failure, or -1 when not applicable. */
+ public int line() {
+ return line;
+ }
+
+ /** 0-based column of the failure, or -1 when not applicable. */
+ public int column() {
+ return column;
+ }
+}
diff --git a/src/main/java/ai/singlr/postgresql/QueryFeature.java b/src/main/java/ai/singlr/postgresql/QueryFeature.java
new file mode 100644
index 0000000..ccbc126
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/QueryFeature.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+/** Syntactic constructs a policy layer may need to distinguish, detected at any nesting depth. */
+public enum QueryFeature {
+ /** A {@code WITH} clause is present. */
+ CTE,
+ /** A {@code WITH RECURSIVE} clause is present. */
+ RECURSIVE_CTE,
+ /** A CTE body is a non-SELECT statement (INSERT, UPDATE, or DELETE). */
+ WRITABLE_CTE,
+ /** A parenthesized SELECT is used as an expression, derived table, or set-returning source. */
+ SUBQUERY,
+ /** {@code UNION}, {@code INTERSECT}, or {@code EXCEPT} combines query branches. */
+ SET_OPERATION,
+ /** A window function {@code OVER} clause or a {@code WINDOW} definition is present. */
+ WINDOW,
+ /** {@code SELECT ... INTO} creates a table from the result set. */
+ SELECT_INTO,
+ /** A row-locking clause such as {@code FOR UPDATE} or {@code FOR SHARE} is present. */
+ ROW_LOCK,
+ /** A {@code LATERAL} relation is present in a FROM clause. */
+ LATERAL,
+ /** A set-returning function is used as a relation in a FROM clause. */
+ FUNCTION_RELATION,
+ /** A star projection ({@code *} or {@code alias.*}) is present. */
+ STAR_PROJECTION,
+ /** A {@code VALUES} list is used as a relation in a FROM clause. */
+ VALUES_RELATION,
+ /** The input contains more than one statement. */
+ MULTIPLE_STATEMENTS
+}
diff --git a/src/main/java/ai/singlr/postgresql/RelationReference.java b/src/main/java/ai/singlr/postgresql/RelationReference.java
new file mode 100644
index 0000000..00b2e18
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/RelationReference.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import java.util.Objects;
+
+/**
+ * A syntactic reference to a relation.
+ *
+ *
Unquoted identifiers are reported case-folded to lowercase, matching PostgreSQL semantics;
+ * quoted identifiers are reported exactly. An unqualified name that matches an in-scope CTE name is
+ * reported as {@link Kind#CTE}; classification is purely syntactic and does not resolve catalog
+ * objects.
+ *
+ * @param schema dotted qualifier preceding the relation name, or null when unqualified
+ * @param name the relation, CTE, or function name
+ * @param alias the alias bound to this relation, or null
+ * @param kind how the relation is referenced
+ */
+public record RelationReference(String schema, String name, String alias, Kind kind) {
+
+ /** How a relation is referenced. */
+ public enum Kind {
+ /** A schema-qualified or unqualified physical relation name. */
+ PHYSICAL,
+ /** An unqualified name matching a common table expression in scope. */
+ CTE,
+ /** A set-returning function used as a relation. */
+ FUNCTION
+ }
+
+ public RelationReference {
+ Objects.requireNonNull(name, "name must not be null");
+ Objects.requireNonNull(kind, "kind must not be null");
+ }
+}
diff --git a/src/main/java/ai/singlr/postgresql/StatementKind.java b/src/main/java/ai/singlr/postgresql/StatementKind.java
new file mode 100644
index 0000000..058e710
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/StatementKind.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+/**
+ * Coarse classification of the first statement in an analyzed SQL string.
+ *
+ *
{@code CREATE}, {@code ALTER}, {@code DROP}, and other schema- or privilege-changing
+ * statements (including {@code GRANT}/{@code REVOKE}, {@code COMMENT ON}, and {@code CREATE INDEX})
+ * classify as {@link #DDL}. Everything else that is not DML — {@code COPY}, {@code CALL}, {@code
+ * DO}, {@code SET}, {@code SHOW}, {@code EXPLAIN}, {@code TRUNCATE}, transaction control, cursors,
+ * and similar commands — classifies as {@link #UTILITY}.
+ */
+public enum StatementKind {
+ SELECT,
+ INSERT,
+ UPDATE,
+ DELETE,
+ MERGE,
+ DDL,
+ UTILITY,
+ UNKNOWN
+}
diff --git a/src/main/java/ai/singlr/postgresql/package-info.java b/src/main/java/ai/singlr/postgresql/package-info.java
new file mode 100644
index 0000000..eb6fc3b
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/package-info.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+/**
+ * Syntactic analysis of complete PostgreSQL statements.
+ *
+ *
{@link ai.singlr.postgresql.PostgresQueryAnalyzer#analyze(String)} parses a full SQL string
+ * through EOF and returns an immutable {@link ai.singlr.postgresql.QueryAnalysis} describing what
+ * the SQL is: statement kind and count, every syntactically reachable relation, column,
+ * and function reference, named parameters ({@code :name}), policy-relevant features, and a
+ * deterministic normalized form suitable for hashing and audit comparison.
+ *
+ *
This package parses and describes SQL. It does not execute SQL, resolve catalog objects,
+ * authorize access, or decide query cost — callers own policy. Analysis is purely syntactic:
+ * relations that look like in-scope CTE names are reported as CTE references, everything else as
+ * physical or function relations, without pretending to resolve database objects.
+ *
+ *
The grammar is the ANTLR grammars-v4 PostgreSQL grammar, vendored at a pinned upstream commit
+ * with one deliberate extension: named parameters such as {@code :start_at} are first-class
+ * expression values. A colon that directly continues an expression — a JSON {@code key:value}
+ * separator or an array-slice bound such as {@code arr[lo:hi]} — stays an operator; the single
+ * ambiguous form {@code arr[:name]} binds to the parameter extension. See the repository NOTICE.md
+ * for provenance and the exact local modifications.
+ */
+package ai.singlr.postgresql;
diff --git a/src/main/java/ai/singlr/postgresql/parser/LexerDispatchingErrorListener.java b/src/main/java/ai/singlr/postgresql/parser/LexerDispatchingErrorListener.java
new file mode 100644
index 0000000..89dff45
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/parser/LexerDispatchingErrorListener.java
@@ -0,0 +1,79 @@
+/*
+PostgreSQL grammar.
+The MIT License (MIT).
+Copyright (c) 2021-2023, Oleksii Kovalov (Oleksii.Kovalov@outlook.com).
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+// scim-sql local modification: added package declaration (upstream files have no package).
+package ai.singlr.postgresql.parser;
+
+import java.util.BitSet;
+import org.antlr.v4.runtime.*;
+import org.antlr.v4.runtime.atn.*;
+import org.antlr.v4.runtime.dfa.*;
+import org.antlr.v4.runtime.misc.*;
+
+public class LexerDispatchingErrorListener implements ANTLRErrorListener
+{
+ Lexer _parent;
+
+ public LexerDispatchingErrorListener(Lexer parent)
+ {
+ _parent = parent;
+ }
+
+ public void syntaxError(Recognizer, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e)
+ {
+ ProxyErrorListener foo = new ProxyErrorListener(_parent.getErrorListeners());
+ foo.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
+ }
+
+ public void reportAmbiguity(Parser recognizer,
+ DFA dfa,
+ int startIndex,
+ int stopIndex,
+ boolean exact,
+ BitSet ambigAlts,
+ ATNConfigSet configs)
+ {
+ ProxyErrorListener foo = new ProxyErrorListener(_parent.getErrorListeners());
+ foo.reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs);
+ }
+
+ public void reportAttemptingFullContext(Parser recognizer,
+ DFA dfa,
+ int startIndex,
+ int stopIndex,
+ BitSet conflictingAlts,
+ ATNConfigSet configs)
+ {
+ ProxyErrorListener foo = new ProxyErrorListener(_parent.getErrorListeners());
+ foo.reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs);
+ }
+
+ public void reportContextSensitivity(Parser recognizer,
+ DFA dfa,
+ int startIndex,
+ int stopIndex,
+ int prediction,
+ ATNConfigSet configs)
+ {
+ ProxyErrorListener foo = new ProxyErrorListener(_parent.getErrorListeners());
+ foo.reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs);
+ }
+}
diff --git a/src/main/java/ai/singlr/postgresql/parser/ParserDispatchingErrorListener.java b/src/main/java/ai/singlr/postgresql/parser/ParserDispatchingErrorListener.java
new file mode 100644
index 0000000..ae1b4f4
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/parser/ParserDispatchingErrorListener.java
@@ -0,0 +1,79 @@
+/*
+PostgreSQL grammar.
+The MIT License (MIT).
+Copyright (c) 2021-2023, Oleksii Kovalov (Oleksii.Kovalov@outlook.com).
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+// scim-sql local modification: added package declaration (upstream files have no package).
+package ai.singlr.postgresql.parser;
+
+import java.util.BitSet;
+import org.antlr.v4.runtime.*;
+import org.antlr.v4.runtime.atn.*;
+import org.antlr.v4.runtime.dfa.*;
+import org.antlr.v4.runtime.misc.*;
+
+public class ParserDispatchingErrorListener implements ANTLRErrorListener
+{
+ Parser _parent;
+
+ public ParserDispatchingErrorListener(Parser parent)
+ {
+ _parent = parent;
+ }
+
+ public void syntaxError(Recognizer, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e)
+ {
+ var foo = new ProxyErrorListener(_parent.getErrorListeners());
+ foo.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
+ }
+
+ public void reportAmbiguity(Parser recognizer,
+ DFA dfa,
+ int startIndex,
+ int stopIndex,
+ boolean exact,
+ BitSet ambigAlts,
+ ATNConfigSet configs)
+ {
+ ProxyErrorListener foo = new ProxyErrorListener(_parent.getErrorListeners());
+ foo.reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs);
+ }
+
+ public void reportAttemptingFullContext(Parser recognizer,
+ DFA dfa,
+ int startIndex,
+ int stopIndex,
+ BitSet conflictingAlts,
+ ATNConfigSet configs)
+ {
+ ProxyErrorListener foo = new ProxyErrorListener(_parent.getErrorListeners());
+ foo.reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs);
+ }
+
+ public void reportContextSensitivity(Parser recognizer,
+ DFA dfa,
+ int startIndex,
+ int stopIndex,
+ int prediction,
+ ATNConfigSet configs)
+ {
+ ProxyErrorListener foo = new ProxyErrorListener(_parent.getErrorListeners());
+ foo.reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs);
+ }
+}
diff --git a/src/main/java/ai/singlr/postgresql/parser/PostgreSQLLexerBase.java b/src/main/java/ai/singlr/postgresql/parser/PostgreSQLLexerBase.java
new file mode 100644
index 0000000..9fd2346
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/parser/PostgreSQLLexerBase.java
@@ -0,0 +1,155 @@
+/*
+PostgreSQL grammar.
+The MIT License (MIT).
+Copyright (c) 2021-2023, Oleksii Kovalov (Oleksii.Kovalov@outlook.com).
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+// scim-sql local modification: added package declaration (upstream files have no package).
+package ai.singlr.postgresql.parser;
+
+import org.antlr.v4.runtime.CharStream;
+import org.antlr.v4.runtime.Lexer;
+import org.antlr.v4.runtime.Token;
+
+import java.util.Set;
+import java.util.Stack;
+
+public abstract class PostgreSQLLexerBase extends Lexer {
+ protected final Stack tags = new Stack<>();
+ protected int commentDepth;
+
+ // scim-sql local modification: a named parameter (:name) is only valid where an expression
+ // starts. When ':name' directly follows a token that can end an expression, the colon is
+ // PostgreSQL's colon operator (a JSON key:value separator or an array-slice bound), so the
+ // greedy PLSQLVARIABLENAME match is split into COLON and the re-lexed name.
+ private static final Set EXPRESSION_END_TOKEN_TYPES = Set.of(
+ PostgreSQLLexer.Identifier,
+ PostgreSQLLexer.QuotedIdentifier,
+ PostgreSQLLexer.StringConstant,
+ PostgreSQLLexer.UnicodeEscapeStringConstant,
+ PostgreSQLLexer.EscapeStringConstant,
+ PostgreSQLLexer.BinaryStringConstant,
+ PostgreSQLLexer.HexadecimalStringConstant,
+ PostgreSQLLexer.EndDollarStringConstant,
+ PostgreSQLLexer.Integral,
+ PostgreSQLLexer.Numeric,
+ PostgreSQLLexer.PARAM,
+ PostgreSQLLexer.PLSQLVARIABLENAME,
+ PostgreSQLLexer.CLOSE_PAREN,
+ PostgreSQLLexer.CLOSE_BRACKET,
+ PostgreSQLLexer.NULL_P,
+ PostgreSQLLexer.TRUE_P,
+ PostgreSQLLexer.FALSE_P,
+ PostgreSQLLexer.END_P,
+ PostgreSQLLexer.CURRENT_DATE,
+ PostgreSQLLexer.CURRENT_TIME,
+ PostgreSQLLexer.CURRENT_TIMESTAMP,
+ PostgreSQLLexer.LOCALTIME,
+ PostgreSQLLexer.LOCALTIMESTAMP,
+ PostgreSQLLexer.CURRENT_ROLE,
+ PostgreSQLLexer.CURRENT_USER,
+ PostgreSQLLexer.SESSION_USER,
+ PostgreSQLLexer.USER,
+ PostgreSQLLexer.CURRENT_CATALOG,
+ PostgreSQLLexer.CURRENT_SCHEMA);
+
+ private int lastDefaultChannelTokenType = Token.INVALID_TYPE;
+
+ protected PostgreSQLLexerBase(CharStream input) {
+ super(input);
+
+ }
+
+ @Override
+ public Token nextToken() {
+ Token token = super.nextToken();
+ if (token.getType() == PostgreSQLLexer.PLSQLVARIABLENAME
+ && EXPRESSION_END_TOKEN_TYPES.contains(lastDefaultChannelTokenType)) {
+ getInputStream().seek(token.getStartIndex() + 1);
+ setLine(token.getLine());
+ setCharPositionInLine(token.getCharPositionInLine() + 1);
+ token = getTokenFactory().create(_tokenFactorySourcePair, PostgreSQLLexer.COLON, null,
+ Token.DEFAULT_CHANNEL, token.getStartIndex(), token.getStartIndex(),
+ token.getLine(), token.getCharPositionInLine());
+ }
+ if (token.getChannel() == Token.DEFAULT_CHANNEL) {
+ lastDefaultChannelTokenType = token.getType();
+ }
+ return token;
+ }
+
+ public void PushTag() {
+ tags.push(getText());
+ }
+
+ public boolean IsTag() {
+ return getText().equals(tags.peek());
+ }
+
+ public void PopTag() {
+ tags.pop();
+ }
+
+ // scim-sql local modification: nested block comments are lexed iteratively with a depth
+ // counter instead of the upstream recursive rule, which was quadratic-time and could
+ // overflow the stack on adversarial nesting.
+ public void EndBlockComment() {
+ commentDepth--;
+ if (commentDepth == 0) {
+ setType(PostgreSQLLexer.BlockComment);
+ setChannel(HIDDEN);
+ popMode();
+ } else {
+ more();
+ }
+ }
+
+ public boolean CheckLaMinus() {
+ return getInputStream().LA(1) != '-';
+ }
+
+ public boolean CheckLaStar() {
+ return getInputStream().LA(1) != '*';
+ }
+
+ public boolean CharIsLetter() {
+ return Character.isLetter(getInputStream().LA(-1));
+ }
+
+ public void HandleNumericFail() {
+ getInputStream().seek(getInputStream().index() - 2);
+ setType(PostgreSQLLexer.Integral);
+ }
+
+ public void HandleLessLessGreaterGreater() {
+ if (getText() == "<<") setType(PostgreSQLLexer.LESS_LESS);
+ if (getText() == ">>") setType(PostgreSQLLexer.GREATER_GREATER);
+ }
+
+ public boolean CheckIfUtf32Letter() {
+ int codePoint = getInputStream().LA(-2) << 8 + getInputStream().LA(-1);
+ char[] c;
+ if (codePoint < 0x10000) {
+ c = new char[]{(char) codePoint};
+ } else {
+ codePoint -= 0x10000;
+ c = new char[]{(char) (codePoint / 0x400 + 0xd800), (char) (codePoint % 0x400 + 0xdc00)};
+ }
+ return Character.isLetter(c[0]);
+ }
+}
diff --git a/src/main/java/ai/singlr/postgresql/parser/PostgreSQLParserBase.java b/src/main/java/ai/singlr/postgresql/parser/PostgreSQLParserBase.java
new file mode 100644
index 0000000..4692f18
--- /dev/null
+++ b/src/main/java/ai/singlr/postgresql/parser/PostgreSQLParserBase.java
@@ -0,0 +1,164 @@
+/*
+PostgreSQL grammar.
+The MIT License (MIT).
+Copyright (c) 2021-2023, Oleksii Kovalov (Oleksii.Kovalov@outlook.com).
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+// scim-sql local modification: added package declaration (upstream files have no package).
+package ai.singlr.postgresql.parser;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.antlr.v4.runtime.*;
+
+public abstract class PostgreSQLParserBase extends Parser {
+
+ public PostgreSQLParserBase(TokenStream input) {
+ super(input);
+ }
+
+ ParserRuleContext GetParsedSqlTree(String script, int line) {
+ PostgreSQLParser ph = GetPostgreSQLParser(script);
+ ParserRuleContext result = ph.root();
+ return result;
+ }
+
+ public void ParseRoutineBody() {
+ PostgreSQLParser.Createfunc_opt_listContext _localctx = (PostgreSQLParser.Createfunc_opt_listContext) this.getContext();
+ String lang = null;
+ for (PostgreSQLParser.Createfunc_opt_itemContext coi : _localctx.createfunc_opt_item()) {
+ if (coi.LANGUAGE() != null) {
+ if (coi.nonreservedword_or_sconst() != null)
+ if (coi.nonreservedword_or_sconst().nonreservedword() != null)
+ if (coi.nonreservedword_or_sconst().nonreservedword().identifier() != null)
+ if (coi.nonreservedword_or_sconst().nonreservedword().identifier()
+ .Identifier() != null) {
+ lang = coi.nonreservedword_or_sconst().nonreservedword().identifier()
+ .Identifier().getText();
+ break;
+ }
+ }
+ }
+ if (null == lang) return;
+ PostgreSQLParser.Createfunc_opt_itemContext func_as = null;
+ for (PostgreSQLParser.Createfunc_opt_itemContext a : _localctx.createfunc_opt_item()) {
+ if (a.func_as() != null) {
+ func_as = a;
+ break;
+
+ }
+
+ }
+ if (func_as != null) {
+ String txt = GetRoutineBodyString(func_as.func_as().sconst(0));
+ switch (lang) {
+ case "plpgsql":
+ //NB: Cannot be done this way.
+ //PostgreSQLParser ph = GetPostgreSQLParser(txt);
+ //func_as.func_as().Definition = ph.plsqlroot();
+ break;
+ case "sql":
+ //func_as.func_as().Definition = ph.root();
+ break;
+ }
+ }
+ }
+
+ private String TrimQuotes(String s) {
+ return (s == null || s.isEmpty()) ? s : s.substring(1, s.length() - 1);
+ }
+
+ public String unquote(String s) {
+ int slength = s.length();
+ StringBuilder r = new StringBuilder(slength);
+ int i = 0;
+ while (i < slength) {
+ Character c = s.charAt(i);
+ r.append(c);
+ if (c == '\'' && i < slength - 1 && (s.charAt(i + 1) == '\'')) i++;
+ i++;
+ }
+ return r.toString();
+ }
+
+ public String GetRoutineBodyString(PostgreSQLParser.SconstContext rule) {
+ PostgreSQLParser.AnysconstContext anysconst = rule.anysconst();
+ org.antlr.v4.runtime.tree.TerminalNode StringConstant = anysconst.StringConstant();
+ if (null != StringConstant) return unquote(TrimQuotes(StringConstant.getText()));
+ org.antlr.v4.runtime.tree.TerminalNode UnicodeEscapeStringConstant = anysconst.UnicodeEscapeStringConstant();
+ if (null != UnicodeEscapeStringConstant) return TrimQuotes(UnicodeEscapeStringConstant.getText());
+ org.antlr.v4.runtime.tree.TerminalNode EscapeStringConstant = anysconst.EscapeStringConstant();
+ if (null != EscapeStringConstant) return TrimQuotes(EscapeStringConstant.getText());
+ String result = "";
+ List dollartext = anysconst.DollarText();
+ for (org.antlr.v4.runtime.tree.TerminalNode s : dollartext) {
+ result += s.getText();
+ }
+ return result;
+ }
+
+ public PostgreSQLParser GetPostgreSQLParser(String script) {
+ CharStream charStream = CharStreams.fromString(script);
+ Lexer lexer = new PostgreSQLLexer(charStream);
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+ PostgreSQLParser parser = new PostgreSQLParser(tokens);
+ lexer.removeErrorListeners();
+ parser.removeErrorListeners();
+ LexerDispatchingErrorListener listener_lexer = new LexerDispatchingErrorListener((Lexer)(((CommonTokenStream)(this.getInputStream())).getTokenSource()));
+ ParserDispatchingErrorListener listener_parser = new ParserDispatchingErrorListener(this);
+ lexer.addErrorListener(listener_lexer);
+ parser.addErrorListener(listener_parser);
+ return parser;
+ }
+
+ // scim-sql local modification: implements PostgreSQL's scanner-level string continuation
+ // (scan.l {quotecontinue}): a quoted constant continues a preceding quote-style constant
+ // only when separated by horizontal whitespace and line comments with at least one newline.
+ // Block comments do not qualify, and dollar-quoted strings never continue.
+ public boolean IsStringContinuation() {
+ var stream = (CommonTokenStream) getInputStream();
+ var previous = stream.LT(-1);
+ if (previous == null || previous.getType() == PostgreSQLLexer.EndDollarStringConstant) {
+ return false;
+ }
+ var hidden = stream.getHiddenTokensToLeft(stream.LT(1).getTokenIndex());
+ if (hidden == null) {
+ return false;
+ }
+ var sawNewline = false;
+ for (var token : hidden) {
+ var type = token.getType();
+ if (type == PostgreSQLLexer.Newline) {
+ sawNewline = true;
+ } else if (type != PostgreSQLLexer.Whitespace && type != PostgreSQLLexer.LineComment) {
+ return false;
+ }
+ }
+ return sawNewline;
+ }
+
+ public boolean OnlyAcceptableOps()
+ {
+ var c = ((CommonTokenStream)this.getInputStream()).LT(1);
+ var text = c.getText();
+ return text.equals("!") || text.equals("!!")
+ || text.equals("!=-")
+ ;
+ }
+}
diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java
index 027721d..612f523 100644
--- a/src/main/java/module-info.java
+++ b/src/main/java/module-info.java
@@ -4,15 +4,22 @@
*/
/**
- * SCIM filter expression to parameterized SQL converter.
+ * SCIM filter expression to parameterized SQL converter and PostgreSQL query analyzer.
*
- * Parses SCIM filtering
- * expressions and converts them to parameterized SQL WHERE clauses. Supports all SCIM comparison
- * operators (eq, ne, gt, lt, ge, le, co, sw, ew), logical operators (and, or, not), presence (pr),
- * and the in operator with typed values (UUID, timestamp, JSON, boolean, number, string).
+ *
The {@code ai.singlr.scimsql} package parses SCIM filtering expressions and
+ * converts them to parameterized SQL WHERE clauses. Supports all SCIM comparison operators (eq, ne,
+ * gt, lt, ge, le, co, sw, ew), logical operators (and, or, not), presence (pr), and the in operator
+ * with typed values (UUID, timestamp, JSON, boolean, number, string).
+ *
+ *
The {@code ai.singlr.postgresql} package parses complete PostgreSQL statements and reports
+ * structural facts — statement kind and count, referenced relations, columns, functions, named
+ * parameters, and policy-relevant syntactic features — without executing SQL or resolving catalog
+ * objects.
*/
module ai.singlr.scimsql {
requires org.antlr.antlr4.runtime;
exports ai.singlr.scimsql;
+ exports ai.singlr.postgresql;
}
diff --git a/src/test/java/ai/singlr/postgresql/FeatureDetectionTest.java b/src/test/java/ai/singlr/postgresql/FeatureDetectionTest.java
new file mode 100644
index 0000000..cd05a53
--- /dev/null
+++ b/src/test/java/ai/singlr/postgresql/FeatureDetectionTest.java
@@ -0,0 +1,186 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+@DisplayName("Feature detection at any depth")
+class FeatureDetectionTest {
+
+ private static void assertFeature(String sql, QueryFeature feature) {
+ var analysis = PostgresQueryAnalyzer.analyze(sql);
+ assertTrue(analysis.features().contains(feature), feature + " expected for: " + sql);
+ }
+
+ private static void assertNoFeature(String sql, QueryFeature feature) {
+ var analysis = PostgresQueryAnalyzer.analyze(sql);
+ assertFalse(analysis.features().contains(feature), feature + " unexpected for: " + sql);
+ }
+
+ @Test
+ @DisplayName("writable CTE is flagged for insert, update and delete bodies")
+ void shouldFlagWritableCte() {
+ assertFeature(
+ "WITH gone AS (DELETE FROM sessions WHERE expired RETURNING id) SELECT * FROM gone",
+ QueryFeature.WRITABLE_CTE);
+ assertFeature(
+ "WITH ins AS (INSERT INTO t VALUES (1) RETURNING id) SELECT * FROM ins",
+ QueryFeature.WRITABLE_CTE);
+ assertFeature(
+ "WITH upd AS (UPDATE t SET a = 1 RETURNING id) SELECT * FROM upd",
+ QueryFeature.WRITABLE_CTE);
+ assertNoFeature("WITH ro AS (SELECT 1) SELECT * FROM ro", QueryFeature.WRITABLE_CTE);
+ }
+
+ @Test
+ @DisplayName("writable CTE is flagged when nested deep inside a subquery")
+ void shouldFlagNestedWritableCte() {
+ assertFeature(
+ "SELECT * FROM t WHERE x IN ("
+ + "SELECT y FROM ("
+ + "WITH w AS (DELETE FROM inner_t RETURNING y) SELECT y FROM w) sub)",
+ QueryFeature.WRITABLE_CTE);
+ }
+
+ @Test
+ @DisplayName("select into is flagged")
+ void shouldFlagSelectInto() {
+ assertFeature("SELECT * INTO backup_users FROM users", QueryFeature.SELECT_INTO);
+ }
+
+ @Test
+ @DisplayName("row locking clauses are flagged")
+ void shouldFlagRowLocks() {
+ assertFeature("SELECT * FROM t FOR UPDATE", QueryFeature.ROW_LOCK);
+ assertFeature("SELECT * FROM t FOR NO KEY UPDATE", QueryFeature.ROW_LOCK);
+ assertFeature("SELECT * FROM t FOR SHARE NOWAIT", QueryFeature.ROW_LOCK);
+ assertFeature("SELECT * FROM t FOR KEY SHARE SKIP LOCKED", QueryFeature.ROW_LOCK);
+ assertNoFeature("SELECT * FROM t FOR READ ONLY", QueryFeature.ROW_LOCK);
+ }
+
+ @Test
+ @DisplayName("row lock nested in a subquery is flagged")
+ void shouldFlagNestedRowLock() {
+ assertFeature(
+ "SELECT * FROM outer_t WHERE id IN (SELECT id FROM inner_t FOR UPDATE)",
+ QueryFeature.ROW_LOCK);
+ }
+
+ @Test
+ @DisplayName("lateral relations are flagged")
+ void shouldFlagLateral() {
+ assertFeature(
+ "SELECT * FROM users u, LATERAL (SELECT * FROM orders o WHERE o.user_id = u.id) x",
+ QueryFeature.LATERAL);
+ assertFeature(
+ "SELECT * FROM users u JOIN LATERAL generate_series(1, u.n) g ON true",
+ QueryFeature.LATERAL);
+ }
+
+ @Test
+ @DisplayName("function relations are flagged and reported")
+ void shouldFlagFunctionRelation() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT * FROM generate_series(1, 10) AS g(n)");
+
+ assertTrue(analysis.features().contains(QueryFeature.FUNCTION_RELATION));
+ assertEquals(
+ List.of(
+ new RelationReference(null, "generate_series", "g", RelationReference.Kind.FUNCTION)),
+ analysis.relations());
+ }
+
+ @Test
+ @DisplayName("schema-qualified function relation keeps schema")
+ void shouldFlagQualifiedFunctionRelation() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT * FROM pg_catalog.pg_ls_dir('.') d");
+
+ assertEquals(
+ List.of(
+ new RelationReference("pg_catalog", "pg_ls_dir", "d", RelationReference.Kind.FUNCTION)),
+ analysis.relations());
+ }
+
+ @Test
+ @DisplayName("rows from lists every function relation")
+ void shouldFlagRowsFrom() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT * FROM ROWS FROM (generate_series(1, 2), generate_series(3, 4)) AS t(a, b)");
+
+ assertTrue(analysis.features().contains(QueryFeature.FUNCTION_RELATION));
+ assertEquals(2, analysis.relations().size());
+ assertEquals(RelationReference.Kind.FUNCTION, analysis.relations().getFirst().kind());
+ }
+
+ @Test
+ @DisplayName("values as a relation is flagged, insert values is not")
+ void shouldFlagValuesRelation() {
+ assertFeature(
+ "SELECT * FROM (VALUES (1, 'a'), (2, 'b')) AS v(id, label)", QueryFeature.VALUES_RELATION);
+ assertNoFeature("INSERT INTO t VALUES (1, 'a')", QueryFeature.VALUES_RELATION);
+ }
+
+ @Test
+ @DisplayName("star projections are flagged anywhere")
+ void shouldFlagStarProjection() {
+ assertFeature("SELECT * FROM t", QueryFeature.STAR_PROJECTION);
+ assertFeature("SELECT t.* FROM t", QueryFeature.STAR_PROJECTION);
+ assertFeature(
+ "WITH d AS (DELETE FROM t RETURNING *) SELECT 1 FROM d", QueryFeature.STAR_PROJECTION);
+ assertFeature("SELECT 1 FROM t WHERE x IN (SELECT * FROM s)", QueryFeature.STAR_PROJECTION);
+ assertNoFeature("SELECT count(*) FROM t", QueryFeature.STAR_PROJECTION);
+ }
+
+ @Test
+ @DisplayName("subqueries are flagged, plain selects are not")
+ void shouldFlagSubquery() {
+ assertFeature("SELECT (SELECT max(id) FROM t) AS m", QueryFeature.SUBQUERY);
+ assertFeature("SELECT * FROM (SELECT 1) sub", QueryFeature.SUBQUERY);
+ assertFeature("SELECT * FROM t WHERE id IN (SELECT id FROM s)", QueryFeature.SUBQUERY);
+ assertNoFeature("SELECT 1", QueryFeature.SUBQUERY);
+ assertNoFeature("(SELECT 1)", QueryFeature.SUBQUERY);
+ assertNoFeature("WITH a AS (SELECT 1) SELECT * FROM a", QueryFeature.SUBQUERY);
+ }
+
+ @Test
+ @DisplayName("set operations nested in subqueries are flagged")
+ void shouldFlagNestedSetOperation() {
+ assertFeature(
+ "SELECT * FROM t WHERE id IN (SELECT id FROM a UNION SELECT id FROM b)",
+ QueryFeature.SET_OPERATION);
+ }
+
+ @Test
+ @DisplayName("multiple statements are counted and flagged")
+ void shouldFlagMultipleStatements() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT 1; SELECT * FROM t; DELETE FROM t");
+
+ assertEquals(3, analysis.statementCount());
+ assertEquals(StatementKind.SELECT, analysis.statementKind());
+ assertTrue(analysis.features().contains(QueryFeature.MULTIPLE_STATEMENTS));
+ assertEquals(2, analysis.relations().size());
+ }
+
+ @Test
+ @DisplayName("cte flags are detected inside insert, update and delete statements")
+ void shouldFlagCteOnDml() {
+ assertFeature(
+ "WITH src AS (SELECT * FROM staging) INSERT INTO t SELECT * FROM src", QueryFeature.CTE);
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH src AS (SELECT id FROM staging) UPDATE t SET a = 1"
+ + " WHERE id IN (SELECT id FROM src)");
+ assertTrue(analysis.features().contains(QueryFeature.CTE));
+ var kinds = analysis.relations().stream().map(r -> r.name() + ":" + r.kind()).toList();
+ assertEquals(List.of("staging:PHYSICAL", "t:PHYSICAL", "src:CTE"), kinds);
+ }
+}
diff --git a/src/test/java/ai/singlr/postgresql/GrammarWarningsTest.java b/src/test/java/ai/singlr/postgresql/GrammarWarningsTest.java
new file mode 100644
index 0000000..00e9833
--- /dev/null
+++ b/src/test/java/ai/singlr/postgresql/GrammarWarningsTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import org.antlr.v4.Tool;
+import org.antlr.v4.tool.ANTLRMessage;
+import org.antlr.v4.tool.ANTLRToolListener;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Pins the expected ANTLR warnings for the vendored PostgreSQL grammar. Upstream ships two benign
+ * warning-146 lexer rules; any new warning or error after a grammar upgrade or local modification
+ * must be reviewed and either fixed or pinned here deliberately.
+ */
+@DisplayName("Vendored grammar warnings are pinned")
+class GrammarWarningsTest {
+
+ private static final Path GRAMMAR_DIR =
+ Path.of("src", "main", "antlr4", "ai", "singlr", "postgresql", "parser");
+
+ private static final List EXPECTED_WARNINGS =
+ List.of(
+ "146:AfterEscapeStringConstantMode_NotContinued",
+ "146:AfterEscapeStringConstantWithNewlineMode_NotContinued");
+
+ @Test
+ @DisplayName("lexer and parser grammars produce exactly the pinned warnings")
+ void shouldMatchPinnedWarnings(@TempDir Path outputDir) throws Exception {
+ assertTrue(Files.exists(GRAMMAR_DIR.resolve("PostgreSQLLexer.g4")));
+ for (var grammar : List.of("PostgreSQLLexer.g4", "PostgreSQLParser.g4")) {
+ Files.copy(GRAMMAR_DIR.resolve(grammar), outputDir.resolve(grammar));
+ }
+
+ var warnings = new ArrayList();
+ var errors = new ArrayList();
+
+ runTool(outputDir, warnings, errors, "PostgreSQLLexer.g4");
+ runTool(outputDir, warnings, errors, "PostgreSQLParser.g4");
+
+ assertEquals(List.of(), errors);
+ assertEquals(EXPECTED_WARNINGS, warnings);
+ }
+
+ private static void runTool(
+ Path outputDir, List warnings, List errors, String grammar) {
+ var tool =
+ new Tool(
+ new String[] {
+ "-o",
+ outputDir.toString(),
+ "-lib",
+ outputDir.toString(),
+ outputDir.resolve(grammar).toString()
+ });
+ tool.removeListeners();
+ tool.addListener(
+ new ANTLRToolListener() {
+ @Override
+ public void info(String msg) {}
+
+ @Override
+ public void error(ANTLRMessage msg) {
+ errors.add(render(msg));
+ }
+
+ @Override
+ public void warning(ANTLRMessage msg) {
+ warnings.add(render(msg));
+ }
+ });
+ tool.processGrammarsOnCommandLine();
+ }
+
+ private static String render(ANTLRMessage msg) {
+ var args = msg.getArgs();
+ return msg.getErrorType().code + (args.length > 0 ? ":" + args[0] : "");
+ }
+}
diff --git a/src/test/java/ai/singlr/postgresql/HostileInputTest.java b/src/test/java/ai/singlr/postgresql/HostileInputTest.java
new file mode 100644
index 0000000..8eb1e6c
--- /dev/null
+++ b/src/test/java/ai/singlr/postgresql/HostileInputTest.java
@@ -0,0 +1,255 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.time.Duration;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+@DisplayName("Hostile and malformed input")
+class HostileInputTest {
+
+ @Test
+ @DisplayName("null and blank input are rejected")
+ void shouldRejectNullAndBlank() {
+ assertThrows(QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze(null));
+ assertThrows(QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze(""));
+ assertThrows(QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze(" \n\t"));
+ }
+
+ @Test
+ @DisplayName("oversized input is rejected before parsing")
+ void shouldRejectOversizedInput() {
+ var oversized = "SELECT " + "1,".repeat(PostgresQueryAnalyzer.MAX_LENGTH / 2) + "1";
+ var exception =
+ assertThrows(QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze(oversized));
+ assertTrue(exception.reason().contains("characters"));
+ }
+
+ @Test
+ @DisplayName("token floods are rejected")
+ void shouldRejectTokenFlood() {
+ var flood = "SELECT " + "1,".repeat(60_000) + "1";
+ assertTrue(flood.length() <= PostgresQueryAnalyzer.MAX_LENGTH);
+ var exception =
+ assertThrows(QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze(flood));
+ assertTrue(exception.reason().contains("tokens"));
+ }
+
+ @Test
+ @DisplayName("deep nesting is rejected deterministically and quickly")
+ void shouldRejectDeepNesting() {
+ var depth = PostgresQueryAnalyzer.MAX_NESTING_DEPTH + 1;
+ var nested = "SELECT " + "(".repeat(depth) + "1" + ")".repeat(depth);
+ assertTimeoutPreemptively(
+ Duration.ofSeconds(10),
+ () -> {
+ var exception =
+ assertThrows(
+ QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze(nested));
+ assertTrue(exception.reason().contains("nesting"));
+ });
+ }
+
+ @Test
+ @DisplayName("nesting at the limit parses")
+ void shouldParseModerateNesting() {
+ var nested = "SELECT " + "(".repeat(40) + "1" + ")".repeat(40);
+ assertEquals(1, PostgresQueryAnalyzer.analyze(nested).statementCount());
+ }
+
+ @Test
+ @DisplayName("semicolons inside every string form do not create statements")
+ void shouldIgnoreEmbeddedSemicolons() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT 'a;b', e'c;d', $$e;f$$, $tag$g;h$tag$, \"col;name\" FROM t"
+ + " -- trailing ; comment\n /* block ; comment */");
+
+ assertEquals(1, analysis.statementCount());
+ }
+
+ @Test
+ @DisplayName("trailing semicolons do not add statements")
+ void shouldIgnoreTrailingSemicolons() {
+ assertEquals(1, PostgresQueryAnalyzer.analyze("SELECT 1;").statementCount());
+ assertEquals(1, PostgresQueryAnalyzer.analyze("SELECT 1;;;").statementCount());
+ }
+
+ @Test
+ @DisplayName("only semicolons or comments is rejected as no statement")
+ void shouldRejectEmptyStatementList() {
+ for (var sql : new String[] {";;", "-- just a comment", "/* nothing */"}) {
+ var exception =
+ assertThrows(QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze(sql));
+ assertTrue(
+ exception.reason().contains("no sql statement") || exception.reason().contains("blank"),
+ sql + " -> " + exception.reason());
+ }
+ }
+
+ @Test
+ @DisplayName("trailing garbage is rejected with a position")
+ void shouldRejectTrailingGarbage() {
+ var exception =
+ assertThrows(
+ QueryAnalysisException.class,
+ () -> PostgresQueryAnalyzer.analyze("SELECT 1 FROM t klaatu barada nikto"));
+ assertEquals("invalid sql syntax", exception.reason());
+ assertEquals(1, exception.line());
+ }
+
+ @Test
+ @DisplayName("syntax errors never leak sql content")
+ void shouldNotLeakSqlContent() {
+ var secret = "SuperSecretLiteral12345";
+ var exception =
+ assertThrows(
+ QueryAnalysisException.class,
+ () -> PostgresQueryAnalyzer.analyze("SELECT WHERE '" + secret + "' &&& %%%"));
+ assertFalse(exception.getMessage().contains(secret));
+ assertFalse(String.valueOf(exception.reason()).contains(secret));
+ }
+
+ @Test
+ @DisplayName("unterminated strings and dollar quotes fail cleanly")
+ void shouldRejectUnterminatedLiterals() {
+ assertThrows(QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze("SELECT 'abc"));
+ assertThrows(
+ QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze("SELECT $tag$abc"));
+ }
+
+ @Test
+ @DisplayName("unicode and exotic identifiers parse")
+ void shouldParseUnicodeIdentifiers() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT \"名前\", \"a\"\"b\" FROM \"таблица\"");
+
+ assertEquals("таблица", analysis.relations().getFirst().name());
+ assertEquals("名前", analysis.columns().getFirst().name());
+ assertEquals("a\"b", analysis.columns().get(1).name());
+ }
+
+ @Test
+ @DisplayName("nested block comments are handled")
+ void shouldHandleNestedBlockComments() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT 1 /* outer /* inner ; */ still outer */");
+
+ assertEquals(1, analysis.statementCount());
+ }
+
+ @Test
+ @DisplayName("deeply nested block comments lex in linear time and constant stack")
+ void shouldHandleDeepBlockCommentNesting() {
+ var sql = "SELECT 1 " + "/*".repeat(5_000) + "x" + "*/".repeat(5_000);
+ assertTimeoutPreemptively(
+ Duration.ofSeconds(5),
+ () -> assertEquals(1, PostgresQueryAnalyzer.analyze(sql).statementCount()));
+ }
+
+ @Test
+ @DisplayName("unterminated nested block comment is rejected")
+ void shouldRejectUnterminatedBlockComment() {
+ assertThrows(
+ QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze("SELECT 1 /* /* x */"));
+ assertThrows(QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze("SELECT 1 /*"));
+ }
+
+ @Test
+ @DisplayName("psql meta-commands are rejected, never treated as statement separators")
+ void shouldRejectPsqlMetaCommands() {
+ for (var sql :
+ new String[] {
+ "SELECT 1; \\! id\n",
+ "SELECT 1;\n\\echo pwned\n",
+ "\\copy secrets TO '/tmp/out'",
+ "SELECT 1\\; SELECT 2;"
+ }) {
+ assertThrows(QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze(sql), sql);
+ }
+ }
+
+ @Test
+ @DisplayName("unicode escaped identifiers are rejected, unicode escaped strings parse")
+ void shouldRejectUnicodeEscapedIdentifiers() {
+ var exception =
+ assertThrows(
+ QueryAnalysisException.class,
+ () -> PostgresQueryAnalyzer.analyze("SELECT * FROM U&\"d\\0061t\""));
+ assertTrue(exception.reason().contains("unicode"));
+ assertThrows(
+ QueryAnalysisException.class,
+ () -> PostgresQueryAnalyzer.analyze("SELECT * FROM U&\"d!0061t\" UESCAPE '!'"));
+ assertEquals(1, PostgresQueryAnalyzer.analyze("SELECT U&'\\0061'").statementCount());
+ }
+
+ @Test
+ @DisplayName("identifiers longer than 63 bytes are rejected before analysis")
+ void shouldRejectOverlengthIdentifiers() {
+ var atLimit = "a".repeat(PostgresQueryAnalyzer.MAX_IDENTIFIER_BYTES);
+ for (var sql :
+ new String[] {
+ "SELECT 1 FROM " + atLimit + "x",
+ "SELECT 1 FROM \"" + atLimit + "x\"",
+ "SELECT \"" + "я".repeat(32) + "\" FROM t"
+ }) {
+ var exception =
+ assertThrows(QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze(sql), sql);
+ assertTrue(exception.reason().contains("63 bytes"), exception.reason());
+ }
+ }
+
+ @Test
+ @DisplayName("identifiers at exactly 63 bytes parse, measured after quote unescaping")
+ void shouldAcceptIdentifiersAtByteLimit() {
+ var atLimit = "a".repeat(PostgresQueryAnalyzer.MAX_IDENTIFIER_BYTES);
+ assertEquals(
+ atLimit,
+ PostgresQueryAnalyzer.analyze("SELECT 1 FROM " + atLimit).relations().getFirst().name());
+
+ var escaped = "a".repeat(62) + "\"\"";
+ assertEquals(
+ "a".repeat(62) + "\"",
+ PostgresQueryAnalyzer.analyze("SELECT 1 FROM \"" + escaped + "\"")
+ .relations()
+ .getFirst()
+ .name());
+ }
+
+ @Test
+ @DisplayName("dollar tag confusion does not break statement counting")
+ void shouldHandleDollarTagTricks() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT $a$ $b$ ; $a$, $b$x$b$ FROM t");
+
+ assertEquals(1, analysis.statementCount());
+ }
+
+ @Test
+ @DisplayName("hostile inputs complete within the time budget")
+ void shouldStayWithinTimeBudget() {
+ var wide = "SELECT " + "abs(x) + ".repeat(2_000) + "1 FROM t";
+ assertTimeoutPreemptively(
+ Duration.ofSeconds(30),
+ () -> assertEquals(1, PostgresQueryAnalyzer.analyze(wide).statementCount()));
+ }
+
+ @Test
+ @DisplayName("exception positions are 1-based line and 0-based column")
+ void shouldReportErrorPosition() {
+ var exception =
+ assertThrows(
+ QueryAnalysisException.class,
+ () -> PostgresQueryAnalyzer.analyze("SELECT 1\nFROM t WHERE"));
+ assertTrue(exception.line() >= 1);
+ assertTrue(exception.column() >= 0);
+ assertTrue(exception.getMessage().contains("line"));
+ }
+}
diff --git a/src/test/java/ai/singlr/postgresql/ModuleDescriptorTest.java b/src/test/java/ai/singlr/postgresql/ModuleDescriptorTest.java
new file mode 100644
index 0000000..9154ea1
--- /dev/null
+++ b/src/test/java/ai/singlr/postgresql/ModuleDescriptorTest.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.lang.module.ModuleFinder;
+import java.nio.file.Path;
+import java.util.Set;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+@DisplayName("Module descriptor")
+class ModuleDescriptorTest {
+
+ @Test
+ @DisplayName("public packages are exported, the vendored parser package is not")
+ void shouldExportOnlyPublicPackages() {
+ var module =
+ ModuleFinder.of(Path.of("target", "classes")).find("ai.singlr.scimsql").orElseThrow();
+
+ Set exports =
+ module.descriptor().exports().stream()
+ .map(java.lang.module.ModuleDescriptor.Exports::source)
+ .collect(java.util.stream.Collectors.toSet());
+
+ assertTrue(exports.contains("ai.singlr.scimsql"));
+ assertTrue(exports.contains("ai.singlr.postgresql"));
+ assertFalse(exports.contains("ai.singlr.postgresql.parser"));
+ }
+}
diff --git a/src/test/java/ai/singlr/postgresql/NamedParameterTest.java b/src/test/java/ai/singlr/postgresql/NamedParameterTest.java
new file mode 100644
index 0000000..3d9bf97
--- /dev/null
+++ b/src/test/java/ai/singlr/postgresql/NamedParameterTest.java
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import java.util.Set;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+@DisplayName("Named parameters")
+class NamedParameterTest {
+
+ @Test
+ @DisplayName("parameters are captured and deduplicated in order")
+ void shouldCaptureAndDeduplicate() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT * FROM events WHERE created_at >= :start_at AND created_at < :end_at"
+ + " AND user_id = :user_id AND tenant_id = :user_id");
+
+ assertEquals(List.of("start_at", "end_at", "user_id"), List.copyOf(analysis.parameters()));
+ }
+
+ @Test
+ @DisplayName("parameter names are reported exactly as written")
+ void shouldPreserveParameterCase() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT :userId, :USER_ID");
+
+ assertEquals(Set.of("userId", "USER_ID"), analysis.parameters());
+ }
+
+ @Test
+ @DisplayName("parameters work in every expression position")
+ void shouldCaptureParametersEverywhere() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "INSERT INTO t (a, b) VALUES (:a, coalesce(:b, 0));"
+ + "UPDATE t SET a = :c WHERE id = ANY(ARRAY[:d]);"
+ + "SELECT * FROM t WHERE x IN (:e, :f) ORDER BY y LIMIT :g OFFSET :h");
+
+ assertEquals(Set.of("a", "b", "c", "d", "e", "f", "g", "h"), analysis.parameters());
+ }
+
+ @Test
+ @DisplayName("typecast, json operators and slices do not produce parameters")
+ void shouldNotConfuseOperators() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT a::text, b ->> 'k', c #>> '{x,y}', arr[1:2] FROM t WHERE d = :real_param");
+
+ assertEquals(Set.of("real_param"), analysis.parameters());
+ }
+
+ @Test
+ @DisplayName("json key colon adjacency is an operator, not a parameter")
+ void shouldNotConfuseJsonColon() {
+ var spaced = PostgresQueryAnalyzer.analyze("SELECT JSON_OBJECT('a' : owner) FROM t");
+ var compact = PostgresQueryAnalyzer.analyze("SELECT JSON_OBJECT('a':owner) FROM t");
+
+ assertEquals(Set.of(), compact.parameters());
+ assertEquals(spaced.normalizedSql(), compact.normalizedSql());
+ assertEquals(
+ Set.of(), PostgresQueryAnalyzer.analyze("SELECT JSON_OBJECT(k:v) FROM t").parameters());
+ }
+
+ @Test
+ @DisplayName("json keys and values may still be parameters")
+ void shouldCaptureJsonValueParameters() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT JSON_OBJECT('k':owner) FROM t WHERE id = :id AND x = JSON_OBJECT('v' : :v)");
+
+ assertEquals(Set.of("id", "v"), analysis.parameters());
+ assertEquals(
+ Set.of("key", "val"),
+ PostgresQueryAnalyzer.analyze("SELECT JSON_OBJECT(:key : :val)").parameters());
+ }
+
+ @Test
+ @DisplayName("array slices with identifier bounds parse and produce no parameters")
+ void shouldNotConfuseSliceBounds() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT arr[lo:hi], arr[1:n], arr[f(x):g(y)] FROM t" + " WHERE x = :p");
+
+ assertEquals(Set.of("p"), analysis.parameters());
+ }
+
+ @Test
+ @DisplayName("colon directly after an opening bracket stays a parameter")
+ void shouldKeepParameterAfterOpeningBracket() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT arr[:idx] FROM t");
+
+ assertEquals(Set.of("idx"), analysis.parameters());
+ }
+
+ @Test
+ @DisplayName("colon-like content in strings, comments and quoted identifiers is inert")
+ void shouldIgnoreColonContent() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT ':not_a_param', \":also_not\", e':nope', $$ :still_not $$"
+ + " -- :comment_param\n"
+ + " /* :block_param */ FROM t WHERE x = :yes");
+
+ assertEquals(Set.of("yes"), analysis.parameters());
+ }
+
+ @Test
+ @DisplayName("parameters are captured at any depth including CTE bodies")
+ void shouldCaptureNestedParameters() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH recent AS (SELECT * FROM events WHERE at > :since)"
+ + " SELECT * FROM recent WHERE kind = ANY(SELECT kind FROM kinds"
+ + " WHERE weight > :min_weight)");
+
+ assertEquals(Set.of("since", "min_weight"), analysis.parameters());
+ }
+
+ @Test
+ @DisplayName("bare colon fails")
+ void shouldRejectBareColon() {
+ assertThrows(
+ QueryAnalysisException.class,
+ () -> PostgresQueryAnalyzer.analyze("SELECT * FROM t WHERE id = :"));
+ }
+
+ @Test
+ @DisplayName("colon followed by space and name fails")
+ void shouldRejectDetachedName() {
+ assertThrows(
+ QueryAnalysisException.class,
+ () -> PostgresQueryAnalyzer.analyze("SELECT * FROM t WHERE id = : user_id"));
+ }
+
+ @Test
+ @DisplayName("quoted parameter syntax fails")
+ void shouldRejectQuotedParameter() {
+ assertThrows(
+ QueryAnalysisException.class,
+ () -> PostgresQueryAnalyzer.analyze("SELECT * FROM t WHERE id = :\"user_id\""));
+ }
+
+ @Test
+ @DisplayName("parameter cannot be used as a relation or alias")
+ void shouldRejectParameterAsIdentifier() {
+ assertThrows(
+ QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze("SELECT 1 FROM :tbl"));
+ assertThrows(
+ QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze("SELECT 1 AS :alias"));
+ }
+
+ @Test
+ @DisplayName("positional dollar parameters are not named parameters")
+ void shouldIgnorePositionalParameters() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT * FROM t WHERE a = $1 AND b = :b");
+
+ assertEquals(Set.of("b"), analysis.parameters());
+ }
+
+ @Test
+ @DisplayName("values are never bound or substituted")
+ void shouldOnlyReportNames() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT :p");
+
+ assertTrue(analysis.normalizedSql().contains(":p"));
+ }
+}
diff --git a/src/test/java/ai/singlr/postgresql/NormalizationTest.java b/src/test/java/ai/singlr/postgresql/NormalizationTest.java
new file mode 100644
index 0000000..aee3adb
--- /dev/null
+++ b/src/test/java/ai/singlr/postgresql/NormalizationTest.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+@DisplayName("Normalization")
+class NormalizationTest {
+
+ private static String normalize(String sql) {
+ return PostgresQueryAnalyzer.analyze(sql).normalizedSql();
+ }
+
+ @Test
+ @DisplayName("whitespace and comments do not change the normalized form")
+ void shouldBeStableAcrossFormatting() {
+ var canonical = normalize("SELECT id, name FROM users WHERE active = true");
+
+ assertEquals(canonical, normalize("SELECT id,\n\tname\nFROM users\nWHERE active = true"));
+ assertEquals(
+ canonical,
+ normalize("SELECT id, -- projection\n name /* the name */ FROM users WHERE active = true"));
+ }
+
+ @Test
+ @DisplayName("keyword and unquoted identifier case do not change the normalized form")
+ void shouldBeStableAcrossCase() {
+ assertEquals(normalize("select id from Users"), normalize("SELECT ID FROM USERS"));
+ }
+
+ @Test
+ @DisplayName("quoted identifiers, strings and parameter names are preserved verbatim")
+ void shouldPreserveSemanticText() {
+ var normalized = normalize("SELECT \"MixedCase\", 'Literal TEXT', :ParamName FROM \"T\"");
+
+ assertEquals("select \"MixedCase\" , 'Literal TEXT' , :ParamName from \"T\"", normalized);
+ }
+
+ @Test
+ @DisplayName("semantic changes produce different normalized forms")
+ void shouldDifferOnSemanticChanges() {
+ var canonical = normalize("SELECT a FROM t WHERE x > 1");
+
+ assertNotEquals(canonical, normalize("SELECT b FROM t WHERE x > 1"));
+ assertNotEquals(canonical, normalize("SELECT a FROM t2 WHERE x > 1"));
+ assertNotEquals(canonical, normalize("SELECT a FROM t WHERE x >= 1"));
+ assertNotEquals(canonical, normalize("SELECT a FROM t WHERE x > 2"));
+ }
+
+ @Test
+ @DisplayName("non-ascii identifier case changes the normalized form")
+ void shouldPreserveNonAsciiIdentifierCase() {
+ assertEquals("select marker from Таблица", normalize("SELECT marker FROM Таблица"));
+ assertNotEquals(
+ normalize("SELECT marker FROM Таблица"), normalize("SELECT marker FROM таблица"));
+ }
+
+ @Test
+ @DisplayName("quoted identifier case changes the normalized form")
+ void shouldDifferOnQuotedIdentifierCase() {
+ assertNotEquals(
+ normalize("SELECT \"Users\".id FROM \"Users\""),
+ normalize("SELECT \"users\".id FROM \"users\""));
+ }
+
+ @Test
+ @DisplayName("parameter name changes the normalized form")
+ void shouldDifferOnParameterName() {
+ assertNotEquals(
+ normalize("SELECT * FROM t WHERE id = :a"), normalize("SELECT * FROM t WHERE id = :b"));
+ }
+
+ @Test
+ @DisplayName("string continuation normalizes to a single line")
+ void shouldStaySingleLine() {
+ assertEquals("select 'a' 'b'", normalize("SELECT 'a'\n'b'"));
+ assertEquals(-1, normalize("SELECT 'a'\n'b'\n'c' FROM t").indexOf('\n'));
+ }
+
+ @Test
+ @DisplayName("normalization keeps dollar-quoted content verbatim")
+ void shouldPreserveDollarQuotedContent() {
+ var normalized = normalize("SELECT $tag$Keep CASE and -- fake comment$tag$");
+
+ assertEquals("select $tag$Keep CASE and -- fake comment$tag$", normalized);
+ }
+}
diff --git a/src/test/java/ai/singlr/postgresql/PostgresQueryAnalyzerTest.java b/src/test/java/ai/singlr/postgresql/PostgresQueryAnalyzerTest.java
new file mode 100644
index 0000000..4777df2
--- /dev/null
+++ b/src/test/java/ai/singlr/postgresql/PostgresQueryAnalyzerTest.java
@@ -0,0 +1,672 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import java.util.Set;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+@DisplayName("PostgresQueryAnalyzer core analysis")
+class PostgresQueryAnalyzerTest {
+
+ @Test
+ @DisplayName("minimal select produces exact analysis")
+ void shouldAnalyzeMinimalSelect() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT 1");
+
+ assertEquals(StatementKind.SELECT, analysis.statementKind());
+ assertEquals(1, analysis.statementCount());
+ assertEquals(List.of(), analysis.relations());
+ assertEquals(List.of(), analysis.columns());
+ assertEquals(List.of(), analysis.functions());
+ assertEquals(Set.of(), analysis.parameters());
+ assertEquals(Set.of(), analysis.features());
+ assertEquals("select 1", analysis.normalizedSql());
+ }
+
+ @Test
+ @DisplayName("simple select reports relation and columns")
+ void shouldAnalyzeSimpleSelect() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT id, name FROM users WHERE active = true");
+
+ assertEquals(StatementKind.SELECT, analysis.statementKind());
+ assertEquals(
+ List.of(new RelationReference(null, "users", null, RelationReference.Kind.PHYSICAL)),
+ analysis.relations());
+ assertEquals(
+ List.of(
+ new ColumnReference(null, "id"),
+ new ColumnReference(null, "name"),
+ new ColumnReference(null, "active")),
+ analysis.columns());
+ }
+
+ @Test
+ @DisplayName("join preserves aliases and qualifiers")
+ void shouldAnalyzeJoin() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT u.id, o.total FROM users u JOIN orders o ON o.user_id = u.id");
+
+ assertEquals(
+ List.of(
+ new RelationReference(null, "users", "u", RelationReference.Kind.PHYSICAL),
+ new RelationReference(null, "orders", "o", RelationReference.Kind.PHYSICAL)),
+ analysis.relations());
+ assertEquals(
+ List.of(
+ new ColumnReference("u", "id"),
+ new ColumnReference("o", "total"),
+ new ColumnReference("o", "user_id"),
+ new ColumnReference("u", "id")),
+ analysis.columns());
+ }
+
+ @Test
+ @DisplayName("aggregate query captures functions in select, group by and having")
+ void shouldAnalyzeAggregate() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT count(id), lower(name) FROM users GROUP BY lower(name)"
+ + " HAVING max(age) > 21");
+
+ var names = analysis.functions().stream().map(FunctionReference::name).toList();
+ assertEquals(List.of("count", "lower", "lower", "max"), names);
+ assertEquals(Set.of(), analysis.features());
+ }
+
+ @Test
+ @DisplayName("window function flags WINDOW and captures function")
+ void shouldAnalyzeWindow() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT rank() OVER (PARTITION BY dept ORDER BY salary) FROM emp");
+
+ assertTrue(analysis.features().contains(QueryFeature.WINDOW));
+ assertEquals("rank", analysis.functions().getFirst().name());
+ }
+
+ @Test
+ @DisplayName("named window clause flags WINDOW")
+ void shouldAnalyzeNamedWindow() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze("SELECT sum(x) OVER w FROM t WINDOW w AS (PARTITION BY y)");
+
+ assertTrue(analysis.features().contains(QueryFeature.WINDOW));
+ }
+
+ @Test
+ @DisplayName("set operations flag SET_OPERATION")
+ void shouldAnalyzeSetOperations() {
+ for (var op : List.of("UNION", "UNION ALL", "INTERSECT", "EXCEPT")) {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT a FROM t1 " + op + " SELECT a FROM t2");
+ assertTrue(
+ analysis.features().contains(QueryFeature.SET_OPERATION), op + " should be flagged");
+ assertEquals(2, analysis.relations().size(), op);
+ }
+ }
+
+ @Test
+ @DisplayName("quoted and schema-qualified identifiers are preserved")
+ void shouldPreserveQuotedIdentifiers() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT \"Weird Name\", MixedCase FROM \"MySchema\".\"MyTable\" mt,"
+ + " analytics.events");
+
+ assertEquals(
+ List.of(
+ new RelationReference("MySchema", "MyTable", "mt", RelationReference.Kind.PHYSICAL),
+ new RelationReference("analytics", "events", null, RelationReference.Kind.PHYSICAL)),
+ analysis.relations());
+ assertEquals("MySchema", analysis.relations().getFirst().schema());
+ assertEquals(
+ List.of(new ColumnReference(null, "Weird Name"), new ColumnReference(null, "mixedcase")),
+ analysis.columns());
+ assertEquals("analytics", analysis.relations().get(1).schema());
+ }
+
+ @Test
+ @DisplayName("nested correlated subquery keeps physical relations at all depths")
+ void shouldAnalyzeCorrelatedSubquery() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT u.id FROM users u WHERE EXISTS ("
+ + "SELECT 1 FROM orders o WHERE o.user_id = u.id AND o.total > ("
+ + "SELECT avg(total) FROM orders))");
+
+ assertTrue(analysis.features().contains(QueryFeature.SUBQUERY));
+ var names = analysis.relations().stream().map(RelationReference::name).toList();
+ assertEquals(List.of("users", "orders", "orders"), names);
+ }
+
+ @Test
+ @DisplayName("CTE references are distinguished from physical relations")
+ void shouldDistinguishCteFromPhysical() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH active AS (SELECT * FROM users WHERE active) SELECT * FROM active, orders");
+
+ assertEquals(
+ List.of(
+ new RelationReference(null, "users", null, RelationReference.Kind.PHYSICAL),
+ new RelationReference(null, "active", null, RelationReference.Kind.CTE),
+ new RelationReference(null, "orders", null, RelationReference.Kind.PHYSICAL)),
+ analysis.relations());
+ assertTrue(analysis.features().contains(QueryFeature.CTE));
+ }
+
+ @Test
+ @DisplayName("non-recursive CTE body referencing its own name is physical")
+ void shouldTreatSelfReferenceInNonRecursiveCteAsPhysical() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze("WITH users AS (SELECT * FROM users) SELECT * FROM users");
+
+ assertEquals(
+ List.of(
+ new RelationReference(null, "users", null, RelationReference.Kind.PHYSICAL),
+ new RelationReference(null, "users", null, RelationReference.Kind.CTE)),
+ analysis.relations());
+ }
+
+ @Test
+ @DisplayName("recursive CTE self-reference is a CTE reference")
+ void shouldTreatSelfReferenceInRecursiveCteAsCte() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH RECURSIVE tree AS ("
+ + "SELECT id, parent_id FROM nodes WHERE parent_id IS NULL"
+ + " UNION ALL SELECT n.id, n.parent_id FROM nodes n JOIN tree t"
+ + " ON n.parent_id = t.id) SELECT * FROM tree");
+
+ assertTrue(analysis.features().contains(QueryFeature.RECURSIVE_CTE));
+ var kinds = analysis.relations().stream().map(r -> r.name() + ":" + r.kind()).toList();
+ assertEquals(List.of("nodes:PHYSICAL", "nodes:PHYSICAL", "tree:CTE", "tree:CTE"), kinds);
+ }
+
+ @Test
+ @DisplayName("later CTE sees earlier sibling CTE")
+ void shouldScopeSiblingCtes() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH a AS (SELECT 1 AS x), b AS (SELECT x FROM a) SELECT * FROM b");
+
+ assertEquals(
+ List.of(
+ new RelationReference(null, "a", null, RelationReference.Kind.CTE),
+ new RelationReference(null, "b", null, RelationReference.Kind.CTE)),
+ analysis.relations());
+ }
+
+ @Test
+ @DisplayName("inner CTE shadows outer physical relation without losing outer physical refs")
+ void shouldHandleShadowedCteNames() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT * FROM users WHERE id IN ("
+ + "WITH users AS (SELECT owner_id FROM projects)"
+ + " SELECT owner_id FROM users)");
+
+ var kinds = analysis.relations().stream().map(r -> r.name() + ":" + r.kind()).toList();
+ assertEquals(List.of("users:PHYSICAL", "projects:PHYSICAL", "users:CTE"), kinds);
+ }
+
+ @Test
+ @DisplayName("schema-qualified reference never matches a CTE name")
+ void shouldNotMatchSchemaQualifiedAsCte() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze("WITH users AS (SELECT 1) SELECT * FROM public.users, users");
+
+ assertEquals(
+ List.of(
+ new RelationReference("public", "users", null, RelationReference.Kind.PHYSICAL),
+ new RelationReference(null, "users", null, RelationReference.Kind.CTE)),
+ analysis.relations());
+ }
+
+ @Test
+ @DisplayName("CTE name never hides an insert target")
+ void shouldKeepInsertTargetPhysicalDespiteCteName() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH target AS (SELECT 1 AS id) INSERT INTO target SELECT id FROM target");
+
+ var kinds = analysis.relations().stream().map(r -> r.name() + ":" + r.kind()).toList();
+ assertEquals(List.of("target:PHYSICAL", "target:CTE"), kinds);
+ }
+
+ @Test
+ @DisplayName("CTE name never hides update and delete targets")
+ void shouldKeepUpdateAndDeleteTargetsPhysicalDespiteCteName() {
+ var update =
+ PostgresQueryAnalyzer.analyze(
+ "WITH target AS (SELECT 1 AS id) UPDATE target SET id = 2 FROM target");
+ var updateKinds = update.relations().stream().map(r -> r.name() + ":" + r.kind()).toList();
+ assertEquals(List.of("target:PHYSICAL", "target:CTE"), updateKinds);
+
+ var delete =
+ PostgresQueryAnalyzer.analyze(
+ "WITH target AS (SELECT 1 AS id) DELETE FROM target USING target t WHERE t.id = 1");
+ var deleteKinds = delete.relations().stream().map(r -> r.name() + ":" + r.kind()).toList();
+ assertEquals(List.of("target:PHYSICAL", "target:CTE"), deleteKinds);
+ }
+
+ @Test
+ @DisplayName("merge accepts a leading with clause and resolves the using source as a CTE")
+ void shouldAnalyzeMergeWithCte() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH src AS (SELECT id FROM staging) MERGE INTO target t USING src s"
+ + " ON t.id = s.id WHEN NOT MATCHED THEN INSERT VALUES (s.id)");
+
+ assertEquals(StatementKind.MERGE, analysis.statementKind());
+ assertTrue(analysis.features().contains(QueryFeature.CTE));
+ var kinds = analysis.relations().stream().map(r -> r.name() + ":" + r.kind()).toList();
+ assertEquals(List.of("staging:PHYSICAL", "target:PHYSICAL", "src:CTE"), kinds);
+ }
+
+ @Test
+ @DisplayName("CTE name never hides a merge target")
+ void shouldKeepMergeTargetPhysicalDespiteCteName() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH target AS (SELECT 1 AS id) MERGE INTO target USING target t"
+ + " ON target.id = t.id WHEN MATCHED THEN DO NOTHING");
+
+ var kinds = analysis.relations().stream().map(r -> r.name() + ":" + r.kind()).toList();
+ assertEquals(List.of("target:PHYSICAL", "target:CTE"), kinds);
+ }
+
+ @Test
+ @DisplayName("CTE name never hides a select into target")
+ void shouldKeepSelectIntoTargetPhysicalDespiteCteName() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH target AS (SELECT 1 AS id) SELECT id INTO target FROM target");
+
+ var kinds = analysis.relations().stream().map(r -> r.name() + ":" + r.kind()).toList();
+ assertEquals(List.of("target:PHYSICAL", "target:CTE"), kinds);
+ }
+
+ @Test
+ @DisplayName("recursive CTE sees later sibling CTE")
+ void shouldScopeForwardReferenceInRecursiveWith() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH RECURSIVE x AS (SELECT * FROM y), y AS (SELECT 1) SELECT * FROM x");
+
+ assertEquals(
+ List.of(
+ new RelationReference(null, "y", null, RelationReference.Kind.CTE),
+ new RelationReference(null, "x", null, RelationReference.Kind.CTE)),
+ analysis.relations());
+ }
+
+ @Test
+ @DisplayName("identifier folding is ascii-only so distinct unicode names stay distinct")
+ void shouldFoldOnlyAsciiIdentifierCase() {
+ var upper = PostgresQueryAnalyzer.analyze("SELECT marker FROM Таблица");
+ var lower = PostgresQueryAnalyzer.analyze("SELECT marker FROM таблица");
+
+ assertEquals("Таблица", upper.relations().getFirst().name());
+ assertEquals("таблица", lower.relations().getFirst().name());
+ }
+
+ @Test
+ @DisplayName("functions are captured in select, where, join, group, window and from")
+ void shouldCaptureFunctionsEverywhere() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT lower(a), rank() OVER (ORDER BY nullif(b, 0))"
+ + " FROM t JOIN generate_series(1, 10) g ON abs(t.x) = g"
+ + " WHERE coalesce(t.y, now()) IS NOT NULL"
+ + " GROUP BY lower(a), date_trunc('day', t.created_at)");
+
+ var names = analysis.functions().stream().map(FunctionReference::name).toList();
+ assertTrue(
+ names.containsAll(
+ List.of(
+ "lower",
+ "rank",
+ "nullif",
+ "generate_series",
+ "abs",
+ "coalesce",
+ "now",
+ "date_trunc")),
+ names.toString());
+ }
+
+ @Test
+ @DisplayName("schema-qualified function keeps schema and location")
+ void shouldCaptureSchemaQualifiedFunction() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT pg_catalog.now()");
+
+ var function = analysis.functions().getFirst();
+ assertEquals("pg_catalog", function.schema());
+ assertEquals("now", function.name());
+ assertEquals(1, function.line());
+ assertEquals(7, function.column());
+ }
+
+ @Test
+ @DisplayName("special-form functions are reported by keyword")
+ void shouldCaptureSpecialFormFunctions() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze("SELECT CAST(a AS int), EXTRACT(YEAR FROM b) FROM t");
+
+ var names = analysis.functions().stream().map(FunctionReference::name).toList();
+ assertEquals(List.of("cast", "extract"), names);
+ }
+
+ @Test
+ @DisplayName("insert reports target relation, columns and parameters")
+ void shouldAnalyzeInsert() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "INSERT INTO audit.events (kind, payload) VALUES (:kind, :payload)");
+
+ assertEquals(StatementKind.INSERT, analysis.statementKind());
+ assertEquals(
+ List.of(new RelationReference("audit", "events", null, RelationReference.Kind.PHYSICAL)),
+ analysis.relations());
+ assertEquals(
+ List.of(new ColumnReference(null, "kind"), new ColumnReference(null, "payload")),
+ analysis.columns());
+ assertEquals(Set.of("kind", "payload"), analysis.parameters());
+ }
+
+ @Test
+ @DisplayName("on conflict arbiter columns are reported")
+ void shouldReportOnConflictArbiterColumns() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "INSERT INTO t (a) VALUES (1) ON CONFLICT (tenant_id) DO NOTHING");
+
+ assertEquals(
+ List.of(new ColumnReference(null, "a"), new ColumnReference(null, "tenant_id")),
+ analysis.columns());
+ }
+
+ @Test
+ @DisplayName("update reports set targets and alias")
+ void shouldAnalyzeUpdate() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze("UPDATE users u SET name = :name WHERE u.id = :id");
+
+ assertEquals(StatementKind.UPDATE, analysis.statementKind());
+ assertEquals(
+ List.of(new RelationReference(null, "users", "u", RelationReference.Kind.PHYSICAL)),
+ analysis.relations());
+ assertEquals(
+ List.of(new ColumnReference(null, "name"), new ColumnReference("u", "id")),
+ analysis.columns());
+ }
+
+ @Test
+ @DisplayName("delete using reports both relations")
+ void shouldAnalyzeDeleteUsing() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "DELETE FROM sessions s USING users u WHERE s.user_id = u.id AND u.banned");
+
+ assertEquals(StatementKind.DELETE, analysis.statementKind());
+ assertEquals(
+ List.of(
+ new RelationReference(null, "sessions", "s", RelationReference.Kind.PHYSICAL),
+ new RelationReference(null, "users", "u", RelationReference.Kind.PHYSICAL)),
+ analysis.relations());
+ }
+
+ @Test
+ @DisplayName("alias without AS keyword is preserved")
+ void shouldCaptureBareAlias() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT * FROM users AS u");
+
+ assertEquals("u", analysis.relations().getFirst().alias());
+ }
+
+ @Test
+ @DisplayName("star projection reports star column reference")
+ void shouldReportStarColumn() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT *, t.* FROM t");
+
+ assertTrue(analysis.features().contains(QueryFeature.STAR_PROJECTION));
+ assertEquals(
+ List.of(new ColumnReference(null, "*"), new ColumnReference("t", "*")), analysis.columns());
+ assertTrue(analysis.columns().getFirst().star());
+ assertNull(analysis.columns().getFirst().qualifier());
+ }
+
+ @Test
+ @DisplayName("array subscript keeps column name without subscript")
+ void shouldHandleArraySubscript() {
+ var analysis = PostgresQueryAnalyzer.analyze("SELECT tags[1] FROM posts");
+
+ assertEquals(List.of(new ColumnReference(null, "tags")), analysis.columns());
+ }
+
+ @Test
+ @DisplayName("join using reports the join columns")
+ void shouldReportJoinUsingColumns() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze("SELECT a.id FROM a JOIN b USING (tenant_id, secret)");
+
+ assertEquals(
+ List.of(
+ new ColumnReference("a", "id"),
+ new ColumnReference(null, "tenant_id"),
+ new ColumnReference(null, "secret")),
+ analysis.columns());
+ }
+
+ @Test
+ @DisplayName("drop table and drop view report their target relations")
+ void shouldReportDroppedRelations() {
+ var dropTable = PostgresQueryAnalyzer.analyze("DROP TABLE private.users, audit");
+
+ assertEquals(StatementKind.DDL, dropTable.statementKind());
+ assertEquals(
+ List.of(
+ new RelationReference("private", "users", null, RelationReference.Kind.PHYSICAL),
+ new RelationReference(null, "audit", null, RelationReference.Kind.PHYSICAL)),
+ dropTable.relations());
+ assertEquals(
+ List.of(new RelationReference("private", "v", null, RelationReference.Kind.PHYSICAL)),
+ PostgresQueryAnalyzer.analyze("DROP VIEW IF EXISTS private.v").relations());
+ assertEquals(
+ List.of(new RelationReference(null, "m", null, RelationReference.Kind.PHYSICAL)),
+ PostgresQueryAnalyzer.analyze("DROP MATERIALIZED VIEW m").relations());
+ assertEquals(
+ List.of(new RelationReference(null, "f", null, RelationReference.Kind.PHYSICAL)),
+ PostgresQueryAnalyzer.analyze("DROP FOREIGN TABLE f").relations());
+ }
+
+ @Test
+ @DisplayName("drop of non-relation objects reports no relations")
+ void shouldNotReportNonRelationDrops() {
+ assertEquals(List.of(), PostgresQueryAnalyzer.analyze("DROP COLLATION c").relations());
+ assertEquals(List.of(), PostgresQueryAnalyzer.analyze("DROP INDEX idx").relations());
+ assertEquals(List.of(), PostgresQueryAnalyzer.analyze("DROP SCHEMA s").relations());
+ }
+
+ @Test
+ @DisplayName("ddl targeting a relation through an object name reports the relation")
+ void shouldReportAnyNameRelationTargets() {
+ var expected =
+ List.of(new RelationReference("private", "users", null, RelationReference.Kind.PHYSICAL));
+
+ assertEquals(
+ expected,
+ PostgresQueryAnalyzer.analyze("COMMENT ON TABLE private.users IS 'x'").relations());
+ assertEquals(
+ expected,
+ PostgresQueryAnalyzer.analyze("SECURITY LABEL ON TABLE private.users IS 'x'").relations());
+ assertEquals(
+ expected, PostgresQueryAnalyzer.analyze("DROP TRIGGER tr ON private.users").relations());
+ assertEquals(
+ expected,
+ PostgresQueryAnalyzer.analyze("DROP POLICY IF EXISTS p ON private.users").relations());
+ assertEquals(
+ expected, PostgresQueryAnalyzer.analyze("DROP RULE r ON private.users").relations());
+ assertEquals(
+ expected,
+ PostgresQueryAnalyzer.analyze("COMMENT ON CONSTRAINT c ON private.users IS 'x'")
+ .relations());
+ assertEquals(
+ expected,
+ PostgresQueryAnalyzer.analyze("COMMENT ON TRIGGER tr ON private.users IS 'x'").relations());
+ }
+
+ @Test
+ @DisplayName("comment and security label on a column report the relation and column")
+ void shouldReportColumnTargetRelations() {
+ var comment = PostgresQueryAnalyzer.analyze("COMMENT ON COLUMN private.users.email IS 'x'");
+
+ assertEquals(
+ List.of(new RelationReference("private", "users", null, RelationReference.Kind.PHYSICAL)),
+ comment.relations());
+ assertEquals(List.of(new ColumnReference("private.users", "email")), comment.columns());
+ assertEquals(
+ List.of(new RelationReference(null, "users", null, RelationReference.Kind.PHYSICAL)),
+ PostgresQueryAnalyzer.analyze("SECURITY LABEL ON COLUMN users.email IS 'x'").relations());
+ }
+
+ @Test
+ @DisplayName("comments on non-relation objects report no relations")
+ void shouldNotReportNonRelationAnyNameTargets() {
+ assertEquals(
+ List.of(), PostgresQueryAnalyzer.analyze("COMMENT ON SEQUENCE s IS 'x'").relations());
+ assertEquals(
+ List.of(),
+ PostgresQueryAnalyzer.analyze("COMMENT ON CONSTRAINT c ON DOMAIN d IS 'x'").relations());
+ assertEquals(
+ List.of(),
+ PostgresQueryAnalyzer.analyze("COMMENT ON OPERATOR CLASS oc USING btree IS 'x'")
+ .relations());
+ assertEquals(
+ List.of(),
+ PostgresQueryAnalyzer.analyze("SECURITY LABEL ON SEQUENCE s IS 'x'").relations());
+ }
+
+ @Test
+ @DisplayName("json aggregates are reported as functions and accept filter and over clauses")
+ void shouldReportJsonAggregates() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT JSON_OBJECTAGG(k : v) FILTER (WHERE v IS NOT NULL) OVER (PARTITION BY g),"
+ + " JSON_ARRAYAGG(v ORDER BY v) FROM t");
+
+ var names = analysis.functions().stream().map(FunctionReference::name).toList();
+ assertTrue(names.contains("json_objectagg"), names.toString());
+ assertTrue(names.contains("json_arrayagg"), names.toString());
+ assertTrue(analysis.features().contains(QueryFeature.WINDOW));
+ }
+
+ @Test
+ @DisplayName("search and cycle clauses parse and keep cte resolution intact")
+ void shouldAnalyzeSearchAndCycleClauses() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "WITH RECURSIVE tr AS (SELECT id, pid FROM edges UNION ALL"
+ + " SELECT e.id, e.pid FROM edges e JOIN tr ON e.pid = tr.id)"
+ + " SEARCH DEPTH FIRST BY id SET ord"
+ + " CYCLE id SET looped USING path"
+ + " SELECT * FROM tr WHERE weight > :w");
+
+ assertTrue(analysis.features().contains(QueryFeature.RECURSIVE_CTE));
+ assertEquals(Set.of("w"), analysis.parameters());
+ var kinds =
+ analysis.relations().stream()
+ .map(relation -> relation.name() + ":" + relation.kind())
+ .toList();
+ assertTrue(kinds.contains("edges:PHYSICAL"), kinds.toString());
+ assertTrue(kinds.contains("tr:CTE"), kinds.toString());
+ }
+
+ @Test
+ @DisplayName("merge returning and by source/target variants report their references")
+ void shouldAnalyzeModernMerge() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "MERGE INTO t USING s ON t.id = s.id"
+ + " WHEN NOT MATCHED BY SOURCE THEN UPDATE SET a = :a"
+ + " WHEN NOT MATCHED BY TARGET THEN INSERT VALUES (1)"
+ + " RETURNING t.id, s.id");
+
+ assertEquals(StatementKind.MERGE, analysis.statementKind());
+ assertEquals(Set.of("a"), analysis.parameters());
+ assertTrue(analysis.columns().contains(new ColumnReference("t", "id")));
+ assertTrue(analysis.columns().contains(new ColumnReference("s", "id")));
+ }
+
+ @Test
+ @DisplayName("xmltable and json_table are function relations with aliases")
+ void shouldReportTableFunctionRelations() {
+ var xml =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT * FROM XMLTABLE('/r' PASSING x COLUMNS c1 int PATH 'c1') AS xt");
+ assertTrue(
+ xml.relations()
+ .contains(
+ new RelationReference(null, "xmltable", "xt", RelationReference.Kind.FUNCTION)),
+ xml.relations().toString());
+ assertTrue(xml.features().contains(QueryFeature.FUNCTION_RELATION));
+
+ var json =
+ PostgresQueryAnalyzer.analyze(
+ "SELECT jt.* FROM JSON_TABLE(j, '$[*]' AS root COLUMNS (seq FOR ORDINALITY,"
+ + " id int PATH '$.id', has_kids boolean EXISTS PATH '$.kids',"
+ + " NESTED PATH '$.kids[*]' COLUMNS (kid text PATH '$.name'))) AS jt"
+ + " WHERE jt.id = :id");
+ assertTrue(
+ json.relations()
+ .contains(
+ new RelationReference(null, "json_table", "jt", RelationReference.Kind.FUNCTION)),
+ json.relations().toString());
+ assertTrue(json.features().contains(QueryFeature.FUNCTION_RELATION));
+ assertEquals(Set.of("id"), json.parameters());
+ }
+
+ @Test
+ @DisplayName("statements nested in a function body do not add to the statement count")
+ void shouldNotCountFunctionBodyStatements() {
+ var analysis =
+ PostgresQueryAnalyzer.analyze(
+ "CREATE PROCEDURE p() LANGUAGE SQL BEGIN ATOMIC"
+ + " DELETE FROM audit; INSERT INTO audit VALUES (1); END");
+
+ assertEquals(1, analysis.statementCount());
+ assertTrue(analysis.relations().stream().anyMatch(relation -> relation.name().equals("audit")));
+ }
+
+ @Test
+ @DisplayName("string constants separated by a newline concatenate as in postgresql")
+ void shouldAcceptNewlineConcatenatedStrings() {
+ assertEquals(1, PostgresQueryAnalyzer.analyze("SELECT 'a'\n'b'").statementCount());
+ assertEquals(
+ 1, PostgresQueryAnalyzer.analyze("SELECT 'a' -- note\n 'b'\n'c'").statementCount());
+ assertEquals(
+ 1, PostgresQueryAnalyzer.analyze("SELECT U&'d!0061t'\n'x' UESCAPE '!'").statementCount());
+ }
+
+ @Test
+ @DisplayName("string adjacency without a plain newline separation stays a syntax error")
+ void shouldRejectSameLineStringAdjacency() {
+ assertThrows(
+ QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze("SELECT 'a' 'b'"));
+ assertThrows(
+ QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze("SELECT 'a' /*\n*/ 'b'"));
+ assertThrows(
+ QueryAnalysisException.class, () -> PostgresQueryAnalyzer.analyze("SELECT $$a$$\n'b'"));
+ }
+}
diff --git a/src/test/java/ai/singlr/postgresql/StatementClassificationTest.java b/src/test/java/ai/singlr/postgresql/StatementClassificationTest.java
new file mode 100644
index 0000000..51b711a
--- /dev/null
+++ b/src/test/java/ai/singlr/postgresql/StatementClassificationTest.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+@DisplayName("Statement classification")
+class StatementClassificationTest {
+
+ @ParameterizedTest(name = "{1}: {0}")
+ @CsvSource(
+ delimiter = '|',
+ textBlock =
+ """
+ SELECT 1 | SELECT
+ TABLE users | SELECT
+ (SELECT 1) | SELECT
+ INSERT INTO t VALUES (1) | INSERT
+ UPDATE t SET a = 1 | UPDATE
+ DELETE FROM t | DELETE
+ MERGE INTO t USING s ON t.id = s.id WHEN MATCHED THEN UPDATE SET a = 1 WHEN NOT MATCHED THEN INSERT VALUES (1) | MERGE
+ MERGE INTO t USING s ON t.id = s.id WHEN MATCHED THEN DO NOTHING | MERGE
+ MERGE INTO t USING s ON t.id = s.id WHEN NOT MATCHED THEN DO NOTHING | MERGE
+ MERGE INTO t USING s ON t.id = s.id WHEN MATCHED AND t.a > 1 THEN UPDATE SET a = 2 WHEN MATCHED THEN DELETE WHEN NOT MATCHED THEN INSERT DEFAULT VALUES | MERGE
+ MERGE INTO t USING s ON t.id = s.id WHEN MATCHED THEN UPDATE SET a = 1 RETURNING t.id | MERGE
+ MERGE INTO t USING s ON t.id = s.id WHEN NOT MATCHED BY SOURCE THEN DELETE WHEN NOT MATCHED BY TARGET THEN INSERT VALUES (1) | MERGE
+ SELECT JSON_OBJECTAGG(k : v) FROM t | SELECT
+ SELECT JSON_ARRAYAGG(v ORDER BY v RETURNING jsonb) FROM t | SELECT
+ SELECT a, b FROM t GROUP BY DISTINCT ROLLUP (a), ROLLUP (b) | SELECT
+ SELECT x IS JSON OBJECT, y IS NOT JSON ARRAY, z IS JSON WITH UNIQUE KEYS FROM t | SELECT
+ WITH RECURSIVE tr AS (SELECT id FROM t2 UNION ALL SELECT t3.id FROM t3 JOIN tr ON t3.pid = tr.id) SEARCH BREADTH FIRST BY id SET ord SELECT * FROM tr | SELECT
+ WITH RECURSIVE tr AS (SELECT id FROM t2 UNION ALL SELECT t3.id FROM t3 JOIN tr ON t3.pid = tr.id) CYCLE id SET looped TO true DEFAULT false USING path SELECT * FROM tr | SELECT
+ SELECT JSON_OBJECT('a' : x FORMAT JSON RETURNING jsonb FORMAT JSON) FROM t | SELECT
+ SELECT breadth, breath, depth, path FROM t | SELECT
+ SELECT a, b FROM t GROUP BY ALL a, b | SELECT
+ MERGE INTO t USING s ON t.id = s.id WHEN NOT MATCHED BY SOURCE THEN DO NOTHING WHEN NOT MATCHED THEN DO NOTHING | MERGE
+ CREATE TABLE tt (r int4range, PRIMARY KEY (r WITHOUT OVERLAPS)) | DDL
+ CREATE TABLE tt (id int, r int4range, UNIQUE (id, r WITHOUT OVERLAPS)) | DDL
+ WITH src AS (SELECT 1 AS id) MERGE INTO t USING src ON t.id = src.id WHEN MATCHED THEN DO NOTHING | MERGE
+ CREATE TABLE t (id int) | DDL
+ CREATE INDEX idx ON t (id) | DDL
+ CREATE VIEW v AS SELECT 1 | DDL
+ CREATE MATERIALIZED VIEW mv AS SELECT 1 | DDL
+ CREATE ROLE reporting | DDL
+ CREATE FUNCTION f() RETURNS int AS 'select 1' LANGUAGE sql | DDL
+ CREATE FUNCTION f(i int) RETURNS int LANGUAGE SQL RETURN i + 1 | DDL
+ CREATE FUNCTION f(i int) RETURNS int RETURN i + 1 | DDL
+ CREATE PROCEDURE p() LANGUAGE SQL BEGIN ATOMIC INSERT INTO audit VALUES (1); END | DDL
+ REASSIGN OWNED BY old_role TO new_role | DDL
+ ALTER TABLE t ADD COLUMN b int | DDL
+ ALTER TABLE t RENAME TO t2 | DDL
+ DROP TABLE t | DDL
+ DROP INDEX idx | DDL
+ GRANT SELECT ON t TO reporting | DDL
+ REVOKE SELECT ON t FROM reporting | DDL
+ COMMENT ON TABLE t IS 'x' | DDL
+ COPY t FROM STDIN | UTILITY
+ COPY (SELECT * FROM t) TO STDOUT | UTILITY
+ CALL do_things(1) | UTILITY
+ DO 'begin end' | UTILITY
+ SET search_path = public | UTILITY
+ SET LOCAL statement_timeout = 100 | UTILITY
+ RESET search_path | UTILITY
+ SHOW server_version | UTILITY
+ BEGIN | UTILITY
+ COMMIT | UTILITY
+ ROLLBACK | UTILITY
+ SAVEPOINT sp | UTILITY
+ TRUNCATE t | UTILITY
+ LOCK TABLE t | UTILITY
+ EXPLAIN SELECT 1 | UTILITY
+ VACUUM t | UTILITY
+ ANALYZE t | UTILITY
+ PREPARE p AS SELECT 1 | UTILITY
+ EXECUTE p | UTILITY
+ DEALLOCATE p | UTILITY
+ LISTEN chan | UTILITY
+ NOTIFY chan | UTILITY
+ CHECKPOINT | UTILITY
+ """)
+ void shouldClassify(String sql, StatementKind expected) {
+ assertEquals(expected, PostgresQueryAnalyzer.analyze(sql).statementKind());
+ }
+
+ @ParameterizedTest(name = "relations survive in {1}: {0}")
+ @CsvSource(
+ delimiter = '|',
+ textBlock =
+ """
+ EXPLAIN SELECT * FROM users | users
+ CREATE VIEW v AS SELECT * FROM users | users
+ CREATE TABLE copy_t AS SELECT * FROM users | users
+ CREATE FUNCTION f() RETURNS int LANGUAGE SQL RETURN (SELECT count(*) FROM users) | users
+ CREATE PROCEDURE p() LANGUAGE SQL BEGIN ATOMIC DELETE FROM users; END | users
+ """)
+ void shouldCaptureRelationsInWrappedStatements(String sql, String relation) {
+ var names =
+ PostgresQueryAnalyzer.analyze(sql).relations().stream()
+ .map(RelationReference::name)
+ .toList();
+ org.junit.jupiter.api.Assertions.assertTrue(names.contains(relation), names.toString());
+ }
+}
diff --git a/src/test/java/ai/singlr/postgresql/UpstreamCorpusTest.java b/src/test/java/ai/singlr/postgresql/UpstreamCorpusTest.java
new file mode 100644
index 0000000..ef8a46a
--- /dev/null
+++ b/src/test/java/ai/singlr/postgresql/UpstreamCorpusTest.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2026 Singular
+ * SPDX-License-Identifier: MIT
+ */
+
+package ai.singlr.postgresql;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+/**
+ * PostgreSQL regression examples vendored from ANTLR grammars-v4 at commit
+ * 76093c04af6a51f38a67d14f7e71ff0a9b4400da (sql/postgresql/examples). Each file must analyze end to
+ * end; failures indicate a regression in the vendored grammar or the analyzer.
+ *
+ * Local modification: psql meta-command lines ({@code \d+}, {@code \set}) were removed and
+ * client-side {@code \;} separators replaced with plain {@code ;}, because the analyzer
+ * deliberately rejects psql meta-commands as non-SQL.
+ */
+@DisplayName("Upstream regression corpus")
+class UpstreamCorpusTest {
+
+ @ParameterizedTest
+ @ValueSource(
+ strings = {
+ "select.sql",
+ "join.sql",
+ "aggregates.sql",
+ "union.sql",
+ "with.sql",
+ "window.sql",
+ "insert.sql",
+ "update.sql",
+ "delete.sql",
+ "case.sql",
+ "subselect.sql",
+ "limit.sql",
+ "transactions.sql",
+ "select_having.sql",
+ "select_distinct.sql"
+ })
+ @DisplayName("corpus file analyzes end to end")
+ void shouldAnalyzeCorpusFile(String file) {
+ var analysis = PostgresQueryAnalyzer.analyze(read(file));
+
+ assertNotNull(analysis.normalizedSql());
+ assertTrue(analysis.statementCount() > 0, file);
+ }
+
+ private static String read(String file) {
+ try (var in = UpstreamCorpusTest.class.getResourceAsStream("/postgresql-corpus/" + file)) {
+ assertNotNull(in, file + " missing from corpus");
+ return new String(in.readAllBytes(), StandardCharsets.UTF_8);
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+}
diff --git a/src/test/resources/postgresql-corpus/aggregates.sql b/src/test/resources/postgresql-corpus/aggregates.sql
new file mode 100644
index 0000000..9910287
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/aggregates.sql
@@ -0,0 +1,1218 @@
+--
+-- AGGREGATES
+--
+
+-- avoid bit-exact output here because operations may not be bit-exact.
+SET extra_float_digits = 0;
+
+SELECT avg(four) AS avg_1 FROM onek;
+
+SELECT avg(a) AS avg_32 FROM aggtest WHERE a < 100;
+
+-- In 7.1, avg(float4) is computed using float8 arithmetic.
+-- Round the result to 3 digits to avoid platform-specific results.
+
+SELECT avg(b)::numeric(10,3) AS avg_107_943 FROM aggtest;
+
+SELECT avg(gpa) AS avg_3_4 FROM ONLY student;
+
+
+SELECT sum(four) AS sum_1500 FROM onek;
+SELECT sum(a) AS sum_198 FROM aggtest;
+SELECT sum(b) AS avg_431_773 FROM aggtest;
+SELECT sum(gpa) AS avg_6_8 FROM ONLY student;
+
+SELECT max(four) AS max_3 FROM onek;
+SELECT max(a) AS max_100 FROM aggtest;
+SELECT max(aggtest.b) AS max_324_78 FROM aggtest;
+SELECT max(student.gpa) AS max_3_7 FROM student;
+
+SELECT stddev_pop(b) FROM aggtest;
+SELECT stddev_samp(b) FROM aggtest;
+SELECT var_pop(b) FROM aggtest;
+SELECT var_samp(b) FROM aggtest;
+
+SELECT stddev_pop(b::numeric) FROM aggtest;
+SELECT stddev_samp(b::numeric) FROM aggtest;
+SELECT var_pop(b::numeric) FROM aggtest;
+SELECT var_samp(b::numeric) FROM aggtest;
+
+-- population variance is defined for a single tuple, sample variance
+-- is not
+SELECT var_pop(1.0::float8), var_samp(2.0::float8);
+SELECT stddev_pop(3.0::float8), stddev_samp(4.0::float8);
+SELECT var_pop('inf'::float8), var_samp('inf'::float8);
+SELECT stddev_pop('inf'::float8), stddev_samp('inf'::float8);
+SELECT var_pop('nan'::float8), var_samp('nan'::float8);
+SELECT stddev_pop('nan'::float8), stddev_samp('nan'::float8);
+SELECT var_pop(1.0::float4), var_samp(2.0::float4);
+SELECT stddev_pop(3.0::float4), stddev_samp(4.0::float4);
+SELECT var_pop('inf'::float4), var_samp('inf'::float4);
+SELECT stddev_pop('inf'::float4), stddev_samp('inf'::float4);
+SELECT var_pop('nan'::float4), var_samp('nan'::float4);
+SELECT stddev_pop('nan'::float4), stddev_samp('nan'::float4);
+SELECT var_pop(1.0::numeric), var_samp(2.0::numeric);
+SELECT stddev_pop(3.0::numeric), stddev_samp(4.0::numeric);
+SELECT var_pop('inf'::numeric), var_samp('inf'::numeric);
+SELECT stddev_pop('inf'::numeric), stddev_samp('inf'::numeric);
+SELECT var_pop('nan'::numeric), var_samp('nan'::numeric);
+SELECT stddev_pop('nan'::numeric), stddev_samp('nan'::numeric);
+
+-- verify correct results for null and NaN inputs
+select sum(null::int4) from generate_series(1,3);
+select sum(null::int8) from generate_series(1,3);
+select sum(null::numeric) from generate_series(1,3);
+select sum(null::float8) from generate_series(1,3);
+select avg(null::int4) from generate_series(1,3);
+select avg(null::int8) from generate_series(1,3);
+select avg(null::numeric) from generate_series(1,3);
+select avg(null::float8) from generate_series(1,3);
+select sum('NaN'::numeric) from generate_series(1,3);
+select avg('NaN'::numeric) from generate_series(1,3);
+
+-- verify correct results for infinite inputs
+SELECT sum(x::float8), avg(x::float8), var_pop(x::float8)
+FROM (VALUES ('1'), ('infinity')) v(x);
+SELECT sum(x::float8), avg(x::float8), var_pop(x::float8)
+FROM (VALUES ('infinity'), ('1')) v(x);
+SELECT sum(x::float8), avg(x::float8), var_pop(x::float8)
+FROM (VALUES ('infinity'), ('infinity')) v(x);
+SELECT sum(x::float8), avg(x::float8), var_pop(x::float8)
+FROM (VALUES ('-infinity'), ('infinity')) v(x);
+SELECT sum(x::float8), avg(x::float8), var_pop(x::float8)
+FROM (VALUES ('-infinity'), ('-infinity')) v(x);
+SELECT sum(x::numeric), avg(x::numeric), var_pop(x::numeric)
+FROM (VALUES ('1'), ('infinity')) v(x);
+SELECT sum(x::numeric), avg(x::numeric), var_pop(x::numeric)
+FROM (VALUES ('infinity'), ('1')) v(x);
+SELECT sum(x::numeric), avg(x::numeric), var_pop(x::numeric)
+FROM (VALUES ('infinity'), ('infinity')) v(x);
+SELECT sum(x::numeric), avg(x::numeric), var_pop(x::numeric)
+FROM (VALUES ('-infinity'), ('infinity')) v(x);
+SELECT sum(x::numeric), avg(x::numeric), var_pop(x::numeric)
+FROM (VALUES ('-infinity'), ('-infinity')) v(x);
+
+-- test accuracy with a large input offset
+SELECT avg(x::float8), var_pop(x::float8)
+FROM (VALUES (100000003), (100000004), (100000006), (100000007)) v(x);
+SELECT avg(x::float8), var_pop(x::float8)
+FROM (VALUES (7000000000005), (7000000000007)) v(x);
+
+-- SQL2003 binary aggregates
+SELECT regr_count(b, a) FROM aggtest;
+SELECT regr_sxx(b, a) FROM aggtest;
+SELECT regr_syy(b, a) FROM aggtest;
+SELECT regr_sxy(b, a) FROM aggtest;
+SELECT regr_avgx(b, a), regr_avgy(b, a) FROM aggtest;
+SELECT regr_r2(b, a) FROM aggtest;
+SELECT regr_slope(b, a), regr_intercept(b, a) FROM aggtest;
+SELECT covar_pop(b, a), covar_samp(b, a) FROM aggtest;
+SELECT corr(b, a) FROM aggtest;
+
+-- check single-tuple behavior
+SELECT covar_pop(1::float8,2::float8), covar_samp(3::float8,4::float8);
+SELECT covar_pop(1::float8,'inf'::float8), covar_samp(3::float8,'inf'::float8);
+SELECT covar_pop(1::float8,'nan'::float8), covar_samp(3::float8,'nan'::float8);
+
+-- test accum and combine functions directly
+CREATE TABLE regr_test (x float8, y float8);
+INSERT INTO regr_test VALUES (10,150),(20,250),(30,350),(80,540),(100,200);
+SELECT count(*), sum(x), regr_sxx(y,x), sum(y),regr_syy(y,x), regr_sxy(y,x)
+FROM regr_test WHERE x IN (10,20,30,80);
+SELECT count(*), sum(x), regr_sxx(y,x), sum(y),regr_syy(y,x), regr_sxy(y,x)
+FROM regr_test;
+SELECT float8_accum('{4,140,2900}'::float8[], 100);
+SELECT float8_regr_accum('{4,140,2900,1290,83075,15050}'::float8[], 200, 100);
+SELECT count(*), sum(x), regr_sxx(y,x), sum(y),regr_syy(y,x), regr_sxy(y,x)
+FROM regr_test WHERE x IN (10,20,30);
+SELECT count(*), sum(x), regr_sxx(y,x), sum(y),regr_syy(y,x), regr_sxy(y,x)
+FROM regr_test WHERE x IN (80,100);
+SELECT float8_combine('{3,60,200}'::float8[], '{0,0,0}'::float8[]);
+SELECT float8_combine('{0,0,0}'::float8[], '{2,180,200}'::float8[]);
+SELECT float8_combine('{3,60,200}'::float8[], '{2,180,200}'::float8[]);
+SELECT float8_regr_combine('{3,60,200,750,20000,2000}'::float8[],
+ '{0,0,0,0,0,0}'::float8[]);
+SELECT float8_regr_combine('{0,0,0,0,0,0}'::float8[],
+ '{2,180,200,740,57800,-3400}'::float8[]);
+SELECT float8_regr_combine('{3,60,200,750,20000,2000}'::float8[],
+ '{2,180,200,740,57800,-3400}'::float8[]);
+DROP TABLE regr_test;
+
+-- test count, distinct
+SELECT count(four) AS cnt_1000 FROM onek;
+SELECT count(DISTINCT four) AS cnt_4 FROM onek;
+
+select ten, count(*), sum(four) from onek
+group by ten order by ten;
+
+select ten, count(four), sum(DISTINCT four) from onek
+group by ten order by ten;
+
+-- user-defined aggregates
+SELECT newavg(four) AS avg_1 FROM onek;
+SELECT newsum(four) AS sum_1500 FROM onek;
+SELECT newcnt(four) AS cnt_1000 FROM onek;
+SELECT newcnt(*) AS cnt_1000 FROM onek;
+SELECT oldcnt(*) AS cnt_1000 FROM onek;
+SELECT sum2(q1,q2) FROM int8_tbl;
+
+-- test for outer-level aggregates
+
+-- this should work
+select ten, sum(distinct four) from onek a
+group by ten
+having exists (select 1 from onek b where sum(distinct a.four) = b.four);
+
+-- this should fail because subquery has an agg of its own in WHERE
+select ten, sum(distinct four) from onek a
+group by ten
+having exists (select 1 from onek b
+ where sum(distinct a.four + b.four) = b.four);
+
+-- Test handling of sublinks within outer-level aggregates.
+-- Per bug report from Daniel Grace.
+select
+ (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1)))
+from tenk1 o;
+
+-- Test handling of Params within aggregate arguments in hashed aggregation.
+-- Per bug report from Jeevan Chalke.
+explain (verbose, costs off)
+select s1, s2, sm
+from generate_series(1, 3) s1,
+ lateral (select s2, sum(s1 + s2) sm
+ from generate_series(1, 3) s2 group by s2) ss
+order by 1, 2;
+select s1, s2, sm
+from generate_series(1, 3) s1,
+ lateral (select s2, sum(s1 + s2) sm
+ from generate_series(1, 3) s2 group by s2) ss
+order by 1, 2;
+
+explain (verbose, costs off)
+select array(select sum(x+y) s
+ from generate_series(1,3) y group by y order by s)
+ from generate_series(1,3) x;
+select array(select sum(x+y) s
+ from generate_series(1,3) y group by y order by s)
+ from generate_series(1,3) x;
+
+--
+-- test for bitwise integer aggregates
+--
+CREATE TEMPORARY TABLE bitwise_test(
+ i2 INT2,
+ i4 INT4,
+ i8 INT8,
+ i INTEGER,
+ x INT2,
+ y BIT(4)
+);
+
+-- empty case
+SELECT
+ BIT_AND(i2) AS aaa,
+ BIT_OR(i4) AS baaa
+FROM bitwise_test;
+
+COPY bitwise_test FROM STDIN NULL 'null';
+
+SELECT
+ BIT_AND(i2) AS "1",
+ BIT_AND(i4) AS "1",
+ BIT_AND(i8) AS "1",
+ BIT_AND(i) AS "?",
+ BIT_AND(x) AS "0",
+ BIT_AND(y) AS "0100",
+
+ BIT_OR(i2) AS "7",
+ BIT_OR(i4) AS "7",
+ BIT_OR(i8) AS "7",
+ BIT_OR(i) AS "?",
+ BIT_OR(x) AS "7",
+ BIT_OR(y) AS "1101"
+FROM bitwise_test;
+
+--
+-- test boolean aggregates
+--
+-- first test all possible transition and final states
+
+SELECT
+ -- boolean and transitions
+ -- null because strict
+ booland_statefunc(NULL, NULL) IS NULL AS "t",
+ booland_statefunc(TRUE, NULL) IS NULL AS "t",
+ booland_statefunc(FALSE, NULL) IS NULL AS "t",
+ booland_statefunc(NULL, TRUE) IS NULL AS "t",
+ booland_statefunc(NULL, FALSE) IS NULL AS "t",
+ -- and actual computations
+ booland_statefunc(TRUE, TRUE) AS "t",
+ NOT booland_statefunc(TRUE, FALSE) AS "t",
+ NOT booland_statefunc(FALSE, TRUE) AS "t",
+ NOT booland_statefunc(FALSE, FALSE) AS "t";
+
+SELECT
+ -- boolean or transitions
+ -- null because strict
+ boolor_statefunc(NULL, NULL) IS NULL AS "t",
+ boolor_statefunc(TRUE, NULL) IS NULL AS "t",
+ boolor_statefunc(FALSE, NULL) IS NULL AS "t",
+ boolor_statefunc(NULL, TRUE) IS NULL AS "t",
+ boolor_statefunc(NULL, FALSE) IS NULL AS "t",
+ -- actual computations
+ boolor_statefunc(TRUE, TRUE) AS "t",
+ boolor_statefunc(TRUE, FALSE) AS "t",
+ boolor_statefunc(FALSE, TRUE) AS "t",
+ NOT boolor_statefunc(FALSE, FALSE) AS "t";
+
+CREATE TEMPORARY TABLE bool_test(
+ b1 BOOL,
+ b2 BOOL,
+ b3 BOOL,
+ b4 BOOL);
+
+-- empty case
+SELECT
+ BOOL_AND(b1) AS "n",
+ BOOL_OR(b3) AS "n"
+FROM bool_test;
+
+COPY bool_test FROM STDIN NULL 'null';
+
+SELECT
+ BOOL_AND(b1) AS "f",
+ BOOL_AND(b2) AS "t",
+ BOOL_AND(b3) AS "f",
+ BOOL_AND(b4) AS "n",
+ BOOL_AND(NOT b2) AS "f",
+ BOOL_AND(NOT b3) AS "t"
+FROM bool_test;
+
+SELECT
+ EVERY(b1) AS "f",
+ EVERY(b2) AS "t",
+ EVERY(b3) AS "f",
+ EVERY(b4) AS "n",
+ EVERY(NOT b2) AS "f",
+ EVERY(NOT b3) AS "t"
+FROM bool_test;
+
+SELECT
+ BOOL_OR(b1) AS "t",
+ BOOL_OR(b2) AS "t",
+ BOOL_OR(b3) AS "f",
+ BOOL_OR(b4) AS "n",
+ BOOL_OR(NOT b2) AS "f",
+ BOOL_OR(NOT b3) AS "t"
+FROM bool_test;
+
+--
+-- Test cases that should be optimized into indexscans instead of
+-- the generic aggregate implementation.
+--
+
+-- Basic cases
+explain (costs off)
+ select min(unique1) from tenk1;
+select min(unique1) from tenk1;
+explain (costs off)
+ select max(unique1) from tenk1;
+select max(unique1) from tenk1;
+explain (costs off)
+ select max(unique1) from tenk1 where unique1 < 42;
+select max(unique1) from tenk1 where unique1 < 42;
+explain (costs off)
+ select max(unique1) from tenk1 where unique1 > 42;
+select max(unique1) from tenk1 where unique1 > 42;
+
+-- the planner may choose a generic aggregate here if parallel query is
+-- enabled, since that plan will be parallel safe and the "optimized"
+-- plan, which has almost identical cost, will not be. we want to test
+-- the optimized plan, so temporarily disable parallel query.
+begin;
+set local max_parallel_workers_per_gather = 0;
+explain (costs off)
+ select max(unique1) from tenk1 where unique1 > 42000;
+select max(unique1) from tenk1 where unique1 > 42000;
+rollback;
+
+-- multi-column index (uses tenk1_thous_tenthous)
+explain (costs off)
+ select max(tenthous) from tenk1 where thousand = 33;
+select max(tenthous) from tenk1 where thousand = 33;
+explain (costs off)
+ select min(tenthous) from tenk1 where thousand = 33;
+select min(tenthous) from tenk1 where thousand = 33;
+
+-- check parameter propagation into an indexscan subquery
+explain (costs off)
+ select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
+ from int4_tbl;
+select f1, (select min(unique1) from tenk1 where unique1 > f1) AS gt
+ from int4_tbl;
+
+-- check some cases that were handled incorrectly in 8.3.0
+explain (costs off)
+ select distinct max(unique2) from tenk1;
+select distinct max(unique2) from tenk1;
+explain (costs off)
+ select max(unique2) from tenk1 order by 1;
+select max(unique2) from tenk1 order by 1;
+explain (costs off)
+ select max(unique2) from tenk1 order by max(unique2);
+select max(unique2) from tenk1 order by max(unique2);
+explain (costs off)
+ select max(unique2) from tenk1 order by max(unique2)+1;
+select max(unique2) from tenk1 order by max(unique2)+1;
+explain (costs off)
+ select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
+select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
+
+-- interesting corner case: constant gets optimized into a seqscan
+explain (costs off)
+ select max(100) from tenk1;
+select max(100) from tenk1;
+
+-- try it on an inheritance tree
+create table minmaxtest(f1 int);
+create table minmaxtest1() inherits (minmaxtest);
+create table minmaxtest2() inherits (minmaxtest);
+create table minmaxtest3() inherits (minmaxtest);
+create index minmaxtesti on minmaxtest(f1);
+create index minmaxtest1i on minmaxtest1(f1);
+create index minmaxtest2i on minmaxtest2(f1 desc);
+create index minmaxtest3i on minmaxtest3(f1) where f1 is not null;
+
+insert into minmaxtest values(11), (12);
+insert into minmaxtest1 values(13), (14);
+insert into minmaxtest2 values(15), (16);
+insert into minmaxtest3 values(17), (18);
+
+explain (costs off)
+ select min(f1), max(f1) from minmaxtest;
+select min(f1), max(f1) from minmaxtest;
+
+-- DISTINCT doesn't do anything useful here, but it shouldn't fail
+explain (costs off)
+ select distinct min(f1), max(f1) from minmaxtest;
+select distinct min(f1), max(f1) from minmaxtest;
+
+drop table minmaxtest cascade;
+
+-- check for correct detection of nested-aggregate errors
+select max(min(unique1)) from tenk1;
+select (select max(min(unique1)) from int8_tbl) from tenk1;
+
+--
+-- Test removal of redundant GROUP BY columns
+--
+
+create temp table t1 (a int, b int, c int, d int, primary key (a, b));
+create temp table t2 (x int, y int, z int, primary key (x, y));
+create temp table t3 (a int, b int, c int, primary key(a, b) deferrable);
+
+-- Non-primary-key columns can be removed from GROUP BY
+explain (costs off) select * from t1 group by a,b,c,d;
+
+-- No removal can happen if the complete PK is not present in GROUP BY
+explain (costs off) select a,c from t1 group by a,c,d;
+
+-- Test removal across multiple relations
+explain (costs off) select *
+from t1 inner join t2 on t1.a = t2.x and t1.b = t2.y
+group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.y,t2.z;
+
+-- Test case where t1 can be optimized but not t2
+explain (costs off) select t1.*,t2.x,t2.z
+from t1 inner join t2 on t1.a = t2.x and t1.b = t2.y
+group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.z;
+
+-- Cannot optimize when PK is deferrable
+explain (costs off) select * from t3 group by a,b,c;
+
+create temp table t1c () inherits (t1);
+
+-- Ensure we don't remove any columns when t1 has a child table
+explain (costs off) select * from t1 group by a,b,c,d;
+
+-- Okay to remove columns if we're only querying the parent.
+explain (costs off) select * from only t1 group by a,b,c,d;
+
+create temp table p_t1 (
+ a int,
+ b int,
+ c int,
+ d int,
+ primary key(a,b)
+) partition by list(a);
+create temp table p_t1_1 partition of p_t1 for values in(1);
+create temp table p_t1_2 partition of p_t1 for values in(2);
+
+-- Ensure we can remove non-PK columns for partitioned tables.
+explain (costs off) select * from p_t1 group by a,b,c,d;
+
+drop table t1 cascade;
+drop table t2;
+drop table t3;
+drop table p_t1;
+
+--
+-- Test GROUP BY matching of join columns that are type-coerced due to USING
+--
+
+create temp table t1(f1 int, f2 bigint);
+create temp table t2(f1 bigint, f22 bigint);
+
+select f1 from t1 left join t2 using (f1) group by f1;
+select f1 from t1 left join t2 using (f1) group by t1.f1;
+select t1.f1 from t1 left join t2 using (f1) group by t1.f1;
+-- only this one should fail:
+select t1.f1 from t1 left join t2 using (f1) group by f1;
+
+drop table t1, t2;
+
+--
+-- Test combinations of DISTINCT and/or ORDER BY
+--
+
+select array_agg(a order by b)
+ from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
+select array_agg(a order by a)
+ from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
+select array_agg(a order by a desc)
+ from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
+select array_agg(b order by a desc)
+ from (values (1,4),(2,3),(3,1),(4,2)) v(a,b);
+
+select array_agg(distinct a)
+ from (values (1),(2),(1),(3),(null),(2)) v(a);
+select array_agg(distinct a order by a)
+ from (values (1),(2),(1),(3),(null),(2)) v(a);
+select array_agg(distinct a order by a desc)
+ from (values (1),(2),(1),(3),(null),(2)) v(a);
+select array_agg(distinct a order by a desc nulls last)
+ from (values (1),(2),(1),(3),(null),(2)) v(a);
+
+-- multi-arg aggs, strict/nonstrict, distinct/order by
+
+select aggfstr(a,b,c)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
+select aggfns(a,b,c)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
+
+select aggfstr(distinct a,b,c)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,3) i;
+select aggfns(distinct a,b,c)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,3) i;
+
+select aggfstr(distinct a,b,c order by b)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,3) i;
+select aggfns(distinct a,b,c order by b)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,3) i;
+
+-- test specific code paths
+
+select aggfns(distinct a,a,c order by c using ~<~,a)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,2) i;
+select aggfns(distinct a,a,c order by c using ~<~)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,2) i;
+select aggfns(distinct a,a,c order by a)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,2) i;
+select aggfns(distinct a,b,c order by a,c using ~<~,b)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,2) i;
+
+-- check node I/O via view creation and usage, also deparsing logic
+
+create view agg_view1 as
+ select aggfns(a,b,c)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
+
+select * from agg_view1;
+select pg_get_viewdef('agg_view1'::regclass);
+
+create or replace view agg_view1 as
+ select aggfns(distinct a,b,c)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,3) i;
+
+select * from agg_view1;
+select pg_get_viewdef('agg_view1'::regclass);
+
+create or replace view agg_view1 as
+ select aggfns(distinct a,b,c order by b)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,3) i;
+
+select * from agg_view1;
+select pg_get_viewdef('agg_view1'::regclass);
+
+create or replace view agg_view1 as
+ select aggfns(a,b,c order by b+1)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
+
+select * from agg_view1;
+select pg_get_viewdef('agg_view1'::regclass);
+
+create or replace view agg_view1 as
+ select aggfns(a,a,c order by b)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
+
+select * from agg_view1;
+select pg_get_viewdef('agg_view1'::regclass);
+
+create or replace view agg_view1 as
+ select aggfns(a,b,c order by c using ~<~)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c);
+
+select * from agg_view1;
+select pg_get_viewdef('agg_view1'::regclass);
+
+create or replace view agg_view1 as
+ select aggfns(distinct a,b,c order by a,c using ~<~,b)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,2) i;
+
+select * from agg_view1;
+select pg_get_viewdef('agg_view1'::regclass);
+
+drop view agg_view1;
+
+-- incorrect DISTINCT usage errors
+
+select aggfns(distinct a,b,c order by i)
+ from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
+select aggfns(distinct a,b,c order by a,b+1)
+ from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
+select aggfns(distinct a,b,c order by a,b,i,c)
+ from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
+select aggfns(distinct a,a,c order by a,b)
+ from (values (1,1,'foo')) v(a,b,c), generate_series(1,2) i;
+
+-- string_agg tests
+select string_agg(a,',') from (values('aaaa'),('bbbb'),('cccc')) g(a);
+select string_agg(a,',') from (values('aaaa'),(null),('bbbb'),('cccc')) g(a);
+select string_agg(a,'AB') from (values(null),(null),('bbbb'),('cccc')) g(a);
+select string_agg(a,',') from (values(null),(null)) g(a);
+
+-- check some implicit casting cases, as per bug #5564
+select string_agg(distinct f1, ',' order by f1) from varchar_tbl; -- ok
+select string_agg(distinct f1::text, ',' order by f1) from varchar_tbl; -- not ok
+select string_agg(distinct f1, ',' order by f1::text) from varchar_tbl; -- not ok
+select string_agg(distinct f1::text, ',' order by f1::text) from varchar_tbl; -- ok
+
+-- string_agg bytea tests
+create table bytea_test_table(v bytea);
+
+select string_agg(v, '') from bytea_test_table;
+
+insert into bytea_test_table values(decode('ff','hex'));
+
+select string_agg(v, '') from bytea_test_table;
+
+insert into bytea_test_table values(decode('aa','hex'));
+
+select string_agg(v, '') from bytea_test_table;
+select string_agg(v, NULL) from bytea_test_table;
+select string_agg(v, decode('ee', 'hex')) from bytea_test_table;
+
+drop table bytea_test_table;
+
+-- FILTER tests
+
+select min(unique1) filter (where unique1 > 100) from tenk1;
+
+select sum(1/ten) filter (where ten > 0) from tenk1;
+
+--select ten, sum(distinct four) filter (where four::text ~ '123') from onek a
+--group by ten;
+
+select ten, sum(distinct four) filter (where four > 10) from onek a
+group by ten
+having exists (select 1 from onek b where sum(distinct a.four) = b.four);
+
+select max(foo COLLATE "C") filter (where (bar collate "POSIX") > '0')
+from (values ('a', 'b')) AS v(foo,bar);
+
+-- outer reference in FILTER (PostgreSQL extension)
+select (select count(*)
+ from (values (1)) t0(inner_c))
+from (values (2),(3)) t1(outer_c); -- inner query is aggregation query
+select (select count(*) filter (where outer_c <> 0)
+ from (values (1)) t0(inner_c))
+from (values (2),(3)) t1(outer_c); -- outer query is aggregation query
+select (select count(inner_c) filter (where outer_c <> 0)
+ from (values (1)) t0(inner_c))
+from (values (2),(3)) t1(outer_c); -- inner query is aggregation query
+select
+ (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1))
+ filter (where o.unique1 < 10))
+from tenk1 o; -- outer query is aggregation query
+
+-- subquery in FILTER clause (PostgreSQL extension)
+select sum(unique1) FILTER (WHERE
+ unique1 IN (SELECT unique1 FROM onek where unique1 < 100)) FROM tenk1;
+
+-- exercise lots of aggregate parts with FILTER
+select aggfns(distinct a,b,c order by a,c using ~<~,b) filter (where a > 1)
+ from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c),
+ generate_series(1,2) i;
+
+-- ordered-set aggregates
+
+select p, percentile_cont(p) within group (order by x::float8)
+from generate_series(1,5) x,
+ (values (0::float8),(0.1),(0.25),(0.4),(0.5),(0.6),(0.75),(0.9),(1)) v(p)
+group by p order by p;
+
+select p, percentile_cont(p order by p) within group (order by x) -- error
+from generate_series(1,5) x,
+ (values (0::float8),(0.1),(0.25),(0.4),(0.5),(0.6),(0.75),(0.9),(1)) v(p)
+group by p order by p;
+
+select p, sum() within group (order by x::float8) -- error
+from generate_series(1,5) x,
+ (values (0::float8),(0.1),(0.25),(0.4),(0.5),(0.6),(0.75),(0.9),(1)) v(p)
+group by p order by p;
+
+select p, percentile_cont(p,p) -- error
+from generate_series(1,5) x,
+ (values (0::float8),(0.1),(0.25),(0.4),(0.5),(0.6),(0.75),(0.9),(1)) v(p)
+group by p order by p;
+
+select percentile_cont(0.5) within group (order by b) from aggtest;
+select percentile_cont(0.5) within group (order by b), sum(b) from aggtest;
+select percentile_cont(0.5) within group (order by thousand) from tenk1;
+select percentile_disc(0.5) within group (order by thousand) from tenk1;
+select rank(3) within group (order by x)
+from (values (1),(1),(2),(2),(3),(3),(4)) v(x);
+select cume_dist(3) within group (order by x)
+from (values (1),(1),(2),(2),(3),(3),(4)) v(x);
+select percent_rank(3) within group (order by x)
+from (values (1),(1),(2),(2),(3),(3),(4),(5)) v(x);
+select dense_rank(3) within group (order by x)
+from (values (1),(1),(2),(2),(3),(3),(4)) v(x);
+
+select percentile_disc(array[0,0.1,0.25,0.5,0.75,0.9,1]) within group (order by thousand)
+from tenk1;
+select percentile_cont(array[0,0.25,0.5,0.75,1]) within group (order by thousand)
+from tenk1;
+select percentile_disc(array[[null,1,0.5],[0.75,0.25,null]]) within group (order by thousand)
+from tenk1;
+select percentile_cont(array[0,1,0.25,0.75,0.5,1,0.3,0.32,0.35,0.38,0.4]) within group (order by x)
+from generate_series(1,6) x;
+
+select ten, mode() within group (order by string4) from tenk1 group by ten;
+
+select percentile_disc(array[0.25,0.5,0.75]) within group (order by x)
+from unnest('{fred,jim,fred,jack,jill,fred,jill,jim,jim,sheila,jim,sheila}'::text[]) u(x);
+
+-- check collation propagates up in suitable cases:
+select pg_collation_for(percentile_disc(1) within group (order by x collate "POSIX"))
+ from (values ('fred'),('jim')) v(x);
+
+-- ordered-set aggs created with CREATE AGGREGATE
+select test_rank(3) within group (order by x)
+from (values (1),(1),(2),(2),(3),(3),(4)) v(x);
+select test_percentile_disc(0.5) within group (order by thousand) from tenk1;
+
+-- ordered-set aggs can't use ungrouped vars in direct args:
+select rank(x) within group (order by x) from generate_series(1,5) x;
+
+-- outer-level agg can't use a grouped arg of a lower level, either:
+select array(select percentile_disc(a) within group (order by x)
+ from (values (0.3),(0.7)) v(a) group by a)
+ from generate_series(1,5) g(x);
+
+-- agg in the direct args is a grouping violation, too:
+select rank(sum(x)) within group (order by x) from generate_series(1,5) x;
+
+-- hypothetical-set type unification and argument-count failures:
+select rank(3) within group (order by x) from (values ('fred'),('jim')) v(x);
+select rank(3) within group (order by stringu1,stringu2) from tenk1;
+select rank('fred') within group (order by x) from generate_series(1,5) x;
+select rank('adam'::text collate "C") within group (order by x collate "POSIX")
+ from (values ('fred'),('jim')) v(x);
+-- hypothetical-set type unification successes:
+select rank('adam'::varchar) within group (order by x) from (values ('fred'),('jim')) v(x);
+select rank('3') within group (order by x) from generate_series(1,5) x;
+
+-- divide by zero check
+select percent_rank(0) within group (order by x) from generate_series(1,0) x;
+
+-- deparse and multiple features:
+create view aggordview1 as
+select ten,
+ percentile_disc(0.5) within group (order by thousand) as p50,
+ percentile_disc(0.5) within group (order by thousand) filter (where hundred=1) as px,
+ rank(5,'AZZZZ',50) within group (order by hundred, string4 desc, hundred)
+ from tenk1
+ group by ten order by ten;
+
+select pg_get_viewdef('aggordview1');
+select * from aggordview1 order by ten;
+drop view aggordview1;
+
+-- variadic aggregates
+select least_agg(q1,q2) from int8_tbl;
+select least_agg(variadic array[q1,q2]) from int8_tbl;
+
+select cleast_agg(q1,q2) from int8_tbl;
+select cleast_agg(4.5,f1) from int4_tbl;
+select cleast_agg(variadic array[4.5,f1]) from int4_tbl;
+select pg_typeof(cleast_agg(variadic array[4.5,f1])) from int4_tbl;
+
+-- test aggregates with common transition functions share the same states
+begin work;
+
+create type avg_state as (total bigint, count bigint);
+
+create or replace function avg_transfn(state avg_state, n int) returns avg_state as
+$$
+declare new_state avg_state;
+begin
+ raise notice 'avg_transfn called with %', n;
+ if state is null then
+ if n is not null then
+ new_state.total := n;
+ new_state.count := 1;
+ return new_state;
+ end if;
+ return null;
+ elsif n is not null then
+ state.total := state.total + n;
+ state.count := state.count + 1;
+ return state;
+ end if;
+
+ return null;
+end
+$$ language plpgsql;
+
+create function avg_finalfn(state avg_state) returns int4 as
+$$
+begin
+ if state is null then
+ return NULL;
+ else
+ return state.total / state.count;
+ end if;
+end
+$$ language plpgsql;
+
+create function sum_finalfn(state avg_state) returns int4 as
+$$
+begin
+ if state is null then
+ return NULL;
+ else
+ return state.total;
+ end if;
+end
+$$ language plpgsql;
+
+create aggregate my_avg(int4)
+(
+ stype = avg_state,
+ sfunc = avg_transfn,
+ finalfunc = avg_finalfn
+);
+
+create aggregate my_sum(int4)
+(
+ stype = avg_state,
+ sfunc = avg_transfn,
+ finalfunc = sum_finalfn
+);
+
+-- aggregate state should be shared as aggs are the same.
+select my_avg(one),my_avg(one) from (values(1),(3)) t(one);
+
+-- aggregate state should be shared as transfn is the same for both aggs.
+select my_avg(one),my_sum(one) from (values(1),(3)) t(one);
+
+-- same as previous one, but with DISTINCT, which requires sorting the input.
+select my_avg(distinct one),my_sum(distinct one) from (values(1),(3),(1)) t(one);
+
+-- shouldn't share states due to the distinctness not matching.
+select my_avg(distinct one),my_sum(one) from (values(1),(3)) t(one);
+
+-- shouldn't share states due to the filter clause not matching.
+select my_avg(one) filter (where one > 1),my_sum(one) from (values(1),(3)) t(one);
+
+-- this should not share the state due to different input columns.
+select my_avg(one),my_sum(two) from (values(1,2),(3,4)) t(one,two);
+
+-- exercise cases where OSAs share state
+select
+ percentile_cont(0.5) within group (order by a),
+ percentile_disc(0.5) within group (order by a)
+from (values(1::float8),(3),(5),(7)) t(a);
+
+select
+ percentile_cont(0.25) within group (order by a),
+ percentile_disc(0.5) within group (order by a)
+from (values(1::float8),(3),(5),(7)) t(a);
+
+-- these can't share state currently
+select
+ rank(4) within group (order by a),
+ dense_rank(4) within group (order by a)
+from (values(1),(3),(5),(7)) t(a);
+
+-- test that aggs with the same sfunc and initcond share the same agg state
+create aggregate my_sum_init(int4)
+(
+ stype = avg_state,
+ sfunc = avg_transfn,
+ finalfunc = sum_finalfn,
+ initcond = '(10,0)'
+);
+
+create aggregate my_avg_init(int4)
+(
+ stype = avg_state,
+ sfunc = avg_transfn,
+ finalfunc = avg_finalfn,
+ initcond = '(10,0)'
+);
+
+create aggregate my_avg_init2(int4)
+(
+ stype = avg_state,
+ sfunc = avg_transfn,
+ finalfunc = avg_finalfn,
+ initcond = '(4,0)'
+);
+
+-- state should be shared if INITCONDs are matching
+select my_sum_init(one),my_avg_init(one) from (values(1),(3)) t(one);
+
+-- Varying INITCONDs should cause the states not to be shared.
+select my_sum_init(one),my_avg_init2(one) from (values(1),(3)) t(one);
+
+rollback;
+
+-- test aggregate state sharing to ensure it works if one aggregate has a
+-- finalfn and the other one has none.
+begin work;
+
+create or replace function sum_transfn(state int4, n int4) returns int4 as
+$$
+declare new_state int4;
+begin
+ raise notice 'sum_transfn called with %', n;
+ if state is null then
+ if n is not null then
+ new_state := n;
+ return new_state;
+ end if;
+ return null;
+ elsif n is not null then
+ state := state + n;
+ return state;
+ end if;
+
+ return null;
+end
+$$ language plpgsql;
+
+create function halfsum_finalfn(state int4) returns int4 as
+$$
+begin
+ if state is null then
+ return NULL;
+ else
+ return state / 2;
+ end if;
+end
+$$ language plpgsql;
+
+create aggregate my_sum(int4)
+(
+ stype = int4,
+ sfunc = sum_transfn
+);
+
+create aggregate my_half_sum(int4)
+(
+ stype = int4,
+ sfunc = sum_transfn,
+ finalfunc = halfsum_finalfn
+);
+
+-- Agg state should be shared even though my_sum has no finalfn
+select my_sum(one),my_half_sum(one) from (values(1),(2),(3),(4)) t(one);
+
+rollback;
+
+
+-- test that the aggregate transition logic correctly handles
+-- transition / combine functions returning NULL
+
+-- First test the case of a normal transition function returning NULL
+BEGIN;
+CREATE FUNCTION balkifnull(int8, int4)
+RETURNS int8
+STRICT
+LANGUAGE plpgsql AS $$
+BEGIN
+ IF $1 IS NULL THEN
+ RAISE 'erroneously called with NULL argument';
+ END IF;
+ RETURN NULL;
+END$$;
+
+CREATE AGGREGATE balk(int4)
+(
+ SFUNC = balkifnull(int8, int4),
+ STYPE = int8,
+ PARALLEL = SAFE,
+ INITCOND = '0'
+);
+
+SELECT balk(hundred) FROM tenk1;
+
+ROLLBACK;
+
+-- Secondly test the case of a parallel aggregate combiner function
+-- returning NULL. For that use normal transition function, but a
+-- combiner function returning NULL.
+BEGIN ISOLATION LEVEL REPEATABLE READ;
+CREATE FUNCTION balkifnull(int8, int8)
+RETURNS int8
+PARALLEL SAFE
+STRICT
+LANGUAGE plpgsql AS $$
+BEGIN
+ IF $1 IS NULL THEN
+ RAISE 'erroneously called with NULL argument';
+ END IF;
+ RETURN NULL;
+END$$;
+
+CREATE AGGREGATE balk(int4)
+(
+ SFUNC = int4_sum(int8, int4),
+ STYPE = int8,
+ COMBINEFUNC = balkifnull(int8, int8),
+ PARALLEL = SAFE,
+ INITCOND = '0'
+);
+
+-- force use of parallelism
+ALTER TABLE tenk1 set (parallel_workers = 4);
+SET LOCAL parallel_setup_cost=0;
+SET LOCAL max_parallel_workers_per_gather=4;
+
+EXPLAIN (COSTS OFF) SELECT balk(hundred) FROM tenk1;
+SELECT balk(hundred) FROM tenk1;
+
+ROLLBACK;
+
+-- test coverage for aggregate combine/serial/deserial functions
+BEGIN ISOLATION LEVEL REPEATABLE READ;
+
+SET parallel_setup_cost = 0;
+SET parallel_tuple_cost = 0;
+SET min_parallel_table_scan_size = 0;
+SET max_parallel_workers_per_gather = 4;
+SET parallel_leader_participation = off;
+SET enable_indexonlyscan = off;
+
+-- variance(int4) covers numeric_poly_combine
+-- sum(int8) covers int8_avg_combine
+-- regr_count(float8, float8) covers int8inc_float8_float8 and aggregates with > 1 arg
+EXPLAIN (COSTS OFF, VERBOSE)
+SELECT variance(unique1::int4), sum(unique1::int8), regr_count(unique1::float8, unique1::float8)
+FROM (SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1) u;
+
+SELECT variance(unique1::int4), sum(unique1::int8), regr_count(unique1::float8, unique1::float8)
+FROM (SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1) u;
+
+-- variance(int8) covers numeric_combine
+-- avg(numeric) covers numeric_avg_combine
+EXPLAIN (COSTS OFF, VERBOSE)
+SELECT variance(unique1::int8), avg(unique1::numeric)
+FROM (SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1) u;
+
+SELECT variance(unique1::int8), avg(unique1::numeric)
+FROM (SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1
+ UNION ALL SELECT * FROM tenk1) u;
+
+ROLLBACK;
+
+-- test coverage for dense_rank
+SELECT dense_rank(x) WITHIN GROUP (ORDER BY x) FROM (VALUES (1),(1),(2),(2),(3),(3)) v(x) GROUP BY (x) ORDER BY 1;
+
+
+-- Ensure that the STRICT checks for aggregates does not take NULLness
+-- of ORDER BY columns into account. See bug report around
+-- 2a505161-2727-2473-7c46-591ed108ac52@email.cz
+SELECT min(x ORDER BY y) FROM (VALUES(1, NULL)) AS d(x,y);
+SELECT min(x ORDER BY y) FROM (VALUES(1, 2)) AS d(x,y);
+
+-- check collation-sensitive matching between grouping expressions
+select v||'a', case v||'a' when 'aa' then 1 else 0 end, count(*)
+ from unnest(array['a','b']) u(v)
+ group by v||'a' order by 1;
+select v||'a', case when v||'a' = 'aa' then 1 else 0 end, count(*)
+ from unnest(array['a','b']) u(v)
+ group by v||'a' order by 1;
+
+-- Make sure that generation of HashAggregate for uniqification purposes
+-- does not lead to array overflow due to unexpected duplicate hash keys
+-- see CAFeeJoKKu0u+A_A9R9316djW-YW3-+Gtgvy3ju655qRHR3jtdA@mail.gmail.com
+explain (costs off)
+ select 1 from tenk1
+ where (hundred, thousand) in (select twothousand, twothousand from onek);
+
+--
+-- Hash Aggregation Spill tests
+--
+
+set enable_sort=false;
+set work_mem='64kB';
+
+select unique1, count(*), sum(twothousand) from tenk1
+group by unique1
+having sum(fivethous) > 4975
+order by sum(twothousand);
+
+set work_mem to default;
+set enable_sort to default;
+
+--
+-- Compare results between plans using sorting and plans using hash
+-- aggregation. Force spilling in both cases by setting work_mem low.
+--
+
+set work_mem='64kB';
+
+create table agg_data_2k as
+select g from generate_series(0, 1999) g;
+analyze agg_data_2k;
+
+create table agg_data_20k as
+select g from generate_series(0, 19999) g;
+analyze agg_data_20k;
+
+-- Produce results with sorting.
+
+set enable_hashagg = false;
+
+set jit_above_cost = 0;
+
+explain (costs off)
+select g%10000 as c1, sum(g::numeric) as c2, count(*) as c3
+ from agg_data_20k group by g%10000;
+
+create table agg_group_1 as
+select g%10000 as c1, sum(g::numeric) as c2, count(*) as c3
+ from agg_data_20k group by g%10000;
+
+create table agg_group_2 as
+select * from
+ (values (100), (300), (500)) as r(a),
+ lateral (
+ select (g/2)::numeric as c1,
+ array_agg(g::numeric) as c2,
+ count(*) as c3
+ from agg_data_2k
+ where g < r.a
+ group by g/2) as s;
+
+set jit_above_cost to default;
+
+create table agg_group_3 as
+select (g/2)::numeric as c1, sum(7::int4) as c2, count(*) as c3
+ from agg_data_2k group by g/2;
+
+create table agg_group_4 as
+select (g/2)::numeric as c1, array_agg(g::numeric) as c2, count(*) as c3
+ from agg_data_2k group by g/2;
+
+-- Produce results with hash aggregation
+
+set enable_hashagg = true;
+set enable_sort = false;
+
+set jit_above_cost = 0;
+
+explain (costs off)
+select g%10000 as c1, sum(g::numeric) as c2, count(*) as c3
+ from agg_data_20k group by g%10000;
+
+create table agg_hash_1 as
+select g%10000 as c1, sum(g::numeric) as c2, count(*) as c3
+ from agg_data_20k group by g%10000;
+
+create table agg_hash_2 as
+select * from
+ (values (100), (300), (500)) as r(a),
+ lateral (
+ select (g/2)::numeric as c1,
+ array_agg(g::numeric) as c2,
+ count(*) as c3
+ from agg_data_2k
+ where g < r.a
+ group by g/2) as s;
+
+set jit_above_cost to default;
+
+create table agg_hash_3 as
+select (g/2)::numeric as c1, sum(7::int4) as c2, count(*) as c3
+ from agg_data_2k group by g/2;
+
+create table agg_hash_4 as
+select (g/2)::numeric as c1, array_agg(g::numeric) as c2, count(*) as c3
+ from agg_data_2k group by g/2;
+
+set enable_sort = true;
+set work_mem to default;
+
+-- Compare group aggregation results to hash aggregation results
+
+(select * from agg_hash_1 except select * from agg_group_1)
+ union all
+(select * from agg_group_1 except select * from agg_hash_1);
+
+(select * from agg_hash_2 except select * from agg_group_2)
+ union all
+(select * from agg_group_2 except select * from agg_hash_2);
+
+(select * from agg_hash_3 except select * from agg_group_3)
+ union all
+(select * from agg_group_3 except select * from agg_hash_3);
+
+(select * from agg_hash_4 except select * from agg_group_4)
+ union all
+(select * from agg_group_4 except select * from agg_hash_4);
+
+drop table agg_group_1;
+drop table agg_group_2;
+drop table agg_group_3;
+drop table agg_group_4;
+drop table agg_hash_1;
+drop table agg_hash_2;
+drop table agg_hash_3;
+drop table agg_hash_4;
diff --git a/src/test/resources/postgresql-corpus/case.sql b/src/test/resources/postgresql-corpus/case.sql
new file mode 100644
index 0000000..17436c5
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/case.sql
@@ -0,0 +1,254 @@
+--
+-- CASE
+-- Test the case statement
+--
+
+CREATE TABLE CASE_TBL (
+ i integer,
+ f double precision
+);
+
+CREATE TABLE CASE2_TBL (
+ i integer,
+ j integer
+);
+
+INSERT INTO CASE_TBL VALUES (1, 10.1);
+INSERT INTO CASE_TBL VALUES (2, 20.2);
+INSERT INTO CASE_TBL VALUES (3, -30.3);
+INSERT INTO CASE_TBL VALUES (4, NULL);
+
+INSERT INTO CASE2_TBL VALUES (1, -1);
+INSERT INTO CASE2_TBL VALUES (2, -2);
+INSERT INTO CASE2_TBL VALUES (3, -3);
+INSERT INTO CASE2_TBL VALUES (2, -4);
+INSERT INTO CASE2_TBL VALUES (1, NULL);
+INSERT INTO CASE2_TBL VALUES (NULL, -6);
+
+--
+-- Simplest examples without tables
+--
+
+SELECT '3' AS "One",
+ CASE
+ WHEN 1 < 2 THEN 3
+ END AS "Simple WHEN";
+
+SELECT '' AS "One",
+ CASE
+ WHEN 1 > 2 THEN 3
+ END AS "Simple default";
+
+SELECT '3' AS "One",
+ CASE
+ WHEN 1 < 2 THEN 3
+ ELSE 4
+ END AS "Simple ELSE";
+
+SELECT '4' AS "One",
+ CASE
+ WHEN 1 > 2 THEN 3
+ ELSE 4
+ END AS "ELSE default";
+
+SELECT '6' AS "One",
+ CASE
+ WHEN 1 > 2 THEN 3
+ WHEN 4 < 5 THEN 6
+ ELSE 7
+ END AS "Two WHEN with default";
+
+
+SELECT '7' AS "None",
+ CASE WHEN random() < 0 THEN 1
+ END AS "NULL on no matches";
+
+-- Constant-expression folding shouldn't evaluate unreachable subexpressions
+SELECT CASE WHEN 1=0 THEN 1/0 WHEN 1=1 THEN 1 ELSE 2/0 END;
+SELECT CASE 1 WHEN 0 THEN 1/0 WHEN 1 THEN 1 ELSE 2/0 END;
+
+-- However we do not currently suppress folding of potentially
+-- reachable subexpressions
+SELECT CASE WHEN i > 100 THEN 1/0 ELSE 0 END FROM case_tbl;
+
+-- Test for cases involving untyped literals in test expression
+SELECT CASE 'a' WHEN 'a' THEN 1 ELSE 2 END;
+
+--
+-- Examples of targets involving tables
+--
+
+SELECT '' AS "Five",
+ CASE
+ WHEN i >= 3 THEN i
+ END AS ">= 3 or Null"
+ FROM CASE_TBL;
+
+SELECT '' AS "Five",
+ CASE WHEN i >= 3 THEN (i + i)
+ ELSE i
+ END AS "Simplest Math"
+ FROM CASE_TBL;
+
+SELECT '' AS "Five", i AS "Value",
+ CASE WHEN (i < 0) THEN 'small'
+ WHEN (i = 0) THEN 'zero'
+ WHEN (i = 1) THEN 'one'
+ WHEN (i = 2) THEN 'two'
+ ELSE 'big'
+ END AS "Category"
+ FROM CASE_TBL;
+
+SELECT '' AS "Five",
+ CASE WHEN ((i < 0) or (i < 0)) THEN 'small'
+ WHEN ((i = 0) or (i = 0)) THEN 'zero'
+ WHEN ((i = 1) or (i = 1)) THEN 'one'
+ WHEN ((i = 2) or (i = 2)) THEN 'two'
+ ELSE 'big'
+ END AS "Category"
+ FROM CASE_TBL;
+
+--
+-- Examples of qualifications involving tables
+--
+
+--
+-- NULLIF() and COALESCE()
+-- Shorthand forms for typical CASE constructs
+-- defined in the SQL standard.
+--
+
+SELECT * FROM CASE_TBL WHERE COALESCE(f,i) = 4;
+
+SELECT * FROM CASE_TBL WHERE NULLIF(f,i) = 2;
+
+SELECT COALESCE(a.f, b.i, b.j)
+ FROM CASE_TBL a, CASE2_TBL b;
+
+SELECT *
+ FROM CASE_TBL a, CASE2_TBL b
+ WHERE COALESCE(a.f, b.i, b.j) = 2;
+
+SELECT '' AS Five, NULLIF(a.i,b.i) AS "NULLIF(a.i,b.i)",
+ NULLIF(b.i, 4) AS "NULLIF(b.i,4)"
+ FROM CASE_TBL a, CASE2_TBL b;
+
+SELECT '' AS "Two", *
+ FROM CASE_TBL a, CASE2_TBL b
+ WHERE COALESCE(f,b.i) = 2;
+
+--
+-- Examples of updates involving tables
+--
+
+UPDATE CASE_TBL
+ SET i = CASE WHEN i >= 3 THEN (- i)
+ ELSE (2 * i) END;
+
+SELECT * FROM CASE_TBL;
+
+UPDATE CASE_TBL
+ SET i = CASE WHEN i >= 2 THEN (2 * i)
+ ELSE (3 * i) END;
+
+SELECT * FROM CASE_TBL;
+
+UPDATE CASE_TBL
+ SET i = CASE WHEN b.i >= 2 THEN (2 * j)
+ ELSE (3 * j) END
+ FROM CASE2_TBL b
+ WHERE j = -CASE_TBL.i;
+
+SELECT * FROM CASE_TBL;
+
+--
+-- Nested CASE expressions
+--
+
+-- This test exercises a bug caused by aliasing econtext->caseValue_isNull
+-- with the isNull argument of the inner CASE's CaseExpr evaluation. After
+-- evaluating the vol(null) expression in the inner CASE's second WHEN-clause,
+-- the isNull flag for the case test value incorrectly became true, causing
+-- the third WHEN-clause not to match. The volatile function calls are needed
+-- to prevent constant-folding in the planner, which would hide the bug.
+
+-- Wrap this in a single transaction so the transient '=' operator doesn't
+-- cause problems in concurrent sessions
+BEGIN;
+
+CREATE FUNCTION vol(text) returns text as
+ 'begin return $1; end' language plpgsql volatile;
+
+SELECT CASE
+ (CASE vol('bar')
+ WHEN 'foo' THEN 'it was foo!'
+ WHEN vol(null) THEN 'null input'
+ WHEN 'bar' THEN 'it was bar!' END
+ )
+ WHEN 'it was foo!' THEN 'foo recognized'
+ WHEN 'it was bar!' THEN 'bar recognized'
+ ELSE 'unrecognized' END;
+
+-- In this case, we can't inline the SQL function without confusing things.
+CREATE DOMAIN foodomain AS text;
+
+CREATE FUNCTION volfoo(text) returns foodomain as
+ 'begin return $1::foodomain; end' language plpgsql volatile;
+
+CREATE FUNCTION inline_eq(foodomain, foodomain) returns boolean as
+ 'SELECT CASE $2::text WHEN $1::text THEN true ELSE false END' language sql;
+
+CREATE OPERATOR = (procedure = inline_eq,
+ leftarg = foodomain, rightarg = foodomain);
+
+SELECT CASE volfoo('bar') WHEN 'foo'::foodomain THEN 'is foo' ELSE 'is not foo' END;
+
+ROLLBACK;
+
+-- Test multiple evaluation of a CASE arg that is a read/write object (#14472)
+-- Wrap this in a single transaction so the transient '=' operator doesn't
+-- cause problems in concurrent sessions
+BEGIN;
+
+CREATE DOMAIN arrdomain AS int[];
+
+CREATE FUNCTION make_ad(int,int) returns arrdomain as
+ 'declare x arrdomain;
+ begin
+ x := array[$1,$2];
+ return x;
+ end' language plpgsql volatile;
+
+CREATE FUNCTION ad_eq(arrdomain, arrdomain) returns boolean as
+ 'begin return array_eq($1, $2); end' language plpgsql;
+
+CREATE OPERATOR = (procedure = ad_eq,
+ leftarg = arrdomain, rightarg = arrdomain);
+
+SELECT CASE make_ad(1,2)
+ WHEN array[2,4]::arrdomain THEN 'wrong'
+ WHEN array[2,5]::arrdomain THEN 'still wrong'
+ WHEN array[1,2]::arrdomain THEN 'right'
+ END;
+
+ROLLBACK;
+
+-- Test interaction of CASE with ArrayCoerceExpr (bug #15471)
+BEGIN;
+
+CREATE TYPE casetestenum AS ENUM ('e', 'f', 'g');
+
+SELECT
+ CASE 'foo'::text
+ WHEN 'foo' THEN ARRAY['a', 'b', 'c', 'd'] || enum_range(NULL::casetestenum)::text[]
+ ELSE ARRAY['x', 'y']
+ END;
+
+ROLLBACK;
+
+--
+-- Clean up
+--
+
+DROP TABLE CASE_TBL;
+DROP TABLE CASE2_TBL;
diff --git a/src/test/resources/postgresql-corpus/delete.sql b/src/test/resources/postgresql-corpus/delete.sql
new file mode 100644
index 0000000..d8cb99e
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/delete.sql
@@ -0,0 +1,25 @@
+CREATE TABLE delete_test (
+ id SERIAL PRIMARY KEY,
+ a INT,
+ b text
+);
+
+INSERT INTO delete_test (a) VALUES (10);
+INSERT INTO delete_test (a, b) VALUES (50, repeat('x', 10000));
+INSERT INTO delete_test (a) VALUES (100);
+
+-- allow an alias to be specified for DELETE's target table
+DELETE FROM delete_test AS dt WHERE dt.a > 75;
+
+-- if an alias is specified, don't allow the original table name
+-- to be referenced
+DELETE FROM delete_test dt WHERE delete_test.a > 25;
+
+SELECT id, a, char_length(b) FROM delete_test;
+
+-- delete a row with a TOASTed value
+DELETE FROM delete_test WHERE a > 25;
+
+SELECT id, a, char_length(b) FROM delete_test;
+
+DROP TABLE delete_test;
diff --git a/src/test/resources/postgresql-corpus/insert.sql b/src/test/resources/postgresql-corpus/insert.sql
new file mode 100644
index 0000000..6c75264
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/insert.sql
@@ -0,0 +1,595 @@
+--
+-- insert with DEFAULT in the target_list
+--
+create table inserttest (col1 int4, col2 int4 NOT NULL, col3 text default 'testing');
+insert into inserttest (col1, col2, col3) values (DEFAULT, DEFAULT, DEFAULT);
+insert into inserttest (col2, col3) values (3, DEFAULT);
+insert into inserttest (col1, col2, col3) values (DEFAULT, 5, DEFAULT);
+insert into inserttest values (DEFAULT, 5, 'test');
+insert into inserttest values (DEFAULT, 7);
+
+select * from inserttest;
+
+--
+-- insert with similar expression / target_list values (all fail)
+--
+insert into inserttest (col1, col2, col3) values (DEFAULT, DEFAULT);
+insert into inserttest (col1, col2, col3) values (1, 2);
+insert into inserttest (col1) values (1, 2);
+insert into inserttest (col1) values (DEFAULT, DEFAULT);
+
+select * from inserttest;
+
+--
+-- VALUES test
+--
+insert into inserttest values(10, 20, '40'), (-1, 2, DEFAULT),
+ ((select 2), (select i from (values(3)) as foo (i)), 'values are fun!');
+
+select * from inserttest;
+
+--
+-- TOASTed value test
+--
+insert into inserttest values(30, 50, repeat('x', 10000));
+
+select col1, col2, char_length(col3) from inserttest;
+
+drop table inserttest;
+
+--
+-- check indirection (field/array assignment), cf bug #14265
+--
+-- these tests are aware that transformInsertStmt has 3 separate code paths
+--
+
+create type insert_test_type as (if1 int, if2 text[]);
+
+create table inserttest (f1 int, f2 int[],
+ f3 insert_test_type, f4 insert_test_type[]);
+
+insert into inserttest (f2[1], f2[2]) values (1,2);
+insert into inserttest (f2[1], f2[2]) values (3,4), (5,6);
+insert into inserttest (f2[1], f2[2]) select 7,8;
+insert into inserttest (f2[1], f2[2]) values (1,default); -- not supported
+
+insert into inserttest (f3.if1, f3.if2) values (1,array['foo']);
+insert into inserttest (f3.if1, f3.if2) values (1,'{foo}'), (2,'{bar}');
+insert into inserttest (f3.if1, f3.if2) select 3, '{baz,quux}';
+insert into inserttest (f3.if1, f3.if2) values (1,default); -- not supported
+
+insert into inserttest (f3.if2[1], f3.if2[2]) values ('foo', 'bar');
+insert into inserttest (f3.if2[1], f3.if2[2]) values ('foo', 'bar'), ('baz', 'quux');
+insert into inserttest (f3.if2[1], f3.if2[2]) select 'bear', 'beer';
+
+insert into inserttest (f4[1].if2[1], f4[1].if2[2]) values ('foo', 'bar');
+insert into inserttest (f4[1].if2[1], f4[1].if2[2]) values ('foo', 'bar'), ('baz', 'quux');
+insert into inserttest (f4[1].if2[1], f4[1].if2[2]) select 'bear', 'beer';
+
+select * from inserttest;
+
+-- also check reverse-listing
+create table inserttest2 (f1 bigint, f2 text);
+create rule irule1 as on insert to inserttest2 do also
+ insert into inserttest (f3.if2[1], f3.if2[2])
+ values (new.f1,new.f2);
+create rule irule2 as on insert to inserttest2 do also
+ insert into inserttest (f4[1].if1, f4[1].if2[2])
+ values (1,'fool'),(new.f1,new.f2);
+create rule irule3 as on insert to inserttest2 do also
+ insert into inserttest (f4[1].if1, f4[1].if2[2])
+ select new.f1, new.f2;
+
+drop table inserttest2;
+drop table inserttest;
+drop type insert_test_type;
+
+-- direct partition inserts should check partition bound constraint
+create table range_parted (
+ a text,
+ b int
+) partition by range (a, (b+0));
+
+-- no partitions, so fail
+insert into range_parted values ('a', 11);
+
+create table part1 partition of range_parted for values from ('a', 1) to ('a', 10);
+create table part2 partition of range_parted for values from ('a', 10) to ('a', 20);
+create table part3 partition of range_parted for values from ('b', 1) to ('b', 10);
+create table part4 partition of range_parted for values from ('b', 10) to ('b', 20);
+
+-- fail
+insert into part1 values ('a', 11);
+insert into part1 values ('b', 1);
+-- ok
+insert into part1 values ('a', 1);
+-- fail
+insert into part4 values ('b', 21);
+insert into part4 values ('a', 10);
+-- ok
+insert into part4 values ('b', 10);
+
+-- fail (partition key a has a NOT NULL constraint)
+insert into part1 values (null);
+-- fail (expression key (b+0) cannot be null either)
+insert into part1 values (1);
+
+create table list_parted (
+ a text,
+ b int
+) partition by list (lower(a));
+create table part_aa_bb partition of list_parted FOR VALUES IN ('aa', 'bb');
+create table part_cc_dd partition of list_parted FOR VALUES IN ('cc', 'dd');
+create table part_null partition of list_parted FOR VALUES IN (null);
+
+-- fail
+insert into part_aa_bb values ('cc', 1);
+insert into part_aa_bb values ('AAa', 1);
+insert into part_aa_bb values (null);
+-- ok
+insert into part_cc_dd values ('cC', 1);
+insert into part_null values (null, 0);
+
+-- check in case of multi-level partitioned table
+create table part_ee_ff partition of list_parted for values in ('ee', 'ff') partition by range (b);
+create table part_ee_ff1 partition of part_ee_ff for values from (1) to (10);
+create table part_ee_ff2 partition of part_ee_ff for values from (10) to (20);
+
+-- test default partition
+create table part_default partition of list_parted default;
+-- Negative test: a row, which would fit in other partition, does not fit
+-- default partition, even when inserted directly
+insert into part_default values ('aa', 2);
+insert into part_default values (null, 2);
+-- ok
+insert into part_default values ('Zz', 2);
+-- test if default partition works as expected for multi-level partitioned
+-- table as well as when default partition itself is further partitioned
+drop table part_default;
+create table part_xx_yy partition of list_parted for values in ('xx', 'yy') partition by list (a);
+create table part_xx_yy_p1 partition of part_xx_yy for values in ('xx');
+create table part_xx_yy_defpart partition of part_xx_yy default;
+create table part_default partition of list_parted default partition by range(b);
+create table part_default_p1 partition of part_default for values from (20) to (30);
+create table part_default_p2 partition of part_default for values from (30) to (40);
+
+-- fail
+insert into part_ee_ff1 values ('EE', 11);
+insert into part_default_p2 values ('gg', 43);
+-- fail (even the parent's, ie, part_ee_ff's partition constraint applies)
+insert into part_ee_ff1 values ('cc', 1);
+insert into part_default values ('gg', 43);
+-- ok
+insert into part_ee_ff1 values ('ff', 1);
+insert into part_ee_ff2 values ('ff', 11);
+insert into part_default_p1 values ('cd', 25);
+insert into part_default_p2 values ('de', 35);
+insert into list_parted values ('ab', 21);
+insert into list_parted values ('xx', 1);
+insert into list_parted values ('yy', 2);
+select tableoid::regclass, * from list_parted;
+
+-- Check tuple routing for partitioned tables
+
+-- fail
+insert into range_parted values ('a', 0);
+-- ok
+insert into range_parted values ('a', 1);
+insert into range_parted values ('a', 10);
+-- fail
+insert into range_parted values ('a', 20);
+-- ok
+insert into range_parted values ('b', 1);
+insert into range_parted values ('b', 10);
+-- fail (partition key (b+0) is null)
+insert into range_parted values ('a');
+
+-- Check default partition
+create table part_def partition of range_parted default;
+-- fail
+insert into part_def values ('b', 10);
+-- ok
+insert into part_def values ('c', 10);
+insert into range_parted values (null, null);
+insert into range_parted values ('a', null);
+insert into range_parted values (null, 19);
+insert into range_parted values ('b', 20);
+
+select tableoid::regclass, * from range_parted;
+-- ok
+insert into list_parted values (null, 1);
+insert into list_parted (a) values ('aA');
+-- fail (partition of part_ee_ff not found in both cases)
+insert into list_parted values ('EE', 0);
+insert into part_ee_ff values ('EE', 0);
+-- ok
+insert into list_parted values ('EE', 1);
+insert into part_ee_ff values ('EE', 10);
+select tableoid::regclass, * from list_parted;
+
+-- some more tests to exercise tuple-routing with multi-level partitioning
+create table part_gg partition of list_parted for values in ('gg') partition by range (b);
+create table part_gg1 partition of part_gg for values from (minvalue) to (1);
+create table part_gg2 partition of part_gg for values from (1) to (10) partition by range (b);
+create table part_gg2_1 partition of part_gg2 for values from (1) to (5);
+create table part_gg2_2 partition of part_gg2 for values from (5) to (10);
+
+create table part_ee_ff3 partition of part_ee_ff for values from (20) to (30) partition by range (b);
+create table part_ee_ff3_1 partition of part_ee_ff3 for values from (20) to (25);
+create table part_ee_ff3_2 partition of part_ee_ff3 for values from (25) to (30);
+
+truncate list_parted;
+insert into list_parted values ('aa'), ('cc');
+insert into list_parted select 'Ff', s.a from generate_series(1, 29) s(a);
+insert into list_parted select 'gg', s.a from generate_series(1, 9) s(a);
+insert into list_parted (b) values (1);
+select tableoid::regclass::text, a, min(b) as min_b, max(b) as max_b from list_parted group by 1, 2 order by 1;
+
+-- direct partition inserts should check hash partition bound constraint
+
+-- Use hand-rolled hash functions and operator classes to get predictable
+-- result on different machines. The hash function for int4 simply returns
+-- the sum of the values passed to it and the one for text returns the length
+-- of the non-empty string value passed to it or 0.
+
+create or replace function part_hashint4_noop(value int4, seed int8)
+returns int8 as $$
+select value + seed;
+$$ language sql immutable;
+
+create operator class part_test_int4_ops
+for type int4
+using hash as
+operator 1 =,
+function 2 part_hashint4_noop(int4, int8);
+
+create or replace function part_hashtext_length(value text, seed int8)
+RETURNS int8 AS $$
+select length(coalesce(value, ''))::int8
+$$ language sql immutable;
+
+create operator class part_test_text_ops
+for type text
+using hash as
+operator 1 =,
+function 2 part_hashtext_length(text, int8);
+
+create table hash_parted (
+ a int
+) partition by hash (a part_test_int4_ops);
+create table hpart0 partition of hash_parted for values with (modulus 4, remainder 0);
+create table hpart1 partition of hash_parted for values with (modulus 4, remainder 1);
+create table hpart2 partition of hash_parted for values with (modulus 4, remainder 2);
+create table hpart3 partition of hash_parted for values with (modulus 4, remainder 3);
+
+insert into hash_parted values(generate_series(1,10));
+
+-- direct insert of values divisible by 4 - ok;
+insert into hpart0 values(12),(16);
+-- fail;
+insert into hpart0 values(11);
+-- 11 % 4 -> 3 remainder i.e. valid data for hpart3 partition
+insert into hpart3 values(11);
+
+-- view data
+select tableoid::regclass as part, a, a%4 as "remainder = a % 4"
+from hash_parted order by part;
+
+-- test \d+ output on a table which has both partitioned and unpartitioned
+-- partitions
+
+-- cleanup
+drop table range_parted, list_parted;
+drop table hash_parted;
+
+-- test that a default partition added as the first partition accepts any value
+-- including null
+create table list_parted (a int) partition by list (a);
+create table part_default partition of list_parted default;
+insert into part_default values (null);
+insert into part_default values (1);
+insert into part_default values (-1);
+select tableoid::regclass, a from list_parted;
+-- cleanup
+drop table list_parted;
+
+-- more tests for certain multi-level partitioning scenarios
+create table mlparted (a int, b int) partition by range (a, b);
+create table mlparted1 (b int not null, a int not null) partition by range ((b+0));
+create table mlparted11 (like mlparted1);
+alter table mlparted11 drop a;
+alter table mlparted11 add a int;
+alter table mlparted11 drop a;
+alter table mlparted11 add a int not null;
+-- attnum for key attribute 'a' is different in mlparted, mlparted1, and mlparted11
+select attrelid::regclass, attname, attnum
+from pg_attribute
+where attname = 'a'
+ and (attrelid = 'mlparted'::regclass
+ or attrelid = 'mlparted1'::regclass
+ or attrelid = 'mlparted11'::regclass)
+order by attrelid::regclass::text;
+
+alter table mlparted1 attach partition mlparted11 for values from (2) to (5);
+alter table mlparted attach partition mlparted1 for values from (1, 2) to (1, 10);
+
+-- check that "(1, 2)" is correctly routed to mlparted11.
+insert into mlparted values (1, 2);
+select tableoid::regclass, * from mlparted;
+
+-- check that proper message is shown after failure to route through mlparted1
+insert into mlparted (a, b) values (1, 5);
+
+truncate mlparted;
+alter table mlparted add constraint check_b check (b = 3);
+
+-- have a BR trigger modify the row such that the check_b is violated
+create function mlparted11_trig_fn()
+returns trigger AS
+$$
+begin
+ NEW.b := 4;
+ return NEW;
+end;
+$$
+language plpgsql;
+create trigger mlparted11_trig before insert ON mlparted11
+ for each row execute procedure mlparted11_trig_fn();
+
+-- check that the correct row is shown when constraint check_b fails after
+-- "(1, 2)" is routed to mlparted11 (actually "(1, 4)" would be shown due
+-- to the BR trigger mlparted11_trig_fn)
+insert into mlparted values (1, 2);
+drop trigger mlparted11_trig on mlparted11;
+drop function mlparted11_trig_fn();
+
+-- check that inserting into an internal partition successfully results in
+-- checking its partition constraint before inserting into the leaf partition
+-- selected by tuple-routing
+insert into mlparted1 (a, b) values (2, 3);
+
+-- check routing error through a list partitioned table when the key is null
+create table lparted_nonullpart (a int, b char) partition by list (b);
+create table lparted_nonullpart_a partition of lparted_nonullpart for values in ('a');
+insert into lparted_nonullpart values (1);
+drop table lparted_nonullpart;
+
+-- check that RETURNING works correctly with tuple-routing
+alter table mlparted drop constraint check_b;
+create table mlparted12 partition of mlparted1 for values from (5) to (10);
+create table mlparted2 (b int not null, a int not null);
+alter table mlparted attach partition mlparted2 for values from (1, 10) to (1, 20);
+create table mlparted3 partition of mlparted for values from (1, 20) to (1, 30);
+create table mlparted4 (like mlparted);
+alter table mlparted4 drop a;
+alter table mlparted4 add a int not null;
+alter table mlparted attach partition mlparted4 for values from (1, 30) to (1, 40);
+with ins (a, b, c) as
+ (insert into mlparted (b, a) select s.a, 1 from generate_series(2, 39) s(a) returning tableoid::regclass, *)
+ select a, b, min(c), max(c) from ins group by a, b order by 1;
+
+alter table mlparted add c text;
+create table mlparted5 (c text, a int not null, b int not null) partition by list (c);
+create table mlparted5a (a int not null, c text, b int not null);
+alter table mlparted5 attach partition mlparted5a for values in ('a');
+alter table mlparted attach partition mlparted5 for values from (1, 40) to (1, 50);
+alter table mlparted add constraint check_b check (a = 1 and b < 45);
+insert into mlparted values (1, 45, 'a');
+create function mlparted5abrtrig_func() returns trigger as $$ begin new.c = 'b'; return new; end; $$ language plpgsql;
+create trigger mlparted5abrtrig before insert on mlparted5a for each row execute procedure mlparted5abrtrig_func();
+insert into mlparted5 (a, b, c) values (1, 40, 'a');
+drop table mlparted5;
+alter table mlparted drop constraint check_b;
+
+-- Check multi-level default partition
+create table mlparted_def partition of mlparted default partition by range(a);
+create table mlparted_def1 partition of mlparted_def for values from (40) to (50);
+create table mlparted_def2 partition of mlparted_def for values from (50) to (60);
+insert into mlparted values (40, 100);
+insert into mlparted_def1 values (42, 100);
+insert into mlparted_def2 values (54, 50);
+-- fail
+insert into mlparted values (70, 100);
+insert into mlparted_def1 values (52, 50);
+insert into mlparted_def2 values (34, 50);
+-- ok
+create table mlparted_defd partition of mlparted_def default;
+insert into mlparted values (70, 100);
+
+select tableoid::regclass, * from mlparted_def;
+
+-- Check multi-level tuple routing with attributes dropped from the
+-- top-most parent. First remove the last attribute.
+alter table mlparted add d int, add e int;
+alter table mlparted drop e;
+create table mlparted5 partition of mlparted
+ for values from (1, 40) to (1, 50) partition by range (c);
+create table mlparted5_ab partition of mlparted5
+ for values from ('a') to ('c') partition by list (c);
+-- This partitioned table should remain with no partitions.
+create table mlparted5_cd partition of mlparted5
+ for values from ('c') to ('e') partition by list (c);
+create table mlparted5_a partition of mlparted5_ab for values in ('a');
+create table mlparted5_b (d int, b int, c text, a int);
+alter table mlparted5_ab attach partition mlparted5_b for values in ('b');
+truncate mlparted;
+insert into mlparted values (1, 2, 'a', 1);
+insert into mlparted values (1, 40, 'a', 1); -- goes to mlparted5_a
+insert into mlparted values (1, 45, 'b', 1); -- goes to mlparted5_b
+insert into mlparted values (1, 45, 'c', 1); -- goes to mlparted5_cd, fails
+insert into mlparted values (1, 45, 'f', 1); -- goes to mlparted5, fails
+select tableoid::regclass, * from mlparted order by a, b, c, d;
+alter table mlparted drop d;
+truncate mlparted;
+-- Remove the before last attribute.
+alter table mlparted add e int, add d int;
+alter table mlparted drop e;
+insert into mlparted values (1, 2, 'a', 1);
+insert into mlparted values (1, 40, 'a', 1); -- goes to mlparted5_a
+insert into mlparted values (1, 45, 'b', 1); -- goes to mlparted5_b
+insert into mlparted values (1, 45, 'c', 1); -- goes to mlparted5_cd, fails
+insert into mlparted values (1, 45, 'f', 1); -- goes to mlparted5, fails
+select tableoid::regclass, * from mlparted order by a, b, c, d;
+alter table mlparted drop d;
+drop table mlparted5;
+
+-- check that message shown after failure to find a partition shows the
+-- appropriate key description (or none) in various situations
+create table key_desc (a int, b int) partition by list ((a+0));
+create table key_desc_1 partition of key_desc for values in (1) partition by range (b);
+
+create user regress_insert_other_user;
+grant select (a) on key_desc_1 to regress_insert_other_user;
+grant insert on key_desc to regress_insert_other_user;
+
+set role regress_insert_other_user;
+-- no key description is shown
+insert into key_desc values (1, 1);
+
+reset role;
+grant select (b) on key_desc_1 to regress_insert_other_user;
+set role regress_insert_other_user;
+-- key description (b)=(1) is now shown
+insert into key_desc values (1, 1);
+
+-- key description is not shown if key contains expression
+insert into key_desc values (2, 1);
+reset role;
+revoke all on key_desc from regress_insert_other_user;
+revoke all on key_desc_1 from regress_insert_other_user;
+drop role regress_insert_other_user;
+drop table key_desc, key_desc_1;
+
+-- test minvalue/maxvalue restrictions
+create table mcrparted (a int, b int, c int) partition by range (a, abs(b), c);
+create table mcrparted0 partition of mcrparted for values from (minvalue, 0, 0) to (1, maxvalue, maxvalue);
+create table mcrparted2 partition of mcrparted for values from (10, 6, minvalue) to (10, maxvalue, minvalue);
+create table mcrparted4 partition of mcrparted for values from (21, minvalue, 0) to (30, 20, minvalue);
+
+-- check multi-column range partitioning expression enforces the same
+-- constraint as what tuple-routing would determine it to be
+create table mcrparted0 partition of mcrparted for values from (minvalue, minvalue, minvalue) to (1, maxvalue, maxvalue);
+create table mcrparted1 partition of mcrparted for values from (2, 1, minvalue) to (10, 5, 10);
+create table mcrparted2 partition of mcrparted for values from (10, 6, minvalue) to (10, maxvalue, maxvalue);
+create table mcrparted3 partition of mcrparted for values from (11, 1, 1) to (20, 10, 10);
+create table mcrparted4 partition of mcrparted for values from (21, minvalue, minvalue) to (30, 20, maxvalue);
+create table mcrparted5 partition of mcrparted for values from (30, 21, 20) to (maxvalue, maxvalue, maxvalue);
+
+-- null not allowed in range partition
+insert into mcrparted values (null, null, null);
+
+-- routed to mcrparted0
+insert into mcrparted values (0, 1, 1);
+insert into mcrparted0 values (0, 1, 1);
+
+-- routed to mcparted1
+insert into mcrparted values (9, 1000, 1);
+insert into mcrparted1 values (9, 1000, 1);
+insert into mcrparted values (10, 5, -1);
+insert into mcrparted1 values (10, 5, -1);
+insert into mcrparted values (2, 1, 0);
+insert into mcrparted1 values (2, 1, 0);
+
+-- routed to mcparted2
+insert into mcrparted values (10, 6, 1000);
+insert into mcrparted2 values (10, 6, 1000);
+insert into mcrparted values (10, 1000, 1000);
+insert into mcrparted2 values (10, 1000, 1000);
+
+-- no partition exists, nor does mcrparted3 accept it
+insert into mcrparted values (11, 1, -1);
+insert into mcrparted3 values (11, 1, -1);
+
+-- routed to mcrparted5
+insert into mcrparted values (30, 21, 20);
+insert into mcrparted5 values (30, 21, 20);
+insert into mcrparted4 values (30, 21, 20); -- error
+
+-- check rows
+select tableoid::regclass::text, * from mcrparted order by 1;
+
+-- cleanup
+drop table mcrparted;
+
+-- check that a BR constraint can't make partition contain violating rows
+create table brtrigpartcon (a int, b text) partition by list (a);
+create table brtrigpartcon1 partition of brtrigpartcon for values in (1);
+create or replace function brtrigpartcon1trigf() returns trigger as $$begin new.a := 2; return new; end$$ language plpgsql;
+create trigger brtrigpartcon1trig before insert on brtrigpartcon1 for each row execute procedure brtrigpartcon1trigf();
+insert into brtrigpartcon values (1, 'hi there');
+insert into brtrigpartcon1 values (1, 'hi there');
+
+-- check that the message shows the appropriate column description in a
+-- situation where the partitioned table is not the primary ModifyTable node
+create table inserttest3 (f1 text default 'foo', f2 text default 'bar', f3 int);
+create role regress_coldesc_role;
+grant insert on inserttest3 to regress_coldesc_role;
+grant insert on brtrigpartcon to regress_coldesc_role;
+revoke select on brtrigpartcon from regress_coldesc_role;
+set role regress_coldesc_role;
+with result as (insert into brtrigpartcon values (1, 'hi there') returning 1)
+ insert into inserttest3 (f3) select * from result;
+reset role;
+
+-- cleanup
+revoke all on inserttest3 from regress_coldesc_role;
+revoke all on brtrigpartcon from regress_coldesc_role;
+drop role regress_coldesc_role;
+drop table inserttest3;
+drop table brtrigpartcon;
+drop function brtrigpartcon1trigf();
+
+-- check that "do nothing" BR triggers work with tuple-routing (this checks
+-- that estate->es_result_relation_info is appropriately set/reset for each
+-- routed tuple)
+create table donothingbrtrig_test (a int, b text) partition by list (a);
+create table donothingbrtrig_test1 (b text, a int);
+create table donothingbrtrig_test2 (c text, b text, a int);
+alter table donothingbrtrig_test2 drop column c;
+create or replace function donothingbrtrig_func() returns trigger as $$begin raise notice 'b: %', new.b; return NULL; end$$ language plpgsql;
+create trigger donothingbrtrig1 before insert on donothingbrtrig_test1 for each row execute procedure donothingbrtrig_func();
+create trigger donothingbrtrig2 before insert on donothingbrtrig_test2 for each row execute procedure donothingbrtrig_func();
+alter table donothingbrtrig_test attach partition donothingbrtrig_test1 for values in (1);
+alter table donothingbrtrig_test attach partition donothingbrtrig_test2 for values in (2);
+insert into donothingbrtrig_test values (1, 'foo'), (2, 'bar');
+copy donothingbrtrig_test from stdout;
+/*
+1 baz
+2 qux
+*/
+select tableoid::regclass, * from donothingbrtrig_test;
+
+-- cleanup
+drop table donothingbrtrig_test;
+drop function donothingbrtrig_func();
+
+-- check multi-column range partitioning with minvalue/maxvalue constraints
+create table mcrparted (a text, b int) partition by range(a, b);
+create table mcrparted1_lt_b partition of mcrparted for values from (minvalue, minvalue) to ('b', minvalue);
+create table mcrparted2_b partition of mcrparted for values from ('b', minvalue) to ('c', minvalue);
+create table mcrparted3_c_to_common partition of mcrparted for values from ('c', minvalue) to ('common', minvalue);
+create table mcrparted4_common_lt_0 partition of mcrparted for values from ('common', minvalue) to ('common', 0);
+create table mcrparted5_common_0_to_10 partition of mcrparted for values from ('common', 0) to ('common', 10);
+create table mcrparted6_common_ge_10 partition of mcrparted for values from ('common', 10) to ('common', maxvalue);
+create table mcrparted7_gt_common_lt_d partition of mcrparted for values from ('common', maxvalue) to ('d', minvalue);
+create table mcrparted8_ge_d partition of mcrparted for values from ('d', minvalue) to (maxvalue, maxvalue);
+
+
+insert into mcrparted values ('aaa', 0), ('b', 0), ('bz', 10), ('c', -10),
+ ('comm', -10), ('common', -10), ('common', 0), ('common', 10),
+ ('commons', 0), ('d', -10), ('e', 0);
+select tableoid::regclass, * from mcrparted order by a, b;
+drop table mcrparted;
+
+-- check that wholerow vars in the RETURNING list work with partitioned tables
+create table returningwrtest (a int) partition by list (a);
+create table returningwrtest1 partition of returningwrtest for values in (1);
+insert into returningwrtest values (1) returning returningwrtest;
+
+-- check also that the wholerow vars in RETURNING list are converted as needed
+alter table returningwrtest add b text;
+create table returningwrtest2 (b text, c int, a int);
+alter table returningwrtest2 drop c;
+alter table returningwrtest attach partition returningwrtest2 for values in (2);
+insert into returningwrtest values (2, 'foo') returning returningwrtest;
+drop table returningwrtest;
diff --git a/src/test/resources/postgresql-corpus/join.sql b/src/test/resources/postgresql-corpus/join.sql
new file mode 100644
index 0000000..1403e0f
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/join.sql
@@ -0,0 +1,2173 @@
+--
+-- JOIN
+-- Test JOIN clauses
+--
+
+CREATE TABLE J1_TBL (
+ i integer,
+ j integer,
+ t text
+);
+
+CREATE TABLE J2_TBL (
+ i integer,
+ k integer
+);
+
+
+INSERT INTO J1_TBL VALUES (1, 4, 'one');
+INSERT INTO J1_TBL VALUES (2, 3, 'two');
+INSERT INTO J1_TBL VALUES (3, 2, 'three');
+INSERT INTO J1_TBL VALUES (4, 1, 'four');
+INSERT INTO J1_TBL VALUES (5, 0, 'five');
+INSERT INTO J1_TBL VALUES (6, 6, 'six');
+INSERT INTO J1_TBL VALUES (7, 7, 'seven');
+INSERT INTO J1_TBL VALUES (8, 8, 'eight');
+INSERT INTO J1_TBL VALUES (0, NULL, 'zero');
+INSERT INTO J1_TBL VALUES (NULL, NULL, 'null');
+INSERT INTO J1_TBL VALUES (NULL, 0, 'zero');
+
+INSERT INTO J2_TBL VALUES (1, -1);
+INSERT INTO J2_TBL VALUES (2, 2);
+INSERT INTO J2_TBL VALUES (3, -3);
+INSERT INTO J2_TBL VALUES (2, 4);
+INSERT INTO J2_TBL VALUES (5, -5);
+INSERT INTO J2_TBL VALUES (5, -5);
+INSERT INTO J2_TBL VALUES (0, NULL);
+INSERT INTO J2_TBL VALUES (NULL, NULL);
+INSERT INTO J2_TBL VALUES (NULL, 0);
+
+-- useful in some tests below
+create temp table onerow();
+insert into onerow default values;
+analyze onerow;
+
+
+--
+-- CORRELATION NAMES
+-- Make sure that table/column aliases are supported
+-- before diving into more complex join syntax.
+--
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL AS tx;
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL tx;
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL AS t1 (a, b, c);
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL t1 (a, b, c);
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e);
+
+SELECT '' AS "xxx", t1.a, t2.e
+ FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e)
+ WHERE t1.a = t2.d;
+
+
+--
+-- CROSS JOIN
+-- Qualifications are not allowed on cross joins,
+-- which degenerate into a standard unqualified inner join.
+--
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL CROSS JOIN J2_TBL;
+
+-- ambiguous column
+SELECT '' AS "xxx", i, k, t
+ FROM J1_TBL CROSS JOIN J2_TBL;
+
+-- resolve previous ambiguity by specifying the table name
+SELECT '' AS "xxx", t1.i, k, t
+ FROM J1_TBL t1 CROSS JOIN J2_TBL t2;
+
+SELECT '' AS "xxx", ii, tt, kk
+ FROM (J1_TBL CROSS JOIN J2_TBL)
+ AS tx (ii, jj, tt, ii2, kk);
+
+SELECT '' AS "xxx", tx.ii, tx.jj, tx.kk
+ FROM (J1_TBL t1 (a, b, c) CROSS JOIN J2_TBL t2 (d, e))
+ AS tx (ii, jj, tt, ii2, kk);
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL CROSS JOIN J2_TBL a CROSS JOIN J2_TBL b;
+
+
+--
+--
+-- Inner joins (equi-joins)
+--
+--
+
+--
+-- Inner joins (equi-joins) with USING clause
+-- The USING syntax changes the shape of the resulting table
+-- by including a column in the USING clause only once in the result.
+--
+
+-- Inner equi-join on specified column
+SELECT '' AS "xxx", *
+ FROM J1_TBL INNER JOIN J2_TBL USING (i);
+
+-- Same as above, slightly different syntax
+SELECT '' AS "xxx", *
+ FROM J1_TBL JOIN J2_TBL USING (i);
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, d) USING (a)
+ ORDER BY a, d;
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, b) USING (b)
+ ORDER BY b, t1.a;
+
+
+--
+-- NATURAL JOIN
+-- Inner equi-join on all columns with the same name
+--
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL NATURAL JOIN J2_TBL;
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (a, d);
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (d, a);
+
+-- mismatch number of columns
+-- currently, Postgres will fill in with underlying names
+SELECT '' AS "xxx", *
+ FROM J1_TBL t1 (a, b) NATURAL JOIN J2_TBL t2 (a);
+
+
+--
+-- Inner joins (equi-joins)
+--
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.i);
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.k);
+
+
+--
+-- Non-equi-joins
+--
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i <= J2_TBL.k);
+
+
+--
+-- Outer joins
+-- Note that OUTER is a noise word
+--
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i)
+ ORDER BY i, k, t;
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL LEFT JOIN J2_TBL USING (i)
+ ORDER BY i, k, t;
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL RIGHT OUTER JOIN J2_TBL USING (i);
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL RIGHT JOIN J2_TBL USING (i);
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL FULL OUTER JOIN J2_TBL USING (i)
+ ORDER BY i, k, t;
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL FULL JOIN J2_TBL USING (i)
+ ORDER BY i, k, t;
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (k = 1);
+
+SELECT '' AS "xxx", *
+ FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (i = 1);
+
+--
+-- semijoin selectivity for <>
+--
+explain (costs off)
+select * from int4_tbl i4, tenk1 a
+where exists(select * from tenk1 b
+ where a.twothousand = b.twothousand and a.fivethous <> b.fivethous)
+ and i4.f1 = a.tenthous;
+
+
+--
+-- More complicated constructs
+--
+
+--
+-- Multiway full join
+--
+
+CREATE TABLE t1 (name TEXT, n INTEGER);
+CREATE TABLE t2 (name TEXT, n INTEGER);
+CREATE TABLE t3 (name TEXT, n INTEGER);
+
+INSERT INTO t1 VALUES ( 'bb', 11 );
+INSERT INTO t2 VALUES ( 'bb', 12 );
+INSERT INTO t2 VALUES ( 'cc', 22 );
+INSERT INTO t2 VALUES ( 'ee', 42 );
+INSERT INTO t3 VALUES ( 'bb', 13 );
+INSERT INTO t3 VALUES ( 'cc', 23 );
+INSERT INTO t3 VALUES ( 'dd', 33 );
+
+SELECT * FROM t1 FULL JOIN t2 USING (name) FULL JOIN t3 USING (name);
+
+--
+-- Test interactions of join syntax and subqueries
+--
+
+-- Basic cases (we expect planner to pull up the subquery here)
+SELECT * FROM
+(SELECT * FROM t2) as s2
+INNER JOIN
+(SELECT * FROM t3) s3
+USING (name);
+
+SELECT * FROM
+(SELECT * FROM t2) as s2
+LEFT JOIN
+(SELECT * FROM t3) s3
+USING (name);
+
+SELECT * FROM
+(SELECT * FROM t2) as s2
+FULL JOIN
+(SELECT * FROM t3) s3
+USING (name);
+
+-- Cases with non-nullable expressions in subquery results;
+-- make sure these go to null as expected
+SELECT * FROM
+(SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
+NATURAL INNER JOIN
+(SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
+
+SELECT * FROM
+(SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
+NATURAL LEFT JOIN
+(SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
+
+SELECT * FROM
+(SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
+NATURAL FULL JOIN
+(SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
+
+SELECT * FROM
+(SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
+NATURAL INNER JOIN
+(SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
+NATURAL INNER JOIN
+(SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
+
+SELECT * FROM
+(SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
+NATURAL FULL JOIN
+(SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
+NATURAL FULL JOIN
+(SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
+
+SELECT * FROM
+(SELECT name, n as s1_n FROM t1) as s1
+NATURAL FULL JOIN
+ (SELECT * FROM
+ (SELECT name, n as s2_n FROM t2) as s2
+ NATURAL FULL JOIN
+ (SELECT name, n as s3_n FROM t3) as s3
+ ) ss2;
+
+SELECT * FROM
+(SELECT name, n as s1_n FROM t1) as s1
+NATURAL FULL JOIN
+ (SELECT * FROM
+ (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
+ NATURAL FULL JOIN
+ (SELECT name, n as s3_n FROM t3) as s3
+ ) ss2;
+
+-- Constants as join keys can also be problematic
+SELECT * FROM
+ (SELECT name, n as s1_n FROM t1) as s1
+FULL JOIN
+ (SELECT name, 2 as s2_n FROM t2) as s2
+ON (s1_n = s2_n);
+
+
+-- Test for propagation of nullability constraints into sub-joins
+
+create temp table x (x1 int, x2 int);
+insert into x values (1,11);
+insert into x values (2,22);
+insert into x values (3,null);
+insert into x values (4,44);
+insert into x values (5,null);
+
+create temp table y (y1 int, y2 int);
+insert into y values (1,111);
+insert into y values (2,222);
+insert into y values (3,333);
+insert into y values (4,null);
+
+select * from x;
+select * from y;
+
+select * from x left join y on (x1 = y1 and x2 is not null);
+select * from x left join y on (x1 = y1 and y2 is not null);
+
+select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
+on (x1 = xx1);
+select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
+on (x1 = xx1 and x2 is not null);
+select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
+on (x1 = xx1 and y2 is not null);
+select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
+on (x1 = xx1 and xx2 is not null);
+-- these should NOT give the same answers as above
+select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
+on (x1 = xx1) where (x2 is not null);
+select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
+on (x1 = xx1) where (y2 is not null);
+select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
+on (x1 = xx1) where (xx2 is not null);
+
+--
+-- regression test: check for bug with propagation of implied equality
+-- to outside an IN
+--
+select count(*) from tenk1 a where unique1 in
+ (select unique1 from tenk1 b join tenk1 c using (unique1)
+ where b.unique2 = 42);
+
+--
+-- regression test: check for failure to generate a plan with multiple
+-- degenerate IN clauses
+--
+select count(*) from tenk1 x where
+ x.unique1 in (select a.f1 from int4_tbl a,float8_tbl b where a.f1=b.f1) and
+ x.unique1 = 0 and
+ x.unique1 in (select aa.f1 from int4_tbl aa,float8_tbl bb where aa.f1=bb.f1);
+
+-- try that with GEQO too
+begin;
+set geqo = on;
+set geqo_threshold = 2;
+select count(*) from tenk1 x where
+ x.unique1 in (select a.f1 from int4_tbl a,float8_tbl b where a.f1=b.f1) and
+ x.unique1 = 0 and
+ x.unique1 in (select aa.f1 from int4_tbl aa,float8_tbl bb where aa.f1=bb.f1);
+rollback;
+
+--
+-- regression test: be sure we cope with proven-dummy append rels
+--
+explain (costs off)
+select aa, bb, unique1, unique1
+ from tenk1 right join b on aa = unique1
+ where bb < bb and bb is null;
+
+select aa, bb, unique1, unique1
+ from tenk1 right join b on aa = unique1
+ where bb < bb and bb is null;
+
+--
+-- regression test: check handling of empty-FROM subquery underneath outer join
+--
+explain (costs off)
+select * from int8_tbl i1 left join (int8_tbl i2 join
+ (select 123 as x) ss on i2.q1 = x) on i1.q2 = i2.q2
+order by 1, 2;
+
+select * from int8_tbl i1 left join (int8_tbl i2 join
+ (select 123 as x) ss on i2.q1 = x) on i1.q2 = i2.q2
+order by 1, 2;
+
+--
+-- regression test: check a case where join_clause_is_movable_into() gives
+-- an imprecise result, causing an assertion failure
+--
+select count(*)
+from
+ (select t3.tenthous as x1, coalesce(t1.stringu1, t2.stringu1) as x2
+ from tenk1 t1
+ left join tenk1 t2 on t1.unique1 = t2.unique1
+ join tenk1 t3 on t1.unique2 = t3.unique2) ss,
+ tenk1 t4,
+ tenk1 t5
+where t4.thousand = t5.unique1 and ss.x1 = t4.tenthous and ss.x2 = t5.stringu1;
+
+--
+-- regression test: check a case where we formerly missed including an EC
+-- enforcement clause because it was expected to be handled at scan level
+--
+explain (costs off)
+select a.f1, b.f1, t.thousand, t.tenthous from
+ tenk1 t,
+ (select sum(f1)+1 as f1 from int4_tbl i4a) a,
+ (select sum(f1) as f1 from int4_tbl i4b) b
+where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
+
+select a.f1, b.f1, t.thousand, t.tenthous from
+ tenk1 t,
+ (select sum(f1)+1 as f1 from int4_tbl i4a) a,
+ (select sum(f1) as f1 from int4_tbl i4b) b
+where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
+
+--
+-- check a case where we formerly got confused by conflicting sort orders
+-- in redundant merge join path keys
+--
+explain (costs off)
+select * from
+ j1_tbl full join
+ (select * from j2_tbl order by j2_tbl.i desc, j2_tbl.k asc) j2_tbl
+ on j1_tbl.i = j2_tbl.i and j1_tbl.i = j2_tbl.k;
+
+select * from
+ j1_tbl full join
+ (select * from j2_tbl order by j2_tbl.i desc, j2_tbl.k asc) j2_tbl
+ on j1_tbl.i = j2_tbl.i and j1_tbl.i = j2_tbl.k;
+
+--
+-- a different check for handling of redundant sort keys in merge joins
+--
+explain (costs off)
+select count(*) from
+ (select * from tenk1 x order by x.thousand, x.twothousand, x.fivethous) x
+ left join
+ (select * from tenk1 y order by y.unique2) y
+ on x.thousand = y.unique2 and x.twothousand = y.hundred and x.fivethous = y.unique2;
+
+select count(*) from
+ (select * from tenk1 x order by x.thousand, x.twothousand, x.fivethous) x
+ left join
+ (select * from tenk1 y order by y.unique2) y
+ on x.thousand = y.unique2 and x.twothousand = y.hundred and x.fivethous = y.unique2;
+
+
+--
+-- Clean up
+--
+
+DROP TABLE t1;
+DROP TABLE t2;
+DROP TABLE t3;
+
+DROP TABLE J1_TBL;
+DROP TABLE J2_TBL;
+
+-- Both DELETE and UPDATE allow the specification of additional tables
+-- to "join" against to determine which rows should be modified.
+
+CREATE TEMP TABLE t1 (a int, b int);
+CREATE TEMP TABLE t2 (a int, b int);
+CREATE TEMP TABLE t3 (x int, y int);
+
+INSERT INTO t1 VALUES (5, 10);
+INSERT INTO t1 VALUES (15, 20);
+INSERT INTO t1 VALUES (100, 100);
+INSERT INTO t1 VALUES (200, 1000);
+INSERT INTO t2 VALUES (200, 2000);
+INSERT INTO t3 VALUES (5, 20);
+INSERT INTO t3 VALUES (6, 7);
+INSERT INTO t3 VALUES (7, 8);
+INSERT INTO t3 VALUES (500, 100);
+
+DELETE FROM t3 USING t1 table1 WHERE t3.x = table1.a;
+SELECT * FROM t3;
+DELETE FROM t3 USING t1 JOIN t2 USING (a) WHERE t3.x > t1.a;
+SELECT * FROM t3;
+DELETE FROM t3 USING t3 t3_other WHERE t3.x = t3_other.x AND t3.y = t3_other.y;
+SELECT * FROM t3;
+
+-- Test join against inheritance tree
+
+create temp table t2a () inherits (t2);
+
+insert into t2a values (200, 2001);
+
+select * from t1 left join t2 on (t1.a = t2.a);
+
+-- Test matching of column name with wrong alias
+
+select t1.x from t1 join t3 on (t1.a = t3.x);
+
+--
+-- regression test for 8.1 merge right join bug
+--
+
+CREATE TEMP TABLE tt1 ( tt1_id int4, joincol int4 );
+INSERT INTO tt1 VALUES (1, 11);
+INSERT INTO tt1 VALUES (2, NULL);
+
+CREATE TEMP TABLE tt2 ( tt2_id int4, joincol int4 );
+INSERT INTO tt2 VALUES (21, 11);
+INSERT INTO tt2 VALUES (22, 11);
+
+set enable_hashjoin to off;
+set enable_nestloop to off;
+
+-- these should give the same results
+
+select tt1.*, tt2.* from tt1 left join tt2 on tt1.joincol = tt2.joincol;
+
+select tt1.*, tt2.* from tt2 right join tt1 on tt1.joincol = tt2.joincol;
+
+reset enable_hashjoin;
+reset enable_nestloop;
+
+--
+-- regression test for bug #13908 (hash join with skew tuples & nbatch increase)
+--
+
+set work_mem to '64kB';
+set enable_mergejoin to off;
+
+explain (costs off)
+select count(*) from tenk1 a, tenk1 b
+ where a.hundred = b.thousand and (b.fivethous % 10) < 10;
+select count(*) from tenk1 a, tenk1 b
+ where a.hundred = b.thousand and (b.fivethous % 10) < 10;
+
+reset work_mem;
+reset enable_mergejoin;
+
+--
+-- regression test for 8.2 bug with improper re-ordering of left joins
+--
+
+create temp table tt3(f1 int, f2 text);
+insert into tt3 select x, repeat('xyzzy', 100) from generate_series(1,10000) x;
+create index tt3i on tt3(f1);
+analyze tt3;
+
+create temp table tt4(f1 int);
+insert into tt4 values (0),(1),(9999);
+analyze tt4;
+
+SELECT a.f1
+FROM tt4 a
+LEFT JOIN (
+ SELECT b.f1
+ FROM tt3 b LEFT JOIN tt3 c ON (b.f1 = c.f1)
+ WHERE c.f1 IS NULL
+) AS d ON (a.f1 = d.f1)
+WHERE d.f1 IS NULL;
+
+--
+-- regression test for proper handling of outer joins within antijoins
+--
+
+create temp table tt4x(c1 int, c2 int, c3 int);
+
+explain (costs off)
+select * from tt4x t1
+where not exists (
+ select 1 from tt4x t2
+ left join tt4x t3 on t2.c3 = t3.c1
+ left join ( select t5.c1 as c1
+ from tt4x t4 left join tt4x t5 on t4.c2 = t5.c1
+ ) a1 on t3.c2 = a1.c1
+ where t1.c1 = t2.c2
+);
+
+--
+-- regression test for problems of the sort depicted in bug #3494
+--
+
+create temp table tt5(f1 int, f2 int);
+create temp table tt6(f1 int, f2 int);
+
+insert into tt5 values(1, 10);
+insert into tt5 values(1, 11);
+
+insert into tt6 values(1, 9);
+insert into tt6 values(1, 2);
+insert into tt6 values(2, 9);
+
+select * from tt5,tt6 where tt5.f1 = tt6.f1 and tt5.f1 = tt5.f2 - tt6.f2;
+
+--
+-- regression test for problems of the sort depicted in bug #3588
+--
+
+create temp table xx (pkxx int);
+create temp table yy (pkyy int, pkxx int);
+
+insert into xx values (1);
+insert into xx values (2);
+insert into xx values (3);
+
+insert into yy values (101, 1);
+insert into yy values (201, 2);
+insert into yy values (301, NULL);
+
+select yy.pkyy as yy_pkyy, yy.pkxx as yy_pkxx, yya.pkyy as yya_pkyy,
+ xxa.pkxx as xxa_pkxx, xxb.pkxx as xxb_pkxx
+from yy
+ left join (SELECT * FROM yy where pkyy = 101) as yya ON yy.pkyy = yya.pkyy
+ left join xx xxa on yya.pkxx = xxa.pkxx
+ left join xx xxb on coalesce (xxa.pkxx, 1) = xxb.pkxx;
+
+--
+-- regression test for improper pushing of constants across outer-join clauses
+-- (as seen in early 8.2.x releases)
+--
+
+create temp table zt1 (f1 int primary key);
+create temp table zt2 (f2 int primary key);
+create temp table zt3 (f3 int primary key);
+insert into zt1 values(53);
+insert into zt2 values(53);
+
+select * from
+ zt2 left join zt3 on (f2 = f3)
+ left join zt1 on (f3 = f1)
+where f2 = 53;
+
+create temp view zv1 as select *,'dummy'::text AS junk from zt1;
+
+select * from
+ zt2 left join zt3 on (f2 = f3)
+ left join zv1 on (f3 = f1)
+where f2 = 53;
+
+--
+-- regression test for improper extraction of OR indexqual conditions
+-- (as seen in early 8.3.x releases)
+--
+
+select a.unique2, a.ten, b.tenthous, b.unique2, b.hundred
+from tenk1 a left join tenk1 b on a.unique2 = b.tenthous
+where a.unique1 = 42 and
+ ((b.unique2 is null and a.ten = 2) or b.hundred = 3);
+
+--
+-- test proper positioning of one-time quals in EXISTS (8.4devel bug)
+--
+prepare foo(bool) as
+ select count(*) from tenk1 a left join tenk1 b
+ on (a.unique2 = b.unique1 and exists
+ (select 1 from tenk1 c where c.thousand = b.unique2 and $1));
+execute foo(true);
+execute foo(false);
+
+--
+-- test for sane behavior with noncanonical merge clauses, per bug #4926
+--
+
+begin;
+
+set enable_mergejoin = 1;
+set enable_hashjoin = 0;
+set enable_nestloop = 0;
+
+create temp table a (i integer);
+create temp table b (x integer, y integer);
+
+select * from a left join b on i = x and i = y and x = i;
+
+rollback;
+
+--
+-- test handling of merge clauses using record_ops
+--
+begin;
+
+create type mycomptype as (id int, v bigint);
+
+create temp table tidv (idv mycomptype);
+create index on tidv (idv);
+
+explain (costs off)
+select a.idv, b.idv from tidv a, tidv b where a.idv = b.idv;
+
+set enable_mergejoin = 0;
+
+explain (costs off)
+select a.idv, b.idv from tidv a, tidv b where a.idv = b.idv;
+
+rollback;
+
+--
+-- test NULL behavior of whole-row Vars, per bug #5025
+--
+select t1.q2, count(t2.*)
+from int8_tbl t1 left join int8_tbl t2 on (t1.q2 = t2.q1)
+group by t1.q2 order by 1;
+
+select t1.q2, count(t2.*)
+from int8_tbl t1 left join (select * from int8_tbl) t2 on (t1.q2 = t2.q1)
+group by t1.q2 order by 1;
+
+select t1.q2, count(t2.*)
+from int8_tbl t1 left join (select * from int8_tbl offset 0) t2 on (t1.q2 = t2.q1)
+group by t1.q2 order by 1;
+
+select t1.q2, count(t2.*)
+from int8_tbl t1 left join
+ (select q1, case when q2=1 then 1 else q2 end as q2 from int8_tbl) t2
+ on (t1.q2 = t2.q1)
+group by t1.q2 order by 1;
+
+--
+-- test incorrect failure to NULL pulled-up subexpressions
+--
+begin;
+
+create temp table a (
+ code char not null,
+ constraint a_pk primary key (code)
+);
+create temp table b (
+ a char not null,
+ num integer not null,
+ constraint b_pk primary key (a, num)
+);
+create temp table c (
+ name char not null,
+ a char,
+ constraint c_pk primary key (name)
+);
+
+insert into a (code) values ('p');
+insert into a (code) values ('q');
+insert into b (a, num) values ('p', 1);
+insert into b (a, num) values ('p', 2);
+insert into c (name, a) values ('A', 'p');
+insert into c (name, a) values ('B', 'q');
+insert into c (name, a) values ('C', null);
+
+select c.name, ss.code, ss.b_cnt, ss.const
+from c left join
+ (select a.code, coalesce(b_grp.cnt, 0) as b_cnt, -1 as const
+ from a left join
+ (select count(1) as cnt, b.a from b group by b.a) as b_grp
+ on a.code = b_grp.a
+ ) as ss
+ on (c.a = ss.code)
+order by c.name;
+
+rollback;
+
+--
+-- test incorrect handling of placeholders that only appear in targetlists,
+-- per bug #6154
+--
+SELECT * FROM
+( SELECT 1 as key1 ) sub1
+LEFT JOIN
+( SELECT sub3.key3, sub4.value2, COALESCE(sub4.value2, 66) as value3 FROM
+ ( SELECT 1 as key3 ) sub3
+ LEFT JOIN
+ ( SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2 FROM
+ ( SELECT 1 as key5 ) sub5
+ LEFT JOIN
+ ( SELECT 2 as key6, 42 as value1 ) sub6
+ ON sub5.key5 = sub6.key6
+ ) sub4
+ ON sub4.key5 = sub3.key3
+) sub2
+ON sub1.key1 = sub2.key3;
+
+-- test the path using join aliases, too
+SELECT * FROM
+( SELECT 1 as key1 ) sub1
+LEFT JOIN
+( SELECT sub3.key3, value2, COALESCE(value2, 66) as value3 FROM
+ ( SELECT 1 as key3 ) sub3
+ LEFT JOIN
+ ( SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2 FROM
+ ( SELECT 1 as key5 ) sub5
+ LEFT JOIN
+ ( SELECT 2 as key6, 42 as value1 ) sub6
+ ON sub5.key5 = sub6.key6
+ ) sub4
+ ON sub4.key5 = sub3.key3
+) sub2
+ON sub1.key1 = sub2.key3;
+
+--
+-- test case where a PlaceHolderVar is used as a nestloop parameter
+--
+
+EXPLAIN (COSTS OFF)
+SELECT qq, unique1
+ FROM
+ ( SELECT COALESCE(q1, 0) AS qq FROM int8_tbl a ) AS ss1
+ FULL OUTER JOIN
+ ( SELECT COALESCE(q2, -1) AS qq FROM int8_tbl b ) AS ss2
+ USING (qq)
+ INNER JOIN tenk1 c ON qq = unique2;
+
+SELECT qq, unique1
+ FROM
+ ( SELECT COALESCE(q1, 0) AS qq FROM int8_tbl a ) AS ss1
+ FULL OUTER JOIN
+ ( SELECT COALESCE(q2, -1) AS qq FROM int8_tbl b ) AS ss2
+ USING (qq)
+ INNER JOIN tenk1 c ON qq = unique2;
+
+--
+-- nested nestloops can require nested PlaceHolderVars
+--
+
+create temp table nt1 (
+ id int primary key,
+ a1 boolean,
+ a2 boolean
+);
+create temp table nt2 (
+ id int primary key,
+ nt1_id int,
+ b1 boolean,
+ b2 boolean,
+ foreign key (nt1_id) references nt1(id)
+);
+create temp table nt3 (
+ id int primary key,
+ nt2_id int,
+ c1 boolean,
+ foreign key (nt2_id) references nt2(id)
+);
+
+insert into nt1 values (1,true,true);
+insert into nt1 values (2,true,false);
+insert into nt1 values (3,false,false);
+insert into nt2 values (1,1,true,true);
+insert into nt2 values (2,2,true,false);
+insert into nt2 values (3,3,false,false);
+insert into nt3 values (1,1,true);
+insert into nt3 values (2,2,false);
+insert into nt3 values (3,3,true);
+
+explain (costs off)
+select nt3.id
+from nt3 as nt3
+ left join
+ (select nt2.*, (nt2.b1 and ss1.a3) AS b3
+ from nt2 as nt2
+ left join
+ (select nt1.*, (nt1.id is not null) as a3 from nt1) as ss1
+ on ss1.id = nt2.nt1_id
+ ) as ss2
+ on ss2.id = nt3.nt2_id
+where nt3.id = 1 and ss2.b3;
+
+select nt3.id
+from nt3 as nt3
+ left join
+ (select nt2.*, (nt2.b1 and ss1.a3) AS b3
+ from nt2 as nt2
+ left join
+ (select nt1.*, (nt1.id is not null) as a3 from nt1) as ss1
+ on ss1.id = nt2.nt1_id
+ ) as ss2
+ on ss2.id = nt3.nt2_id
+where nt3.id = 1 and ss2.b3;
+
+--
+-- test case where a PlaceHolderVar is propagated into a subquery
+--
+
+explain (costs off)
+select * from
+ int8_tbl t1 left join
+ (select q1 as x, 42 as y from int8_tbl t2) ss
+ on t1.q2 = ss.x
+where
+ 1 = (select 1 from int8_tbl t3 where ss.y is not null limit 1)
+order by 1,2;
+
+select * from
+ int8_tbl t1 left join
+ (select q1 as x, 42 as y from int8_tbl t2) ss
+ on t1.q2 = ss.x
+where
+ 1 = (select 1 from int8_tbl t3 where ss.y is not null limit 1)
+order by 1,2;
+
+--
+-- test the corner cases FULL JOIN ON TRUE and FULL JOIN ON FALSE
+--
+select * from int4_tbl a full join int4_tbl b on true;
+select * from int4_tbl a full join int4_tbl b on false;
+
+--
+-- test for ability to use a cartesian join when necessary
+--
+
+create temp table q1 as select 1 as q1;
+create temp table q2 as select 0 as q2;
+analyze q1;
+analyze q2;
+
+explain (costs off)
+select * from
+ tenk1 join int4_tbl on f1 = twothousand,
+ q1, q2
+where q1 = thousand or q2 = thousand;
+
+explain (costs off)
+select * from
+ tenk1 join int4_tbl on f1 = twothousand,
+ q1, q2
+where thousand = (q1 + q2);
+
+--
+-- test ability to generate a suitable plan for a star-schema query
+--
+
+explain (costs off)
+select * from
+ tenk1, int8_tbl a, int8_tbl b
+where thousand = a.q1 and tenthous = b.q1 and a.q2 = 1 and b.q2 = 2;
+
+--
+-- test a corner case in which we shouldn't apply the star-schema optimization
+--
+
+explain (costs off)
+select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
+ tenk1 t1
+ inner join int4_tbl i1
+ left join (select v1.x2, v2.y1, 11 AS d1
+ from (select 1,0 from onerow) v1(x1,x2)
+ left join (select 3,1 from onerow) v2(y1,y2)
+ on v1.x1 = v2.y2) subq1
+ on (i1.f1 = subq1.x2)
+ on (t1.unique2 = subq1.d1)
+ left join tenk1 t2
+ on (subq1.y1 = t2.unique1)
+where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
+
+select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
+ tenk1 t1
+ inner join int4_tbl i1
+ left join (select v1.x2, v2.y1, 11 AS d1
+ from (select 1,0 from onerow) v1(x1,x2)
+ left join (select 3,1 from onerow) v2(y1,y2)
+ on v1.x1 = v2.y2) subq1
+ on (i1.f1 = subq1.x2)
+ on (t1.unique2 = subq1.d1)
+ left join tenk1 t2
+ on (subq1.y1 = t2.unique1)
+where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
+
+-- variant that isn't quite a star-schema case
+
+select ss1.d1 from
+ tenk1 as t1
+ inner join tenk1 as t2
+ on t1.tenthous = t2.ten
+ inner join
+ int8_tbl as i8
+ left join int4_tbl as i4
+ inner join (select 64::information_schema.cardinal_number as d1
+ from tenk1 t3,
+ lateral (select abs(t3.unique1) + random()) ss0(x)
+ where t3.fivethous < 0) as ss1
+ on i4.f1 = ss1.d1
+ on i8.q1 = i4.f1
+ on t1.tenthous = ss1.d1
+where t1.unique1 < i4.f1;
+
+-- this variant is foldable by the remove-useless-RESULT-RTEs code
+
+explain (costs off)
+select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
+ tenk1 t1
+ inner join int4_tbl i1
+ left join (select v1.x2, v2.y1, 11 AS d1
+ from (values(1,0)) v1(x1,x2)
+ left join (values(3,1)) v2(y1,y2)
+ on v1.x1 = v2.y2) subq1
+ on (i1.f1 = subq1.x2)
+ on (t1.unique2 = subq1.d1)
+ left join tenk1 t2
+ on (subq1.y1 = t2.unique1)
+where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
+
+select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
+ tenk1 t1
+ inner join int4_tbl i1
+ left join (select v1.x2, v2.y1, 11 AS d1
+ from (values(1,0)) v1(x1,x2)
+ left join (values(3,1)) v2(y1,y2)
+ on v1.x1 = v2.y2) subq1
+ on (i1.f1 = subq1.x2)
+ on (t1.unique2 = subq1.d1)
+ left join tenk1 t2
+ on (subq1.y1 = t2.unique1)
+where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
+
+-- Here's a variant that we can't fold too aggressively, though,
+-- or we end up with noplace to evaluate the lateral PHV
+explain (verbose, costs off)
+select * from
+ (select 1 as x) ss1 left join (select 2 as y) ss2 on (true),
+ lateral (select ss2.y as z limit 1) ss3;
+select * from
+ (select 1 as x) ss1 left join (select 2 as y) ss2 on (true),
+ lateral (select ss2.y as z limit 1) ss3;
+
+--
+-- test inlining of immutable functions
+--
+create function f_immutable_int4(i integer) returns integer as
+$$ begin return i; end; $$ language plpgsql immutable;
+
+-- check optimization of function scan with join
+explain (costs off)
+select unique1 from tenk1, (select * from f_immutable_int4(1) x) x
+where x = unique1;
+
+explain (verbose, costs off)
+select unique1, x.*
+from tenk1, (select *, random() from f_immutable_int4(1) x) x
+where x = unique1;
+
+explain (costs off)
+select unique1 from tenk1, f_immutable_int4(1) x where x = unique1;
+
+explain (costs off)
+select unique1 from tenk1, lateral f_immutable_int4(1) x where x = unique1;
+
+explain (costs off)
+select unique1, x from tenk1 join f_immutable_int4(1) x on unique1 = x;
+
+explain (costs off)
+select unique1, x from tenk1 left join f_immutable_int4(1) x on unique1 = x;
+
+explain (costs off)
+select unique1, x from tenk1 right join f_immutable_int4(1) x on unique1 = x;
+
+explain (costs off)
+select unique1, x from tenk1 full join f_immutable_int4(1) x on unique1 = x;
+
+-- check that pullup of a const function allows further const-folding
+explain (costs off)
+select unique1 from tenk1, f_immutable_int4(1) x where x = 42;
+
+-- test inlining of immutable functions with PlaceHolderVars
+explain (costs off)
+select nt3.id
+from nt3 as nt3
+ left join
+ (select nt2.*, (nt2.b1 or i4 = 42) AS b3
+ from nt2 as nt2
+ left join
+ f_immutable_int4(0) i4
+ on i4 = nt2.nt1_id
+ ) as ss2
+ on ss2.id = nt3.nt2_id
+where nt3.id = 1 and ss2.b3;
+
+drop function f_immutable_int4(int);
+
+-- test inlining when function returns composite
+
+create function mki8(bigint, bigint) returns int8_tbl as
+$$select row($1,$2)::int8_tbl$$ language sql;
+
+create function mki4(int) returns int4_tbl as
+$$select row($1)::int4_tbl$$ language sql;
+
+explain (verbose, costs off)
+select * from mki8(1,2);
+select * from mki8(1,2);
+
+explain (verbose, costs off)
+select * from mki4(42);
+select * from mki4(42);
+
+drop function mki8(bigint, bigint);
+drop function mki4(int);
+
+--
+-- test extraction of restriction OR clauses from join OR clause
+-- (we used to only do this for indexable clauses)
+--
+
+explain (costs off)
+select * from tenk1 a join tenk1 b on
+ (a.unique1 = 1 and b.unique1 = 2) or (a.unique2 = 3 and b.hundred = 4);
+explain (costs off)
+select * from tenk1 a join tenk1 b on
+ (a.unique1 = 1 and b.unique1 = 2) or (a.unique2 = 3 and b.ten = 4);
+explain (costs off)
+select * from tenk1 a join tenk1 b on
+ (a.unique1 = 1 and b.unique1 = 2) or
+ ((a.unique2 = 3 or a.unique2 = 7) and b.hundred = 4);
+
+--
+-- test placement of movable quals in a parameterized join tree
+--
+
+explain (costs off)
+select * from tenk1 t1 left join
+ (tenk1 t2 join tenk1 t3 on t2.thousand = t3.unique2)
+ on t1.hundred = t2.hundred and t1.ten = t3.ten
+where t1.unique1 = 1;
+
+explain (costs off)
+select * from tenk1 t1 left join
+ (tenk1 t2 join tenk1 t3 on t2.thousand = t3.unique2)
+ on t1.hundred = t2.hundred and t1.ten + t2.ten = t3.ten
+where t1.unique1 = 1;
+
+explain (costs off)
+select count(*) from
+ tenk1 a join tenk1 b on a.unique1 = b.unique2
+ left join tenk1 c on a.unique2 = b.unique1 and c.thousand = a.thousand
+ join int4_tbl on b.thousand = f1;
+
+select count(*) from
+ tenk1 a join tenk1 b on a.unique1 = b.unique2
+ left join tenk1 c on a.unique2 = b.unique1 and c.thousand = a.thousand
+ join int4_tbl on b.thousand = f1;
+
+explain (costs off)
+select b.unique1 from
+ tenk1 a join tenk1 b on a.unique1 = b.unique2
+ left join tenk1 c on b.unique1 = 42 and c.thousand = a.thousand
+ join int4_tbl i1 on b.thousand = f1
+ right join int4_tbl i2 on i2.f1 = b.tenthous
+ order by 1;
+
+select b.unique1 from
+ tenk1 a join tenk1 b on a.unique1 = b.unique2
+ left join tenk1 c on b.unique1 = 42 and c.thousand = a.thousand
+ join int4_tbl i1 on b.thousand = f1
+ right join int4_tbl i2 on i2.f1 = b.tenthous
+ order by 1;
+
+explain (costs off)
+select * from
+(
+ select unique1, q1, coalesce(unique1, -1) + q1 as fault
+ from int8_tbl left join tenk1 on (q2 = unique2)
+) ss
+where fault = 122
+order by fault;
+
+select * from
+(
+ select unique1, q1, coalesce(unique1, -1) + q1 as fault
+ from int8_tbl left join tenk1 on (q2 = unique2)
+) ss
+where fault = 122
+order by fault;
+
+explain (costs off)
+select * from
+(values (1, array[10,20]), (2, array[20,30])) as v1(v1x,v1ys)
+left join (values (1, 10), (2, 20)) as v2(v2x,v2y) on v2x = v1x
+left join unnest(v1ys) as u1(u1y) on u1y = v2y;
+
+select * from
+(values (1, array[10,20]), (2, array[20,30])) as v1(v1x,v1ys)
+left join (values (1, 10), (2, 20)) as v2(v2x,v2y) on v2x = v1x
+left join unnest(v1ys) as u1(u1y) on u1y = v2y;
+
+--
+-- test handling of potential equivalence clauses above outer joins
+--
+
+explain (costs off)
+select q1, unique2, thousand, hundred
+ from int8_tbl a left join tenk1 b on q1 = unique2
+ where coalesce(thousand,123) = q1 and q1 = coalesce(hundred,123);
+
+select q1, unique2, thousand, hundred
+ from int8_tbl a left join tenk1 b on q1 = unique2
+ where coalesce(thousand,123) = q1 and q1 = coalesce(hundred,123);
+
+explain (costs off)
+select f1, unique2, case when unique2 is null then f1 else 0 end
+ from int4_tbl a left join tenk1 b on f1 = unique2
+ where (case when unique2 is null then f1 else 0 end) = 0;
+
+select f1, unique2, case when unique2 is null then f1 else 0 end
+ from int4_tbl a left join tenk1 b on f1 = unique2
+ where (case when unique2 is null then f1 else 0 end) = 0;
+
+--
+-- another case with equivalence clauses above outer joins (bug #8591)
+--
+
+explain (costs off)
+select a.unique1, b.unique1, c.unique1, coalesce(b.twothousand, a.twothousand)
+ from tenk1 a left join tenk1 b on b.thousand = a.unique1 left join tenk1 c on c.unique2 = coalesce(b.twothousand, a.twothousand)
+ where a.unique2 < 10 and coalesce(b.twothousand, a.twothousand) = 44;
+
+select a.unique1, b.unique1, c.unique1, coalesce(b.twothousand, a.twothousand)
+ from tenk1 a left join tenk1 b on b.thousand = a.unique1 left join tenk1 c on c.unique2 = coalesce(b.twothousand, a.twothousand)
+ where a.unique2 < 10 and coalesce(b.twothousand, a.twothousand) = 44;
+
+--
+-- check handling of join aliases when flattening multiple levels of subquery
+--
+
+explain (verbose, costs off)
+select foo1.join_key as foo1_id, foo3.join_key AS foo3_id, bug_field from
+ (values (0),(1)) foo1(join_key)
+left join
+ (select join_key, bug_field from
+ (select ss1.join_key, ss1.bug_field from
+ (select f1 as join_key, 666 as bug_field from int4_tbl i1) ss1
+ ) foo2
+ left join
+ (select unique2 as join_key from tenk1 i2) ss2
+ using (join_key)
+ ) foo3
+using (join_key);
+
+select foo1.join_key as foo1_id, foo3.join_key AS foo3_id, bug_field from
+ (values (0),(1)) foo1(join_key)
+left join
+ (select join_key, bug_field from
+ (select ss1.join_key, ss1.bug_field from
+ (select f1 as join_key, 666 as bug_field from int4_tbl i1) ss1
+ ) foo2
+ left join
+ (select unique2 as join_key from tenk1 i2) ss2
+ using (join_key)
+ ) foo3
+using (join_key);
+
+--
+-- test successful handling of nested outer joins with degenerate join quals
+--
+
+explain (verbose, costs off)
+select t1.* from
+ text_tbl t1
+ left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
+ left join int8_tbl i8
+ left join (select *, null::int as d2 from int8_tbl i8b2) b2
+ on (i8.q1 = b2.q1)
+ on (b2.d2 = b1.q2)
+ on (t1.f1 = b1.d1)
+ left join int4_tbl i4
+ on (i8.q2 = i4.f1);
+
+select t1.* from
+ text_tbl t1
+ left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
+ left join int8_tbl i8
+ left join (select *, null::int as d2 from int8_tbl i8b2) b2
+ on (i8.q1 = b2.q1)
+ on (b2.d2 = b1.q2)
+ on (t1.f1 = b1.d1)
+ left join int4_tbl i4
+ on (i8.q2 = i4.f1);
+
+explain (verbose, costs off)
+select t1.* from
+ text_tbl t1
+ left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
+ left join int8_tbl i8
+ left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2) b2
+ on (i8.q1 = b2.q1)
+ on (b2.d2 = b1.q2)
+ on (t1.f1 = b1.d1)
+ left join int4_tbl i4
+ on (i8.q2 = i4.f1);
+
+select t1.* from
+ text_tbl t1
+ left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
+ left join int8_tbl i8
+ left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2) b2
+ on (i8.q1 = b2.q1)
+ on (b2.d2 = b1.q2)
+ on (t1.f1 = b1.d1)
+ left join int4_tbl i4
+ on (i8.q2 = i4.f1);
+
+explain (verbose, costs off)
+select t1.* from
+ text_tbl t1
+ left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
+ left join int8_tbl i8
+ left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2
+ where q1 = f1) b2
+ on (i8.q1 = b2.q1)
+ on (b2.d2 = b1.q2)
+ on (t1.f1 = b1.d1)
+ left join int4_tbl i4
+ on (i8.q2 = i4.f1);
+
+select t1.* from
+ text_tbl t1
+ left join (select *, '***'::text as d1 from int8_tbl i8b1) b1
+ left join int8_tbl i8
+ left join (select *, null::int as d2 from int8_tbl i8b2, int4_tbl i4b2
+ where q1 = f1) b2
+ on (i8.q1 = b2.q1)
+ on (b2.d2 = b1.q2)
+ on (t1.f1 = b1.d1)
+ left join int4_tbl i4
+ on (i8.q2 = i4.f1);
+
+explain (verbose, costs off)
+select * from
+ text_tbl t1
+ inner join int8_tbl i8
+ on i8.q2 = 456
+ right join text_tbl t2
+ on t1.f1 = 'doh!'
+ left join int4_tbl i4
+ on i8.q1 = i4.f1;
+
+select * from
+ text_tbl t1
+ inner join int8_tbl i8
+ on i8.q2 = 456
+ right join text_tbl t2
+ on t1.f1 = 'doh!'
+ left join int4_tbl i4
+ on i8.q1 = i4.f1;
+
+--
+-- test for appropriate join order in the presence of lateral references
+--
+
+explain (verbose, costs off)
+select * from
+ text_tbl t1
+ left join int8_tbl i8
+ on i8.q2 = 123,
+ lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss
+where t1.f1 = ss.f1;
+
+select * from
+ text_tbl t1
+ left join int8_tbl i8
+ on i8.q2 = 123,
+ lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss
+where t1.f1 = ss.f1;
+
+explain (verbose, costs off)
+select * from
+ text_tbl t1
+ left join int8_tbl i8
+ on i8.q2 = 123,
+ lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss1,
+ lateral (select ss1.* from text_tbl t3 limit 1) as ss2
+where t1.f1 = ss2.f1;
+
+select * from
+ text_tbl t1
+ left join int8_tbl i8
+ on i8.q2 = 123,
+ lateral (select i8.q1, t2.f1 from text_tbl t2 limit 1) as ss1,
+ lateral (select ss1.* from text_tbl t3 limit 1) as ss2
+where t1.f1 = ss2.f1;
+
+explain (verbose, costs off)
+select 1 from
+ text_tbl as tt1
+ inner join text_tbl as tt2 on (tt1.f1 = 'foo')
+ left join text_tbl as tt3 on (tt3.f1 = 'foo')
+ left join text_tbl as tt4 on (tt3.f1 = tt4.f1),
+ lateral (select tt4.f1 as c0 from text_tbl as tt5 limit 1) as ss1
+where tt1.f1 = ss1.c0;
+
+select 1 from
+ text_tbl as tt1
+ inner join text_tbl as tt2 on (tt1.f1 = 'foo')
+ left join text_tbl as tt3 on (tt3.f1 = 'foo')
+ left join text_tbl as tt4 on (tt3.f1 = tt4.f1),
+ lateral (select tt4.f1 as c0 from text_tbl as tt5 limit 1) as ss1
+where tt1.f1 = ss1.c0;
+
+--
+-- check a case in which a PlaceHolderVar forces join order
+--
+
+explain (verbose, costs off)
+select ss2.* from
+ int4_tbl i41
+ left join int8_tbl i8
+ join (select i42.f1 as c1, i43.f1 as c2, 42 as c3
+ from int4_tbl i42, int4_tbl i43) ss1
+ on i8.q1 = ss1.c2
+ on i41.f1 = ss1.c1,
+ lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2
+where ss1.c2 = 0;
+
+select ss2.* from
+ int4_tbl i41
+ left join int8_tbl i8
+ join (select i42.f1 as c1, i43.f1 as c2, 42 as c3
+ from int4_tbl i42, int4_tbl i43) ss1
+ on i8.q1 = ss1.c2
+ on i41.f1 = ss1.c1,
+ lateral (select i41.*, i8.*, ss1.* from text_tbl limit 1) ss2
+where ss1.c2 = 0;
+
+--
+-- test successful handling of full join underneath left join (bug #14105)
+--
+
+explain (costs off)
+select * from
+ (select 1 as id) as xx
+ left join
+ (tenk1 as a1 full join (select 1 as id) as yy on (a1.unique1 = yy.id))
+ on (xx.id = coalesce(yy.id));
+
+select * from
+ (select 1 as id) as xx
+ left join
+ (tenk1 as a1 full join (select 1 as id) as yy on (a1.unique1 = yy.id))
+ on (xx.id = coalesce(yy.id));
+
+--
+-- test ability to push constants through outer join clauses
+--
+
+explain (costs off)
+ select * from int4_tbl a left join tenk1 b on f1 = unique2 where f1 = 0;
+
+explain (costs off)
+ select * from tenk1 a full join tenk1 b using(unique2) where unique2 = 42;
+
+--
+-- test that quals attached to an outer join have correct semantics,
+-- specifically that they don't re-use expressions computed below the join;
+-- we force a mergejoin so that coalesce(b.q1, 1) appears as a join input
+--
+
+set enable_hashjoin to off;
+set enable_nestloop to off;
+
+explain (verbose, costs off)
+ select a.q2, b.q1
+ from int8_tbl a left join int8_tbl b on a.q2 = coalesce(b.q1, 1)
+ where coalesce(b.q1, 1) > 0;
+select a.q2, b.q1
+ from int8_tbl a left join int8_tbl b on a.q2 = coalesce(b.q1, 1)
+ where coalesce(b.q1, 1) > 0;
+
+reset enable_hashjoin;
+reset enable_nestloop;
+
+--
+-- test join removal
+--
+
+begin;
+
+CREATE TEMP TABLE a (id int PRIMARY KEY, b_id int);
+CREATE TEMP TABLE b (id int PRIMARY KEY, c_id int);
+CREATE TEMP TABLE c (id int PRIMARY KEY);
+CREATE TEMP TABLE d (a int, b int);
+INSERT INTO a VALUES (0, 0), (1, NULL);
+INSERT INTO b VALUES (0, 0), (1, NULL);
+INSERT INTO c VALUES (0), (1);
+INSERT INTO d VALUES (1,3), (2,2), (3,1);
+
+-- all three cases should be optimizable into a simple seqscan
+explain (costs off) SELECT a.* FROM a LEFT JOIN b ON a.b_id = b.id;
+explain (costs off) SELECT b.* FROM b LEFT JOIN c ON b.c_id = c.id;
+explain (costs off)
+ SELECT a.* FROM a LEFT JOIN (b left join c on b.c_id = c.id)
+ ON (a.b_id = b.id);
+
+-- check optimization of outer join within another special join
+explain (costs off)
+select id from a where id in (
+ select b.id from b left join c on b.id = c.id
+);
+
+-- check that join removal works for a left join when joining a subquery
+-- that is guaranteed to be unique by its GROUP BY clause
+explain (costs off)
+select d.* from d left join (select * from b group by b.id, b.c_id) s
+ on d.a = s.id and d.b = s.c_id;
+
+-- similarly, but keying off a DISTINCT clause
+explain (costs off)
+select d.* from d left join (select distinct * from b) s
+ on d.a = s.id and d.b = s.c_id;
+
+-- join removal is not possible when the GROUP BY contains a column that is
+-- not in the join condition. (Note: as of 9.6, we notice that b.id is a
+-- primary key and so drop b.c_id from the GROUP BY of the resulting plan;
+-- but this happens too late for join removal in the outer plan level.)
+explain (costs off)
+select d.* from d left join (select * from b group by b.id, b.c_id) s
+ on d.a = s.id;
+
+-- similarly, but keying off a DISTINCT clause
+explain (costs off)
+select d.* from d left join (select distinct * from b) s
+ on d.a = s.id;
+
+-- check join removal works when uniqueness of the join condition is enforced
+-- by a UNION
+explain (costs off)
+select d.* from d left join (select id from a union select id from b) s
+ on d.a = s.id;
+
+-- check join removal with a cross-type comparison operator
+explain (costs off)
+select i8.* from int8_tbl i8 left join (select f1 from int4_tbl group by f1) i4
+ on i8.q1 = i4.f1;
+
+-- check join removal with lateral references
+explain (costs off)
+select 1 from (select a.id FROM a left join b on a.b_id = b.id) q,
+ lateral generate_series(1, q.id) gs(i) where q.id = gs.i;
+
+rollback;
+
+create temp table parent (k int primary key, pd int);
+create temp table child (k int unique, cd int);
+insert into parent values (1, 10), (2, 20), (3, 30);
+insert into child values (1, 100), (4, 400);
+
+-- this case is optimizable
+select p.* from parent p left join child c on (p.k = c.k);
+explain (costs off)
+ select p.* from parent p left join child c on (p.k = c.k);
+
+-- this case is not
+select p.*, linked from parent p
+ left join (select c.*, true as linked from child c) as ss
+ on (p.k = ss.k);
+explain (costs off)
+ select p.*, linked from parent p
+ left join (select c.*, true as linked from child c) as ss
+ on (p.k = ss.k);
+
+-- check for a 9.0rc1 bug: join removal breaks pseudoconstant qual handling
+select p.* from
+ parent p left join child c on (p.k = c.k)
+ where p.k = 1 and p.k = 2;
+explain (costs off)
+select p.* from
+ parent p left join child c on (p.k = c.k)
+ where p.k = 1 and p.k = 2;
+
+select p.* from
+ (parent p left join child c on (p.k = c.k)) join parent x on p.k = x.k
+ where p.k = 1 and p.k = 2;
+explain (costs off)
+select p.* from
+ (parent p left join child c on (p.k = c.k)) join parent x on p.k = x.k
+ where p.k = 1 and p.k = 2;
+
+-- bug 5255: this is not optimizable by join removal
+begin;
+
+CREATE TEMP TABLE a (id int PRIMARY KEY);
+CREATE TEMP TABLE b (id int PRIMARY KEY, a_id int);
+INSERT INTO a VALUES (0), (1);
+INSERT INTO b VALUES (0, 0), (1, NULL);
+
+SELECT * FROM b LEFT JOIN a ON (b.a_id = a.id) WHERE (a.id IS NULL OR a.id > 0);
+SELECT b.* FROM b LEFT JOIN a ON (b.a_id = a.id) WHERE (a.id IS NULL OR a.id > 0);
+
+rollback;
+
+-- another join removal bug: this is not optimizable, either
+begin;
+
+create temp table innertab (id int8 primary key, dat1 int8);
+insert into innertab values(123, 42);
+
+SELECT * FROM
+ (SELECT 1 AS x) ss1
+ LEFT JOIN
+ (SELECT q1, q2, COALESCE(dat1, q1) AS y
+ FROM int8_tbl LEFT JOIN innertab ON q2 = id) ss2
+ ON true;
+
+rollback;
+
+-- another join removal bug: we must clean up correctly when removing a PHV
+begin;
+
+create temp table uniquetbl (f1 text unique);
+
+explain (costs off)
+select t1.* from
+ uniquetbl as t1
+ left join (select *, '***'::text as d1 from uniquetbl) t2
+ on t1.f1 = t2.f1
+ left join uniquetbl t3
+ on t2.d1 = t3.f1;
+
+explain (costs off)
+select t0.*
+from
+ text_tbl t0
+ left join
+ (select case t1.ten when 0 then 'doh!'::text else null::text end as case1,
+ t1.stringu2
+ from tenk1 t1
+ join int4_tbl i4 ON i4.f1 = t1.unique2
+ left join uniquetbl u1 ON u1.f1 = t1.string4) ss
+ on t0.f1 = ss.case1
+where ss.stringu2 !~* ss.case1;
+
+select t0.*
+from
+ text_tbl t0
+ left join
+ (select case t1.ten when 0 then 'doh!'::text else null::text end as case1,
+ t1.stringu2
+ from tenk1 t1
+ join int4_tbl i4 ON i4.f1 = t1.unique2
+ left join uniquetbl u1 ON u1.f1 = t1.string4) ss
+ on t0.f1 = ss.case1
+where ss.stringu2 !~* ss.case1;
+
+rollback;
+
+-- bug #8444: we've historically allowed duplicate aliases within aliased JOINs
+
+select * from
+ int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = f1; -- error
+select * from
+ int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = y.f1; -- error
+select * from
+ int8_tbl x join (int4_tbl x cross join int4_tbl y(ff)) j on q1 = f1; -- ok
+
+--
+-- Test hints given on incorrect column references are useful
+--
+
+select t1.uunique1 from
+ tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, prefer "t1" suggestion
+select t2.uunique1 from
+ tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, prefer "t2" suggestion
+select uunique1 from
+ tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, suggest both at once
+
+--
+-- Take care to reference the correct RTE
+--
+
+select atts.relid::regclass, s.* from pg_stats s join
+ pg_attribute a on s.attname = a.attname and s.tablename =
+ a.attrelid::regclass::text join (select unnest(indkey) attnum,
+ indexrelid from pg_index i) atts on atts.attnum = a.attnum where
+ schemaname != 'pg_catalog';
+
+--
+-- Test LATERAL
+--
+
+select unique2, x.*
+from tenk1 a, lateral (select * from int4_tbl b where f1 = a.unique1) x;
+explain (costs off)
+ select unique2, x.*
+ from tenk1 a, lateral (select * from int4_tbl b where f1 = a.unique1) x;
+select unique2, x.*
+from int4_tbl x, lateral (select unique2 from tenk1 where f1 = unique1) ss;
+explain (costs off)
+ select unique2, x.*
+ from int4_tbl x, lateral (select unique2 from tenk1 where f1 = unique1) ss;
+explain (costs off)
+ select unique2, x.*
+ from int4_tbl x cross join lateral (select unique2 from tenk1 where f1 = unique1) ss;
+select unique2, x.*
+from int4_tbl x left join lateral (select unique1, unique2 from tenk1 where f1 = unique1) ss on true;
+explain (costs off)
+ select unique2, x.*
+ from int4_tbl x left join lateral (select unique1, unique2 from tenk1 where f1 = unique1) ss on true;
+
+-- check scoping of lateral versus parent references
+-- the first of these should return int8_tbl.q2, the second int8_tbl.q1
+select *, (select r from (select q1 as q2) x, (select q2 as r) y) from int8_tbl;
+select *, (select r from (select q1 as q2) x, lateral (select q2 as r) y) from int8_tbl;
+
+-- lateral with function in FROM
+select count(*) from tenk1 a, lateral generate_series(1,two) g;
+explain (costs off)
+ select count(*) from tenk1 a, lateral generate_series(1,two) g;
+explain (costs off)
+ select count(*) from tenk1 a cross join lateral generate_series(1,two) g;
+-- don't need the explicit LATERAL keyword for functions
+explain (costs off)
+ select count(*) from tenk1 a, generate_series(1,two) g;
+
+-- lateral with UNION ALL subselect
+explain (costs off)
+ select * from generate_series(100,200) g,
+ lateral (select * from int8_tbl a where g = q1 union all
+ select * from int8_tbl b where g = q2) ss;
+select * from generate_series(100,200) g,
+ lateral (select * from int8_tbl a where g = q1 union all
+ select * from int8_tbl b where g = q2) ss;
+
+-- lateral with VALUES
+explain (costs off)
+ select count(*) from tenk1 a,
+ tenk1 b join lateral (values(a.unique1)) ss(x) on b.unique2 = ss.x;
+select count(*) from tenk1 a,
+ tenk1 b join lateral (values(a.unique1)) ss(x) on b.unique2 = ss.x;
+
+-- lateral with VALUES, no flattening possible
+explain (costs off)
+ select count(*) from tenk1 a,
+ tenk1 b join lateral (values(a.unique1),(-1)) ss(x) on b.unique2 = ss.x;
+select count(*) from tenk1 a,
+ tenk1 b join lateral (values(a.unique1),(-1)) ss(x) on b.unique2 = ss.x;
+
+-- lateral injecting a strange outer join condition
+explain (costs off)
+ select * from int8_tbl a,
+ int8_tbl x left join lateral (select a.q1 from int4_tbl y) ss(z)
+ on x.q2 = ss.z
+ order by a.q1, a.q2, x.q1, x.q2, ss.z;
+select * from int8_tbl a,
+ int8_tbl x left join lateral (select a.q1 from int4_tbl y) ss(z)
+ on x.q2 = ss.z
+ order by a.q1, a.q2, x.q1, x.q2, ss.z;
+
+-- lateral reference to a join alias variable
+select * from (select f1/2 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1,
+ lateral (select x) ss2(y);
+select * from (select f1 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1,
+ lateral (values(x)) ss2(y);
+select * from ((select f1/2 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1) j,
+ lateral (select x) ss2(y);
+
+-- lateral references requiring pullup
+select * from (values(1)) x(lb),
+ lateral generate_series(lb,4) x4;
+select * from (select f1/1000000000 from int4_tbl) x(lb),
+ lateral generate_series(lb,4) x4;
+select * from (values(1)) x(lb),
+ lateral (values(lb)) y(lbcopy);
+select * from (values(1)) x(lb),
+ lateral (select lb from int4_tbl) y(lbcopy);
+select * from
+ int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
+ lateral (values(x.q1,y.q1,y.q2)) v(xq1,yq1,yq2);
+select * from
+ int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
+ lateral (select x.q1,y.q1,y.q2) v(xq1,yq1,yq2);
+select x.* from
+ int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
+ lateral (select x.q1,y.q1,y.q2) v(xq1,yq1,yq2);
+select v.* from
+ (int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1)
+ left join int4_tbl z on z.f1 = x.q2,
+ lateral (select x.q1,y.q1 union all select x.q2,y.q2) v(vx,vy);
+select v.* from
+ (int8_tbl x left join (select q1,(select coalesce(q2,0)) q2 from int8_tbl) y on x.q2 = y.q1)
+ left join int4_tbl z on z.f1 = x.q2,
+ lateral (select x.q1,y.q1 union all select x.q2,y.q2) v(vx,vy);
+select v.* from
+ (int8_tbl x left join (select q1,(select coalesce(q2,0)) q2 from int8_tbl) y on x.q2 = y.q1)
+ left join int4_tbl z on z.f1 = x.q2,
+ lateral (select x.q1,y.q1 from onerow union all select x.q2,y.q2 from onerow) v(vx,vy);
+
+explain (verbose, costs off)
+select * from
+ int8_tbl a left join
+ lateral (select *, a.q2 as x from int8_tbl b) ss on a.q2 = ss.q1;
+select * from
+ int8_tbl a left join
+ lateral (select *, a.q2 as x from int8_tbl b) ss on a.q2 = ss.q1;
+explain (verbose, costs off)
+select * from
+ int8_tbl a left join
+ lateral (select *, coalesce(a.q2, 42) as x from int8_tbl b) ss on a.q2 = ss.q1;
+select * from
+ int8_tbl a left join
+ lateral (select *, coalesce(a.q2, 42) as x from int8_tbl b) ss on a.q2 = ss.q1;
+
+-- lateral can result in join conditions appearing below their
+-- real semantic level
+explain (verbose, costs off)
+select * from int4_tbl i left join
+ lateral (select * from int2_tbl j where i.f1 = j.f1) k on true;
+select * from int4_tbl i left join
+ lateral (select * from int2_tbl j where i.f1 = j.f1) k on true;
+explain (verbose, costs off)
+select * from int4_tbl i left join
+ lateral (select coalesce(i) from int2_tbl j where i.f1 = j.f1) k on true;
+select * from int4_tbl i left join
+ lateral (select coalesce(i) from int2_tbl j where i.f1 = j.f1) k on true;
+explain (verbose, costs off)
+select * from int4_tbl a,
+ lateral (
+ select * from int4_tbl b left join int8_tbl c on (b.f1 = q1 and a.f1 = q2)
+ ) ss;
+select * from int4_tbl a,
+ lateral (
+ select * from int4_tbl b left join int8_tbl c on (b.f1 = q1 and a.f1 = q2)
+ ) ss;
+
+-- lateral reference in a PlaceHolderVar evaluated at join level
+explain (verbose, costs off)
+select * from
+ int8_tbl a left join lateral
+ (select b.q1 as bq1, c.q1 as cq1, least(a.q1,b.q1,c.q1) from
+ int8_tbl b cross join int8_tbl c) ss
+ on a.q2 = ss.bq1;
+select * from
+ int8_tbl a left join lateral
+ (select b.q1 as bq1, c.q1 as cq1, least(a.q1,b.q1,c.q1) from
+ int8_tbl b cross join int8_tbl c) ss
+ on a.q2 = ss.bq1;
+
+-- case requiring nested PlaceHolderVars
+explain (verbose, costs off)
+select * from
+ int8_tbl c left join (
+ int8_tbl a left join (select q1, coalesce(q2,42) as x from int8_tbl b) ss1
+ on a.q2 = ss1.q1
+ cross join
+ lateral (select q1, coalesce(ss1.x,q2) as y from int8_tbl d) ss2
+ ) on c.q2 = ss2.q1,
+ lateral (select ss2.y offset 0) ss3;
+
+-- case that breaks the old ph_may_need optimization
+explain (verbose, costs off)
+select c.*,a.*,ss1.q1,ss2.q1,ss3.* from
+ int8_tbl c left join (
+ int8_tbl a left join
+ (select q1, coalesce(q2,f1) as x from int8_tbl b, int4_tbl b2
+ where q1 < f1) ss1
+ on a.q2 = ss1.q1
+ cross join
+ lateral (select q1, coalesce(ss1.x,q2) as y from int8_tbl d) ss2
+ ) on c.q2 = ss2.q1,
+ lateral (select * from int4_tbl i where ss2.y > f1) ss3;
+
+-- check processing of postponed quals (bug #9041)
+explain (verbose, costs off)
+select * from
+ (select 1 as x offset 0) x cross join (select 2 as y offset 0) y
+ left join lateral (
+ select * from (select 3 as z offset 0) z where z.z = x.x
+ ) zz on zz.z = y.y;
+
+-- check dummy rels with lateral references (bug #15694)
+explain (verbose, costs off)
+select * from int8_tbl i8 left join lateral
+ (select *, i8.q2 from int4_tbl where false) ss on true;
+explain (verbose, costs off)
+select * from int8_tbl i8 left join lateral
+ (select *, i8.q2 from int4_tbl i1, int4_tbl i2 where false) ss on true;
+
+-- check handling of nested appendrels inside LATERAL
+select * from
+ ((select 2 as v) union all (select 3 as v)) as q1
+ cross join lateral
+ ((select * from
+ ((select 4 as v) union all (select 5 as v)) as q3)
+ union all
+ (select q1.v)
+ ) as q2;
+
+-- check we don't try to do a unique-ified semijoin with LATERAL
+explain (verbose, costs off)
+select * from
+ (values (0,9998), (1,1000)) v(id,x),
+ lateral (select f1 from int4_tbl
+ where f1 = any (select unique1 from tenk1
+ where unique2 = v.x offset 0)) ss;
+select * from
+ (values (0,9998), (1,1000)) v(id,x),
+ lateral (select f1 from int4_tbl
+ where f1 = any (select unique1 from tenk1
+ where unique2 = v.x offset 0)) ss;
+
+-- check proper extParam/allParam handling (this isn't exactly a LATERAL issue,
+-- but we can make the test case much more compact with LATERAL)
+explain (verbose, costs off)
+select * from (values (0), (1)) v(id),
+lateral (select * from int8_tbl t1,
+ lateral (select * from
+ (select * from int8_tbl t2
+ where q1 = any (select q2 from int8_tbl t3
+ where q2 = (select greatest(t1.q1,t2.q2))
+ and (select v.id=0)) offset 0) ss2) ss
+ where t1.q1 = ss.q2) ss0;
+
+select * from (values (0), (1)) v(id),
+lateral (select * from int8_tbl t1,
+ lateral (select * from
+ (select * from int8_tbl t2
+ where q1 = any (select q2 from int8_tbl t3
+ where q2 = (select greatest(t1.q1,t2.q2))
+ and (select v.id=0)) offset 0) ss2) ss
+ where t1.q1 = ss.q2) ss0;
+
+-- test some error cases where LATERAL should have been used but wasn't
+select f1,g from int4_tbl a, (select f1 as g) ss;
+select f1,g from int4_tbl a, (select a.f1 as g) ss;
+select f1,g from int4_tbl a cross join (select f1 as g) ss;
+select f1,g from int4_tbl a cross join (select a.f1 as g) ss;
+-- SQL:2008 says the left table is in scope but illegal to access here
+select f1,g from int4_tbl a right join lateral generate_series(0, a.f1) g on true;
+select f1,g from int4_tbl a full join lateral generate_series(0, a.f1) g on true;
+-- check we complain about ambiguous table references
+select * from
+ int8_tbl x cross join (int4_tbl x cross join lateral (select x.f1) ss);
+-- LATERAL can be used to put an aggregate into the FROM clause of its query
+select 1 from tenk1 a, lateral (select max(a.unique1) from int4_tbl b) ss;
+
+-- check behavior of LATERAL in UPDATE/DELETE
+
+create temp table xx1 as select f1 as x1, -f1 as x2 from int4_tbl;
+
+-- error, can't do this:
+update xx1 set x2 = f1 from (select * from int4_tbl where f1 = x1) ss;
+update xx1 set x2 = f1 from (select * from int4_tbl where f1 = xx1.x1) ss;
+-- can't do it even with LATERAL:
+update xx1 set x2 = f1 from lateral (select * from int4_tbl where f1 = x1) ss;
+-- we might in future allow something like this, but for now it's an error:
+update xx1 set x2 = f1 from xx1, lateral (select * from int4_tbl where f1 = x1) ss;
+
+-- also errors:
+delete from xx1 using (select * from int4_tbl where f1 = x1) ss;
+delete from xx1 using (select * from int4_tbl where f1 = xx1.x1) ss;
+delete from xx1 using lateral (select * from int4_tbl where f1 = x1) ss;
+
+--
+-- test LATERAL reference propagation down a multi-level inheritance hierarchy
+-- produced for a multi-level partitioned table hierarchy.
+--
+create table join_pt1 (a int, b int, c varchar) partition by range(a);
+create table join_pt1p1 partition of join_pt1 for values from (0) to (100) partition by range(b);
+create table join_pt1p2 partition of join_pt1 for values from (100) to (200);
+create table join_pt1p1p1 partition of join_pt1p1 for values from (0) to (100);
+insert into join_pt1 values (1, 1, 'x'), (101, 101, 'y');
+create table join_ut1 (a int, b int, c varchar);
+insert into join_ut1 values (101, 101, 'y'), (2, 2, 'z');
+explain (verbose, costs off)
+select t1.b, ss.phv from join_ut1 t1 left join lateral
+ (select t2.a as t2a, t3.a t3a, least(t1.a, t2.a, t3.a) phv
+ from join_pt1 t2 join join_ut1 t3 on t2.a = t3.b) ss
+ on t1.a = ss.t2a order by t1.a;
+select t1.b, ss.phv from join_ut1 t1 left join lateral
+ (select t2.a as t2a, t3.a t3a, least(t1.a, t2.a, t3.a) phv
+ from join_pt1 t2 join join_ut1 t3 on t2.a = t3.b) ss
+ on t1.a = ss.t2a order by t1.a;
+
+drop table join_pt1;
+drop table join_ut1;
+--
+-- test that foreign key join estimation performs sanely for outer joins
+--
+
+begin;
+
+create table fkest (a int, b int, c int unique, primary key(a,b));
+create table fkest1 (a int, b int, primary key(a,b));
+
+insert into fkest select x/10, x%10, x from generate_series(1,1000) x;
+insert into fkest1 select x/10, x%10 from generate_series(1,1000) x;
+
+alter table fkest1
+ add constraint fkest1_a_b_fkey foreign key (a,b) references fkest;
+
+analyze fkest;
+analyze fkest1;
+
+explain (costs off)
+select *
+from fkest f
+ left join fkest1 f1 on f.a = f1.a and f.b = f1.b
+ left join fkest1 f2 on f.a = f2.a and f.b = f2.b
+ left join fkest1 f3 on f.a = f3.a and f.b = f3.b
+where f.c = 1;
+
+rollback;
+
+--
+-- test planner's ability to mark joins as unique
+--
+
+create table j1 (id int primary key);
+create table j2 (id int primary key);
+create table j3 (id int);
+
+insert into j1 values(1),(2),(3);
+insert into j2 values(1),(2),(3);
+insert into j3 values(1),(1);
+
+analyze j1;
+analyze j2;
+analyze j3;
+
+-- ensure join is properly marked as unique
+explain (verbose, costs off)
+select * from j1 inner join j2 on j1.id = j2.id;
+
+-- ensure join is not unique when not an equi-join
+explain (verbose, costs off)
+select * from j1 inner join j2 on j1.id > j2.id;
+
+-- ensure non-unique rel is not chosen as inner
+explain (verbose, costs off)
+select * from j1 inner join j3 on j1.id = j3.id;
+
+-- ensure left join is marked as unique
+explain (verbose, costs off)
+select * from j1 left join j2 on j1.id = j2.id;
+
+-- ensure right join is marked as unique
+explain (verbose, costs off)
+select * from j1 right join j2 on j1.id = j2.id;
+
+-- ensure full join is marked as unique
+explain (verbose, costs off)
+select * from j1 full join j2 on j1.id = j2.id;
+
+-- a clauseless (cross) join can't be unique
+explain (verbose, costs off)
+select * from j1 cross join j2;
+
+-- ensure a natural join is marked as unique
+explain (verbose, costs off)
+select * from j1 natural join j2;
+
+-- ensure a distinct clause allows the inner to become unique
+explain (verbose, costs off)
+select * from j1
+inner join (select distinct id from j3) j3 on j1.id = j3.id;
+
+-- ensure group by clause allows the inner to become unique
+explain (verbose, costs off)
+select * from j1
+inner join (select id from j3 group by id) j3 on j1.id = j3.id;
+
+drop table j1;
+drop table j2;
+drop table j3;
+
+-- test more complex permutations of unique joins
+
+create table j1 (id1 int, id2 int, primary key(id1,id2));
+create table j2 (id1 int, id2 int, primary key(id1,id2));
+create table j3 (id1 int, id2 int, primary key(id1,id2));
+
+insert into j1 values(1,1),(1,2);
+insert into j2 values(1,1);
+insert into j3 values(1,1);
+
+analyze j1;
+analyze j2;
+analyze j3;
+
+-- ensure there's no unique join when not all columns which are part of the
+-- unique index are seen in the join clause
+explain (verbose, costs off)
+select * from j1
+inner join j2 on j1.id1 = j2.id1;
+
+-- ensure proper unique detection with multiple join quals
+explain (verbose, costs off)
+select * from j1
+inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2;
+
+-- ensure we don't detect the join to be unique when quals are not part of the
+-- join condition
+explain (verbose, costs off)
+select * from j1
+inner join j2 on j1.id1 = j2.id1 where j1.id2 = 1;
+
+-- as above, but for left joins.
+explain (verbose, costs off)
+select * from j1
+left join j2 on j1.id1 = j2.id1 where j1.id2 = 1;
+
+-- validate logic in merge joins which skips mark and restore.
+-- it should only do this if all quals which were used to detect the unique
+-- are present as join quals, and not plain quals.
+set enable_nestloop to 0;
+set enable_hashjoin to 0;
+set enable_sort to 0;
+
+-- create indexes that will be preferred over the PKs to perform the join
+create index j1_id1_idx on j1 (id1) where id1 % 1000 = 1;
+create index j2_id1_idx on j2 (id1) where id1 % 1000 = 1;
+
+-- need an additional row in j2, if we want j2_id1_idx to be preferred
+insert into j2 values(1,2);
+analyze j2;
+
+explain (costs off) select * from j1
+inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
+where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1;
+
+select * from j1
+inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
+where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1;
+
+-- Exercise array keys mark/restore B-Tree code
+explain (costs off) select * from j1
+inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
+where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 = any (array[1]);
+
+select * from j1
+inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
+where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 = any (array[1]);
+
+-- Exercise array keys "find extreme element" B-Tree code
+explain (costs off) select * from j1
+inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
+where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 >= any (array[1,5]);
+
+select * from j1
+inner join j2 on j1.id1 = j2.id1 and j1.id2 = j2.id2
+where j1.id1 % 1000 = 1 and j2.id1 % 1000 = 1 and j2.id1 >= any (array[1,5]);
+
+reset enable_nestloop;
+reset enable_hashjoin;
+reset enable_sort;
+
+drop table j1;
+drop table j2;
+drop table j3;
+
+-- check that semijoin inner is not seen as unique for a portion of the outerrel
+explain (verbose, costs off)
+select t1.unique1, t2.hundred
+from onek t1, tenk1 t2
+where exists (select 1 from tenk1 t3
+ where t3.thousand = t1.unique1 and t3.tenthous = t2.hundred)
+ and t1.unique1 < 1;
+
+-- ... unless it actually is unique
+create table j3 as select unique1, tenthous from onek;
+vacuum analyze j3;
+create unique index on j3(unique1, tenthous);
+
+explain (verbose, costs off)
+select t1.unique1, t2.hundred
+from onek t1, tenk1 t2
+where exists (select 1 from j3
+ where j3.unique1 = t1.unique1 and j3.tenthous = t2.hundred)
+ and t1.unique1 < 1;
+
+drop table j3;
diff --git a/src/test/resources/postgresql-corpus/limit.sql b/src/test/resources/postgresql-corpus/limit.sql
new file mode 100644
index 0000000..6d4da6c
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/limit.sql
@@ -0,0 +1,194 @@
+--
+-- LIMIT
+-- Check the LIMIT/OFFSET feature of SELECT
+--
+
+SELECT ''::text AS two, unique1, unique2, stringu1
+ FROM onek WHERE unique1 > 50
+ ORDER BY unique1 LIMIT 2;
+SELECT ''::text AS five, unique1, unique2, stringu1
+ FROM onek WHERE unique1 > 60
+ ORDER BY unique1 LIMIT 5;
+SELECT ''::text AS two, unique1, unique2, stringu1
+ FROM onek WHERE unique1 > 60 AND unique1 < 63
+ ORDER BY unique1 LIMIT 5;
+SELECT ''::text AS three, unique1, unique2, stringu1
+ FROM onek WHERE unique1 > 100
+ ORDER BY unique1 LIMIT 3 OFFSET 20;
+SELECT ''::text AS zero, unique1, unique2, stringu1
+ FROM onek WHERE unique1 < 50
+ ORDER BY unique1 DESC LIMIT 8 OFFSET 99;
+SELECT ''::text AS eleven, unique1, unique2, stringu1
+ FROM onek WHERE unique1 < 50
+ ORDER BY unique1 DESC LIMIT 20 OFFSET 39;
+SELECT ''::text AS ten, unique1, unique2, stringu1
+ FROM onek
+ ORDER BY unique1 OFFSET 990;
+SELECT ''::text AS five, unique1, unique2, stringu1
+ FROM onek
+ ORDER BY unique1 OFFSET 990 LIMIT 5;
+SELECT ''::text AS five, unique1, unique2, stringu1
+ FROM onek
+ ORDER BY unique1 LIMIT 5 OFFSET 900;
+
+-- Test null limit and offset. The planner would discard a simple null
+-- constant, so to ensure executor is exercised, do this:
+select * from int8_tbl limit (case when random() < 0.5 then null::bigint end);
+select * from int8_tbl offset (case when random() < 0.5 then null::bigint end);
+
+-- Test assorted cases involving backwards fetch from a LIMIT plan node
+begin;
+
+declare c1 cursor for select * from int8_tbl limit 10;
+fetch all in c1;
+fetch 1 in c1;
+fetch backward 1 in c1;
+fetch backward all in c1;
+fetch backward 1 in c1;
+fetch all in c1;
+
+declare c2 cursor for select * from int8_tbl limit 3;
+fetch all in c2;
+fetch 1 in c2;
+fetch backward 1 in c2;
+fetch backward all in c2;
+fetch backward 1 in c2;
+fetch all in c2;
+
+declare c3 cursor for select * from int8_tbl offset 3;
+fetch all in c3;
+fetch 1 in c3;
+fetch backward 1 in c3;
+fetch backward all in c3;
+fetch backward 1 in c3;
+fetch all in c3;
+
+declare c4 cursor for select * from int8_tbl offset 10;
+fetch all in c4;
+fetch 1 in c4;
+fetch backward 1 in c4;
+fetch backward all in c4;
+fetch backward 1 in c4;
+fetch all in c4;
+
+declare c5 cursor for select * from int8_tbl order by q1 fetch first 2 rows with ties;
+fetch all in c5;
+fetch 1 in c5;
+fetch backward 1 in c5;
+fetch backward 1 in c5;
+fetch all in c5;
+fetch backward all in c5;
+fetch all in c5;
+fetch backward all in c5;
+
+rollback;
+
+-- Stress test for variable LIMIT in conjunction with bounded-heap sorting
+
+SELECT
+ (SELECT n
+ FROM (VALUES (1)) AS x,
+ (SELECT n FROM generate_series(1,10) AS n
+ ORDER BY n LIMIT 1 OFFSET s-1) AS y) AS z
+ FROM generate_series(1,10) AS s;
+
+--
+-- Test behavior of volatile and set-returning functions in conjunction
+-- with ORDER BY and LIMIT.
+--
+
+create temp sequence testseq;
+
+explain (verbose, costs off)
+select unique1, unique2, nextval('testseq')
+ from tenk1 order by unique2 limit 10;
+
+select unique1, unique2, nextval('testseq')
+ from tenk1 order by unique2 limit 10;
+
+select currval('testseq');
+
+explain (verbose, costs off)
+select unique1, unique2, nextval('testseq')
+ from tenk1 order by tenthous limit 10;
+
+select unique1, unique2, nextval('testseq')
+ from tenk1 order by tenthous limit 10;
+
+select currval('testseq');
+
+explain (verbose, costs off)
+select unique1, unique2, generate_series(1,10)
+ from tenk1 order by unique2 limit 7;
+
+select unique1, unique2, generate_series(1,10)
+ from tenk1 order by unique2 limit 7;
+
+explain (verbose, costs off)
+select unique1, unique2, generate_series(1,10)
+ from tenk1 order by tenthous limit 7;
+
+select unique1, unique2, generate_series(1,10)
+ from tenk1 order by tenthous limit 7;
+
+
+
+-- test for failure to set all aggregates' aggtranstype
+explain (verbose, costs off)
+select sum(tenthous) as s1, sum(tenthous) + random()*0 as s2
+ from tenk1 group by thousand order by thousand limit 3;
+
+select sum(tenthous) as s1, sum(tenthous) + random()*0 as s2
+ from tenk1 group by thousand order by thousand limit 3;
+
+--
+-- FETCH FIRST
+-- Check the WITH TIES clause
+--
+
+SELECT thousand
+ FROM onek WHERE thousand < 5
+ ORDER BY thousand FETCH FIRST 2 ROW WITH TIES;
+
+SELECT thousand
+ FROM onek WHERE thousand < 5
+ ORDER BY thousand FETCH FIRST ROWS WITH TIES;
+
+SELECT thousand
+ FROM onek WHERE thousand < 5
+ ORDER BY thousand FETCH FIRST 1 ROW WITH TIES;
+
+SELECT thousand
+ FROM onek WHERE thousand < 5
+ ORDER BY thousand FETCH FIRST 2 ROW ONLY;
+
+-- should fail
+SELECT ''::text AS two, unique1, unique2, stringu1
+ FROM onek WHERE unique1 > 50
+ FETCH FIRST 2 ROW WITH TIES;
+
+-- test ruleutils
+CREATE VIEW limit_thousand_v_1 AS SELECT thousand FROM onek WHERE thousand < 995
+ ORDER BY thousand FETCH FIRST 5 ROWS WITH TIES OFFSET 10;
+CREATE VIEW limit_thousand_v_2 AS SELECT thousand FROM onek WHERE thousand < 995
+ ORDER BY thousand OFFSET 10 FETCH FIRST 5 ROWS ONLY;
+CREATE VIEW limit_thousand_v_3 AS SELECT thousand FROM onek WHERE thousand < 995
+ ORDER BY thousand FETCH FIRST NULL ROWS WITH TIES; -- fails
+CREATE VIEW limit_thousand_v_3 AS SELECT thousand FROM onek WHERE thousand < 995
+ ORDER BY thousand FETCH FIRST (NULL+1) ROWS WITH TIES;
+CREATE VIEW limit_thousand_v_4 AS SELECT thousand FROM onek WHERE thousand < 995
+ ORDER BY thousand FETCH FIRST NULL ROWS ONLY;
+-- leave these views
+-- use of random() is to keep planner from folding the expressions together
+explain (verbose, costs off)
+select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2;
+
+select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2;
+
+explain (verbose, costs off)
+select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2
+order by s2 desc;
+
+select generate_series(0,2) as s1, generate_series((random()*.1)::int,2) as s2
+order by s2 desc;
+
diff --git a/src/test/resources/postgresql-corpus/select.sql b/src/test/resources/postgresql-corpus/select.sql
new file mode 100644
index 0000000..b5929b2
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/select.sql
@@ -0,0 +1,264 @@
+--
+-- SELECT
+--
+
+-- btree index
+-- awk '{if($1<10){print;}else{next;}}' onek.data | sort +0n -1
+--
+SELECT * FROM onek
+ WHERE onek.unique1 < 10
+ ORDER BY onek.unique1;
+
+--
+-- awk '{if($1<20){print $1,$14;}else{next;}}' onek.data | sort +0nr -1
+--
+SELECT onek.unique1, onek.stringu1 FROM onek
+ WHERE onek.unique1 < 20
+ ORDER BY unique1 using >;
+
+--
+-- awk '{if($1>980){print $1,$14;}else{next;}}' onek.data | sort +1d -2
+--
+SELECT onek.unique1, onek.stringu1 FROM onek
+ WHERE onek.unique1 > 980
+ ORDER BY stringu1 using <;
+
+--
+-- awk '{if($1>980){print $1,$16;}else{next;}}' onek.data |
+-- sort +1d -2 +0nr -1
+--
+SELECT onek.unique1, onek.string4 FROM onek
+ WHERE onek.unique1 > 980
+ ORDER BY string4 using <, unique1 using >;
+
+--
+-- awk '{if($1>980){print $1,$16;}else{next;}}' onek.data |
+-- sort +1dr -2 +0n -1
+--
+SELECT onek.unique1, onek.string4 FROM onek
+ WHERE onek.unique1 > 980
+ ORDER BY string4 using >, unique1 using <;
+
+--
+-- awk '{if($1<20){print $1,$16;}else{next;}}' onek.data |
+-- sort +0nr -1 +1d -2
+--
+SELECT onek.unique1, onek.string4 FROM onek
+ WHERE onek.unique1 < 20
+ ORDER BY unique1 using >, string4 using <;
+
+--
+-- awk '{if($1<20){print $1,$16;}else{next;}}' onek.data |
+-- sort +0n -1 +1dr -2
+--
+SELECT onek.unique1, onek.string4 FROM onek
+ WHERE onek.unique1 < 20
+ ORDER BY unique1 using <, string4 using >;
+
+--
+-- test partial btree indexes
+--
+-- As of 7.2, planner probably won't pick an indexscan without stats,
+-- so ANALYZE first. Also, we want to prevent it from picking a bitmapscan
+-- followed by sort, because that could hide index ordering problems.
+--
+ANALYZE onek2;
+
+SET enable_seqscan TO off;
+SET enable_bitmapscan TO off;
+SET enable_sort TO off;
+
+--
+-- awk '{if($1<10){print $0;}else{next;}}' onek.data | sort +0n -1
+--
+SELECT onek2.* FROM onek2 WHERE onek2.unique1 < 10;
+
+--
+-- awk '{if($1<20){print $1,$14;}else{next;}}' onek.data | sort +0nr -1
+--
+SELECT onek2.unique1, onek2.stringu1 FROM onek2
+ WHERE onek2.unique1 < 20
+ ORDER BY unique1 using >;
+
+--
+-- awk '{if($1>980){print $1,$14;}else{next;}}' onek.data | sort +1d -2
+--
+SELECT onek2.unique1, onek2.stringu1 FROM onek2
+ WHERE onek2.unique1 > 980;
+
+RESET enable_seqscan;
+RESET enable_bitmapscan;
+RESET enable_sort;
+
+
+SELECT two, stringu1, ten, string4
+ INTO TABLE tmp
+ FROM onek;
+
+--
+-- awk '{print $1,$2;}' person.data |
+-- awk '{if(NF!=2){print $3,$2;}else{print;}}' - emp.data |
+-- awk '{if(NF!=2){print $3,$2;}else{print;}}' - student.data |
+-- awk 'BEGIN{FS=" ";}{if(NF!=2){print $4,$5;}else{print;}}' - stud_emp.data
+--
+-- SELECT name, age FROM person*; ??? check if different
+SELECT p.name, p.age FROM person* p;
+
+--
+-- awk '{print $1,$2;}' person.data |
+-- awk '{if(NF!=2){print $3,$2;}else{print;}}' - emp.data |
+-- awk '{if(NF!=2){print $3,$2;}else{print;}}' - student.data |
+-- awk 'BEGIN{FS=" ";}{if(NF!=1){print $4,$5;}else{print;}}' - stud_emp.data |
+-- sort +1nr -2
+--
+SELECT p.name, p.age FROM person* p ORDER BY age using >, name;
+
+--
+-- Test some cases involving whole-row Var referencing a subquery
+--
+select foo from (select 1 offset 0) as foo;
+select foo from (select null offset 0) as foo;
+select foo from (select 'xyzzy',1,null offset 0) as foo;
+
+--
+-- Test VALUES lists
+--
+select * from onek, (values(147, 'RFAAAA'), (931, 'VJAAAA')) as v (i, j)
+ WHERE onek.unique1 = v.i and onek.stringu1 = v.j;
+
+-- a more complex case
+-- looks like we're coding lisp :-)
+select * from onek,
+ (values ((select i from
+ (values(10000), (2), (389), (1000), (2000), ((select 10029))) as foo(i)
+ order by i asc limit 1))) bar (i)
+ where onek.unique1 = bar.i;
+
+-- try VALUES in a subquery
+select * from onek
+ where (unique1,ten) in (values (1,1), (20,0), (99,9), (17,99))
+ order by unique1;
+
+-- VALUES is also legal as a standalone query or a set-operation member
+VALUES (1,2), (3,4+4), (7,77.7);
+
+VALUES (1,2), (3,4+4), (7,77.7)
+UNION ALL
+SELECT 2+2, 57
+UNION ALL
+TABLE int8_tbl;
+
+--
+-- Test ORDER BY options
+--
+
+CREATE TEMP TABLE foo (f1 int);
+
+INSERT INTO foo VALUES (42),(3),(10),(7),(null),(null),(1);
+
+SELECT * FROM foo ORDER BY f1;
+SELECT * FROM foo ORDER BY f1 ASC; -- same thing
+SELECT * FROM foo ORDER BY f1 NULLS FIRST;
+SELECT * FROM foo ORDER BY f1 DESC;
+SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
+
+-- check if indexscans do the right things
+CREATE INDEX fooi ON foo (f1);
+SET enable_sort = false;
+
+SELECT * FROM foo ORDER BY f1;
+SELECT * FROM foo ORDER BY f1 NULLS FIRST;
+SELECT * FROM foo ORDER BY f1 DESC;
+SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
+
+DROP INDEX fooi;
+CREATE INDEX fooi ON foo (f1 DESC);
+
+SELECT * FROM foo ORDER BY f1;
+SELECT * FROM foo ORDER BY f1 NULLS FIRST;
+SELECT * FROM foo ORDER BY f1 DESC;
+SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
+
+DROP INDEX fooi;
+CREATE INDEX fooi ON foo (f1 DESC NULLS LAST);
+
+SELECT * FROM foo ORDER BY f1;
+SELECT * FROM foo ORDER BY f1 NULLS FIRST;
+SELECT * FROM foo ORDER BY f1 DESC;
+SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
+
+--
+-- Test planning of some cases with partial indexes
+--
+
+-- partial index is usable
+explain (costs off)
+select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
+select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
+-- actually run the query with an analyze to use the partial index
+explain (costs off, analyze on, timing off, summary off)
+select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
+explain (costs off)
+select unique2 from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
+select unique2 from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
+-- partial index predicate implies clause, so no need for retest
+explain (costs off)
+select * from onek2 where unique2 = 11 and stringu1 < 'B';
+select * from onek2 where unique2 = 11 and stringu1 < 'B';
+explain (costs off)
+select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
+select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
+-- but if it's an update target, must retest anyway
+explain (costs off)
+select unique2 from onek2 where unique2 = 11 and stringu1 < 'B' for update;
+select unique2 from onek2 where unique2 = 11 and stringu1 < 'B' for update;
+-- partial index is not applicable
+explain (costs off)
+select unique2 from onek2 where unique2 = 11 and stringu1 < 'C';
+select unique2 from onek2 where unique2 = 11 and stringu1 < 'C';
+-- partial index implies clause, but bitmap scan must recheck predicate anyway
+SET enable_indexscan TO off;
+explain (costs off)
+select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
+select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
+RESET enable_indexscan;
+-- check multi-index cases too
+explain (costs off)
+select unique1, unique2 from onek2
+ where (unique2 = 11 or unique1 = 0) and stringu1 < 'B';
+select unique1, unique2 from onek2
+ where (unique2 = 11 or unique1 = 0) and stringu1 < 'B';
+explain (costs off)
+select unique1, unique2 from onek2
+ where (unique2 = 11 and stringu1 < 'B') or unique1 = 0;
+select unique1, unique2 from onek2
+ where (unique2 = 11 and stringu1 < 'B') or unique1 = 0;
+
+--
+-- Test some corner cases that have been known to confuse the planner
+--
+
+-- ORDER BY on a constant doesn't really need any sorting
+SELECT 1 AS x ORDER BY x;
+
+-- But ORDER BY on a set-valued expression does
+create function sillysrf(int) returns setof int as
+ 'values (1),(10),(2),($1)' language sql immutable;
+
+select sillysrf(42);
+select sillysrf(-1) order by 1;
+
+drop function sillysrf(int);
+
+-- X = X isn't a no-op, it's effectively X IS NOT NULL assuming = is strict
+-- (see bug #5084)
+select * from (values (2),(null),(1)) v(k) where k = k order by k;
+select * from (values (2),(null),(1)) v(k) where k = k;
+
+-- Test partitioned tables with no partitions, which should be handled the
+-- same as the non-inheritance case when expanding its RTE.
+create table list_parted_tbl (a int,b int) partition by list (a);
+create table list_parted_tbl1 partition of list_parted_tbl
+ for values in (1) partition by list(b);
+explain (costs off) select * from list_parted_tbl;
+drop table list_parted_tbl;
diff --git a/src/test/resources/postgresql-corpus/select_distinct.sql b/src/test/resources/postgresql-corpus/select_distinct.sql
new file mode 100644
index 0000000..3310274
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/select_distinct.sql
@@ -0,0 +1,137 @@
+--
+-- SELECT_DISTINCT
+--
+
+--
+-- awk '{print $3;}' onek.data | sort -n | uniq
+--
+SELECT DISTINCT two FROM tmp ORDER BY 1;
+
+--
+-- awk '{print $5;}' onek.data | sort -n | uniq
+--
+SELECT DISTINCT ten FROM tmp ORDER BY 1;
+
+--
+-- awk '{print $16;}' onek.data | sort -d | uniq
+--
+SELECT DISTINCT string4 FROM tmp ORDER BY 1;
+
+--
+-- awk '{print $3,$16,$5;}' onek.data | sort -d | uniq |
+-- sort +0n -1 +1d -2 +2n -3
+--
+SELECT DISTINCT two, string4, ten
+ FROM tmp
+ ORDER BY two using <, string4 using <, ten using <;
+
+--
+-- awk '{print $2;}' person.data |
+-- awk '{if(NF!=1){print $2;}else{print;}}' - emp.data |
+-- awk '{if(NF!=1){print $2;}else{print;}}' - student.data |
+-- awk 'BEGIN{FS=" ";}{if(NF!=1){print $5;}else{print;}}' - stud_emp.data |
+-- sort -n -r | uniq
+--
+SELECT DISTINCT p.age FROM person* p ORDER BY age using >;
+
+--
+-- Check mentioning same column more than once
+--
+
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT count(*) FROM
+ (SELECT DISTINCT two, four, two FROM tenk1) ss;
+
+SELECT count(*) FROM
+ (SELECT DISTINCT two, four, two FROM tenk1) ss;
+
+--
+-- Compare results between plans using sorting and plans using hash
+-- aggregation. Force spilling in both cases by setting work_mem low.
+--
+
+SET work_mem='64kB';
+
+-- Produce results with sorting.
+
+SET enable_hashagg=FALSE;
+
+SET jit_above_cost=0;
+
+EXPLAIN (costs off)
+SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
+
+CREATE TABLE distinct_group_1 AS
+SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
+
+SET jit_above_cost TO DEFAULT;
+
+CREATE TABLE distinct_group_2 AS
+SELECT DISTINCT (g%1000)::text FROM generate_series(0,9999) g;
+
+SET enable_hashagg=TRUE;
+
+-- Produce results with hash aggregation.
+
+SET enable_sort=FALSE;
+
+SET jit_above_cost=0;
+
+EXPLAIN (costs off)
+SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
+
+CREATE TABLE distinct_hash_1 AS
+SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
+
+SET jit_above_cost TO DEFAULT;
+
+CREATE TABLE distinct_hash_2 AS
+SELECT DISTINCT (g%1000)::text FROM generate_series(0,9999) g;
+
+SET enable_sort=TRUE;
+
+SET work_mem TO DEFAULT;
+
+-- Compare results
+
+(SELECT * FROM distinct_hash_1 EXCEPT SELECT * FROM distinct_group_1)
+ UNION ALL
+(SELECT * FROM distinct_group_1 EXCEPT SELECT * FROM distinct_hash_1);
+
+(SELECT * FROM distinct_hash_1 EXCEPT SELECT * FROM distinct_group_1)
+ UNION ALL
+(SELECT * FROM distinct_group_1 EXCEPT SELECT * FROM distinct_hash_1);
+
+DROP TABLE distinct_hash_1;
+DROP TABLE distinct_hash_2;
+DROP TABLE distinct_group_1;
+DROP TABLE distinct_group_2;
+
+--
+-- Also, some tests of IS DISTINCT FROM, which doesn't quite deserve its
+-- very own regression file.
+--
+
+CREATE TEMP TABLE disttable (f1 integer);
+INSERT INTO DISTTABLE VALUES(1);
+INSERT INTO DISTTABLE VALUES(2);
+INSERT INTO DISTTABLE VALUES(3);
+INSERT INTO DISTTABLE VALUES(NULL);
+
+-- basic cases
+SELECT f1, f1 IS DISTINCT FROM 2 as "not 2" FROM disttable;
+SELECT f1, f1 IS DISTINCT FROM NULL as "not null" FROM disttable;
+SELECT f1, f1 IS DISTINCT FROM f1 as "false" FROM disttable;
+SELECT f1, f1 IS DISTINCT FROM f1+1 as "not null" FROM disttable;
+
+-- check that optimizer constant-folds it properly
+SELECT 1 IS DISTINCT FROM 2 as "yes";
+SELECT 2 IS DISTINCT FROM 2 as "no";
+SELECT 2 IS DISTINCT FROM null as "yes";
+SELECT null IS DISTINCT FROM null as "no";
+
+-- negated form
+SELECT 1 IS NOT DISTINCT FROM 2 as "no";
+SELECT 2 IS NOT DISTINCT FROM 2 as "yes";
+SELECT 2 IS NOT DISTINCT FROM null as "no";
+SELECT null IS NOT DISTINCT FROM null as "yes";
diff --git a/src/test/resources/postgresql-corpus/select_having.sql b/src/test/resources/postgresql-corpus/select_having.sql
new file mode 100644
index 0000000..bc0cdc0
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/select_having.sql
@@ -0,0 +1,50 @@
+--
+-- SELECT_HAVING
+--
+
+-- load test data
+CREATE TABLE test_having (a int, b int, c char(8), d char);
+INSERT INTO test_having VALUES (0, 1, 'XXXX', 'A');
+INSERT INTO test_having VALUES (1, 2, 'AAAA', 'b');
+INSERT INTO test_having VALUES (2, 2, 'AAAA', 'c');
+INSERT INTO test_having VALUES (3, 3, 'BBBB', 'D');
+INSERT INTO test_having VALUES (4, 3, 'BBBB', 'e');
+INSERT INTO test_having VALUES (5, 3, 'bbbb', 'F');
+INSERT INTO test_having VALUES (6, 4, 'cccc', 'g');
+INSERT INTO test_having VALUES (7, 4, 'cccc', 'h');
+INSERT INTO test_having VALUES (8, 4, 'CCCC', 'I');
+INSERT INTO test_having VALUES (9, 4, 'CCCC', 'j');
+
+SELECT b, c FROM test_having
+ GROUP BY b, c HAVING count(*) = 1 ORDER BY b, c;
+
+-- HAVING is effectively equivalent to WHERE in this case
+SELECT b, c FROM test_having
+ GROUP BY b, c HAVING b = 3 ORDER BY b, c;
+
+SELECT lower(c), count(c) FROM test_having
+ GROUP BY lower(c) HAVING count(*) > 2 OR min(a) = max(a)
+ ORDER BY lower(c);
+
+SELECT c, max(a) FROM test_having
+ GROUP BY c HAVING count(*) > 2 OR min(a) = max(a)
+ ORDER BY c;
+
+-- test degenerate cases involving HAVING without GROUP BY
+-- Per SQL spec, these should generate 0 or 1 row, even without aggregates
+
+SELECT min(a), max(a) FROM test_having HAVING min(a) = max(a);
+SELECT min(a), max(a) FROM test_having HAVING min(a) < max(a);
+
+-- errors: ungrouped column references
+SELECT a FROM test_having HAVING min(a) < max(a);
+SELECT 1 AS one FROM test_having HAVING a > 1;
+
+-- the really degenerate case: need not scan table at all
+SELECT 1 AS one FROM test_having HAVING 1 > 2;
+SELECT 1 AS one FROM test_having HAVING 1 < 2;
+
+-- and just to prove that we aren't scanning the table:
+SELECT 1 AS one FROM test_having WHERE 1/a = 1 HAVING 1 < 2;
+
+DROP TABLE test_having;
diff --git a/src/test/resources/postgresql-corpus/subselect.sql b/src/test/resources/postgresql-corpus/subselect.sql
new file mode 100644
index 0000000..a56057b
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/subselect.sql
@@ -0,0 +1,859 @@
+--
+-- SUBSELECT
+--
+
+SELECT 1 AS one WHERE 1 IN (SELECT 1);
+
+SELECT 1 AS zero WHERE 1 NOT IN (SELECT 1);
+
+SELECT 1 AS zero WHERE 1 IN (SELECT 2);
+
+-- Check grammar's handling of extra parens in assorted contexts
+
+SELECT * FROM (SELECT 1 AS x) ss;
+SELECT * FROM ((SELECT 1 AS x)) ss;
+
+(SELECT 2) UNION SELECT 2;
+((SELECT 2)) UNION SELECT 2;
+
+SELECT ((SELECT 2) UNION SELECT 2);
+SELECT (((SELECT 2)) UNION SELECT 2);
+
+SELECT (SELECT ARRAY[1,2,3])[1];
+SELECT ((SELECT ARRAY[1,2,3]))[2];
+SELECT (((SELECT ARRAY[1,2,3])))[3];
+
+-- Set up some simple test tables
+
+CREATE TABLE SUBSELECT_TBL (
+ f1 integer,
+ f2 integer,
+ f3 float
+);
+
+INSERT INTO SUBSELECT_TBL VALUES (1, 2, 3);
+INSERT INTO SUBSELECT_TBL VALUES (2, 3, 4);
+INSERT INTO SUBSELECT_TBL VALUES (3, 4, 5);
+INSERT INTO SUBSELECT_TBL VALUES (1, 1, 1);
+INSERT INTO SUBSELECT_TBL VALUES (2, 2, 2);
+INSERT INTO SUBSELECT_TBL VALUES (3, 3, 3);
+INSERT INTO SUBSELECT_TBL VALUES (6, 7, 8);
+INSERT INTO SUBSELECT_TBL VALUES (8, 9, NULL);
+
+SELECT '' AS eight, * FROM SUBSELECT_TBL;
+
+-- Uncorrelated subselects
+
+SELECT '' AS two, f1 AS "Constant Select" FROM SUBSELECT_TBL
+ WHERE f1 IN (SELECT 1);
+
+SELECT '' AS six, f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
+ WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL);
+
+SELECT '' AS six, f1 AS "Uncorrelated Field" FROM SUBSELECT_TBL
+ WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE
+ f2 IN (SELECT f1 FROM SUBSELECT_TBL));
+
+SELECT '' AS three, f1, f2
+ FROM SUBSELECT_TBL
+ WHERE (f1, f2) NOT IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
+ WHERE f3 IS NOT NULL);
+
+-- Correlated subselects
+
+SELECT '' AS six, f1 AS "Correlated Field", f2 AS "Second Field"
+ FROM SUBSELECT_TBL upper
+ WHERE f1 IN (SELECT f2 FROM SUBSELECT_TBL WHERE f1 = upper.f1);
+
+SELECT '' AS six, f1 AS "Correlated Field", f3 AS "Second Field"
+ FROM SUBSELECT_TBL upper
+ WHERE f1 IN
+ (SELECT f2 FROM SUBSELECT_TBL WHERE CAST(upper.f2 AS float) = f3);
+
+SELECT '' AS six, f1 AS "Correlated Field", f3 AS "Second Field"
+ FROM SUBSELECT_TBL upper
+ WHERE f3 IN (SELECT upper.f1 + f2 FROM SUBSELECT_TBL
+ WHERE f2 = CAST(f3 AS integer));
+
+SELECT '' AS five, f1 AS "Correlated Field"
+ FROM SUBSELECT_TBL
+ WHERE (f1, f2) IN (SELECT f2, CAST(f3 AS int4) FROM SUBSELECT_TBL
+ WHERE f3 IS NOT NULL);
+
+--
+-- Use some existing tables in the regression test
+--
+
+SELECT '' AS eight, ss.f1 AS "Correlated Field", ss.f3 AS "Second Field"
+ FROM SUBSELECT_TBL ss
+ WHERE f1 NOT IN (SELECT f1+1 FROM INT4_TBL
+ WHERE f1 != ss.f1 AND f1 < 2147483647);
+
+select q1, float8(count(*)) / (select count(*) from int8_tbl)
+from int8_tbl group by q1 order by q1;
+
+-- Unspecified-type literals in output columns should resolve as text
+
+SELECT *, pg_typeof(f1) FROM
+ (SELECT 'foo' AS f1 FROM generate_series(1,3)) ss ORDER BY 1;
+
+-- ... unless there's context to suggest differently
+
+explain (verbose, costs off) select '42' union all select '43';
+explain (verbose, costs off) select '42' union all select 43;
+
+-- check materialization of an initplan reference (bug #14524)
+explain (verbose, costs off)
+select 1 = all (select (select 1));
+select 1 = all (select (select 1));
+
+--
+-- Check EXISTS simplification with LIMIT
+--
+explain (costs off)
+select * from int4_tbl o where exists
+ (select 1 from int4_tbl i where i.f1=o.f1 limit null);
+explain (costs off)
+select * from int4_tbl o where not exists
+ (select 1 from int4_tbl i where i.f1=o.f1 limit 1);
+explain (costs off)
+select * from int4_tbl o where exists
+ (select 1 from int4_tbl i where i.f1=o.f1 limit 0);
+
+--
+-- Test cases to catch unpleasant interactions between IN-join processing
+-- and subquery pullup.
+--
+
+select count(*) from
+ (select 1 from tenk1 a
+ where unique1 IN (select hundred from tenk1 b)) ss;
+select count(distinct ss.ten) from
+ (select ten from tenk1 a
+ where unique1 IN (select hundred from tenk1 b)) ss;
+select count(*) from
+ (select 1 from tenk1 a
+ where unique1 IN (select distinct hundred from tenk1 b)) ss;
+select count(distinct ss.ten) from
+ (select ten from tenk1 a
+ where unique1 IN (select distinct hundred from tenk1 b)) ss;
+
+--
+-- Test cases to check for overenthusiastic optimization of
+-- "IN (SELECT DISTINCT ...)" and related cases. Per example from
+-- Luca Pireddu and Michael Fuhr.
+--
+
+CREATE TEMP TABLE foo (id integer);
+CREATE TEMP TABLE bar (id1 integer, id2 integer);
+
+INSERT INTO foo VALUES (1);
+
+INSERT INTO bar VALUES (1, 1);
+INSERT INTO bar VALUES (2, 2);
+INSERT INTO bar VALUES (3, 1);
+
+-- These cases require an extra level of distinct-ing above subquery s
+SELECT * FROM foo WHERE id IN
+ (SELECT id2 FROM (SELECT DISTINCT id1, id2 FROM bar) AS s);
+SELECT * FROM foo WHERE id IN
+ (SELECT id2 FROM (SELECT id1,id2 FROM bar GROUP BY id1,id2) AS s);
+SELECT * FROM foo WHERE id IN
+ (SELECT id2 FROM (SELECT id1, id2 FROM bar UNION
+ SELECT id1, id2 FROM bar) AS s);
+
+-- These cases do not
+SELECT * FROM foo WHERE id IN
+ (SELECT id2 FROM (SELECT DISTINCT ON (id2) id1, id2 FROM bar) AS s);
+SELECT * FROM foo WHERE id IN
+ (SELECT id2 FROM (SELECT id2 FROM bar GROUP BY id2) AS s);
+SELECT * FROM foo WHERE id IN
+ (SELECT id2 FROM (SELECT id2 FROM bar UNION
+ SELECT id2 FROM bar) AS s);
+
+--
+-- Test case to catch problems with multiply nested sub-SELECTs not getting
+-- recalculated properly. Per bug report from Didier Moens.
+--
+
+CREATE TABLE orderstest (
+ approver_ref integer,
+ po_ref integer,
+ ordercanceled boolean
+);
+
+INSERT INTO orderstest VALUES (1, 1, false);
+INSERT INTO orderstest VALUES (66, 5, false);
+INSERT INTO orderstest VALUES (66, 6, false);
+INSERT INTO orderstest VALUES (66, 7, false);
+INSERT INTO orderstest VALUES (66, 1, true);
+INSERT INTO orderstest VALUES (66, 8, false);
+INSERT INTO orderstest VALUES (66, 1, false);
+INSERT INTO orderstest VALUES (77, 1, false);
+INSERT INTO orderstest VALUES (1, 1, false);
+INSERT INTO orderstest VALUES (66, 1, false);
+INSERT INTO orderstest VALUES (1, 1, false);
+
+CREATE VIEW orders_view AS
+SELECT *,
+(SELECT CASE
+ WHEN ord.approver_ref=1 THEN '---' ELSE 'Approved'
+ END) AS "Approved",
+(SELECT CASE
+ WHEN ord.ordercanceled
+ THEN 'Canceled'
+ ELSE
+ (SELECT CASE
+ WHEN ord.po_ref=1
+ THEN
+ (SELECT CASE
+ WHEN ord.approver_ref=1
+ THEN '---'
+ ELSE 'Approved'
+ END)
+ ELSE 'PO'
+ END)
+END) AS "Status",
+(CASE
+ WHEN ord.ordercanceled
+ THEN 'Canceled'
+ ELSE
+ (CASE
+ WHEN ord.po_ref=1
+ THEN
+ (CASE
+ WHEN ord.approver_ref=1
+ THEN '---'
+ ELSE 'Approved'
+ END)
+ ELSE 'PO'
+ END)
+END) AS "Status_OK"
+FROM orderstest ord;
+
+SELECT * FROM orders_view;
+
+DROP TABLE orderstest cascade;
+
+--
+-- Test cases to catch situations where rule rewriter fails to propagate
+-- hasSubLinks flag correctly. Per example from Kyle Bateman.
+--
+
+create temp table parts (
+ partnum text,
+ cost float8
+);
+
+create temp table shipped (
+ ttype char(2),
+ ordnum int4,
+ partnum text,
+ value float8
+);
+
+create temp view shipped_view as
+ select * from shipped where ttype = 'wt';
+
+create rule shipped_view_insert as on insert to shipped_view do instead
+ insert into shipped values('wt', new.ordnum, new.partnum, new.value);
+
+insert into parts (partnum, cost) values (1, 1234.56);
+
+insert into shipped_view (ordnum, partnum, value)
+ values (0, 1, (select cost from parts where partnum = '1'));
+
+select * from shipped_view;
+
+create rule shipped_view_update as on update to shipped_view do instead
+ update shipped set partnum = new.partnum, value = new.value
+ where ttype = new.ttype and ordnum = new.ordnum;
+
+update shipped_view set value = 11
+ from int4_tbl a join int4_tbl b
+ on (a.f1 = (select f1 from int4_tbl c where c.f1=b.f1))
+ where ordnum = a.f1;
+
+select * from shipped_view;
+
+select f1, ss1 as relabel from
+ (select *, (select sum(f1) from int4_tbl b where f1 >= a.f1) as ss1
+ from int4_tbl a) ss;
+
+--
+-- Test cases involving PARAM_EXEC parameters and min/max index optimizations.
+-- Per bug report from David Sanchez i Gregori.
+--
+
+select * from (
+ select max(unique1) from tenk1 as a
+ where exists (select 1 from tenk1 as b where b.thousand = a.unique2)
+) ss;
+
+select * from (
+ select min(unique1) from tenk1 as a
+ where not exists (select 1 from tenk1 as b where b.unique2 = 10000)
+) ss;
+
+--
+-- Test that an IN implemented using a UniquePath does unique-ification
+-- with the right semantics, as per bug #4113. (Unfortunately we have
+-- no simple way to ensure that this test case actually chooses that type
+-- of plan, but it does in releases 7.4-8.3. Note that an ordering difference
+-- here might mean that some other plan type is being used, rendering the test
+-- pointless.)
+--
+
+create temp table numeric_table (num_col numeric);
+insert into numeric_table values (1), (1.000000000000000000001), (2), (3);
+
+create temp table float_table (float_col float8);
+insert into float_table values (1), (2), (3);
+
+select * from float_table
+ where float_col in (select num_col from numeric_table);
+
+select * from numeric_table
+ where num_col in (select float_col from float_table);
+
+--
+-- Test case for bug #4290: bogus calculation of subplan param sets
+--
+
+create temp table ta (id int primary key, val int);
+
+insert into ta values(1,1);
+insert into ta values(2,2);
+
+create temp table tb (id int primary key, aval int);
+
+insert into tb values(1,1);
+insert into tb values(2,1);
+insert into tb values(3,2);
+insert into tb values(4,2);
+
+create temp table tc (id int primary key, aid int);
+
+insert into tc values(1,1);
+insert into tc values(2,2);
+
+select
+ ( select min(tb.id) from tb
+ where tb.aval = (select ta.val from ta where ta.id = tc.aid) ) as min_tb_id
+from tc;
+
+--
+-- Test case for 8.3 "failed to locate grouping columns" bug
+--
+
+create temp table t1 (f1 numeric(14,0), f2 varchar(30));
+
+select * from
+ (select distinct f1, f2, (select f2 from t1 x where x.f1 = up.f1) as fs
+ from t1 up) ss
+group by f1,f2,fs;
+
+--
+-- Test case for bug #5514 (mishandling of whole-row Vars in subselects)
+--
+
+create temp table table_a(id integer);
+insert into table_a values (42);
+
+create temp view view_a as select * from table_a;
+
+select view_a from view_a;
+select (select view_a) from view_a;
+select (select (select view_a)) from view_a;
+select (select (a.*)::text) from view_a a;
+
+--
+-- Check that whole-row Vars reading the result of a subselect don't include
+-- any junk columns therein
+--
+
+select q from (select max(f1) from int4_tbl group by f1 order by f1) q;
+with q as (select max(f1) from int4_tbl group by f1 order by f1)
+ select q from q;
+
+--
+-- Test case for sublinks pulled up into joinaliasvars lists in an
+-- inherited update/delete query
+--
+
+begin; -- this shouldn't delete anything, but be safe
+
+delete from road
+where exists (
+ select 1
+ from
+ int4_tbl cross join
+ ( select f1, array(select q1 from int8_tbl) as arr
+ from text_tbl ) ss
+ where road.name = ss.f1 );
+
+rollback;
+
+--
+-- Test case for sublinks pushed down into subselects via join alias expansion
+--
+
+select
+ (select sq1) as qq1
+from
+ (select exists(select 1 from int4_tbl where f1 = q2) as sq1, 42 as dummy
+ from int8_tbl) sq0
+ join
+ int4_tbl i4 on dummy = i4.f1;
+
+--
+-- Test case for subselect within UPDATE of INSERT...ON CONFLICT DO UPDATE
+--
+create temp table upsert(key int4 primary key, val text);
+insert into upsert values(1, 'val') on conflict (key) do update set val = 'not seen';
+insert into upsert values(1, 'val') on conflict (key) do update set val = 'seen with subselect ' || (select f1 from int4_tbl where f1 != 0 limit 1)::text;
+
+select * from upsert;
+
+with aa as (select 'int4_tbl' u from int4_tbl limit 1)
+insert into upsert values (1, 'x'), (999, 'y')
+on conflict (key) do update set val = (select u from aa)
+returning *;
+
+--
+-- Test case for cross-type partial matching in hashed subplan (bug #7597)
+--
+
+create temp table outer_7597 (f1 int4, f2 int4);
+insert into outer_7597 values (0, 0);
+insert into outer_7597 values (1, 0);
+insert into outer_7597 values (0, null);
+insert into outer_7597 values (1, null);
+
+create temp table inner_7597(c1 int8, c2 int8);
+insert into inner_7597 values(0, null);
+
+select * from outer_7597 where (f1, f2) not in (select * from inner_7597);
+
+--
+-- Similar test case using text that verifies that collation
+-- information is passed through by execTuplesEqual() in nodeSubplan.c
+-- (otherwise it would error in texteq())
+--
+
+create temp table outer_text (f1 text, f2 text);
+insert into outer_text values ('a', 'a');
+insert into outer_text values ('b', 'a');
+insert into outer_text values ('a', null);
+insert into outer_text values ('b', null);
+
+create temp table inner_text (c1 text, c2 text);
+insert into inner_text values ('a', null);
+
+select * from outer_text where (f1, f2) not in (select * from inner_text);
+
+--
+-- Another test case for cross-type hashed subplans: comparison of
+-- inner-side values must be done with appropriate operator
+--
+
+explain (verbose, costs off)
+select 'foo'::text in (select 'bar'::name union all select 'bar'::name);
+
+select 'foo'::text in (select 'bar'::name union all select 'bar'::name);
+
+--
+-- Test case for premature memory release during hashing of subplan output
+--
+
+select '1'::text in (select '1'::name union all select '1'::name);
+
+--
+-- Test case for planner bug with nested EXISTS handling
+--
+select a.thousand from tenk1 a, tenk1 b
+where a.thousand = b.thousand
+ and exists ( select 1 from tenk1 c where b.hundred = c.hundred
+ and not exists ( select 1 from tenk1 d
+ where a.thousand = d.thousand ) );
+
+--
+-- Check that nested sub-selects are not pulled up if they contain volatiles
+--
+explain (verbose, costs off)
+ select x, x from
+ (select (select now()) as x from (values(1),(2)) v(y)) ss;
+explain (verbose, costs off)
+ select x, x from
+ (select (select random()) as x from (values(1),(2)) v(y)) ss;
+explain (verbose, costs off)
+ select x, x from
+ (select (select now() where y=y) as x from (values(1),(2)) v(y)) ss;
+explain (verbose, costs off)
+ select x, x from
+ (select (select random() where y=y) as x from (values(1),(2)) v(y)) ss;
+
+--
+-- Test rescan of a hashed subplan (the use of random() is to prevent the
+-- sub-select from being pulled up, which would result in not hashing)
+--
+explain (verbose, costs off)
+select sum(ss.tst::int) from
+ onek o cross join lateral (
+ select i.ten in (select f1 from int4_tbl where f1 <= o.hundred) as tst,
+ random() as r
+ from onek i where i.unique1 = o.unique1 ) ss
+where o.ten = 0;
+
+select sum(ss.tst::int) from
+ onek o cross join lateral (
+ select i.ten in (select f1 from int4_tbl where f1 <= o.hundred) as tst,
+ random() as r
+ from onek i where i.unique1 = o.unique1 ) ss
+where o.ten = 0;
+
+--
+-- Test rescan of a SetOp node
+--
+explain (costs off)
+select count(*) from
+ onek o cross join lateral (
+ select * from onek i1 where i1.unique1 = o.unique1
+ except
+ select * from onek i2 where i2.unique1 = o.unique2
+ ) ss
+where o.ten = 1;
+
+select count(*) from
+ onek o cross join lateral (
+ select * from onek i1 where i1.unique1 = o.unique1
+ except
+ select * from onek i2 where i2.unique1 = o.unique2
+ ) ss
+where o.ten = 1;
+
+--
+-- Test rescan of a RecursiveUnion node
+--
+explain (costs off)
+select sum(o.four), sum(ss.a) from
+ onek o cross join lateral (
+ with recursive x(a) as
+ (select o.four as a
+ union
+ select a + 1 from x
+ where a < 10)
+ select * from x
+ ) ss
+where o.ten = 1;
+
+select sum(o.four), sum(ss.a) from
+ onek o cross join lateral (
+ with recursive x(a) as
+ (select o.four as a
+ union
+ select a + 1 from x
+ where a < 10)
+ select * from x
+ ) ss
+where o.ten = 1;
+
+--
+-- Check we don't misoptimize a NOT IN where the subquery returns no rows.
+--
+create temp table notinouter (a int);
+create temp table notininner (b int not null);
+insert into notinouter values (null), (1);
+
+select * from notinouter where a not in (select b from notininner);
+
+--
+-- Check we behave sanely in corner case of empty SELECT list (bug #8648)
+--
+create temp table nocolumns();
+select exists(select * from nocolumns);
+
+--
+-- Check behavior with a SubPlan in VALUES (bug #14924)
+--
+select val.x
+ from generate_series(1,10) as s(i),
+ lateral (
+ values ((select s.i + 1)), (s.i + 101)
+ ) as val(x)
+where s.i < 10 and (select val.x) < 110;
+
+-- another variant of that (bug #16213)
+explain (verbose, costs off)
+select * from
+(values
+ (3 not in (select * from (values (1), (2)) ss1)),
+ (false)
+) ss;
+
+select * from
+(values
+ (3 not in (select * from (values (1), (2)) ss1)),
+ (false)
+) ss;
+
+--
+-- Check sane behavior with nested IN SubLinks
+--
+explain (verbose, costs off)
+select * from int4_tbl where
+ (case when f1 in (select unique1 from tenk1 a) then f1 else null end) in
+ (select ten from tenk1 b);
+select * from int4_tbl where
+ (case when f1 in (select unique1 from tenk1 a) then f1 else null end) in
+ (select ten from tenk1 b);
+
+--
+-- Check for incorrect optimization when IN subquery contains a SRF
+--
+explain (verbose, costs off)
+select * from int4_tbl o where (f1, f1) in
+ (select f1, generate_series(1,50) / 10 g from int4_tbl i group by f1);
+select * from int4_tbl o where (f1, f1) in
+ (select f1, generate_series(1,50) / 10 g from int4_tbl i group by f1);
+
+--
+-- check for over-optimization of whole-row Var referencing an Append plan
+--
+select (select q from
+ (select 1,2,3 where f1 > 0
+ union all
+ select 4,5,6.0 where f1 <= 0
+ ) q )
+from int4_tbl;
+
+--
+-- Check for sane handling of a lateral reference in a subquery's quals
+-- (most of the complication here is to prevent the test case from being
+-- flattened too much)
+--
+explain (verbose, costs off)
+select * from
+ int4_tbl i4,
+ lateral (
+ select i4.f1 > 1 as b, 1 as id
+ from (select random() order by 1) as t1
+ union all
+ select true as b, 2 as id
+ ) as t2
+where b and f1 >= 0;
+
+select * from
+ int4_tbl i4,
+ lateral (
+ select i4.f1 > 1 as b, 1 as id
+ from (select random() order by 1) as t1
+ union all
+ select true as b, 2 as id
+ ) as t2
+where b and f1 >= 0;
+
+--
+-- Check that volatile quals aren't pushed down past a DISTINCT:
+-- nextval() should not be called more than the nominal number of times
+--
+create temp sequence ts1;
+
+select * from
+ (select distinct ten from tenk1) ss
+ where ten < 10 + nextval('ts1')
+ order by 1;
+
+select nextval('ts1');
+
+--
+-- Check that volatile quals aren't pushed down past a set-returning function;
+-- while a nonvolatile qual can be, if it doesn't reference the SRF.
+--
+create function tattle(x int, y int) returns bool
+volatile language plpgsql as $$
+begin
+ raise notice 'x = %, y = %', x, y;
+ return x > y;
+end$$;
+
+explain (verbose, costs off)
+select * from
+ (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
+ where tattle(x, 8);
+
+select * from
+ (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
+ where tattle(x, 8);
+
+-- if we pretend it's stable, we get different results:
+alter function tattle(x int, y int) stable;
+
+explain (verbose, costs off)
+select * from
+ (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
+ where tattle(x, 8);
+
+select * from
+ (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
+ where tattle(x, 8);
+
+-- although even a stable qual should not be pushed down if it references SRF
+explain (verbose, costs off)
+select * from
+ (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
+ where tattle(x, u);
+
+select * from
+ (select 9 as x, unnest(array[1,2,3,11,12,13]) as u) ss
+ where tattle(x, u);
+
+drop function tattle(x int, y int);
+
+--
+-- Test that LIMIT can be pushed to SORT through a subquery that just projects
+-- columns. We check for that having happened by looking to see if EXPLAIN
+-- ANALYZE shows that a top-N sort was used. We must suppress or filter away
+-- all the non-invariant parts of the EXPLAIN ANALYZE output.
+--
+create table sq_limit (pk int primary key, c1 int, c2 int);
+insert into sq_limit values
+ (1, 1, 1),
+ (2, 2, 2),
+ (3, 3, 3),
+ (4, 4, 4),
+ (5, 1, 1),
+ (6, 2, 2),
+ (7, 3, 3),
+ (8, 4, 4);
+
+create function explain_sq_limit() returns setof text language plpgsql as
+$$
+declare ln text;
+begin
+ for ln in
+ explain (analyze, summary off, timing off, costs off)
+ select * from (select pk,c2 from sq_limit order by c1,pk) as x limit 3
+ loop
+ ln := regexp_replace(ln, 'Memory: \S*', 'Memory: xxx');
+ return next ln;
+ end loop;
+end;
+$$;
+
+select * from explain_sq_limit();
+
+select * from (select pk,c2 from sq_limit order by c1,pk) as x limit 3;
+
+drop function explain_sq_limit();
+
+drop table sq_limit;
+
+--
+-- Ensure that backward scan direction isn't propagated into
+-- expression subqueries (bug #15336)
+--
+
+begin;
+
+declare c1 scroll cursor for
+ select * from generate_series(1,4) i
+ where i <> all (values (2),(3));
+
+move forward all in c1;
+fetch backward all in c1;
+
+commit;
+
+--
+-- Tests for CTE inlining behavior
+--
+
+-- Basic subquery that can be inlined
+explain (verbose, costs off)
+with x as (select * from (select f1 from subselect_tbl) ss)
+select * from x where f1 = 1;
+
+-- Explicitly request materialization
+explain (verbose, costs off)
+with x as materialized (select * from (select f1 from subselect_tbl) ss)
+select * from x where f1 = 1;
+
+-- Stable functions are safe to inline
+explain (verbose, costs off)
+with x as (select * from (select f1, now() from subselect_tbl) ss)
+select * from x where f1 = 1;
+
+-- Volatile functions prevent inlining
+explain (verbose, costs off)
+with x as (select * from (select f1, random() from subselect_tbl) ss)
+select * from x where f1 = 1;
+
+-- SELECT FOR UPDATE cannot be inlined
+explain (verbose, costs off)
+with x as (select * from (select f1 from subselect_tbl for update) ss)
+select * from x where f1 = 1;
+
+-- Multiply-referenced CTEs are inlined only when requested
+explain (verbose, costs off)
+with x as (select * from (select f1, now() as n from subselect_tbl) ss)
+select * from x, x x2 where x.n = x2.n;
+
+explain (verbose, costs off)
+with x as not materialized (select * from (select f1, now() as n from subselect_tbl) ss)
+select * from x, x x2 where x.n = x2.n;
+
+-- Multiply-referenced CTEs can't be inlined if they contain outer self-refs
+explain (verbose, costs off)
+with recursive x(a) as
+ ((values ('a'), ('b'))
+ union all
+ (with z as not materialized (select * from x)
+ select z.a || z1.a as a from z cross join z as z1
+ where length(z.a || z1.a) < 5))
+select * from x;
+
+with recursive x(a) as
+ ((values ('a'), ('b'))
+ union all
+ (with z as not materialized (select * from x)
+ select z.a || z1.a as a from z cross join z as z1
+ where length(z.a || z1.a) < 5))
+select * from x;
+
+explain (verbose, costs off)
+with recursive x(a) as
+ ((values ('a'), ('b'))
+ union all
+ (with z as not materialized (select * from x)
+ select z.a || z.a as a from z
+ where length(z.a || z.a) < 5))
+select * from x;
+
+with recursive x(a) as
+ ((values ('a'), ('b'))
+ union all
+ (with z as not materialized (select * from x)
+ select z.a || z.a as a from z
+ where length(z.a || z.a) < 5))
+select * from x;
+
+-- Check handling of outer references
+explain (verbose, costs off)
+with x as (select * from int4_tbl)
+select * from (with y as (select * from x) select * from y) ss;
+
+explain (verbose, costs off)
+with x as materialized (select * from int4_tbl)
+select * from (with y as (select * from x) select * from y) ss;
+
+-- Ensure that we inline the currect CTE when there are
+-- multiple CTEs with the same name
+explain (verbose, costs off)
+with x as (select 1 as y)
+select * from (with x as (select 2 as y) select * from x) ss;
+
+-- Row marks are not pushed into CTEs
+explain (verbose, costs off)
+with x as (select * from subselect_tbl)
+select * from x for update;
diff --git a/src/test/resources/postgresql-corpus/transactions.sql b/src/test/resources/postgresql-corpus/transactions.sql
new file mode 100644
index 0000000..03767f8
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/transactions.sql
@@ -0,0 +1,585 @@
+--
+-- TRANSACTIONS
+--
+
+BEGIN;
+
+SELECT *
+ INTO TABLE xacttest
+ FROM aggtest;
+
+INSERT INTO xacttest (a, b) VALUES (777, 777.777);
+
+END;
+
+-- should retrieve one value--
+SELECT a FROM xacttest WHERE a > 100;
+
+
+BEGIN;
+
+CREATE TABLE disappear (a int4);
+
+DELETE FROM aggtest;
+
+-- should be empty
+SELECT * FROM aggtest;
+
+ABORT;
+
+-- should not exist
+SELECT oid FROM pg_class WHERE relname = 'disappear';
+
+-- should have members again
+SELECT * FROM aggtest;
+
+
+-- Read-only tests
+
+CREATE TABLE writetest (a int);
+CREATE TEMPORARY TABLE temptest (a int);
+
+BEGIN;
+SET TRANSACTION ISOLATION LEVEL SERIALIZABLE, READ ONLY, DEFERRABLE; -- ok
+SELECT * FROM writetest; -- ok
+SET TRANSACTION READ WRITE; --fail
+COMMIT;
+
+BEGIN;
+SET TRANSACTION READ ONLY; -- ok
+SET TRANSACTION READ WRITE; -- ok
+SET TRANSACTION READ ONLY; -- ok
+SELECT * FROM writetest; -- ok
+SAVEPOINT x;
+SET TRANSACTION READ ONLY; -- ok
+SELECT * FROM writetest; -- ok
+SET TRANSACTION READ ONLY; -- ok
+SET TRANSACTION READ WRITE; --fail
+COMMIT;
+
+BEGIN;
+SET TRANSACTION READ WRITE; -- ok
+SAVEPOINT x;
+SET TRANSACTION READ WRITE; -- ok
+SET TRANSACTION READ ONLY; -- ok
+SELECT * FROM writetest; -- ok
+SET TRANSACTION READ ONLY; -- ok
+SET TRANSACTION READ WRITE; --fail
+COMMIT;
+
+BEGIN;
+SET TRANSACTION READ WRITE; -- ok
+SAVEPOINT x;
+SET TRANSACTION READ ONLY; -- ok
+SELECT * FROM writetest; -- ok
+ROLLBACK TO SAVEPOINT x;
+SHOW transaction_read_only; -- off
+SAVEPOINT y;
+SET TRANSACTION READ ONLY; -- ok
+SELECT * FROM writetest; -- ok
+RELEASE SAVEPOINT y;
+SHOW transaction_read_only; -- off
+COMMIT;
+
+SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;
+
+DROP TABLE writetest; -- fail
+INSERT INTO writetest VALUES (1); -- fail
+SELECT * FROM writetest; -- ok
+DELETE FROM temptest; -- ok
+UPDATE temptest SET a = 0 FROM writetest WHERE temptest.a = 1 AND writetest.a = temptest.a; -- ok
+PREPARE test AS UPDATE writetest SET a = 0; -- ok
+EXECUTE test; -- fail
+SELECT * FROM writetest, temptest; -- ok
+CREATE TABLE test AS SELECT * FROM writetest; -- fail
+
+START TRANSACTION READ WRITE;
+DROP TABLE writetest; -- ok
+COMMIT;
+
+-- Subtransactions, basic tests
+-- create & drop tables
+SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE;
+CREATE TABLE trans_foobar (a int);
+BEGIN;
+ CREATE TABLE trans_foo (a int);
+ SAVEPOINT one;
+ DROP TABLE trans_foo;
+ CREATE TABLE trans_bar (a int);
+ ROLLBACK TO SAVEPOINT one;
+ RELEASE SAVEPOINT one;
+ SAVEPOINT two;
+ CREATE TABLE trans_baz (a int);
+ RELEASE SAVEPOINT two;
+ drop TABLE trans_foobar;
+ CREATE TABLE trans_barbaz (a int);
+COMMIT;
+-- should exist: trans_barbaz, trans_baz, trans_foo
+SELECT * FROM trans_foo; -- should be empty
+SELECT * FROM trans_bar; -- shouldn't exist
+SELECT * FROM trans_barbaz; -- should be empty
+SELECT * FROM trans_baz; -- should be empty
+
+-- inserts
+BEGIN;
+ INSERT INTO trans_foo VALUES (1);
+ SAVEPOINT one;
+ INSERT into trans_bar VALUES (1);
+ ROLLBACK TO one;
+ RELEASE SAVEPOINT one;
+ SAVEPOINT two;
+ INSERT into trans_barbaz VALUES (1);
+ RELEASE two;
+ SAVEPOINT three;
+ SAVEPOINT four;
+ INSERT INTO trans_foo VALUES (2);
+ RELEASE SAVEPOINT four;
+ ROLLBACK TO SAVEPOINT three;
+ RELEASE SAVEPOINT three;
+ INSERT INTO trans_foo VALUES (3);
+COMMIT;
+SELECT * FROM trans_foo; -- should have 1 and 3
+SELECT * FROM trans_barbaz; -- should have 1
+
+-- test whole-tree commit
+BEGIN;
+ SAVEPOINT one;
+ SELECT trans_foo;
+ ROLLBACK TO SAVEPOINT one;
+ RELEASE SAVEPOINT one;
+ SAVEPOINT two;
+ CREATE TABLE savepoints (a int);
+ SAVEPOINT three;
+ INSERT INTO savepoints VALUES (1);
+ SAVEPOINT four;
+ INSERT INTO savepoints VALUES (2);
+ SAVEPOINT five;
+ INSERT INTO savepoints VALUES (3);
+ ROLLBACK TO SAVEPOINT five;
+COMMIT;
+COMMIT; -- should not be in a transaction block
+SELECT * FROM savepoints;
+
+-- test whole-tree rollback
+BEGIN;
+ SAVEPOINT one;
+ DELETE FROM savepoints WHERE a=1;
+ RELEASE SAVEPOINT one;
+ SAVEPOINT two;
+ DELETE FROM savepoints WHERE a=1;
+ SAVEPOINT three;
+ DELETE FROM savepoints WHERE a=2;
+ROLLBACK;
+COMMIT; -- should not be in a transaction block
+
+SELECT * FROM savepoints;
+
+-- test whole-tree commit on an aborted subtransaction
+BEGIN;
+ INSERT INTO savepoints VALUES (4);
+ SAVEPOINT one;
+ INSERT INTO savepoints VALUES (5);
+ SELECT trans_foo;
+COMMIT;
+SELECT * FROM savepoints;
+
+BEGIN;
+ INSERT INTO savepoints VALUES (6);
+ SAVEPOINT one;
+ INSERT INTO savepoints VALUES (7);
+ RELEASE SAVEPOINT one;
+ INSERT INTO savepoints VALUES (8);
+COMMIT;
+-- rows 6 and 8 should have been created by the same xact
+SELECT a.xmin = b.xmin FROM savepoints a, savepoints b WHERE a.a=6 AND b.a=8;
+-- rows 6 and 7 should have been created by different xacts
+SELECT a.xmin = b.xmin FROM savepoints a, savepoints b WHERE a.a=6 AND b.a=7;
+
+BEGIN;
+ INSERT INTO savepoints VALUES (9);
+ SAVEPOINT one;
+ INSERT INTO savepoints VALUES (10);
+ ROLLBACK TO SAVEPOINT one;
+ INSERT INTO savepoints VALUES (11);
+COMMIT;
+SELECT a FROM savepoints WHERE a in (9, 10, 11);
+-- rows 9 and 11 should have been created by different xacts
+SELECT a.xmin = b.xmin FROM savepoints a, savepoints b WHERE a.a=9 AND b.a=11;
+
+BEGIN;
+ INSERT INTO savepoints VALUES (12);
+ SAVEPOINT one;
+ INSERT INTO savepoints VALUES (13);
+ SAVEPOINT two;
+ INSERT INTO savepoints VALUES (14);
+ ROLLBACK TO SAVEPOINT one;
+ INSERT INTO savepoints VALUES (15);
+ SAVEPOINT two;
+ INSERT INTO savepoints VALUES (16);
+ SAVEPOINT three;
+ INSERT INTO savepoints VALUES (17);
+COMMIT;
+SELECT a FROM savepoints WHERE a BETWEEN 12 AND 17;
+
+BEGIN;
+ INSERT INTO savepoints VALUES (18);
+ SAVEPOINT one;
+ INSERT INTO savepoints VALUES (19);
+ SAVEPOINT two;
+ INSERT INTO savepoints VALUES (20);
+ ROLLBACK TO SAVEPOINT one;
+ INSERT INTO savepoints VALUES (21);
+ ROLLBACK TO SAVEPOINT one;
+ INSERT INTO savepoints VALUES (22);
+COMMIT;
+SELECT a FROM savepoints WHERE a BETWEEN 18 AND 22;
+
+DROP TABLE savepoints;
+
+-- only in a transaction block:
+SAVEPOINT one;
+ROLLBACK TO SAVEPOINT one;
+RELEASE SAVEPOINT one;
+
+-- Only "rollback to" allowed in aborted state
+BEGIN;
+ SAVEPOINT one;
+ SELECT 0/0;
+ SAVEPOINT two; -- ignored till the end of ...
+ RELEASE SAVEPOINT one; -- ignored till the end of ...
+ ROLLBACK TO SAVEPOINT one;
+ SELECT 1;
+COMMIT;
+SELECT 1; -- this should work
+
+-- check non-transactional behavior of cursors
+BEGIN;
+ DECLARE c CURSOR FOR SELECT unique2 FROM tenk1 ORDER BY unique2;
+ SAVEPOINT one;
+ FETCH 10 FROM c;
+ ROLLBACK TO SAVEPOINT one;
+ FETCH 10 FROM c;
+ RELEASE SAVEPOINT one;
+ FETCH 10 FROM c;
+ CLOSE c;
+ DECLARE c CURSOR FOR SELECT unique2/0 FROM tenk1 ORDER BY unique2;
+ SAVEPOINT two;
+ FETCH 10 FROM c;
+ ROLLBACK TO SAVEPOINT two;
+ -- c is now dead to the world ...
+ FETCH 10 FROM c;
+ ROLLBACK TO SAVEPOINT two;
+ RELEASE SAVEPOINT two;
+ FETCH 10 FROM c;
+COMMIT;
+
+--
+-- Check that "stable" functions are really stable. They should not be
+-- able to see the partial results of the calling query. (Ideally we would
+-- also check that they don't see commits of concurrent transactions, but
+-- that's a mite hard to do within the limitations of pg_regress.)
+--
+select * from xacttest;
+
+create or replace function max_xacttest() returns smallint language sql as
+'select max(a) from xacttest' stable;
+
+begin;
+update xacttest set a = max_xacttest() + 10 where a > 0;
+select * from xacttest;
+rollback;
+
+-- But a volatile function can see the partial results of the calling query
+create or replace function max_xacttest() returns smallint language sql as
+'select max(a) from xacttest' volatile;
+
+begin;
+update xacttest set a = max_xacttest() + 10 where a > 0;
+select * from xacttest;
+rollback;
+
+-- Now the same test with plpgsql (since it depends on SPI which is different)
+create or replace function max_xacttest() returns smallint language plpgsql as
+'begin return max(a) from xacttest; end' stable;
+
+begin;
+update xacttest set a = max_xacttest() + 10 where a > 0;
+select * from xacttest;
+rollback;
+
+create or replace function max_xacttest() returns smallint language plpgsql as
+'begin return max(a) from xacttest; end' volatile;
+
+begin;
+update xacttest set a = max_xacttest() + 10 where a > 0;
+select * from xacttest;
+rollback;
+
+
+-- test case for problems with dropping an open relation during abort
+BEGIN;
+ savepoint x;
+ CREATE TABLE koju (a INT UNIQUE);
+ INSERT INTO koju VALUES (1);
+ INSERT INTO koju VALUES (1);
+ rollback to x;
+
+ CREATE TABLE koju (a INT UNIQUE);
+ INSERT INTO koju VALUES (1);
+ INSERT INTO koju VALUES (1);
+ROLLBACK;
+
+DROP TABLE trans_foo;
+DROP TABLE trans_baz;
+DROP TABLE trans_barbaz;
+
+
+-- test case for problems with revalidating an open relation during abort
+create function inverse(int) returns float8 as
+$$
+begin
+ analyze revalidate_bug;
+ return 1::float8/$1;
+exception
+ when division_by_zero then return 0;
+end$$ language plpgsql volatile;
+
+create table revalidate_bug (c float8 unique);
+insert into revalidate_bug values (1);
+insert into revalidate_bug values (inverse(0));
+
+drop table revalidate_bug;
+drop function inverse(int);
+
+
+-- verify that cursors created during an aborted subtransaction are
+-- closed, but that we do not rollback the effect of any FETCHs
+-- performed in the aborted subtransaction
+begin;
+
+savepoint x;
+create table abc (a int);
+insert into abc values (5);
+insert into abc values (10);
+declare foo cursor for select * from abc;
+fetch from foo;
+rollback to x;
+
+-- should fail
+fetch from foo;
+commit;
+
+begin;
+
+create table abc (a int);
+insert into abc values (5);
+insert into abc values (10);
+insert into abc values (15);
+declare foo cursor for select * from abc;
+
+fetch from foo;
+
+savepoint x;
+fetch from foo;
+rollback to x;
+
+fetch from foo;
+
+abort;
+
+
+-- Test for proper cleanup after a failure in a cursor portal
+-- that was created in an outer subtransaction
+CREATE FUNCTION invert(x float8) RETURNS float8 LANGUAGE plpgsql AS
+$$ begin return 1/x; end $$;
+
+CREATE FUNCTION create_temp_tab() RETURNS text
+LANGUAGE plpgsql AS $$
+BEGIN
+ CREATE TEMP TABLE new_table (f1 float8);
+ -- case of interest is that we fail while holding an open
+ -- relcache reference to new_table
+ INSERT INTO new_table SELECT invert(0.0);
+ RETURN 'foo';
+END $$;
+
+BEGIN;
+DECLARE ok CURSOR FOR SELECT * FROM int8_tbl;
+DECLARE ctt CURSOR FOR SELECT create_temp_tab();
+FETCH ok;
+SAVEPOINT s1;
+FETCH ok; -- should work
+FETCH ctt; -- error occurs here
+ROLLBACK TO s1;
+FETCH ok; -- should work
+FETCH ctt; -- must be rejected
+COMMIT;
+
+DROP FUNCTION create_temp_tab();
+DROP FUNCTION invert(x float8);
+
+
+-- Tests for AND CHAIN
+
+CREATE TABLE abc (a int);
+
+-- set nondefault value so we have something to override below
+SET default_transaction_read_only = on;
+
+START TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ WRITE, DEFERRABLE;
+SHOW transaction_isolation;
+SHOW transaction_read_only;
+SHOW transaction_deferrable;
+INSERT INTO abc VALUES (1);
+INSERT INTO abc VALUES (2);
+COMMIT AND CHAIN; -- TBLOCK_END
+SHOW transaction_isolation;
+SHOW transaction_read_only;
+SHOW transaction_deferrable;
+INSERT INTO abc VALUES ('error');
+INSERT INTO abc VALUES (3); -- check it's really aborted
+COMMIT AND CHAIN; -- TBLOCK_ABORT_END
+SHOW transaction_isolation;
+SHOW transaction_read_only;
+SHOW transaction_deferrable;
+INSERT INTO abc VALUES (4);
+COMMIT;
+
+START TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ WRITE, DEFERRABLE;
+SHOW transaction_isolation;
+SHOW transaction_read_only;
+SHOW transaction_deferrable;
+SAVEPOINT x;
+INSERT INTO abc VALUES ('error');
+COMMIT AND CHAIN; -- TBLOCK_ABORT_PENDING
+SHOW transaction_isolation;
+SHOW transaction_read_only;
+SHOW transaction_deferrable;
+INSERT INTO abc VALUES (5);
+COMMIT;
+
+-- different mix of options just for fun
+START TRANSACTION ISOLATION LEVEL SERIALIZABLE, READ WRITE, NOT DEFERRABLE;
+SHOW transaction_isolation;
+SHOW transaction_read_only;
+SHOW transaction_deferrable;
+INSERT INTO abc VALUES (6);
+ROLLBACK AND CHAIN; -- TBLOCK_ABORT_PENDING
+SHOW transaction_isolation;
+SHOW transaction_read_only;
+SHOW transaction_deferrable;
+INSERT INTO abc VALUES ('error');
+ROLLBACK AND CHAIN; -- TBLOCK_ABORT_END
+SHOW transaction_isolation;
+SHOW transaction_read_only;
+SHOW transaction_deferrable;
+ROLLBACK;
+
+-- not allowed outside a transaction block
+COMMIT AND CHAIN; -- error
+ROLLBACK AND CHAIN; -- error
+
+SELECT * FROM abc ORDER BY 1;
+
+RESET default_transaction_read_only;
+
+DROP TABLE abc;
+
+
+-- Test assorted behaviors around the implicit transaction block created
+-- when multiple SQL commands are sent in a single Query message. These
+-- tests rely on the fact that psql will not break SQL commands apart at a
+-- backslash-quoted semicolon, but will send them as one Query.
+
+create temp table i_table (f1 int);
+
+-- psql will show only the last result in a multi-statement Query
+SELECT 1; SELECT 2; SELECT 3;
+
+select * from i_table;
+
+rollback; -- we are not in a transaction at this point
+
+-- can use regular begin/commit/rollback within a single Query
+begin; insert into i_table values(3); commit;
+rollback; -- we are not in a transaction at this point
+begin; insert into i_table values(4); rollback;
+rollback; -- we are not in a transaction at this point
+
+-- begin converts implicit transaction into a regular one that
+-- can extend past the end of the Query
+select 1; begin; insert into i_table values(5);
+commit;
+select 1; begin; insert into i_table values(6);
+rollback;
+
+-- commit in implicit-transaction state commits but issues a warning.
+insert into i_table values(7); commit; insert into i_table values(8); select 1/0;
+-- similarly, rollback aborts but issues a warning.
+insert into i_table values(9); rollback; select 2;
+
+select * from i_table;
+
+rollback; -- we are not in a transaction at this point
+
+-- implicit transaction block is still a transaction block, for e.g. VACUUM
+SELECT 1; VACUUM;
+SELECT 1; COMMIT; VACUUM;
+
+-- we disallow savepoint-related commands in implicit-transaction state
+SELECT 1; SAVEPOINT sp;
+SELECT 1; COMMIT; SAVEPOINT sp;
+ROLLBACK TO SAVEPOINT sp; SELECT 2;
+SELECT 2; RELEASE SAVEPOINT sp; SELECT 3;
+
+-- but this is OK, because the BEGIN converts it to a regular xact
+SELECT 1; BEGIN; SAVEPOINT sp; ROLLBACK TO SAVEPOINT sp; COMMIT;
+
+
+-- Tests for AND CHAIN in implicit transaction blocks
+
+SET TRANSACTION READ ONLY; COMMIT AND CHAIN; -- error
+SHOW transaction_read_only;
+
+SET TRANSACTION READ ONLY; ROLLBACK AND CHAIN; -- error
+SHOW transaction_read_only;
+
+CREATE TABLE abc (a int);
+
+-- COMMIT/ROLLBACK + COMMIT/ROLLBACK AND CHAIN
+INSERT INTO abc VALUES (7); COMMIT; INSERT INTO abc VALUES (8); COMMIT AND CHAIN; -- 7 commit, 8 error
+INSERT INTO abc VALUES (9); ROLLBACK; INSERT INTO abc VALUES (10); ROLLBACK AND CHAIN; -- 9 rollback, 10 error
+
+-- COMMIT/ROLLBACK AND CHAIN + COMMIT/ROLLBACK
+INSERT INTO abc VALUES (11); COMMIT AND CHAIN; INSERT INTO abc VALUES (12); COMMIT; -- 11 error, 12 not reached
+INSERT INTO abc VALUES (13); ROLLBACK AND CHAIN; INSERT INTO abc VALUES (14); ROLLBACK; -- 13 error, 14 not reached
+
+-- START TRANSACTION + COMMIT/ROLLBACK AND CHAIN
+START TRANSACTION ISOLATION LEVEL REPEATABLE READ; INSERT INTO abc VALUES (15); COMMIT AND CHAIN; -- 15 ok
+SHOW transaction_isolation; -- transaction is active at this point
+COMMIT;
+
+START TRANSACTION ISOLATION LEVEL REPEATABLE READ; INSERT INTO abc VALUES (16); ROLLBACK AND CHAIN; -- 16 ok
+SHOW transaction_isolation; -- transaction is active at this point
+ROLLBACK;
+
+-- START TRANSACTION + COMMIT/ROLLBACK + COMMIT/ROLLBACK AND CHAIN
+START TRANSACTION ISOLATION LEVEL REPEATABLE READ; INSERT INTO abc VALUES (17); COMMIT; INSERT INTO abc VALUES (18); COMMIT AND CHAIN; -- 17 commit, 18 error
+SHOW transaction_isolation; -- out of transaction block
+
+START TRANSACTION ISOLATION LEVEL REPEATABLE READ; INSERT INTO abc VALUES (19); ROLLBACK; INSERT INTO abc VALUES (20); ROLLBACK AND CHAIN; -- 19 rollback, 20 error
+SHOW transaction_isolation; -- out of transaction block
+
+SELECT * FROM abc ORDER BY 1;
+
+DROP TABLE abc;
+
+
+-- Test for successful cleanup of an aborted transaction at session exit.
+-- THIS MUST BE THE LAST TEST IN THIS FILE.
+
+begin;
+select 1/0;
+rollback to X;
+
+-- DO NOT ADD ANYTHING HERE.
diff --git a/src/test/resources/postgresql-corpus/union.sql b/src/test/resources/postgresql-corpus/union.sql
new file mode 100644
index 0000000..5f4881d
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/union.sql
@@ -0,0 +1,442 @@
+--
+-- UNION (also INTERSECT, EXCEPT)
+--
+
+-- Simple UNION constructs
+
+SELECT 1 AS two UNION SELECT 2 ORDER BY 1;
+
+SELECT 1 AS one UNION SELECT 1 ORDER BY 1;
+
+SELECT 1 AS two UNION ALL SELECT 2;
+
+SELECT 1 AS two UNION ALL SELECT 1;
+
+SELECT 1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
+
+SELECT 1 AS two UNION SELECT 2 UNION SELECT 2 ORDER BY 1;
+
+SELECT 1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
+
+SELECT 1.1 AS two UNION SELECT 2.2 ORDER BY 1;
+
+-- Mixed types
+
+SELECT 1.1 AS two UNION SELECT 2 ORDER BY 1;
+
+SELECT 1 AS two UNION SELECT 2.2 ORDER BY 1;
+
+SELECT 1 AS one UNION SELECT 1.0::float8 ORDER BY 1;
+
+SELECT 1.1 AS two UNION ALL SELECT 2 ORDER BY 1;
+
+SELECT 1.0::float8 AS two UNION ALL SELECT 1 ORDER BY 1;
+
+SELECT 1.1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
+
+SELECT 1.1::float8 AS two UNION SELECT 2 UNION SELECT 2.0::float8 ORDER BY 1;
+
+SELECT 1.1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
+
+SELECT 1.1 AS two UNION (SELECT 2 UNION ALL SELECT 2) ORDER BY 1;
+
+--
+-- Try testing from tables...
+--
+
+SELECT f1 AS five FROM FLOAT8_TBL
+UNION
+SELECT f1 FROM FLOAT8_TBL
+ORDER BY 1;
+
+SELECT f1 AS ten FROM FLOAT8_TBL
+UNION ALL
+SELECT f1 FROM FLOAT8_TBL;
+
+SELECT f1 AS nine FROM FLOAT8_TBL
+UNION
+SELECT f1 FROM INT4_TBL
+ORDER BY 1;
+
+SELECT f1 AS ten FROM FLOAT8_TBL
+UNION ALL
+SELECT f1 FROM INT4_TBL;
+
+SELECT f1 AS five FROM FLOAT8_TBL
+ WHERE f1 BETWEEN -1e6 AND 1e6
+UNION
+SELECT f1 FROM INT4_TBL
+ WHERE f1 BETWEEN 0 AND 1000000
+ORDER BY 1;
+
+SELECT CAST(f1 AS char(4)) AS three FROM VARCHAR_TBL
+UNION
+SELECT f1 FROM CHAR_TBL
+ORDER BY 1;
+
+SELECT f1 AS three FROM VARCHAR_TBL
+UNION
+SELECT CAST(f1 AS varchar) FROM CHAR_TBL
+ORDER BY 1;
+
+SELECT f1 AS eight FROM VARCHAR_TBL
+UNION ALL
+SELECT f1 FROM CHAR_TBL;
+
+SELECT f1 AS five FROM TEXT_TBL
+UNION
+SELECT f1 FROM VARCHAR_TBL
+UNION
+SELECT TRIM(TRAILING FROM f1) FROM CHAR_TBL
+ORDER BY 1;
+
+--
+-- INTERSECT and EXCEPT
+--
+
+SELECT q2 FROM int8_tbl INTERSECT SELECT q1 FROM int8_tbl ORDER BY 1;
+
+SELECT q2 FROM int8_tbl INTERSECT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
+
+SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
+
+SELECT q2 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
+
+SELECT q2 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q1 FROM int8_tbl ORDER BY 1;
+
+SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY 1;
+
+SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q2 FROM int8_tbl ORDER BY 1;
+
+SELECT q1 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q2 FROM int8_tbl ORDER BY 1;
+
+SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl FOR NO KEY UPDATE;
+
+-- nested cases
+(SELECT 1,2,3 UNION SELECT 4,5,6) INTERSECT SELECT 4,5,6;
+(SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) INTERSECT SELECT 4,5,6;
+(SELECT 1,2,3 UNION SELECT 4,5,6) EXCEPT SELECT 4,5,6;
+(SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) EXCEPT SELECT 4,5,6;
+
+-- exercise both hashed and sorted implementations of INTERSECT/EXCEPT
+
+set enable_hashagg to on;
+
+explain (costs off)
+select count(*) from
+ ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
+select count(*) from
+ ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
+
+explain (costs off)
+select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
+select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
+
+set enable_hashagg to off;
+
+explain (costs off)
+select count(*) from
+ ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
+select count(*) from
+ ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
+
+explain (costs off)
+select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
+select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
+
+reset enable_hashagg;
+
+--
+-- Mixed types
+--
+
+SELECT f1 FROM float8_tbl INTERSECT SELECT f1 FROM int4_tbl ORDER BY 1;
+
+SELECT f1 FROM float8_tbl EXCEPT SELECT f1 FROM int4_tbl ORDER BY 1;
+
+--
+-- Operator precedence and (((((extra))))) parentheses
+--
+
+SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl ORDER BY 1;
+
+SELECT q1 FROM int8_tbl INTERSECT (((SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) ORDER BY 1;
+
+(((SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl ORDER BY 1))) UNION ALL SELECT q2 FROM int8_tbl;
+
+SELECT q1 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
+
+SELECT q1 FROM int8_tbl UNION ALL (((SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1)));
+
+(((SELECT q1 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
+
+--
+-- Subqueries with ORDER BY & LIMIT clauses
+--
+
+-- In this syntax, ORDER BY/LIMIT apply to the result of the EXCEPT
+SELECT q1,q2 FROM int8_tbl EXCEPT SELECT q2,q1 FROM int8_tbl
+ORDER BY q2,q1;
+
+-- This should fail, because q2 isn't a name of an EXCEPT output column
+SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1;
+
+-- But this should work:
+SELECT q1 FROM int8_tbl EXCEPT (((SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1))) ORDER BY 1;
+
+--
+-- New syntaxes (7.1) permit new tests
+--
+
+(((((select * from int8_tbl)))));
+
+--
+-- Check behavior with empty select list (allowed since 9.4)
+--
+
+select union select;
+select intersect select;
+select except select;
+
+-- check hashed implementation
+set enable_hashagg = true;
+set enable_sort = false;
+
+explain (costs off)
+select from generate_series(1,5) union select from generate_series(1,3);
+explain (costs off)
+select from generate_series(1,5) intersect select from generate_series(1,3);
+
+select from generate_series(1,5) union select from generate_series(1,3);
+select from generate_series(1,5) union all select from generate_series(1,3);
+select from generate_series(1,5) intersect select from generate_series(1,3);
+select from generate_series(1,5) intersect all select from generate_series(1,3);
+select from generate_series(1,5) except select from generate_series(1,3);
+select from generate_series(1,5) except all select from generate_series(1,3);
+
+-- check sorted implementation
+set enable_hashagg = false;
+set enable_sort = true;
+
+explain (costs off)
+select from generate_series(1,5) union select from generate_series(1,3);
+explain (costs off)
+select from generate_series(1,5) intersect select from generate_series(1,3);
+
+select from generate_series(1,5) union select from generate_series(1,3);
+select from generate_series(1,5) union all select from generate_series(1,3);
+select from generate_series(1,5) intersect select from generate_series(1,3);
+select from generate_series(1,5) intersect all select from generate_series(1,3);
+select from generate_series(1,5) except select from generate_series(1,3);
+select from generate_series(1,5) except all select from generate_series(1,3);
+
+reset enable_hashagg;
+reset enable_sort;
+
+--
+-- Check handling of a case with unknown constants. We don't guarantee
+-- an undecorated constant will work in all cases, but historically this
+-- usage has worked, so test we don't break it.
+--
+
+SELECT a.f1 FROM (SELECT 'test' AS f1 FROM varchar_tbl) a
+UNION
+SELECT b.f1 FROM (SELECT f1 FROM varchar_tbl) b
+ORDER BY 1;
+
+-- This should fail, but it should produce an error cursor
+SELECT '3.4'::numeric UNION SELECT 'foo';
+
+--
+-- Test that expression-index constraints can be pushed down through
+-- UNION or UNION ALL
+--
+
+CREATE TEMP TABLE t1 (a text, b text);
+CREATE INDEX t1_ab_idx on t1 ((a || b));
+CREATE TEMP TABLE t2 (ab text primary key);
+INSERT INTO t1 VALUES ('a', 'b'), ('x', 'y');
+INSERT INTO t2 VALUES ('ab'), ('xy');
+
+set enable_seqscan = off;
+set enable_indexscan = on;
+set enable_bitmapscan = off;
+
+explain (costs off)
+ SELECT * FROM
+ (SELECT a || b AS ab FROM t1
+ UNION ALL
+ SELECT * FROM t2) t
+ WHERE ab = 'ab';
+
+explain (costs off)
+ SELECT * FROM
+ (SELECT a || b AS ab FROM t1
+ UNION
+ SELECT * FROM t2) t
+ WHERE ab = 'ab';
+
+--
+-- Test that ORDER BY for UNION ALL can be pushed down to inheritance
+-- children.
+--
+
+CREATE TEMP TABLE t1c (b text, a text);
+ALTER TABLE t1c INHERIT t1;
+CREATE TEMP TABLE t2c (primary key (ab)) INHERITS (t2);
+INSERT INTO t1c VALUES ('v', 'w'), ('c', 'd'), ('m', 'n'), ('e', 'f');
+INSERT INTO t2c VALUES ('vw'), ('cd'), ('mn'), ('ef');
+CREATE INDEX t1c_ab_idx on t1c ((a || b));
+
+set enable_seqscan = on;
+set enable_indexonlyscan = off;
+
+explain (costs off)
+ SELECT * FROM
+ (SELECT a || b AS ab FROM t1
+ UNION ALL
+ SELECT ab FROM t2) t
+ ORDER BY 1 LIMIT 8;
+
+ SELECT * FROM
+ (SELECT a || b AS ab FROM t1
+ UNION ALL
+ SELECT ab FROM t2) t
+ ORDER BY 1 LIMIT 8;
+
+reset enable_seqscan;
+reset enable_indexscan;
+reset enable_bitmapscan;
+
+-- This simpler variant of the above test has been observed to fail differently
+
+create table events (event_id int primary key);
+create table other_events (event_id int primary key);
+create table events_child () inherits (events);
+
+explain (costs off)
+select event_id
+ from (select event_id from events
+ union all
+ select event_id from other_events) ss
+ order by event_id;
+
+drop table events_child, events, other_events;
+
+reset enable_indexonlyscan;
+
+-- Test constraint exclusion of UNION ALL subqueries
+explain (costs off)
+ SELECT * FROM
+ (SELECT 1 AS t, * FROM tenk1 a
+ UNION ALL
+ SELECT 2 AS t, * FROM tenk1 b) c
+ WHERE t = 2;
+
+-- Test that we push quals into UNION sub-selects only when it's safe
+explain (costs off)
+SELECT * FROM
+ (SELECT 1 AS t, 2 AS x
+ UNION
+ SELECT 2 AS t, 4 AS x) ss
+WHERE x < 4
+ORDER BY x;
+
+SELECT * FROM
+ (SELECT 1 AS t, 2 AS x
+ UNION
+ SELECT 2 AS t, 4 AS x) ss
+WHERE x < 4
+ORDER BY x;
+
+explain (costs off)
+SELECT * FROM
+ (SELECT 1 AS t, generate_series(1,10) AS x
+ UNION
+ SELECT 2 AS t, 4 AS x) ss
+WHERE x < 4
+ORDER BY x;
+
+SELECT * FROM
+ (SELECT 1 AS t, generate_series(1,10) AS x
+ UNION
+ SELECT 2 AS t, 4 AS x) ss
+WHERE x < 4
+ORDER BY x;
+
+explain (costs off)
+SELECT * FROM
+ (SELECT 1 AS t, (random()*3)::int AS x
+ UNION
+ SELECT 2 AS t, 4 AS x) ss
+WHERE x > 3
+ORDER BY x;
+
+SELECT * FROM
+ (SELECT 1 AS t, (random()*3)::int AS x
+ UNION
+ SELECT 2 AS t, 4 AS x) ss
+WHERE x > 3
+ORDER BY x;
+
+-- Test cases where the native ordering of a sub-select has more pathkeys
+-- than the outer query cares about
+explain (costs off)
+select distinct q1 from
+ (select distinct * from int8_tbl i81
+ union all
+ select distinct * from int8_tbl i82) ss
+where q2 = q2;
+
+select distinct q1 from
+ (select distinct * from int8_tbl i81
+ union all
+ select distinct * from int8_tbl i82) ss
+where q2 = q2;
+
+explain (costs off)
+select distinct q1 from
+ (select distinct * from int8_tbl i81
+ union all
+ select distinct * from int8_tbl i82) ss
+where -q1 = q2;
+
+select distinct q1 from
+ (select distinct * from int8_tbl i81
+ union all
+ select distinct * from int8_tbl i82) ss
+where -q1 = q2;
+
+-- Test proper handling of parameterized appendrel paths when the
+-- potential join qual is expensive
+create function expensivefunc(int) returns int
+language plpgsql immutable strict cost 10000
+as $$begin return $1; end$$;
+
+create temp table t3 as select generate_series(-1000,1000) as x;
+create index t3i on t3 (expensivefunc(x));
+analyze t3;
+
+explain (costs off)
+select * from
+ (select * from t3 a union all select * from t3 b) ss
+ join int4_tbl on f1 = expensivefunc(x);
+select * from
+ (select * from t3 a union all select * from t3 b) ss
+ join int4_tbl on f1 = expensivefunc(x);
+
+drop table t3;
+drop function expensivefunc(int);
+
+-- Test handling of appendrel quals that const-simplify into an AND
+explain (costs off)
+select * from
+ (select *, 0 as x from int8_tbl a
+ union all
+ select *, 1 as x from int8_tbl b) ss
+where (x = 0) or (q1 >= q2 and q1 <= q2);
+select * from
+ (select *, 0 as x from int8_tbl a
+ union all
+ select *, 1 as x from int8_tbl b) ss
+where (x = 0) or (q1 >= q2 and q1 <= q2);
diff --git a/src/test/resources/postgresql-corpus/update.sql b/src/test/resources/postgresql-corpus/update.sql
new file mode 100644
index 0000000..3f36374
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/update.sql
@@ -0,0 +1,611 @@
+--
+-- UPDATE syntax tests
+--
+
+CREATE TABLE update_test (
+ a INT DEFAULT 10,
+ b INT,
+ c TEXT
+);
+
+CREATE TABLE upsert_test (
+ a INT PRIMARY KEY,
+ b TEXT
+);
+
+INSERT INTO update_test VALUES (5, 10, 'foo');
+INSERT INTO update_test(b, a) VALUES (15, 10);
+
+SELECT * FROM update_test;
+
+UPDATE update_test SET a = DEFAULT, b = DEFAULT;
+
+SELECT * FROM update_test;
+
+-- aliases for the UPDATE target table
+UPDATE update_test AS t SET b = 10 WHERE t.a = 10;
+
+SELECT * FROM update_test;
+
+UPDATE update_test t SET b = t.b + 10 WHERE t.a = 10;
+
+SELECT * FROM update_test;
+
+--
+-- Test VALUES in FROM
+--
+
+UPDATE update_test SET a=v.i FROM (VALUES(100, 20)) AS v(i, j)
+ WHERE update_test.b = v.j;
+
+SELECT * FROM update_test;
+
+-- fail, wrong data type:
+UPDATE update_test SET a = v.* FROM (VALUES(100, 20)) AS v(i, j)
+ WHERE update_test.b = v.j;
+
+--
+-- Test multiple-set-clause syntax
+--
+
+INSERT INTO update_test SELECT a,b+1,c FROM update_test;
+SELECT * FROM update_test;
+
+UPDATE update_test SET (c,b,a) = ('bugle', b+11, DEFAULT) WHERE c = 'foo';
+SELECT * FROM update_test;
+UPDATE update_test SET (c,b) = ('car', a+b), a = a + 1 WHERE a = 10;
+SELECT * FROM update_test;
+-- fail, multi assignment to same column:
+UPDATE update_test SET (c,b) = ('car', a+b), b = a + 1 WHERE a = 10;
+
+-- uncorrelated sub-select:
+UPDATE update_test
+ SET (b,a) = (select a,b from update_test where b = 41 and c = 'car')
+ WHERE a = 100 AND b = 20;
+SELECT * FROM update_test;
+-- correlated sub-select:
+UPDATE update_test o
+ SET (b,a) = (select a+1,b from update_test i
+ where i.a=o.a and i.b=o.b and i.c is not distinct from o.c);
+SELECT * FROM update_test;
+-- fail, multiple rows supplied:
+UPDATE update_test SET (b,a) = (select a+1,b from update_test);
+-- set to null if no rows supplied:
+UPDATE update_test SET (b,a) = (select a+1,b from update_test where a = 1000)
+ WHERE a = 11;
+SELECT * FROM update_test;
+-- *-expansion should work in this context:
+UPDATE update_test SET (a,b) = ROW(v.*) FROM (VALUES(21, 100)) AS v(i, j)
+ WHERE update_test.a = v.i;
+-- you might expect this to work, but syntactically it's not a RowExpr:
+UPDATE update_test SET (a,b) = (v.*) FROM (VALUES(21, 101)) AS v(i, j)
+ WHERE update_test.a = v.i;
+
+-- if an alias for the target table is specified, don't allow references
+-- to the original table name
+UPDATE update_test AS t SET b = update_test.b + 10 WHERE t.a = 10;
+
+-- Make sure that we can update to a TOASTed value.
+UPDATE update_test SET c = repeat('x', 10000) WHERE c = 'car';
+SELECT a, b, char_length(c) FROM update_test;
+
+-- Check multi-assignment with a Result node to handle a one-time filter.
+EXPLAIN (VERBOSE, COSTS OFF)
+UPDATE update_test t
+ SET (a, b) = (SELECT b, a FROM update_test s WHERE s.a = t.a)
+ WHERE CURRENT_USER = SESSION_USER;
+UPDATE update_test t
+ SET (a, b) = (SELECT b, a FROM update_test s WHERE s.a = t.a)
+ WHERE CURRENT_USER = SESSION_USER;
+SELECT a, b, char_length(c) FROM update_test;
+
+-- Test ON CONFLICT DO UPDATE
+INSERT INTO upsert_test VALUES(1, 'Boo');
+-- uncorrelated sub-select:
+WITH aaa AS (SELECT 1 AS a, 'Foo' AS b) INSERT INTO upsert_test
+ VALUES (1, 'Bar') ON CONFLICT(a)
+ DO UPDATE SET (b, a) = (SELECT b, a FROM aaa) RETURNING *;
+-- correlated sub-select:
+INSERT INTO upsert_test VALUES (1, 'Baz') ON CONFLICT(a)
+ DO UPDATE SET (b, a) = (SELECT b || ', Correlated', a from upsert_test i WHERE i.a = upsert_test.a)
+ RETURNING *;
+-- correlated sub-select (EXCLUDED.* alias):
+INSERT INTO upsert_test VALUES (1, 'Bat') ON CONFLICT(a)
+ DO UPDATE SET (b, a) = (SELECT b || ', Excluded', a from upsert_test i WHERE i.a = excluded.a)
+ RETURNING *;
+
+-- ON CONFLICT using system attributes in RETURNING, testing both the
+-- inserting and updating paths. See bug report at:
+-- https://www.postgresql.org/message-id/73436355-6432-49B1-92ED-1FE4F7E7E100%40finefun.com.au
+INSERT INTO upsert_test VALUES (2, 'Beeble') ON CONFLICT(a)
+ DO UPDATE SET (b, a) = (SELECT b || ', Excluded', a from upsert_test i WHERE i.a = excluded.a)
+ RETURNING tableoid::regclass, xmin = pg_current_xact_id()::xid AS xmin_correct, xmax = 0 AS xmax_correct;
+-- currently xmax is set after a conflict - that's probably not good,
+-- but it seems worthwhile to have to be explicit if that changes.
+INSERT INTO upsert_test VALUES (2, 'Brox') ON CONFLICT(a)
+ DO UPDATE SET (b, a) = (SELECT b || ', Excluded', a from upsert_test i WHERE i.a = excluded.a)
+ RETURNING tableoid::regclass, xmin = pg_current_xact_id()::xid AS xmin_correct, xmax = pg_current_xact_id()::xid AS xmax_correct;
+
+
+DROP TABLE update_test;
+DROP TABLE upsert_test;
+
+
+---------------------------
+-- UPDATE with row movement
+---------------------------
+
+-- When a partitioned table receives an UPDATE to the partitioned key and the
+-- new values no longer meet the partition's bound, the row must be moved to
+-- the correct partition for the new partition key (if one exists). We must
+-- also ensure that updatable views on partitioned tables properly enforce any
+-- WITH CHECK OPTION that is defined. The situation with triggers in this case
+-- also requires thorough testing as partition key updates causing row
+-- movement convert UPDATEs into DELETE+INSERT.
+
+CREATE TABLE range_parted (
+ a text,
+ b bigint,
+ c numeric,
+ d int,
+ e varchar
+) PARTITION BY RANGE (a, b);
+
+-- Create partitions intentionally in descending bound order, so as to test
+-- that update-row-movement works with the leaf partitions not in bound order.
+CREATE TABLE part_b_20_b_30 (e varchar, c numeric, a text, b bigint, d int);
+ALTER TABLE range_parted ATTACH PARTITION part_b_20_b_30 FOR VALUES FROM ('b', 20) TO ('b', 30);
+CREATE TABLE part_b_10_b_20 (e varchar, c numeric, a text, b bigint, d int) PARTITION BY RANGE (c);
+CREATE TABLE part_b_1_b_10 PARTITION OF range_parted FOR VALUES FROM ('b', 1) TO ('b', 10);
+ALTER TABLE range_parted ATTACH PARTITION part_b_10_b_20 FOR VALUES FROM ('b', 10) TO ('b', 20);
+CREATE TABLE part_a_10_a_20 PARTITION OF range_parted FOR VALUES FROM ('a', 10) TO ('a', 20);
+CREATE TABLE part_a_1_a_10 PARTITION OF range_parted FOR VALUES FROM ('a', 1) TO ('a', 10);
+
+-- Check that partition-key UPDATE works sanely on a partitioned table that
+-- does not have any child partitions.
+UPDATE part_b_10_b_20 set b = b - 6;
+
+-- Create some more partitions following the above pattern of descending bound
+-- order, but let's make the situation a bit more complex by having the
+-- attribute numbers of the columns vary from their parent partition.
+CREATE TABLE part_c_100_200 (e varchar, c numeric, a text, b bigint, d int) PARTITION BY range (abs(d));
+ALTER TABLE part_c_100_200 DROP COLUMN e, DROP COLUMN c, DROP COLUMN a;
+ALTER TABLE part_c_100_200 ADD COLUMN c numeric, ADD COLUMN e varchar, ADD COLUMN a text;
+ALTER TABLE part_c_100_200 DROP COLUMN b;
+ALTER TABLE part_c_100_200 ADD COLUMN b bigint;
+CREATE TABLE part_d_1_15 PARTITION OF part_c_100_200 FOR VALUES FROM (1) TO (15);
+CREATE TABLE part_d_15_20 PARTITION OF part_c_100_200 FOR VALUES FROM (15) TO (20);
+
+ALTER TABLE part_b_10_b_20 ATTACH PARTITION part_c_100_200 FOR VALUES FROM (100) TO (200);
+
+CREATE TABLE part_c_1_100 (e varchar, d int, c numeric, b bigint, a text);
+ALTER TABLE part_b_10_b_20 ATTACH PARTITION part_c_1_100 FOR VALUES FROM (1) TO (100);
+
+----:init_range_parted;
+----:show_data;
+
+-- The order of subplans should be in bound order
+EXPLAIN (costs off) UPDATE range_parted set c = c - 50 WHERE c > 97;
+
+-- fail, row movement happens only within the partition subtree.
+UPDATE part_c_100_200 set c = c - 20, d = c WHERE c = 105;
+-- fail, no partition key update, so no attempt to move tuple,
+-- but "a = 'a'" violates partition constraint enforced by root partition)
+UPDATE part_b_10_b_20 set a = 'a';
+-- ok, partition key update, no constraint violation
+UPDATE range_parted set d = d - 10 WHERE d > 10;
+-- ok, no partition key update, no constraint violation
+UPDATE range_parted set e = d;
+-- No row found
+UPDATE part_c_1_100 set c = c + 20 WHERE c = 98;
+-- ok, row movement
+UPDATE part_b_10_b_20 set c = c + 20 returning c, b, a;
+--:show_data;
+
+-- fail, row movement happens only within the partition subtree.
+UPDATE part_b_10_b_20 set b = b - 6 WHERE c > 116 returning *;
+-- ok, row movement, with subset of rows moved into different partition.
+UPDATE range_parted set b = b - 6 WHERE c > 116 returning a, b + c;
+
+----:show_data;
+
+-- Common table needed for multiple test scenarios.
+CREATE TABLE mintab(c1 int);
+INSERT into mintab VALUES (120);
+
+-- update partition key using updatable view.
+CREATE VIEW upview AS SELECT * FROM range_parted WHERE (select c > c1 FROM mintab) WITH CHECK OPTION;
+-- ok
+UPDATE upview set c = 199 WHERE b = 4;
+-- fail, check option violation
+UPDATE upview set c = 120 WHERE b = 4;
+-- fail, row movement with check option violation
+UPDATE upview set a = 'b', b = 15, c = 120 WHERE b = 4;
+-- ok, row movement, check option passes
+UPDATE upview set a = 'b', b = 15 WHERE b = 4;
+
+--:show_data;
+
+-- cleanup
+DROP VIEW upview;
+
+-- RETURNING having whole-row vars.
+--:init_range_parted;
+UPDATE range_parted set c = 95 WHERE a = 'b' and b > 10 and c > 100 returning (range_parted), *;
+--:show_data;
+
+
+-- Transition tables with update row movement
+----:init_range_parted;
+
+CREATE FUNCTION trans_updatetrigfunc() RETURNS trigger LANGUAGE plpgsql AS
+$$
+ begin
+ raise notice 'trigger = %, old table = %, new table = %',
+ TG_NAME,
+ (select string_agg(old_table::text, ', ' ORDER BY a) FROM old_table),
+ (select string_agg(new_table::text, ', ' ORDER BY a) FROM new_table);
+ return null;
+ end;
+$$;
+
+CREATE TRIGGER trans_updatetrig
+ AFTER UPDATE ON range_parted REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
+ FOR EACH STATEMENT EXECUTE PROCEDURE trans_updatetrigfunc();
+
+UPDATE range_parted set c = (case when c = 96 then 110 else c + 1 end ) WHERE a = 'b' and b > 10 and c >= 96;
+--:show_data;
+----:init_range_parted;
+
+-- Enabling OLD TABLE capture for both DELETE as well as UPDATE stmt triggers
+-- should not cause DELETEd rows to be captured twice. Similar thing for
+-- INSERT triggers and inserted rows.
+CREATE TRIGGER trans_deletetrig
+ AFTER DELETE ON range_parted REFERENCING OLD TABLE AS old_table
+ FOR EACH STATEMENT EXECUTE PROCEDURE trans_updatetrigfunc();
+CREATE TRIGGER trans_inserttrig
+ AFTER INSERT ON range_parted REFERENCING NEW TABLE AS new_table
+ FOR EACH STATEMENT EXECUTE PROCEDURE trans_updatetrigfunc();
+UPDATE range_parted set c = c + 50 WHERE a = 'b' and b > 10 and c >= 96;
+--:show_data;
+DROP TRIGGER trans_deletetrig ON range_parted;
+DROP TRIGGER trans_inserttrig ON range_parted;
+-- Don't drop trans_updatetrig yet. It is required below.
+
+-- Test with transition tuple conversion happening for rows moved into the
+-- new partition. This requires a trigger that references transition table
+-- (we already have trans_updatetrig). For inserted rows, the conversion
+-- is not usually needed, because the original tuple is already compatible with
+-- the desired transition tuple format. But conversion happens when there is a
+-- BR trigger because the trigger can change the inserted row. So install a
+-- BR triggers on those child partitions where the rows will be moved.
+CREATE FUNCTION func_parted_mod_b() RETURNS trigger AS $$
+BEGIN
+ NEW.b = NEW.b + 1;
+ return NEW;
+END $$ language plpgsql;
+CREATE TRIGGER trig_c1_100 BEFORE UPDATE OR INSERT ON part_c_1_100
+ FOR EACH ROW EXECUTE PROCEDURE func_parted_mod_b();
+CREATE TRIGGER trig_d1_15 BEFORE UPDATE OR INSERT ON part_d_1_15
+ FOR EACH ROW EXECUTE PROCEDURE func_parted_mod_b();
+CREATE TRIGGER trig_d15_20 BEFORE UPDATE OR INSERT ON part_d_15_20
+ FOR EACH ROW EXECUTE PROCEDURE func_parted_mod_b();
+----:init_range_parted;
+UPDATE range_parted set c = (case when c = 96 then 110 else c + 1 end) WHERE a = 'b' and b > 10 and c >= 96;
+--:show_data;
+----:init_range_parted;
+UPDATE range_parted set c = c + 50 WHERE a = 'b' and b > 10 and c >= 96;
+--:show_data;
+
+-- Case where per-partition tuple conversion map array is allocated, but the
+-- map is not required for the particular tuple that is routed, thanks to
+-- matching table attributes of the partition and the target table.
+----:init_range_parted;
+UPDATE range_parted set b = 15 WHERE b = 1;
+--:show_data;
+
+DROP TRIGGER trans_updatetrig ON range_parted;
+DROP TRIGGER trig_c1_100 ON part_c_1_100;
+DROP TRIGGER trig_d1_15 ON part_d_1_15;
+DROP TRIGGER trig_d15_20 ON part_d_15_20;
+DROP FUNCTION func_parted_mod_b();
+
+-- RLS policies with update-row-movement
+-----------------------------------------
+
+ALTER TABLE range_parted ENABLE ROW LEVEL SECURITY;
+CREATE USER regress_range_parted_user;
+GRANT ALL ON range_parted, mintab TO regress_range_parted_user;
+CREATE POLICY seeall ON range_parted AS PERMISSIVE FOR SELECT USING (true);
+CREATE POLICY policy_range_parted ON range_parted for UPDATE USING (true) WITH CHECK (c % 2 = 0);
+
+----:init_range_parted;
+SET SESSION AUTHORIZATION regress_range_parted_user;
+-- This should fail with RLS violation error while moving row from
+-- part_a_10_a_20 to part_d_1_15, because we are setting 'c' to an odd number.
+UPDATE range_parted set a = 'b', c = 151 WHERE a = 'a' and c = 200;
+
+RESET SESSION AUTHORIZATION;
+-- Create a trigger on part_d_1_15
+CREATE FUNCTION func_d_1_15() RETURNS trigger AS $$
+BEGIN
+ NEW.c = NEW.c + 1; -- Make even numbers odd, or vice versa
+ return NEW;
+END $$ LANGUAGE plpgsql;
+CREATE TRIGGER trig_d_1_15 BEFORE INSERT ON part_d_1_15
+ FOR EACH ROW EXECUTE PROCEDURE func_d_1_15();
+
+----:init_range_parted;
+SET SESSION AUTHORIZATION regress_range_parted_user;
+
+-- Here, RLS checks should succeed while moving row from part_a_10_a_20 to
+-- part_d_1_15. Even though the UPDATE is setting 'c' to an odd number, the
+-- trigger at the destination partition again makes it an even number.
+UPDATE range_parted set a = 'b', c = 151 WHERE a = 'a' and c = 200;
+
+RESET SESSION AUTHORIZATION;
+----:init_range_parted;
+SET SESSION AUTHORIZATION regress_range_parted_user;
+-- This should fail with RLS violation error. Even though the UPDATE is setting
+-- 'c' to an even number, the trigger at the destination partition again makes
+-- it an odd number.
+UPDATE range_parted set a = 'b', c = 150 WHERE a = 'a' and c = 200;
+
+-- Cleanup
+RESET SESSION AUTHORIZATION;
+DROP TRIGGER trig_d_1_15 ON part_d_1_15;
+DROP FUNCTION func_d_1_15();
+
+-- Policy expression contains SubPlan
+RESET SESSION AUTHORIZATION;
+----:init_range_parted;
+CREATE POLICY policy_range_parted_subplan on range_parted
+ AS RESTRICTIVE for UPDATE USING (true)
+ WITH CHECK ((SELECT range_parted.c <= c1 FROM mintab));
+SET SESSION AUTHORIZATION regress_range_parted_user;
+-- fail, mintab has row with c1 = 120
+UPDATE range_parted set a = 'b', c = 122 WHERE a = 'a' and c = 200;
+-- ok
+UPDATE range_parted set a = 'b', c = 120 WHERE a = 'a' and c = 200;
+
+-- RLS policy expression contains whole row.
+
+RESET SESSION AUTHORIZATION;
+----:init_range_parted;
+CREATE POLICY policy_range_parted_wholerow on range_parted AS RESTRICTIVE for UPDATE USING (true)
+ WITH CHECK (range_parted = row('b', 10, 112, 1, NULL)::range_parted);
+SET SESSION AUTHORIZATION regress_range_parted_user;
+-- ok, should pass the RLS check
+UPDATE range_parted set a = 'b', c = 112 WHERE a = 'a' and c = 200;
+RESET SESSION AUTHORIZATION;
+----:init_range_parted;
+SET SESSION AUTHORIZATION regress_range_parted_user;
+-- fail, the whole row RLS check should fail
+UPDATE range_parted set a = 'b', c = 116 WHERE a = 'a' and c = 200;
+
+-- Cleanup
+RESET SESSION AUTHORIZATION;
+DROP POLICY policy_range_parted ON range_parted;
+DROP POLICY policy_range_parted_subplan ON range_parted;
+DROP POLICY policy_range_parted_wholerow ON range_parted;
+REVOKE ALL ON range_parted, mintab FROM regress_range_parted_user;
+DROP USER regress_range_parted_user;
+DROP TABLE mintab;
+
+
+-- statement triggers with update row movement
+---------------------------------------------------
+
+----:init_range_parted;
+
+CREATE FUNCTION trigfunc() returns trigger language plpgsql as
+$$
+ begin
+ raise notice 'trigger = % fired on table % during %',
+ TG_NAME, TG_TABLE_NAME, TG_OP;
+ return null;
+ end;
+$$;
+-- Triggers on root partition
+CREATE TRIGGER parent_delete_trig
+ AFTER DELETE ON range_parted for each statement execute procedure trigfunc();
+CREATE TRIGGER parent_update_trig
+ AFTER UPDATE ON range_parted for each statement execute procedure trigfunc();
+CREATE TRIGGER parent_insert_trig
+ AFTER INSERT ON range_parted for each statement execute procedure trigfunc();
+
+-- Triggers on leaf partition part_c_1_100
+CREATE TRIGGER c1_delete_trig
+ AFTER DELETE ON part_c_1_100 for each statement execute procedure trigfunc();
+CREATE TRIGGER c1_update_trig
+ AFTER UPDATE ON part_c_1_100 for each statement execute procedure trigfunc();
+CREATE TRIGGER c1_insert_trig
+ AFTER INSERT ON part_c_1_100 for each statement execute procedure trigfunc();
+
+-- Triggers on leaf partition part_d_1_15
+CREATE TRIGGER d1_delete_trig
+ AFTER DELETE ON part_d_1_15 for each statement execute procedure trigfunc();
+CREATE TRIGGER d1_update_trig
+ AFTER UPDATE ON part_d_1_15 for each statement execute procedure trigfunc();
+CREATE TRIGGER d1_insert_trig
+ AFTER INSERT ON part_d_1_15 for each statement execute procedure trigfunc();
+-- Triggers on leaf partition part_d_15_20
+CREATE TRIGGER d15_delete_trig
+ AFTER DELETE ON part_d_15_20 for each statement execute procedure trigfunc();
+CREATE TRIGGER d15_update_trig
+ AFTER UPDATE ON part_d_15_20 for each statement execute procedure trigfunc();
+CREATE TRIGGER d15_insert_trig
+ AFTER INSERT ON part_d_15_20 for each statement execute procedure trigfunc();
+
+-- Move all rows from part_c_100_200 to part_c_1_100. None of the delete or
+-- insert statement triggers should be fired.
+UPDATE range_parted set c = c - 50 WHERE c > 97;
+--:show_data;
+
+DROP TRIGGER parent_delete_trig ON range_parted;
+DROP TRIGGER parent_update_trig ON range_parted;
+DROP TRIGGER parent_insert_trig ON range_parted;
+DROP TRIGGER c1_delete_trig ON part_c_1_100;
+DROP TRIGGER c1_update_trig ON part_c_1_100;
+DROP TRIGGER c1_insert_trig ON part_c_1_100;
+DROP TRIGGER d1_delete_trig ON part_d_1_15;
+DROP TRIGGER d1_update_trig ON part_d_1_15;
+DROP TRIGGER d1_insert_trig ON part_d_1_15;
+DROP TRIGGER d15_delete_trig ON part_d_15_20;
+DROP TRIGGER d15_update_trig ON part_d_15_20;
+DROP TRIGGER d15_insert_trig ON part_d_15_20;
+
+
+-- Creating default partition for range
+----:init_range_parted;
+create table part_def partition of range_parted default;
+insert into range_parted values ('c', 9);
+-- ok
+update part_def set a = 'd' where a = 'c';
+-- fail
+update part_def set a = 'a' where a = 'd';
+
+--:show_data;
+
+-- Update row movement from non-default to default partition.
+-- fail, default partition is not under part_a_10_a_20;
+UPDATE part_a_10_a_20 set a = 'ad' WHERE a = 'a';
+-- ok
+UPDATE range_parted set a = 'ad' WHERE a = 'a';
+UPDATE range_parted set a = 'bd' WHERE a = 'b';
+--:show_data;
+-- Update row movement from default to non-default partitions.
+-- ok
+UPDATE range_parted set a = 'a' WHERE a = 'ad';
+UPDATE range_parted set a = 'b' WHERE a = 'bd';
+--:show_data;
+
+-- Cleanup: range_parted no longer needed.
+DROP TABLE range_parted;
+
+CREATE TABLE list_parted (
+ a text,
+ b int
+) PARTITION BY list (a);
+CREATE TABLE list_part1 PARTITION OF list_parted for VALUES in ('a', 'b');
+CREATE TABLE list_default PARTITION OF list_parted default;
+INSERT into list_part1 VALUES ('a', 1);
+INSERT into list_default VALUES ('d', 10);
+
+-- fail
+UPDATE list_default set a = 'a' WHERE a = 'd';
+-- ok
+UPDATE list_default set a = 'x' WHERE a = 'd';
+
+DROP TABLE list_parted;
+
+--------------
+-- Some more update-partition-key test scenarios below. This time use list
+-- partitions.
+--------------
+
+-- Setup for list partitions
+CREATE TABLE list_parted (a numeric, b int, c int8) PARTITION BY list (a);
+CREATE TABLE sub_parted PARTITION OF list_parted for VALUES in (1) PARTITION BY list (b);
+
+CREATE TABLE sub_part1(b int, c int8, a numeric);
+ALTER TABLE sub_parted ATTACH PARTITION sub_part1 for VALUES in (1);
+CREATE TABLE sub_part2(b int, c int8, a numeric);
+ALTER TABLE sub_parted ATTACH PARTITION sub_part2 for VALUES in (2);
+
+CREATE TABLE list_part1(a numeric, b int, c int8);
+ALTER TABLE list_parted ATTACH PARTITION list_part1 for VALUES in (2,3);
+
+INSERT into list_parted VALUES (2,5,50);
+INSERT into list_parted VALUES (3,6,60);
+INSERT into sub_parted VALUES (1,1,60);
+INSERT into sub_parted VALUES (1,2,10);
+
+-- Test partition constraint violation when intermediate ancestor is used and
+-- constraint is inherited from upper root.
+UPDATE sub_parted set a = 2 WHERE c = 10;
+
+-- Test update-partition-key, where the unpruned partitions do not have their
+-- partition keys updated.
+SELECT tableoid::regclass::text, * FROM list_parted WHERE a = 2 ORDER BY 1;
+UPDATE list_parted set b = c + a WHERE a = 2;
+SELECT tableoid::regclass::text, * FROM list_parted WHERE a = 2 ORDER BY 1;
+
+
+-- Test the case where BR UPDATE triggers change the partition key.
+CREATE FUNCTION func_parted_mod_b() returns trigger as $$
+BEGIN
+ NEW.b = 2; -- This is changing partition key column.
+ return NEW;
+END $$ LANGUAGE plpgsql;
+CREATE TRIGGER parted_mod_b before update on sub_part1
+ for each row execute procedure func_parted_mod_b();
+
+SELECT tableoid::regclass::text, * FROM list_parted ORDER BY 1, 2, 3, 4;
+
+-- This should do the tuple routing even though there is no explicit
+-- partition-key update, because there is a trigger on sub_part1.
+UPDATE list_parted set c = 70 WHERE b = 1;
+SELECT tableoid::regclass::text, * FROM list_parted ORDER BY 1, 2, 3, 4;
+
+DROP TRIGGER parted_mod_b ON sub_part1;
+
+-- If BR DELETE trigger prevented DELETE from happening, we should also skip
+-- the INSERT if that delete is part of UPDATE=>DELETE+INSERT.
+CREATE OR REPLACE FUNCTION func_parted_mod_b() returns trigger as $$
+BEGIN
+ raise notice 'Trigger: Got OLD row %, but returning NULL', OLD;
+ return NULL;
+END $$ LANGUAGE plpgsql;
+CREATE TRIGGER trig_skip_delete before delete on sub_part2
+ for each row execute procedure func_parted_mod_b();
+UPDATE list_parted set b = 1 WHERE c = 70;
+SELECT tableoid::regclass::text, * FROM list_parted ORDER BY 1, 2, 3, 4;
+-- Drop the trigger. Now the row should be moved.
+DROP TRIGGER trig_skip_delete ON sub_part2;
+UPDATE list_parted set b = 1 WHERE c = 70;
+SELECT tableoid::regclass::text, * FROM list_parted ORDER BY 1, 2, 3, 4;
+DROP FUNCTION func_parted_mod_b();
+
+-- UPDATE partition-key with FROM clause. If join produces multiple output
+-- rows for the same row to be modified, we should tuple-route the row only
+-- once. There should not be any rows inserted.
+CREATE TABLE non_parted (id int);
+INSERT into non_parted VALUES (1), (1), (1), (2), (2), (2), (3), (3), (3);
+UPDATE list_parted t1 set a = 2 FROM non_parted t2 WHERE t1.a = t2.id and a = 1;
+SELECT tableoid::regclass::text, * FROM list_parted ORDER BY 1, 2, 3, 4;
+DROP TABLE non_parted;
+
+-- Cleanup: list_parted no longer needed.
+DROP TABLE list_parted;
+
+-- create custom operator class and hash function, for the same reason
+-- explained in alter_table.sql
+create or replace function dummy_hashint4(a int4, seed int8) returns int8 as
+$$ begin return (a + seed); end; $$ language 'plpgsql' immutable;
+create operator class custom_opclass for type int4 using hash as
+operator 1 = , function 2 dummy_hashint4(int4, int8);
+
+create table hash_parted (
+ a int,
+ b int
+) partition by hash (a custom_opclass, b custom_opclass);
+create table hpart1 partition of hash_parted for values with (modulus 2, remainder 1);
+create table hpart2 partition of hash_parted for values with (modulus 4, remainder 2);
+create table hpart3 partition of hash_parted for values with (modulus 8, remainder 0);
+create table hpart4 partition of hash_parted for values with (modulus 8, remainder 4);
+insert into hpart1 values (1, 1);
+insert into hpart2 values (2, 5);
+insert into hpart4 values (3, 4);
+
+-- fail
+update hpart1 set a = 3, b=4 where a = 1;
+-- ok, row movement
+update hash_parted set b = b - 1 where b = 1;
+-- ok
+update hash_parted set b = b + 8 where b = 1;
+
+-- cleanup
+drop table hash_parted;
+drop operator class custom_opclass using hash;
+drop function dummy_hashint4(a int4, seed int8);
diff --git a/src/test/resources/postgresql-corpus/window.sql b/src/test/resources/postgresql-corpus/window.sql
new file mode 100644
index 0000000..e0ab365
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/window.sql
@@ -0,0 +1,1306 @@
+--
+-- WINDOW FUNCTIONS
+--
+
+CREATE TEMPORARY TABLE empsalary (
+ depname varchar,
+ empno bigint,
+ salary int,
+ enroll_date date
+);
+
+INSERT INTO empsalary VALUES
+('develop', 10, 5200, '2007-08-01'),
+('sales', 1, 5000, '2006-10-01'),
+('personnel', 5, 3500, '2007-12-10'),
+('sales', 4, 4800, '2007-08-08'),
+('personnel', 2, 3900, '2006-12-23'),
+('develop', 7, 4200, '2008-01-01'),
+('develop', 9, 4500, '2008-01-01'),
+('sales', 3, 4800, '2007-08-01'),
+('develop', 8, 6000, '2006-10-01'),
+('develop', 11, 5200, '2007-08-15');
+
+SELECT depname, empno, salary, sum(salary) OVER (PARTITION BY depname) FROM empsalary ORDER BY depname, salary;
+
+SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary) FROM empsalary;
+
+-- with GROUP BY
+SELECT four, ten, SUM(SUM(four)) OVER (PARTITION BY four), AVG(ten) FROM tenk1
+GROUP BY four, ten ORDER BY four, ten;
+
+SELECT depname, empno, salary, sum(salary) OVER w FROM empsalary WINDOW w AS (PARTITION BY depname);
+
+SELECT depname, empno, salary, rank() OVER w FROM empsalary WINDOW w AS (PARTITION BY depname ORDER BY salary) ORDER BY rank() OVER w;
+
+-- empty window specification
+SELECT COUNT(*) OVER () FROM tenk1 WHERE unique2 < 10;
+
+SELECT COUNT(*) OVER w FROM tenk1 WHERE unique2 < 10 WINDOW w AS ();
+
+-- no window operation
+SELECT four FROM tenk1 WHERE FALSE WINDOW w AS (PARTITION BY ten);
+
+-- cumulative aggregate
+SELECT sum(four) OVER (PARTITION BY ten ORDER BY unique2) AS sum_1, ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT row_number() OVER (ORDER BY unique2) FROM tenk1 WHERE unique2 < 10;
+
+SELECT rank() OVER (PARTITION BY four ORDER BY ten) AS rank_1, ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT dense_rank() OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT percent_rank() OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT cume_dist() OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT ntile(3) OVER (ORDER BY ten, four), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT ntile(NULL) OVER (ORDER BY ten, four), ten, four FROM tenk1 LIMIT 2;
+
+SELECT lag(ten) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT lag(ten, four) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT lag(ten, four, 0) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT lead(ten) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT lead(ten * 2, 1) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT lead(ten * 2, 1, -1) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT first_value(ten) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+-- last_value returns the last row of the frame, which is CURRENT ROW in ORDER BY window.
+SELECT last_value(four) OVER (ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
+
+SELECT last_value(ten) OVER (PARTITION BY four), ten, four FROM
+ (SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten)s
+ ORDER BY four, ten;
+
+SELECT nth_value(ten, four + 1) OVER (PARTITION BY four), ten, four
+ FROM (SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten)s;
+
+SELECT ten, two, sum(hundred) AS gsum, sum(sum(hundred)) OVER (PARTITION BY two ORDER BY ten) AS wsum
+FROM tenk1 GROUP BY ten, two;
+
+SELECT count(*) OVER (PARTITION BY four), four FROM (SELECT * FROM tenk1 WHERE two = 1)s WHERE unique2 < 10;
+
+SELECT (count(*) OVER (PARTITION BY four ORDER BY ten) +
+ sum(hundred) OVER (PARTITION BY four ORDER BY ten))::varchar AS cntsum
+ FROM tenk1 WHERE unique2 < 10;
+
+-- opexpr with different windows evaluation.
+SELECT * FROM(
+ SELECT count(*) OVER (PARTITION BY four ORDER BY ten) +
+ sum(hundred) OVER (PARTITION BY two ORDER BY ten) AS total,
+ count(*) OVER (PARTITION BY four ORDER BY ten) AS fourcount,
+ sum(hundred) OVER (PARTITION BY two ORDER BY ten) AS twosum
+ FROM tenk1
+)sub
+WHERE total <> fourcount + twosum;
+
+SELECT avg(four) OVER (PARTITION BY four ORDER BY thousand / 100) FROM tenk1 WHERE unique2 < 10;
+
+SELECT ten, two, sum(hundred) AS gsum, sum(sum(hundred)) OVER win AS wsum
+FROM tenk1 GROUP BY ten, two WINDOW win AS (PARTITION BY two ORDER BY ten);
+
+-- more than one window with GROUP BY
+SELECT sum(salary),
+ row_number() OVER (ORDER BY depname),
+ sum(sum(salary)) OVER (ORDER BY depname DESC)
+FROM empsalary GROUP BY depname;
+
+-- identical windows with different names
+SELECT sum(salary) OVER w1, count(*) OVER w2
+FROM empsalary WINDOW w1 AS (ORDER BY salary), w2 AS (ORDER BY salary);
+
+-- subplan
+SELECT lead(ten, (SELECT two FROM tenk1 WHERE s.unique2 = unique2)) OVER (PARTITION BY four ORDER BY ten)
+FROM tenk1 s WHERE unique2 < 10;
+
+-- empty table
+SELECT count(*) OVER (PARTITION BY four) FROM (SELECT * FROM tenk1 WHERE FALSE)s;
+
+-- mixture of agg/wfunc in the same window
+SELECT sum(salary) OVER w, rank() OVER w FROM empsalary WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);
+
+-- strict aggs
+SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDER BY empno), MAX(depadj) OVER () FROM(
+ SELECT *,
+ CASE WHEN enroll_date < '2008-01-01' THEN 2008 - extract(YEAR FROM enroll_date) END * 500 AS bonus,
+ CASE WHEN
+ AVG(salary) OVER (PARTITION BY depname) < salary
+ THEN 200 END AS depadj FROM empsalary
+)s;
+
+-- window function over ungrouped agg over empty row set (bug before 9.1)
+SELECT SUM(COUNT(f1)) OVER () FROM int4_tbl WHERE f1=42;
+
+-- window function with ORDER BY an expression involving aggregates (9.1 bug)
+select ten,
+ sum(unique1) + sum(unique2) as res,
+ rank() over (order by sum(unique1) + sum(unique2)) as rank
+from tenk1
+group by ten order by ten;
+
+-- window and aggregate with GROUP BY expression (9.2 bug)
+explain (costs off)
+select first_value(max(x)) over (), y
+ from (select unique1 as x, ten+four as y from tenk1) ss
+ group by y;
+
+-- test non-default frame specifications
+SELECT four, ten,
+ sum(ten) over (partition by four order by ten),
+ last_value(ten) over (partition by four order by ten)
+FROM (select distinct ten, four from tenk1) ss;
+
+SELECT four, ten,
+ sum(ten) over (partition by four order by ten range between unbounded preceding and current row),
+ last_value(ten) over (partition by four order by ten range between unbounded preceding and current row)
+FROM (select distinct ten, four from tenk1) ss;
+
+SELECT four, ten,
+ sum(ten) over (partition by four order by ten range between unbounded preceding and unbounded following),
+ last_value(ten) over (partition by four order by ten range between unbounded preceding and unbounded following)
+FROM (select distinct ten, four from tenk1) ss;
+
+SELECT four, ten/4 as two,
+ sum(ten/4) over (partition by four order by ten/4 range between unbounded preceding and current row),
+ last_value(ten/4) over (partition by four order by ten/4 range between unbounded preceding and current row)
+FROM (select distinct ten, four from tenk1) ss;
+
+SELECT four, ten/4 as two,
+ sum(ten/4) over (partition by four order by ten/4 rows between unbounded preceding and current row),
+ last_value(ten/4) over (partition by four order by ten/4 rows between unbounded preceding and current row)
+FROM (select distinct ten, four from tenk1) ss;
+
+SELECT sum(unique1) over (order by four range between current row and unbounded following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (rows between current row and unbounded following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (rows between 2 preceding and 2 following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (rows between 2 preceding and 2 following exclude no others),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (rows between 2 preceding and 2 following exclude current row),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (rows between 2 preceding and 2 following exclude group),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (rows between 2 preceding and 2 following exclude ties),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT first_value(unique1) over (ORDER BY four rows between current row and 2 following exclude current row),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT first_value(unique1) over (ORDER BY four rows between current row and 2 following exclude group),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT first_value(unique1) over (ORDER BY four rows between current row and 2 following exclude ties),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT last_value(unique1) over (ORDER BY four rows between current row and 2 following exclude current row),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT last_value(unique1) over (ORDER BY four rows between current row and 2 following exclude group),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT last_value(unique1) over (ORDER BY four rows between current row and 2 following exclude ties),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (rows between 2 preceding and 1 preceding),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (rows between 1 following and 3 following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (rows between unbounded preceding and 1 following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (w range between current row and unbounded following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10 WINDOW w AS (order by four);
+
+SELECT sum(unique1) over (w range between unbounded preceding and current row exclude current row),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10 WINDOW w AS (order by four);
+
+SELECT sum(unique1) over (w range between unbounded preceding and current row exclude group),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10 WINDOW w AS (order by four);
+
+SELECT sum(unique1) over (w range between unbounded preceding and current row exclude ties),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10 WINDOW w AS (order by four);
+
+SELECT first_value(unique1) over w,
+ nth_value(unique1, 2) over w AS nth_2,
+ last_value(unique1) over w, unique1, four
+FROM tenk1 WHERE unique1 < 10
+WINDOW w AS (order by four range between current row and unbounded following);
+
+SELECT sum(unique1) over
+ (order by unique1
+ rows (SELECT unique1 FROM tenk1 ORDER BY unique1 LIMIT 1) + 1 PRECEDING),
+ unique1
+FROM tenk1 WHERE unique1 < 10;
+
+CREATE TEMP VIEW v_window AS
+ SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following) as sum_rows
+ FROM generate_series(1, 10) i;
+
+SELECT * FROM v_window;
+
+SELECT pg_get_viewdef('v_window');
+
+CREATE OR REPLACE TEMP VIEW v_window AS
+ SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following
+ exclude current row) as sum_rows FROM generate_series(1, 10) i;
+
+SELECT * FROM v_window;
+
+SELECT pg_get_viewdef('v_window');
+
+CREATE OR REPLACE TEMP VIEW v_window AS
+ SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following
+ exclude group) as sum_rows FROM generate_series(1, 10) i;
+
+SELECT * FROM v_window;
+
+SELECT pg_get_viewdef('v_window');
+
+CREATE OR REPLACE TEMP VIEW v_window AS
+ SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following
+ exclude ties) as sum_rows FROM generate_series(1, 10) i;
+
+SELECT * FROM v_window;
+
+SELECT pg_get_viewdef('v_window');
+
+CREATE OR REPLACE TEMP VIEW v_window AS
+ SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following
+ exclude no others) as sum_rows FROM generate_series(1, 10) i;
+
+SELECT * FROM v_window;
+
+SELECT pg_get_viewdef('v_window');
+
+CREATE OR REPLACE TEMP VIEW v_window AS
+ SELECT i, sum(i) over (order by i groups between 1 preceding and 1 following) as sum_rows FROM generate_series(1, 10) i;
+
+SELECT * FROM v_window;
+
+SELECT pg_get_viewdef('v_window');
+
+DROP VIEW v_window;
+
+CREATE TEMP VIEW v_window AS
+ SELECT i, min(i) over (order by i range between '1 day' preceding and '10 days' following) as min_i
+ FROM generate_series(now(), now()+'100 days'::interval, '1 hour') i;
+
+SELECT pg_get_viewdef('v_window');
+
+-- RANGE offset PRECEDING/FOLLOWING tests
+
+SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four desc range between 2::int8 preceding and 1::int2 preceding),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding exclude no others),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding exclude current row),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding exclude group),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding exclude ties),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four range between 2::int8 preceding and 6::int2 following exclude ties),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four range between 2::int8 preceding and 6::int2 following exclude group),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (partition by four order by unique1 range between 5::int8 preceding and 6::int2 following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (partition by four order by unique1 range between 5::int8 preceding and 6::int2 following
+ exclude current row),unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+select sum(salary) over (order by enroll_date range between '1 year'::interval preceding and '1 year'::interval following),
+ salary, enroll_date from empsalary;
+
+select sum(salary) over (order by enroll_date desc range between '1 year'::interval preceding and '1 year'::interval following),
+ salary, enroll_date from empsalary;
+
+select sum(salary) over (order by enroll_date desc range between '1 year'::interval following and '1 year'::interval following),
+ salary, enroll_date from empsalary;
+
+select sum(salary) over (order by enroll_date range between '1 year'::interval preceding and '1 year'::interval following
+ exclude current row), salary, enroll_date from empsalary;
+
+select sum(salary) over (order by enroll_date range between '1 year'::interval preceding and '1 year'::interval following
+ exclude group), salary, enroll_date from empsalary;
+
+select sum(salary) over (order by enroll_date range between '1 year'::interval preceding and '1 year'::interval following
+ exclude ties), salary, enroll_date from empsalary;
+
+select first_value(salary) over(order by salary range between 1000 preceding and 1000 following),
+ lead(salary) over(order by salary range between 1000 preceding and 1000 following),
+ nth_value(salary, 1) over(order by salary range between 1000 preceding and 1000 following),
+ salary from empsalary;
+
+select last_value(salary) over(order by salary range between 1000 preceding and 1000 following),
+ lag(salary) over(order by salary range between 1000 preceding and 1000 following),
+ salary from empsalary;
+
+select first_value(salary) over(order by salary range between 1000 following and 3000 following
+ exclude current row),
+ lead(salary) over(order by salary range between 1000 following and 3000 following exclude ties),
+ nth_value(salary, 1) over(order by salary range between 1000 following and 3000 following
+ exclude ties),
+ salary from empsalary;
+
+select last_value(salary) over(order by salary range between 1000 following and 3000 following
+ exclude group),
+ lag(salary) over(order by salary range between 1000 following and 3000 following exclude group),
+ salary from empsalary;
+
+select first_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
+ exclude ties),
+ last_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following),
+ salary, enroll_date from empsalary;
+
+select first_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
+ exclude ties),
+ last_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
+ exclude ties),
+ salary, enroll_date from empsalary;
+
+select first_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
+ exclude group),
+ last_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
+ exclude group),
+ salary, enroll_date from empsalary;
+
+select first_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
+ exclude current row),
+ last_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
+ exclude current row),
+ salary, enroll_date from empsalary;
+
+-- RANGE offset PRECEDING/FOLLOWING with null values
+select x, y,
+ first_value(y) over w,
+ last_value(y) over w
+from
+ (select x, x as y from generate_series(1,5) as x
+ union all select null, 42
+ union all select null, 43) ss
+window w as
+ (order by x asc nulls first range between 2 preceding and 2 following);
+
+select x, y,
+ first_value(y) over w,
+ last_value(y) over w
+from
+ (select x, x as y from generate_series(1,5) as x
+ union all select null, 42
+ union all select null, 43) ss
+window w as
+ (order by x asc nulls last range between 2 preceding and 2 following);
+
+select x, y,
+ first_value(y) over w,
+ last_value(y) over w
+from
+ (select x, x as y from generate_series(1,5) as x
+ union all select null, 42
+ union all select null, 43) ss
+window w as
+ (order by x desc nulls first range between 2 preceding and 2 following);
+
+select x, y,
+ first_value(y) over w,
+ last_value(y) over w
+from
+ (select x, x as y from generate_series(1,5) as x
+ union all select null, 42
+ union all select null, 43) ss
+window w as
+ (order by x desc nulls last range between 2 preceding and 2 following);
+
+-- Check overflow behavior for various integer sizes
+
+select x, last_value(x) over (order by x::smallint range between current row and 2147450884 following)
+from generate_series(32764, 32766) x;
+
+select x, last_value(x) over (order by x::smallint desc range between current row and 2147450885 following)
+from generate_series(-32766, -32764) x;
+
+select x, last_value(x) over (order by x range between current row and 4 following)
+from generate_series(2147483644, 2147483646) x;
+
+select x, last_value(x) over (order by x desc range between current row and 5 following)
+from generate_series(-2147483646, -2147483644) x;
+
+select x, last_value(x) over (order by x range between current row and 4 following)
+from generate_series(9223372036854775804, 9223372036854775806) x;
+
+select x, last_value(x) over (order by x desc range between current row and 5 following)
+from generate_series(-9223372036854775806, -9223372036854775804) x;
+
+-- Test in_range for other numeric datatypes
+
+create temp table numerics(
+ id int,
+ f_float4 float4,
+ f_float8 float8,
+ f_numeric numeric
+);
+
+insert into numerics values
+(0, '-infinity', '-infinity', '-infinity'),
+(1, -3, -3, -3),
+(2, -1, -1, -1),
+(3, 0, 0, 0),
+(4, 1.1, 1.1, 1.1),
+(5, 1.12, 1.12, 1.12),
+(6, 2, 2, 2),
+(7, 100, 100, 100),
+(8, 'infinity', 'infinity', 'infinity'),
+(9, 'NaN', 'NaN', 'NaN');
+
+select id, f_float4, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float4 range between
+ 1 preceding and 1 following);
+select id, f_float4, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float4 range between
+ 1 preceding and 1.1::float4 following);
+select id, f_float4, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float4 range between
+ 'inf' preceding and 'inf' following);
+select id, f_float4, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float4 range between
+ 'inf' preceding and 'inf' preceding);
+select id, f_float4, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float4 range between
+ 'inf' following and 'inf' following);
+select id, f_float4, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float4 range between
+ 1.1 preceding and 'NaN' following); -- error, NaN disallowed
+
+select id, f_float8, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float8 range between
+ 1 preceding and 1 following);
+select id, f_float8, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float8 range between
+ 1 preceding and 1.1::float8 following);
+select id, f_float8, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float8 range between
+ 'inf' preceding and 'inf' following);
+select id, f_float8, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float8 range between
+ 'inf' preceding and 'inf' preceding);
+select id, f_float8, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float8 range between
+ 'inf' following and 'inf' following);
+select id, f_float8, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_float8 range between
+ 1.1 preceding and 'NaN' following); -- error, NaN disallowed
+
+select id, f_numeric, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_numeric range between
+ 1 preceding and 1 following);
+select id, f_numeric, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_numeric range between
+ 1 preceding and 1.1::numeric following);
+select id, f_numeric, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_numeric range between
+ 1 preceding and 1.1::float8 following); -- currently unsupported
+select id, f_numeric, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_numeric range between
+ 'inf' preceding and 'inf' following);
+select id, f_numeric, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_numeric range between
+ 'inf' preceding and 'inf' preceding);
+select id, f_numeric, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_numeric range between
+ 'inf' following and 'inf' following);
+select id, f_numeric, first_value(id) over w, last_value(id) over w
+from numerics
+window w as (order by f_numeric range between
+ 1.1 preceding and 'NaN' following); -- error, NaN disallowed
+
+-- Test in_range for other datetime datatypes
+
+create temp table datetimes(
+ id int,
+ f_time time,
+ f_timetz timetz,
+ f_interval interval,
+ f_timestamptz timestamptz,
+ f_timestamp timestamp
+);
+
+insert into datetimes values
+(1, '11:00', '11:00 BST', '1 year', '2000-10-19 10:23:54+01', '2000-10-19 10:23:54'),
+(2, '12:00', '12:00 BST', '2 years', '2001-10-19 10:23:54+01', '2001-10-19 10:23:54'),
+(3, '13:00', '13:00 BST', '3 years', '2001-10-19 10:23:54+01', '2001-10-19 10:23:54'),
+(4, '14:00', '14:00 BST', '4 years', '2002-10-19 10:23:54+01', '2002-10-19 10:23:54'),
+(5, '15:00', '15:00 BST', '5 years', '2003-10-19 10:23:54+01', '2003-10-19 10:23:54'),
+(6, '15:00', '15:00 BST', '5 years', '2004-10-19 10:23:54+01', '2004-10-19 10:23:54'),
+(7, '17:00', '17:00 BST', '7 years', '2005-10-19 10:23:54+01', '2005-10-19 10:23:54'),
+(8, '18:00', '18:00 BST', '8 years', '2006-10-19 10:23:54+01', '2006-10-19 10:23:54'),
+(9, '19:00', '19:00 BST', '9 years', '2007-10-19 10:23:54+01', '2007-10-19 10:23:54'),
+(10, '20:00', '20:00 BST', '10 years', '2008-10-19 10:23:54+01', '2008-10-19 10:23:54');
+
+select id, f_time, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_time range between
+ '70 min'::interval preceding and '2 hours'::interval following);
+
+select id, f_time, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_time desc range between
+ '70 min' preceding and '2 hours' following);
+
+select id, f_timetz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timetz range between
+ '70 min'::interval preceding and '2 hours'::interval following);
+
+select id, f_timetz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timetz desc range between
+ '70 min' preceding and '2 hours' following);
+
+select id, f_interval, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_interval range between
+ '1 year'::interval preceding and '1 year'::interval following);
+
+select id, f_interval, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_interval desc range between
+ '1 year' preceding and '1 year' following);
+
+select id, f_timestamptz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamptz range between
+ '1 year'::interval preceding and '1 year'::interval following);
+
+select id, f_timestamptz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamptz desc range between
+ '1 year' preceding and '1 year' following);
+
+select id, f_timestamp, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamp range between
+ '1 year'::interval preceding and '1 year'::interval following);
+
+select id, f_timestamp, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamp desc range between
+ '1 year' preceding and '1 year' following);
+
+-- RANGE offset PRECEDING/FOLLOWING error cases
+select sum(salary) over (order by enroll_date, salary range between '1 year'::interval preceding and '2 years'::interval following
+ exclude ties), salary, enroll_date from empsalary;
+
+select sum(salary) over (range between '1 year'::interval preceding and '2 years'::interval following
+ exclude ties), salary, enroll_date from empsalary;
+
+select sum(salary) over (order by depname range between '1 year'::interval preceding and '2 years'::interval following
+ exclude ties), salary, enroll_date from empsalary;
+
+select max(enroll_date) over (order by enroll_date range between 1 preceding and 2 following
+ exclude ties), salary, enroll_date from empsalary;
+
+select max(enroll_date) over (order by salary range between -1 preceding and 2 following
+ exclude ties), salary, enroll_date from empsalary;
+
+select max(enroll_date) over (order by salary range between 1 preceding and -2 following
+ exclude ties), salary, enroll_date from empsalary;
+
+select max(enroll_date) over (order by salary range between '1 year'::interval preceding and '2 years'::interval following
+ exclude ties), salary, enroll_date from empsalary;
+
+select max(enroll_date) over (order by enroll_date range between '1 year'::interval preceding and '-2 years'::interval following
+ exclude ties), salary, enroll_date from empsalary;
+
+-- GROUPS tests
+
+SELECT sum(unique1) over (order by four groups between unbounded preceding and current row),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between unbounded preceding and unbounded following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between current row and unbounded following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between 1 preceding and unbounded following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between 1 following and unbounded following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between unbounded preceding and 2 following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between 2 preceding and 1 preceding),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between 2 preceding and 1 following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between 0 preceding and 0 following),
+ unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between 2 preceding and 1 following
+ exclude current row), unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between 2 preceding and 1 following
+ exclude group), unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (order by four groups between 2 preceding and 1 following
+ exclude ties), unique1, four
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (partition by ten
+ order by four groups between 0 preceding and 0 following),unique1, four, ten
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (partition by ten
+ order by four groups between 0 preceding and 0 following exclude current row), unique1, four, ten
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (partition by ten
+ order by four groups between 0 preceding and 0 following exclude group), unique1, four, ten
+FROM tenk1 WHERE unique1 < 10;
+
+SELECT sum(unique1) over (partition by ten
+ order by four groups between 0 preceding and 0 following exclude ties), unique1, four, ten
+FROM tenk1 WHERE unique1 < 10;
+
+select first_value(salary) over(order by enroll_date groups between 1 preceding and 1 following),
+ lead(salary) over(order by enroll_date groups between 1 preceding and 1 following),
+ nth_value(salary, 1) over(order by enroll_date groups between 1 preceding and 1 following),
+ salary, enroll_date from empsalary;
+
+select last_value(salary) over(order by enroll_date groups between 1 preceding and 1 following),
+ lag(salary) over(order by enroll_date groups between 1 preceding and 1 following),
+ salary, enroll_date from empsalary;
+
+select first_value(salary) over(order by enroll_date groups between 1 following and 3 following
+ exclude current row),
+ lead(salary) over(order by enroll_date groups between 1 following and 3 following exclude ties),
+ nth_value(salary, 1) over(order by enroll_date groups between 1 following and 3 following
+ exclude ties),
+ salary, enroll_date from empsalary;
+
+select last_value(salary) over(order by enroll_date groups between 1 following and 3 following
+ exclude group),
+ lag(salary) over(order by enroll_date groups between 1 following and 3 following exclude group),
+ salary, enroll_date from empsalary;
+
+-- Show differences in offset interpretation between ROWS, RANGE, and GROUPS
+WITH cte (x) AS (
+ SELECT * FROM generate_series(1, 35, 2)
+)
+SELECT x, (sum(x) over w)
+FROM cte
+WINDOW w AS (ORDER BY x rows between 1 preceding and 1 following);
+
+WITH cte (x) AS (
+ SELECT * FROM generate_series(1, 35, 2)
+)
+SELECT x, (sum(x) over w)
+FROM cte
+WINDOW w AS (ORDER BY x range between 1 preceding and 1 following);
+
+WITH cte (x) AS (
+ SELECT * FROM generate_series(1, 35, 2)
+)
+SELECT x, (sum(x) over w)
+FROM cte
+WINDOW w AS (ORDER BY x groups between 1 preceding and 1 following);
+
+WITH cte (x) AS (
+ select 1 union all select 1 union all select 1 union all
+ SELECT * FROM generate_series(5, 49, 2)
+)
+SELECT x, (sum(x) over w)
+FROM cte
+WINDOW w AS (ORDER BY x rows between 1 preceding and 1 following);
+
+WITH cte (x) AS (
+ select 1 union all select 1 union all select 1 union all
+ SELECT * FROM generate_series(5, 49, 2)
+)
+SELECT x, (sum(x) over w)
+FROM cte
+WINDOW w AS (ORDER BY x range between 1 preceding and 1 following);
+
+WITH cte (x) AS (
+ select 1 union all select 1 union all select 1 union all
+ SELECT * FROM generate_series(5, 49, 2)
+)
+SELECT x, (sum(x) over w)
+FROM cte
+WINDOW w AS (ORDER BY x groups between 1 preceding and 1 following);
+
+-- with UNION
+SELECT count(*) OVER (PARTITION BY four) FROM (SELECT * FROM tenk1 UNION ALL SELECT * FROM tenk2)s LIMIT 0;
+
+-- check some degenerate cases
+create temp table t1 (f1 int, f2 int8);
+insert into t1 values (1,1),(1,2),(2,2);
+
+select f1, sum(f1) over (partition by f1
+ range between 1 preceding and 1 following)
+from t1 where f1 = f2; -- error, must have order by
+explain (costs off)
+select f1, sum(f1) over (partition by f1 order by f2
+ range between 1 preceding and 1 following)
+from t1 where f1 = f2;
+select f1, sum(f1) over (partition by f1 order by f2
+ range between 1 preceding and 1 following)
+from t1 where f1 = f2;
+select f1, sum(f1) over (partition by f1, f1 order by f2
+ range between 2 preceding and 1 preceding)
+from t1 where f1 = f2;
+select f1, sum(f1) over (partition by f1, f2 order by f2
+ range between 1 following and 2 following)
+from t1 where f1 = f2;
+
+select f1, sum(f1) over (partition by f1
+ groups between 1 preceding and 1 following)
+from t1 where f1 = f2; -- error, must have order by
+explain (costs off)
+select f1, sum(f1) over (partition by f1 order by f2
+ groups between 1 preceding and 1 following)
+from t1 where f1 = f2;
+select f1, sum(f1) over (partition by f1 order by f2
+ groups between 1 preceding and 1 following)
+from t1 where f1 = f2;
+select f1, sum(f1) over (partition by f1, f1 order by f2
+ groups between 2 preceding and 1 preceding)
+from t1 where f1 = f2;
+select f1, sum(f1) over (partition by f1, f2 order by f2
+ groups between 1 following and 2 following)
+from t1 where f1 = f2;
+
+-- ordering by a non-integer constant is allowed
+SELECT rank() OVER (ORDER BY length('abc'));
+
+-- can't order by another window function
+SELECT rank() OVER (ORDER BY rank() OVER (ORDER BY random()));
+
+-- some other errors
+SELECT * FROM empsalary WHERE row_number() OVER (ORDER BY salary) < 10;
+
+SELECT * FROM empsalary INNER JOIN tenk1 ON row_number() OVER (ORDER BY salary) < 10;
+
+SELECT rank() OVER (ORDER BY 1), count(*) FROM empsalary GROUP BY 1;
+
+--SELECT * FROM rank() OVER (ORDER BY random());
+
+DELETE FROM empsalary WHERE (rank() OVER (ORDER BY random())) > 10;
+
+DELETE FROM empsalary RETURNING rank() OVER (ORDER BY random());
+
+SELECT count(*) OVER w FROM tenk1 WINDOW w AS (ORDER BY unique1), w AS (ORDER BY unique1);
+
+--SELECT rank() OVER (PARTITION BY four, ORDER BY ten) FROM tenk1;
+
+SELECT count() OVER () FROM tenk1;
+
+SELECT generate_series(1, 100) OVER () FROM empsalary;
+
+SELECT ntile(0) OVER (ORDER BY ten), ten, four FROM tenk1;
+
+SELECT nth_value(four, 0) OVER (ORDER BY ten), ten, four FROM tenk1;
+
+-- filter
+
+SELECT sum(salary), row_number() OVER (ORDER BY depname), sum(
+ sum(salary) FILTER (WHERE enroll_date > '2007-01-01')
+) FILTER (WHERE depname <> 'sales') OVER (ORDER BY depname DESC) AS "filtered_sum",
+ depname
+FROM empsalary GROUP BY depname;
+
+-- Test pushdown of quals into a subquery containing window functions
+
+-- pushdown is safe because all PARTITION BY clauses include depname:
+EXPLAIN (COSTS OFF)
+SELECT * FROM
+ (SELECT depname,
+ sum(salary) OVER (PARTITION BY depname) depsalary,
+ min(salary) OVER (PARTITION BY depname || 'A', depname) depminsalary
+ FROM empsalary) emp
+WHERE depname = 'sales';
+
+-- pushdown is unsafe because there's a PARTITION BY clause without depname:
+EXPLAIN (COSTS OFF)
+SELECT * FROM
+ (SELECT depname,
+ sum(salary) OVER (PARTITION BY enroll_date) enroll_salary,
+ min(salary) OVER (PARTITION BY depname) depminsalary
+ FROM empsalary) emp
+WHERE depname = 'sales';
+
+-- Test Sort node collapsing
+EXPLAIN (COSTS OFF)
+SELECT * FROM
+ (SELECT depname,
+ sum(salary) OVER (PARTITION BY depname order by empno) depsalary,
+ min(salary) OVER (PARTITION BY depname, empno order by enroll_date) depminsalary
+ FROM empsalary) emp
+WHERE depname = 'sales';
+
+-- Test Sort node reordering
+EXPLAIN (COSTS OFF)
+SELECT
+ lead(1) OVER (PARTITION BY depname ORDER BY salary, enroll_date),
+ lag(1) OVER (PARTITION BY depname ORDER BY salary,enroll_date,empno)
+FROM empsalary;
+
+-- cleanup
+DROP TABLE empsalary;
+
+-- test user-defined window function with named args and default args
+CREATE FUNCTION nth_value_def(val anyelement, n integer = 1) RETURNS anyelement
+ LANGUAGE internal WINDOW IMMUTABLE STRICT AS 'window_nth_value';
+
+SELECT nth_value_def(n := 2, val := ten) OVER (PARTITION BY four), ten, four
+ FROM (SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten) s;
+
+SELECT nth_value_def(ten) OVER (PARTITION BY four), ten, four
+ FROM (SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten) s;
+
+--
+-- Test the basic moving-aggregate machinery
+--
+
+-- create aggregates that record the series of transform calls (these are
+-- intentionally not true inverses)
+
+CREATE FUNCTION logging_sfunc_nonstrict(text, anyelement) RETURNS text AS
+$$ SELECT COALESCE($1, '') || '*' || quote_nullable($2) $$
+LANGUAGE SQL IMMUTABLE;
+
+CREATE FUNCTION logging_msfunc_nonstrict(text, anyelement) RETURNS text AS
+$$ SELECT COALESCE($1, '') || '+' || quote_nullable($2) $$
+LANGUAGE SQL IMMUTABLE;
+
+CREATE FUNCTION logging_minvfunc_nonstrict(text, anyelement) RETURNS text AS
+$$ SELECT $1 || '-' || quote_nullable($2) $$
+LANGUAGE SQL IMMUTABLE;
+
+CREATE AGGREGATE logging_agg_nonstrict (anyelement)
+(
+ stype = text,
+ sfunc = logging_sfunc_nonstrict,
+ mstype = text,
+ msfunc = logging_msfunc_nonstrict,
+ minvfunc = logging_minvfunc_nonstrict
+);
+
+CREATE AGGREGATE logging_agg_nonstrict_initcond (anyelement)
+(
+ stype = text,
+ sfunc = logging_sfunc_nonstrict,
+ mstype = text,
+ msfunc = logging_msfunc_nonstrict,
+ minvfunc = logging_minvfunc_nonstrict,
+ initcond = 'I',
+ minitcond = 'MI'
+);
+
+CREATE FUNCTION logging_sfunc_strict(text, anyelement) RETURNS text AS
+$$ SELECT $1 || '*' || quote_nullable($2) $$
+LANGUAGE SQL STRICT IMMUTABLE;
+
+CREATE FUNCTION logging_msfunc_strict(text, anyelement) RETURNS text AS
+$$ SELECT $1 || '+' || quote_nullable($2) $$
+LANGUAGE SQL STRICT IMMUTABLE;
+
+CREATE FUNCTION logging_minvfunc_strict(text, anyelement) RETURNS text AS
+$$ SELECT $1 || '-' || quote_nullable($2) $$
+LANGUAGE SQL STRICT IMMUTABLE;
+
+CREATE AGGREGATE logging_agg_strict (text)
+(
+ stype = text,
+ sfunc = logging_sfunc_strict,
+ mstype = text,
+ msfunc = logging_msfunc_strict,
+ minvfunc = logging_minvfunc_strict
+);
+
+CREATE AGGREGATE logging_agg_strict_initcond (anyelement)
+(
+ stype = text,
+ sfunc = logging_sfunc_strict,
+ mstype = text,
+ msfunc = logging_msfunc_strict,
+ minvfunc = logging_minvfunc_strict,
+ initcond = 'I',
+ minitcond = 'MI'
+);
+
+-- test strict and non-strict cases
+SELECT
+ p::text || ',' || i::text || ':' || COALESCE(v::text, 'NULL') AS row,
+ logging_agg_nonstrict(v) over wnd as nstrict,
+ logging_agg_nonstrict_initcond(v) over wnd as nstrict_init,
+ logging_agg_strict(v::text) over wnd as strict,
+ logging_agg_strict_initcond(v) over wnd as strict_init
+FROM (VALUES
+ (1, 1, NULL),
+ (1, 2, 'a'),
+ (1, 3, 'b'),
+ (1, 4, NULL),
+ (1, 5, NULL),
+ (1, 6, 'c'),
+ (2, 1, NULL),
+ (2, 2, 'x'),
+ (3, 1, 'z')
+) AS t(p, i, v)
+WINDOW wnd AS (PARTITION BY P ORDER BY i ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
+ORDER BY p, i;
+
+-- and again, but with filter
+SELECT
+ p::text || ',' || i::text || ':' ||
+ CASE WHEN f THEN COALESCE(v::text, 'NULL') ELSE '-' END as row,
+ logging_agg_nonstrict(v) filter(where f) over wnd as nstrict_filt,
+ logging_agg_nonstrict_initcond(v) filter(where f) over wnd as nstrict_init_filt,
+ logging_agg_strict(v::text) filter(where f) over wnd as strict_filt,
+ logging_agg_strict_initcond(v) filter(where f) over wnd as strict_init_filt
+FROM (VALUES
+ (1, 1, true, NULL),
+ (1, 2, false, 'a'),
+ (1, 3, true, 'b'),
+ (1, 4, false, NULL),
+ (1, 5, false, NULL),
+ (1, 6, false, 'c'),
+ (2, 1, false, NULL),
+ (2, 2, true, 'x'),
+ (3, 1, true, 'z')
+) AS t(p, i, f, v)
+WINDOW wnd AS (PARTITION BY p ORDER BY i ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
+ORDER BY p, i;
+
+-- test that volatile arguments disable moving-aggregate mode
+SELECT
+ i::text || ':' || COALESCE(v::text, 'NULL') as row,
+ logging_agg_strict(v::text)
+ over wnd as inverse,
+ logging_agg_strict(v::text || CASE WHEN random() < 0 then '?' ELSE '' END)
+ over wnd as noinverse
+FROM (VALUES
+ (1, 'a'),
+ (2, 'b'),
+ (3, 'c')
+) AS t(i, v)
+WINDOW wnd AS (ORDER BY i ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
+ORDER BY i;
+
+SELECT
+ i::text || ':' || COALESCE(v::text, 'NULL') as row,
+ logging_agg_strict(v::text) filter(where true)
+ over wnd as inverse,
+ logging_agg_strict(v::text) filter(where random() >= 0)
+ over wnd as noinverse
+FROM (VALUES
+ (1, 'a'),
+ (2, 'b'),
+ (3, 'c')
+) AS t(i, v)
+WINDOW wnd AS (ORDER BY i ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
+ORDER BY i;
+
+-- test that non-overlapping windows don't use inverse transitions
+SELECT
+ logging_agg_strict(v::text) OVER wnd
+FROM (VALUES
+ (1, 'a'),
+ (2, 'b'),
+ (3, 'c')
+) AS t(i, v)
+WINDOW wnd AS (ORDER BY i ROWS BETWEEN CURRENT ROW AND CURRENT ROW)
+ORDER BY i;
+
+-- test that returning NULL from the inverse transition functions
+-- restarts the aggregation from scratch. The second aggregate is supposed
+-- to test cases where only some aggregates restart, the third one checks
+-- that one aggregate restarting doesn't cause others to restart.
+
+CREATE FUNCTION sum_int_randrestart_minvfunc(int4, int4) RETURNS int4 AS
+$$ SELECT CASE WHEN random() < 0.2 THEN NULL ELSE $1 - $2 END $$
+LANGUAGE SQL STRICT;
+
+CREATE AGGREGATE sum_int_randomrestart (int4)
+(
+ stype = int4,
+ sfunc = int4pl,
+ mstype = int4,
+ msfunc = int4pl,
+ minvfunc = sum_int_randrestart_minvfunc
+);
+
+WITH
+vs AS (
+ SELECT i, (random() * 100)::int4 AS v
+ FROM generate_series(1, 100) AS i
+),
+sum_following AS (
+ SELECT i, SUM(v) OVER
+ (ORDER BY i DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS s
+ FROM vs
+)
+SELECT DISTINCT
+ sum_following.s = sum_int_randomrestart(v) OVER fwd AS eq1,
+ -sum_following.s = sum_int_randomrestart(-v) OVER fwd AS eq2,
+ 100*3+(vs.i-1)*3 = length(logging_agg_nonstrict(''::text) OVER fwd) AS eq3
+FROM vs
+JOIN sum_following ON sum_following.i = vs.i
+WINDOW fwd AS (
+ ORDER BY vs.i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
+);
+
+--
+-- Test various built-in aggregates that have moving-aggregate support
+--
+
+-- test inverse transition functions handle NULLs properly
+SELECT i,AVG(v::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,AVG(v::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,AVG(v::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,AVG(v::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1.5),(2,2.5),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,AVG(v::interval) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,'1 sec'),(2,'2 sec'),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,SUM(v::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,SUM(v::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,SUM(v::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,SUM(v::money) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,'1.10'),(2,'2.20'),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,SUM(v::interval) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,'1 sec'),(2,'2 sec'),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,SUM(v::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1.1),(2,2.2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT SUM(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1.01),(2,2),(3,3)) v(i,n);
+
+SELECT i,COUNT(v) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,COUNT(*) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT VAR_POP(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VAR_POP(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VAR_POP(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VAR_POP(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VAR_SAMP(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VAR_SAMP(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VAR_SAMP(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VAR_SAMP(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VARIANCE(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VARIANCE(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VARIANCE(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT VARIANCE(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT STDDEV_POP(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
+
+SELECT STDDEV_POP(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
+
+SELECT STDDEV_POP(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
+
+SELECT STDDEV_POP(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
+
+SELECT STDDEV_SAMP(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
+
+SELECT STDDEV_SAMP(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
+
+SELECT STDDEV_SAMP(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
+
+SELECT STDDEV_SAMP(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
+
+SELECT STDDEV(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT STDDEV(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT STDDEV(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+SELECT STDDEV(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
+ FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
+
+-- test that inverse transition functions work with various frame options
+SELECT i,SUM(v::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND CURRENT ROW)
+ FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,SUM(v::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING)
+ FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
+
+SELECT i,SUM(v::int) OVER (ORDER BY i ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
+ FROM (VALUES(1,1),(2,2),(3,3),(4,4)) t(i,v);
+
+-- ensure aggregate over numeric properly recovers from NaN values
+SELECT a, b,
+ SUM(b) OVER(ORDER BY A ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
+FROM (VALUES(1,1::numeric),(2,2),(3,'NaN'),(4,3),(5,4)) t(a,b);
+
+-- It might be tempting for someone to add an inverse trans function for
+-- float and double precision. This should not be done as it can give incorrect
+-- results. This test should fail if anyone ever does this without thinking too
+-- hard about it.
+SELECT to_char(SUM(n::float8) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING),'999999999999999999999D9')
+ FROM (VALUES(1,1e20),(2,1)) n(i,n);
+
+SELECT i, b, bool_and(b) OVER w, bool_or(b) OVER w
+ FROM (VALUES (1,true), (2,true), (3,false), (4,false), (5,true)) v(i,b)
+ WINDOW w AS (ORDER BY i ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING);
+
+-- Tests for problems with failure to walk or mutate expressions
+-- within window frame clauses.
+
+-- test walker (fails with collation error if expressions are not walked)
+SELECT array_agg(i) OVER w
+ FROM generate_series(1,5) i
+WINDOW w AS (ORDER BY i ROWS BETWEEN (('foo' < 'foobar')::integer) PRECEDING AND CURRENT ROW);
+
+-- test mutator (fails when inlined if expressions are not mutated)
+CREATE FUNCTION pg_temp.f(group_size BIGINT) RETURNS SETOF integer[]
+AS $$
+ SELECT array_agg(s) OVER w
+ FROM generate_series(1,5) s
+ WINDOW w AS (ORDER BY s ROWS BETWEEN CURRENT ROW AND GROUP_SIZE FOLLOWING)
+$$ LANGUAGE SQL STABLE;
+
+EXPLAIN (costs off) SELECT * FROM pg_temp.f(2);
+SELECT * FROM pg_temp.f(2);
diff --git a/src/test/resources/postgresql-corpus/with.sql b/src/test/resources/postgresql-corpus/with.sql
new file mode 100644
index 0000000..9b4077b
--- /dev/null
+++ b/src/test/resources/postgresql-corpus/with.sql
@@ -0,0 +1,1040 @@
+--
+-- Tests for common table expressions (WITH query, ... SELECT ...)
+--
+
+-- Basic WITH
+WITH q1(x,y) AS (SELECT 1,2)
+SELECT * FROM q1, q1 AS q2;
+
+-- Multiple uses are evaluated only once
+SELECT count(*) FROM (
+ WITH q1(x) AS (SELECT random() FROM generate_series(1, 5))
+ SELECT * FROM q1
+ UNION
+ SELECT * FROM q1
+) ss;
+
+-- WITH RECURSIVE
+
+-- sum of 1..100
+WITH RECURSIVE t(n) AS (
+ VALUES (1)
+UNION ALL
+ SELECT n+1 FROM t WHERE n < 100
+)
+SELECT sum(n) FROM t;
+
+WITH RECURSIVE t(n) AS (
+ SELECT (VALUES(1))
+UNION ALL
+ SELECT n+1 FROM t WHERE n < 5
+)
+SELECT * FROM t;
+
+-- recursive view
+CREATE RECURSIVE VIEW nums (n) AS
+ VALUES (1)
+UNION ALL
+ SELECT n+1 FROM nums WHERE n < 5;
+
+SELECT * FROM nums;
+
+CREATE OR REPLACE RECURSIVE VIEW nums (n) AS
+ VALUES (1)
+UNION ALL
+ SELECT n+1 FROM nums WHERE n < 6;
+
+SELECT * FROM nums;
+
+-- This is an infinite loop with UNION ALL, but not with UNION
+WITH RECURSIVE t(n) AS (
+ SELECT 1
+UNION
+ SELECT 10-n FROM t)
+SELECT * FROM t;
+
+-- This'd be an infinite loop, but outside query reads only as much as needed
+WITH RECURSIVE t(n) AS (
+ VALUES (1)
+UNION ALL
+ SELECT n+1 FROM t)
+SELECT * FROM t LIMIT 10;
+
+-- UNION case should have same property
+WITH RECURSIVE t(n) AS (
+ SELECT 1
+UNION
+ SELECT n+1 FROM t)
+SELECT * FROM t LIMIT 10;
+
+-- Test behavior with an unknown-type literal in the WITH
+WITH q AS (SELECT 'foo' AS x)
+SELECT x, x IS OF (text) AS is_text FROM q;
+
+WITH RECURSIVE t(n) AS (
+ SELECT 'foo'
+UNION ALL
+ SELECT n || ' bar' FROM t WHERE length(n) < 20
+)
+SELECT n, n IS OF (text) AS is_text FROM t;
+
+-- In a perfect world, this would work and resolve the literal as int ...
+-- but for now, we have to be content with resolving to text too soon.
+WITH RECURSIVE t(n) AS (
+ SELECT '7'
+UNION ALL
+ SELECT n+1 FROM t WHERE n < 10
+)
+SELECT n, n IS OF (int) AS is_int FROM t;
+
+--
+-- Some examples with a tree
+--
+-- department structure represented here is as follows:
+--
+-- ROOT-+->A-+->B-+->C
+-- | |
+-- | +->D-+->F
+-- +->E-+->G
+
+CREATE TEMP TABLE department (
+ id INTEGER PRIMARY KEY, -- department ID
+ parent_department INTEGER REFERENCES department, -- upper department ID
+ name TEXT -- department name
+);
+
+INSERT INTO department VALUES (0, NULL, 'ROOT');
+INSERT INTO department VALUES (1, 0, 'A');
+INSERT INTO department VALUES (2, 1, 'B');
+INSERT INTO department VALUES (3, 2, 'C');
+INSERT INTO department VALUES (4, 2, 'D');
+INSERT INTO department VALUES (5, 0, 'E');
+INSERT INTO department VALUES (6, 4, 'F');
+INSERT INTO department VALUES (7, 5, 'G');
+
+
+-- extract all departments under 'A'. Result should be A, B, C, D and F
+WITH RECURSIVE subdepartment AS
+(
+ -- non recursive term
+ SELECT name as root_name, * FROM department WHERE name = 'A'
+
+ UNION ALL
+
+ -- recursive term
+ SELECT sd.root_name, d.* FROM department AS d, subdepartment AS sd
+ WHERE d.parent_department = sd.id
+)
+SELECT * FROM subdepartment ORDER BY name;
+
+-- extract all departments under 'A' with "level" number
+WITH RECURSIVE subdepartment(level, id, parent_department, name) AS
+(
+ -- non recursive term
+ SELECT 1, * FROM department WHERE name = 'A'
+
+ UNION ALL
+
+ -- recursive term
+ SELECT sd.level + 1, d.* FROM department AS d, subdepartment AS sd
+ WHERE d.parent_department = sd.id
+)
+SELECT * FROM subdepartment ORDER BY name;
+
+-- extract all departments under 'A' with "level" number.
+-- Only shows level 2 or more
+WITH RECURSIVE subdepartment(level, id, parent_department, name) AS
+(
+ -- non recursive term
+ SELECT 1, * FROM department WHERE name = 'A'
+
+ UNION ALL
+
+ -- recursive term
+ SELECT sd.level + 1, d.* FROM department AS d, subdepartment AS sd
+ WHERE d.parent_department = sd.id
+)
+SELECT * FROM subdepartment WHERE level >= 2 ORDER BY name;
+
+-- "RECURSIVE" is ignored if the query has no self-reference
+WITH RECURSIVE subdepartment AS
+(
+ -- note lack of recursive UNION structure
+ SELECT * FROM department WHERE name = 'A'
+)
+SELECT * FROM subdepartment ORDER BY name;
+
+-- inside subqueries
+SELECT count(*) FROM (
+ WITH RECURSIVE t(n) AS (
+ SELECT 1 UNION ALL SELECT n + 1 FROM t WHERE n < 500
+ )
+ SELECT * FROM t) AS t WHERE n < (
+ SELECT count(*) FROM (
+ WITH RECURSIVE t(n) AS (
+ SELECT 1 UNION ALL SELECT n + 1 FROM t WHERE n < 100
+ )
+ SELECT * FROM t WHERE n < 50000
+ ) AS t WHERE n < 100);
+
+-- use same CTE twice at different subquery levels
+WITH q1(x,y) AS (
+ SELECT hundred, sum(ten) FROM tenk1 GROUP BY hundred
+ )
+SELECT count(*) FROM q1 WHERE y > (SELECT sum(y)/100 FROM q1 qsub);
+
+-- via a VIEW
+CREATE TEMPORARY VIEW vsubdepartment AS
+ WITH RECURSIVE subdepartment AS
+ (
+ -- non recursive term
+ SELECT * FROM department WHERE name = 'A'
+ UNION ALL
+ -- recursive term
+ SELECT d.* FROM department AS d, subdepartment AS sd
+ WHERE d.parent_department = sd.id
+ )
+ SELECT * FROM subdepartment;
+
+SELECT * FROM vsubdepartment ORDER BY name;
+
+-- Check reverse listing
+SELECT pg_get_viewdef('vsubdepartment'::regclass);
+SELECT pg_get_viewdef('vsubdepartment'::regclass, true);
+
+-- Another reverse-listing example
+CREATE VIEW sums_1_100 AS
+WITH RECURSIVE t(n) AS (
+ VALUES (1)
+UNION ALL
+ SELECT n+1 FROM t WHERE n < 100
+)
+SELECT sum(n) FROM t;
+
+
+-- corner case in which sub-WITH gets initialized first
+with recursive q as (
+ select * from department
+ union all
+ (with x as (select * from q)
+ select * from x)
+ )
+select * from q limit 24;
+
+with recursive q as (
+ select * from department
+ union all
+ (with recursive x as (
+ select * from department
+ union all
+ (select * from q union all select * from x)
+ )
+ select * from x)
+ )
+select * from q limit 32;
+
+-- recursive term has sub-UNION
+WITH RECURSIVE t(i,j) AS (
+ VALUES (1,2)
+ UNION ALL
+ SELECT t2.i, t.j+1 FROM
+ (SELECT 2 AS i UNION ALL SELECT 3 AS i) AS t2
+ JOIN t ON (t2.i = t.i+1))
+
+ SELECT * FROM t;
+
+--
+-- different tree example
+--
+CREATE TEMPORARY TABLE tree(
+ id INTEGER PRIMARY KEY,
+ parent_id INTEGER REFERENCES tree(id)
+);
+
+INSERT INTO tree
+VALUES (1, NULL), (2, 1), (3,1), (4,2), (5,2), (6,2), (7,3), (8,3),
+ (9,4), (10,4), (11,7), (12,7), (13,7), (14, 9), (15,11), (16,11);
+
+--
+-- get all paths from "second level" nodes to leaf nodes
+--
+WITH RECURSIVE t(id, path) AS (
+ VALUES(1,ARRAY[]::integer[])
+UNION ALL
+ SELECT tree.id, t.path || tree.id
+ FROM tree JOIN t ON (tree.parent_id = t.id)
+)
+SELECT t1.*, t2.* FROM t AS t1 JOIN t AS t2 ON
+ (t1.path[1] = t2.path[1] AND
+ array_upper(t1.path,1) = 1 AND
+ array_upper(t2.path,1) > 1)
+ ORDER BY t1.id, t2.id;
+
+-- just count 'em
+WITH RECURSIVE t(id, path) AS (
+ VALUES(1,ARRAY[]::integer[])
+UNION ALL
+ SELECT tree.id, t.path || tree.id
+ FROM tree JOIN t ON (tree.parent_id = t.id)
+)
+SELECT t1.id, count(t2.*) FROM t AS t1 JOIN t AS t2 ON
+ (t1.path[1] = t2.path[1] AND
+ array_upper(t1.path,1) = 1 AND
+ array_upper(t2.path,1) > 1)
+ GROUP BY t1.id
+ ORDER BY t1.id;
+
+-- this variant tickled a whole-row-variable bug in 8.4devel
+WITH RECURSIVE t(id, path) AS (
+ VALUES(1,ARRAY[]::integer[])
+UNION ALL
+ SELECT tree.id, t.path || tree.id
+ FROM tree JOIN t ON (tree.parent_id = t.id)
+)
+SELECT t1.id, t2.path, t2 FROM t AS t1 JOIN t AS t2 ON
+(t1.id=t2.id);
+
+--
+-- test cycle detection
+--
+create temp table graph( f int, t int, label text );
+
+insert into graph values
+ (1, 2, 'arc 1 -> 2'),
+ (1, 3, 'arc 1 -> 3'),
+ (2, 3, 'arc 2 -> 3'),
+ (1, 4, 'arc 1 -> 4'),
+ (4, 5, 'arc 4 -> 5'),
+ (5, 1, 'arc 5 -> 1');
+
+with recursive search_graph(f, t, label, path, cycle) as (
+ select *, array[row(g.f, g.t)], false from graph g
+ union all
+ select g.*, path || row(g.f, g.t), row(g.f, g.t) = any(path)
+ from graph g, search_graph sg
+ where g.f = sg.t and not cycle
+)
+select * from search_graph;
+
+-- ordering by the path column has same effect as SEARCH DEPTH FIRST
+with recursive search_graph(f, t, label, path, cycle) as (
+ select *, array[row(g.f, g.t)], false from graph g
+ union all
+ select g.*, path || row(g.f, g.t), row(g.f, g.t) = any(path)
+ from graph g, search_graph sg
+ where g.f = sg.t and not cycle
+)
+select * from search_graph order by path;
+
+--
+-- test multiple WITH queries
+--
+WITH RECURSIVE
+ y (id) AS (VALUES (1)),
+ x (id) AS (SELECT * FROM y UNION ALL SELECT id+1 FROM x WHERE id < 5)
+SELECT * FROM x;
+
+-- forward reference OK
+WITH RECURSIVE
+ x(id) AS (SELECT * FROM y UNION ALL SELECT id+1 FROM x WHERE id < 5),
+ y(id) AS (values (1))
+ SELECT * FROM x;
+
+WITH RECURSIVE
+ x(id) AS
+ (VALUES (1) UNION ALL SELECT id+1 FROM x WHERE id < 5),
+ y(id) AS
+ (VALUES (1) UNION ALL SELECT id+1 FROM y WHERE id < 10)
+ SELECT y.*, x.* FROM y LEFT JOIN x USING (id);
+
+WITH RECURSIVE
+ x(id) AS
+ (VALUES (1) UNION ALL SELECT id+1 FROM x WHERE id < 5),
+ y(id) AS
+ (VALUES (1) UNION ALL SELECT id+1 FROM x WHERE id < 10)
+ SELECT y.*, x.* FROM y LEFT JOIN x USING (id);
+
+WITH RECURSIVE
+ x(id) AS
+ (SELECT 1 UNION ALL SELECT id+1 FROM x WHERE id < 3 ),
+ y(id) AS
+ (SELECT * FROM x UNION ALL SELECT * FROM x),
+ z(id) AS
+ (SELECT * FROM x UNION ALL SELECT id+1 FROM z WHERE id < 10)
+ SELECT * FROM z;
+
+WITH RECURSIVE
+ x(id) AS
+ (SELECT 1 UNION ALL SELECT id+1 FROM x WHERE id < 3 ),
+ y(id) AS
+ (SELECT * FROM x UNION ALL SELECT * FROM x),
+ z(id) AS
+ (SELECT * FROM y UNION ALL SELECT id+1 FROM z WHERE id < 10)
+ SELECT * FROM z;
+
+--
+-- Test WITH attached to a data-modifying statement
+--
+
+CREATE TEMPORARY TABLE y (a INTEGER);
+INSERT INTO y SELECT generate_series(1, 10);
+
+WITH t AS (
+ SELECT a FROM y
+)
+INSERT INTO y
+SELECT a+20 FROM t RETURNING *;
+
+SELECT * FROM y;
+
+WITH t AS (
+ SELECT a FROM y
+)
+UPDATE y SET a = y.a-10 FROM t WHERE y.a > 20 AND t.a = y.a RETURNING y.a;
+
+SELECT * FROM y;
+
+WITH RECURSIVE t(a) AS (
+ SELECT 11
+ UNION ALL
+ SELECT a+1 FROM t WHERE a < 50
+)
+DELETE FROM y USING t WHERE t.a = y.a RETURNING y.a;
+
+SELECT * FROM y;
+
+DROP TABLE y;
+
+--
+-- error cases
+--
+
+-- INTERSECT
+WITH RECURSIVE x(n) AS (SELECT 1 INTERSECT SELECT n+1 FROM x)
+ SELECT * FROM x;
+
+WITH RECURSIVE x(n) AS (SELECT 1 INTERSECT ALL SELECT n+1 FROM x)
+ SELECT * FROM x;
+
+-- EXCEPT
+WITH RECURSIVE x(n) AS (SELECT 1 EXCEPT SELECT n+1 FROM x)
+ SELECT * FROM x;
+
+WITH RECURSIVE x(n) AS (SELECT 1 EXCEPT ALL SELECT n+1 FROM x)
+ SELECT * FROM x;
+
+-- no non-recursive term
+WITH RECURSIVE x(n) AS (SELECT n FROM x)
+ SELECT * FROM x;
+
+-- recursive term in the left hand side (strictly speaking, should allow this)
+WITH RECURSIVE x(n) AS (SELECT n FROM x UNION ALL SELECT 1)
+ SELECT * FROM x;
+
+CREATE TEMPORARY TABLE y (a INTEGER);
+INSERT INTO y SELECT generate_series(1, 10);
+
+-- LEFT JOIN
+
+WITH RECURSIVE x(n) AS (SELECT a FROM y WHERE a = 1
+ UNION ALL
+ SELECT x.n+1 FROM y LEFT JOIN x ON x.n = y.a WHERE n < 10)
+SELECT * FROM x;
+
+-- RIGHT JOIN
+WITH RECURSIVE x(n) AS (SELECT a FROM y WHERE a = 1
+ UNION ALL
+ SELECT x.n+1 FROM x RIGHT JOIN y ON x.n = y.a WHERE n < 10)
+SELECT * FROM x;
+
+-- FULL JOIN
+WITH RECURSIVE x(n) AS (SELECT a FROM y WHERE a = 1
+ UNION ALL
+ SELECT x.n+1 FROM x FULL JOIN y ON x.n = y.a WHERE n < 10)
+SELECT * FROM x;
+
+-- subquery
+WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x
+ WHERE n IN (SELECT * FROM x))
+ SELECT * FROM x;
+
+-- aggregate functions
+WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT count(*) FROM x)
+ SELECT * FROM x;
+
+WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT sum(n) FROM x)
+ SELECT * FROM x;
+
+-- ORDER BY
+WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x ORDER BY 1)
+ SELECT * FROM x;
+
+-- LIMIT/OFFSET
+WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x LIMIT 10 OFFSET 1)
+ SELECT * FROM x;
+
+-- FOR UPDATE
+WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x FOR UPDATE)
+ SELECT * FROM x;
+
+-- target list has a recursive query name
+WITH RECURSIVE x(id) AS (values (1)
+ UNION ALL
+ SELECT (SELECT * FROM x) FROM x WHERE id < 5
+) SELECT * FROM x;
+
+-- mutual recursive query (not implemented)
+WITH RECURSIVE
+ x (id) AS (SELECT 1 UNION ALL SELECT id+1 FROM y WHERE id < 5),
+ y (id) AS (SELECT 1 UNION ALL SELECT id+1 FROM x WHERE id < 5)
+SELECT * FROM x;
+
+-- non-linear recursion is not allowed
+WITH RECURSIVE foo(i) AS
+ (values (1)
+ UNION ALL
+ (SELECT i+1 FROM foo WHERE i < 10
+ UNION ALL
+ SELECT i+1 FROM foo WHERE i < 5)
+) SELECT * FROM foo;
+
+WITH RECURSIVE foo(i) AS
+ (values (1)
+ UNION ALL
+ SELECT * FROM
+ (SELECT i+1 FROM foo WHERE i < 10
+ UNION ALL
+ SELECT i+1 FROM foo WHERE i < 5) AS t
+) SELECT * FROM foo;
+
+WITH RECURSIVE foo(i) AS
+ (values (1)
+ UNION ALL
+ (SELECT i+1 FROM foo WHERE i < 10
+ EXCEPT
+ SELECT i+1 FROM foo WHERE i < 5)
+) SELECT * FROM foo;
+
+WITH RECURSIVE foo(i) AS
+ (values (1)
+ UNION ALL
+ (SELECT i+1 FROM foo WHERE i < 10
+ INTERSECT
+ SELECT i+1 FROM foo WHERE i < 5)
+) SELECT * FROM foo;
+
+-- Wrong type induced from non-recursive term
+WITH RECURSIVE foo(i) AS
+ (SELECT i FROM (VALUES(1),(2)) t(i)
+ UNION ALL
+ SELECT (i+1)::numeric(10,0) FROM foo WHERE i < 10)
+SELECT * FROM foo;
+
+-- rejects different typmod, too (should we allow this?)
+WITH RECURSIVE foo(i) AS
+ (SELECT i::numeric(3,0) FROM (VALUES(1),(2)) t(i)
+ UNION ALL
+ SELECT (i+1)::numeric(10,0) FROM foo WHERE i < 10)
+SELECT * FROM foo;
+
+-- disallow OLD/NEW reference in CTE
+CREATE TEMPORARY TABLE x (n integer);
+CREATE RULE r2 AS ON UPDATE TO x DO INSTEAD
+ WITH t AS (SELECT OLD.*) UPDATE y SET a = t.n FROM t;
+
+--
+-- test for bug #4902
+--
+with cte(foo) as ( values(42) ) values((select foo from cte));
+with cte(foo) as ( select 42 ) select * from ((select foo from cte)) q;
+
+-- test CTE referencing an outer-level variable (to see that changed-parameter
+-- signaling still works properly after fixing this bug)
+select ( with cte(foo) as ( values(f1) )
+ select (select foo from cte) )
+from int4_tbl;
+
+select ( with cte(foo) as ( values(f1) )
+ values((select foo from cte)) )
+from int4_tbl;
+
+--
+-- test for nested-recursive-WITH bug
+--
+WITH RECURSIVE t(j) AS (
+ WITH RECURSIVE s(i) AS (
+ VALUES (1)
+ UNION ALL
+ SELECT i+1 FROM s WHERE i < 10
+ )
+ SELECT i FROM s
+ UNION ALL
+ SELECT j+1 FROM t WHERE j < 10
+)
+SELECT * FROM t;
+
+--
+-- test WITH attached to intermediate-level set operation
+--
+
+WITH outermost(x) AS (
+ SELECT 1
+ UNION (WITH innermost as (SELECT 2)
+ SELECT * FROM innermost
+ UNION SELECT 3)
+)
+SELECT * FROM outermost ORDER BY 1;
+
+WITH outermost(x) AS (
+ SELECT 1
+ UNION (WITH innermost as (SELECT 2)
+ SELECT * FROM outermost -- fail
+ UNION SELECT * FROM innermost)
+)
+SELECT * FROM outermost ORDER BY 1;
+
+WITH RECURSIVE outermost(x) AS (
+ SELECT 1
+ UNION (WITH innermost as (SELECT 2)
+ SELECT * FROM outermost
+ UNION SELECT * FROM innermost)
+)
+SELECT * FROM outermost ORDER BY 1;
+
+WITH RECURSIVE outermost(x) AS (
+ WITH innermost as (SELECT 2 FROM outermost) -- fail
+ SELECT * FROM innermost
+ UNION SELECT * from outermost
+)
+SELECT * FROM outermost ORDER BY 1;
+
+--
+-- This test will fail with the old implementation of PARAM_EXEC parameter
+-- assignment, because the "q1" Var passed down to A's targetlist subselect
+-- looks exactly like the "A.id" Var passed down to C's subselect, causing
+-- the old code to give them the same runtime PARAM_EXEC slot. But the
+-- lifespans of the two parameters overlap, thanks to B also reading A.
+--
+
+with
+A as ( select q2 as id, (select q1) as x from int8_tbl ),
+B as ( select id, row_number() over (partition by id) as r from A ),
+C as ( select A.id, array(select B.id from B where B.id = A.id) from A )
+select * from C;
+
+--
+-- Test CTEs read in non-initialization orders
+--
+
+WITH RECURSIVE
+ tab(id_key,link) AS (VALUES (1,17), (2,17), (3,17), (4,17), (6,17), (5,17)),
+ iter (id_key, row_type, link) AS (
+ SELECT 0, 'base', 17
+ UNION ALL (
+ WITH remaining(id_key, row_type, link, min) AS (
+ SELECT tab.id_key, 'true'::text, iter.link, MIN(tab.id_key) OVER ()
+ FROM tab INNER JOIN iter USING (link)
+ WHERE tab.id_key > iter.id_key
+ ),
+ first_remaining AS (
+ SELECT id_key, row_type, link
+ FROM remaining
+ WHERE id_key=min
+ ),
+ effect AS (
+ SELECT tab.id_key, 'new'::text, tab.link
+ FROM first_remaining e INNER JOIN tab ON e.id_key=tab.id_key
+ WHERE e.row_type = 'false'
+ )
+ SELECT * FROM first_remaining
+ UNION ALL SELECT * FROM effect
+ )
+ )
+SELECT * FROM iter;
+
+WITH RECURSIVE
+ tab(id_key,link) AS (VALUES (1,17), (2,17), (3,17), (4,17), (6,17), (5,17)),
+ iter (id_key, row_type, link) AS (
+ SELECT 0, 'base', 17
+ UNION (
+ WITH remaining(id_key, row_type, link, min) AS (
+ SELECT tab.id_key, 'true'::text, iter.link, MIN(tab.id_key) OVER ()
+ FROM tab INNER JOIN iter USING (link)
+ WHERE tab.id_key > iter.id_key
+ ),
+ first_remaining AS (
+ SELECT id_key, row_type, link
+ FROM remaining
+ WHERE id_key=min
+ ),
+ effect AS (
+ SELECT tab.id_key, 'new'::text, tab.link
+ FROM first_remaining e INNER JOIN tab ON e.id_key=tab.id_key
+ WHERE e.row_type = 'false'
+ )
+ SELECT * FROM first_remaining
+ UNION ALL SELECT * FROM effect
+ )
+ )
+SELECT * FROM iter;
+
+--
+-- Data-modifying statements in WITH
+--
+
+-- INSERT ... RETURNING
+WITH t AS (
+ INSERT INTO y
+ VALUES
+ (11),
+ (12),
+ (13),
+ (14),
+ (15),
+ (16),
+ (17),
+ (18),
+ (19),
+ (20)
+ RETURNING *
+)
+SELECT * FROM t;
+
+SELECT * FROM y;
+
+-- UPDATE ... RETURNING
+WITH t AS (
+ UPDATE y
+ SET a=a+1
+ RETURNING *
+)
+SELECT * FROM t;
+
+SELECT * FROM y;
+
+-- DELETE ... RETURNING
+WITH t AS (
+ DELETE FROM y
+ WHERE a <= 10
+ RETURNING *
+)
+SELECT * FROM t;
+
+SELECT * FROM y;
+
+-- forward reference
+WITH RECURSIVE t AS (
+ INSERT INTO y
+ SELECT a+5 FROM t2 WHERE a > 5
+ RETURNING *
+), t2 AS (
+ UPDATE y SET a=a-11 RETURNING *
+)
+SELECT * FROM t
+UNION ALL
+SELECT * FROM t2;
+
+SELECT * FROM y;
+
+-- unconditional DO INSTEAD rule
+CREATE RULE y_rule AS ON DELETE TO y DO INSTEAD
+ INSERT INTO y VALUES(42) RETURNING *;
+
+WITH t AS (
+ DELETE FROM y RETURNING *
+)
+SELECT * FROM t;
+
+SELECT * FROM y;
+
+DROP RULE y_rule ON y;
+
+-- check merging of outer CTE with CTE in a rule action
+CREATE TEMP TABLE bug6051 AS
+ select i from generate_series(1,3) as t(i);
+
+SELECT * FROM bug6051;
+
+WITH t1 AS ( DELETE FROM bug6051 RETURNING * )
+INSERT INTO bug6051 SELECT * FROM t1;
+
+SELECT * FROM bug6051;
+
+CREATE TEMP TABLE bug6051_2 (i int);
+
+CREATE RULE bug6051_ins AS ON INSERT TO bug6051 DO INSTEAD
+ INSERT INTO bug6051_2
+ SELECT NEW.i;
+
+WITH t1 AS ( DELETE FROM bug6051 RETURNING * )
+INSERT INTO bug6051 SELECT * FROM t1;
+
+SELECT * FROM bug6051;
+SELECT * FROM bug6051_2;
+
+-- a truly recursive CTE in the same list
+WITH RECURSIVE t(a) AS (
+ SELECT 0
+ UNION ALL
+ SELECT a+1 FROM t WHERE a+1 < 5
+), t2 as (
+ INSERT INTO y
+ SELECT * FROM t RETURNING *
+)
+SELECT * FROM t2 JOIN y USING (a) ORDER BY a;
+
+SELECT * FROM y;
+
+-- data-modifying WITH in a modifying statement
+WITH t AS (
+ DELETE FROM y
+ WHERE a <= 10
+ RETURNING *
+)
+INSERT INTO y SELECT -a FROM t RETURNING *;
+
+SELECT * FROM y;
+
+-- check that WITH query is run to completion even if outer query isn't
+WITH t AS (
+ UPDATE y SET a = a * 100 RETURNING *
+)
+SELECT * FROM t LIMIT 10;
+
+SELECT * FROM y;
+
+-- data-modifying WITH containing INSERT...ON CONFLICT DO UPDATE
+CREATE TABLE withz AS SELECT i AS k, (i || ' v')::text v FROM generate_series(1, 16, 3) i;
+ALTER TABLE withz ADD UNIQUE (k);
+
+WITH t AS (
+ INSERT INTO withz SELECT i, 'insert'
+ FROM generate_series(0, 16) i
+ ON CONFLICT (k) DO UPDATE SET v = withz.v || ', now update'
+ RETURNING *
+)
+SELECT * FROM t JOIN y ON t.k = y.a ORDER BY a, k;
+
+-- Test EXCLUDED.* reference within CTE
+WITH aa AS (
+ INSERT INTO withz VALUES(1, 5) ON CONFLICT (k) DO UPDATE SET v = EXCLUDED.v
+ WHERE withz.k != EXCLUDED.k
+ RETURNING *
+)
+SELECT * FROM aa;
+
+-- New query/snapshot demonstrates side-effects of previous query.
+SELECT * FROM withz ORDER BY k;
+
+--
+-- Ensure subqueries within the update clause work, even if they
+-- reference outside values
+--
+WITH aa AS (SELECT 1 a, 2 b)
+INSERT INTO withz VALUES(1, 'insert')
+ON CONFLICT (k) DO UPDATE SET v = (SELECT b || ' update' FROM aa WHERE a = 1 LIMIT 1);
+WITH aa AS (SELECT 1 a, 2 b)
+INSERT INTO withz VALUES(1, 'insert')
+ON CONFLICT (k) DO UPDATE SET v = ' update' WHERE withz.k = (SELECT a FROM aa);
+WITH aa AS (SELECT 1 a, 2 b)
+INSERT INTO withz VALUES(1, 'insert')
+ON CONFLICT (k) DO UPDATE SET v = (SELECT b || ' update' FROM aa WHERE a = 1 LIMIT 1);
+WITH aa AS (SELECT 'a' a, 'b' b UNION ALL SELECT 'a' a, 'b' b)
+INSERT INTO withz VALUES(1, 'insert')
+ON CONFLICT (k) DO UPDATE SET v = (SELECT b || ' update' FROM aa WHERE a = 'a' LIMIT 1);
+WITH aa AS (SELECT 1 a, 2 b)
+INSERT INTO withz VALUES(1, (SELECT b || ' insert' FROM aa WHERE a = 1 ))
+ON CONFLICT (k) DO UPDATE SET v = (SELECT b || ' update' FROM aa WHERE a = 1 LIMIT 1);
+
+-- Update a row more than once, in different parts of a wCTE. That is
+-- an allowed, presumably very rare, edge case, but since it was
+-- broken in the past, having a test seems worthwhile.
+WITH simpletup AS (
+ SELECT 2 k, 'Green' v),
+upsert_cte AS (
+ INSERT INTO withz VALUES(2, 'Blue') ON CONFLICT (k) DO
+ UPDATE SET (k, v) = (SELECT k, v FROM simpletup WHERE simpletup.k = withz.k)
+ RETURNING k, v)
+INSERT INTO withz VALUES(2, 'Red') ON CONFLICT (k) DO
+UPDATE SET (k, v) = (SELECT k, v FROM upsert_cte WHERE upsert_cte.k = withz.k)
+RETURNING k, v;
+
+DROP TABLE withz;
+
+-- check that run to completion happens in proper ordering
+
+TRUNCATE TABLE y;
+INSERT INTO y SELECT generate_series(1, 3);
+CREATE TEMPORARY TABLE yy (a INTEGER);
+
+WITH RECURSIVE t1 AS (
+ INSERT INTO y SELECT * FROM y RETURNING *
+), t2 AS (
+ INSERT INTO yy SELECT * FROM t1 RETURNING *
+)
+SELECT 1;
+
+SELECT * FROM y;
+SELECT * FROM yy;
+
+WITH RECURSIVE t1 AS (
+ INSERT INTO yy SELECT * FROM t2 RETURNING *
+), t2 AS (
+ INSERT INTO y SELECT * FROM y RETURNING *
+)
+SELECT 1;
+
+SELECT * FROM y;
+SELECT * FROM yy;
+
+-- triggers
+
+TRUNCATE TABLE y;
+INSERT INTO y SELECT generate_series(1, 10);
+
+CREATE FUNCTION y_trigger() RETURNS trigger AS $$
+begin
+ raise notice 'y_trigger: a = %', new.a;
+ return new;
+end;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER y_trig BEFORE INSERT ON y FOR EACH ROW
+ EXECUTE PROCEDURE y_trigger();
+
+WITH t AS (
+ INSERT INTO y
+ VALUES
+ (21),
+ (22),
+ (23)
+ RETURNING *
+)
+SELECT * FROM t;
+
+SELECT * FROM y;
+
+DROP TRIGGER y_trig ON y;
+
+CREATE TRIGGER y_trig AFTER INSERT ON y FOR EACH ROW
+ EXECUTE PROCEDURE y_trigger();
+
+WITH t AS (
+ INSERT INTO y
+ VALUES
+ (31),
+ (32),
+ (33)
+ RETURNING *
+)
+SELECT * FROM t LIMIT 1;
+
+SELECT * FROM y;
+
+DROP TRIGGER y_trig ON y;
+
+CREATE OR REPLACE FUNCTION y_trigger() RETURNS trigger AS $$
+begin
+ raise notice 'y_trigger';
+ return null;
+end;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER y_trig AFTER INSERT ON y FOR EACH STATEMENT
+ EXECUTE PROCEDURE y_trigger();
+
+WITH t AS (
+ INSERT INTO y
+ VALUES
+ (41),
+ (42),
+ (43)
+ RETURNING *
+)
+SELECT * FROM t;
+
+SELECT * FROM y;
+
+DROP TRIGGER y_trig ON y;
+DROP FUNCTION y_trigger();
+
+-- WITH attached to inherited UPDATE or DELETE
+
+CREATE TEMP TABLE parent ( id int, val text );
+CREATE TEMP TABLE child1 ( ) INHERITS ( parent );
+CREATE TEMP TABLE child2 ( ) INHERITS ( parent );
+
+INSERT INTO parent VALUES ( 1, 'p1' );
+INSERT INTO child1 VALUES ( 11, 'c11' ),( 12, 'c12' );
+INSERT INTO child2 VALUES ( 23, 'c21' ),( 24, 'c22' );
+
+WITH rcte AS ( SELECT sum(id) AS totalid FROM parent )
+UPDATE parent SET id = id + totalid FROM rcte;
+
+SELECT * FROM parent;
+
+WITH wcte AS ( INSERT INTO child1 VALUES ( 42, 'new' ) RETURNING id AS newid )
+UPDATE parent SET id = id + newid FROM wcte;
+
+SELECT * FROM parent;
+
+WITH rcte AS ( SELECT max(id) AS maxid FROM parent )
+DELETE FROM parent USING rcte WHERE id = maxid;
+
+SELECT * FROM parent;
+
+WITH wcte AS ( INSERT INTO child2 VALUES ( 42, 'new2' ) RETURNING id AS newid )
+DELETE FROM parent USING wcte WHERE id = newid;
+
+SELECT * FROM parent;
+
+-- check EXPLAIN VERBOSE for a wCTE with RETURNING
+
+EXPLAIN (VERBOSE, COSTS OFF)
+WITH wcte AS ( INSERT INTO int8_tbl VALUES ( 42, 47 ) RETURNING q2 )
+DELETE FROM a USING wcte WHERE aa = q2;
+
+-- error cases
+
+-- data-modifying WITH tries to use its own output
+WITH RECURSIVE t AS (
+ INSERT INTO y
+ SELECT * FROM t
+)
+VALUES(FALSE);
+
+-- no RETURNING in a referenced data-modifying WITH
+WITH t AS (
+ INSERT INTO y VALUES(0)
+)
+SELECT * FROM t;
+
+-- data-modifying WITH allowed only at the top level
+SELECT * FROM (
+ WITH t AS (UPDATE y SET a=a+1 RETURNING *)
+ SELECT * FROM t
+) ss;
+
+-- most variants of rules aren't allowed
+CREATE RULE y_rule AS ON INSERT TO y WHERE a=0 DO INSTEAD DELETE FROM y;
+WITH t AS (
+ INSERT INTO y VALUES(0)
+)
+VALUES(FALSE);
+DROP RULE y_rule ON y;
+
+-- check that parser lookahead for WITH doesn't cause any odd behavior
+--create table foo (with baz); -- fail, WITH is a reserved word
+--create table foo (with ordinality); -- fail, WITH is a reserved word
+with ordinality as (select 1 as x) select * from ordinality;
+
+-- check sane response to attempt to modify CTE relation
+WITH test AS (SELECT 42) INSERT INTO test VALUES (1);
+
+-- check response to attempt to modify table with same name as a CTE (perhaps
+-- surprisingly it works, because CTEs don't hide tables from data-modifying
+-- statements)
+create temp table test (i int);
+with test as (select 42) insert into test select * from test;
+select * from test;
+drop table test;