Skip to content

Commit f880f35

Browse files
feat: parse PostgreSQL IMPORT FOREIGN SCHEMA (#2909)
* feat: parse PostgreSQL IMPORT FOREIGN SCHEMA Add ImportForeignSchema for IMPORT FOREIGN SCHEMA remote [{LIMIT TO | EXCEPT} (table, ...)] FROM SERVER server INTO local [OPTIONS (...)], read with the PostgreSQL dialect or without one. The table list reuses the relation targets of LOCK and TRUNCATE, and OPTIONS the generic options of the other foreign-data statements. The EXASOL IMPORT statement and other dialects are unchanged. Signed-off-by: minleejae <mmj9808@gmail.com> * style: align PostgreSQL feature comment with formatter Apply the single comment placement required by Spotless after merging upstream. Java tokens are unchanged. Validation: targeted spotlessJavaCheck and git diff --check pass. --------- Signed-off-by: minleejae <mmj9808@gmail.com> Co-authored-by: manticore-projects <andreas@manticore-projects.com>
1 parent 7702ff3 commit f880f35

22 files changed

Lines changed: 635 additions & 15 deletions

‎README.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,22 @@ and missing syntax gets added on demand — [open an issue](https://github.com/J
148148
| **Oracle vector types** | `VECTOR(*, *)` · `VECTOR(*, FLOAT32)` · `VECTOR(3, *)`, including dense and sparse storage formats |
149149
| **PostgreSQL ownership** | `DROP OWNED` · `REASSIGN OWNED` |
150150
| **Two-phase commit** | PostgreSQL `PREPARE TRANSACTION` · `COMMIT PREPARED` · `ROLLBACK PREPARED` · MySQL `XA START`/`END`/`PREPARE`/`COMMIT`/`ROLLBACK`/`RECOVER` |
151-
| **MySQL table maintenance** | `ANALYZE` · `CHECK` · `CHECKSUM` · `OPTIMIZE` · `REPAIR TABLE` |
152151
| **PostgreSQL maintenance** | `VACUUM` · `ANALYZE` · `CLUSTER` · `CHECKPOINT` |
152+
| **PostgreSQL session statements** | `DISCARD` · `LISTEN` · `UNLISTEN` · `SHOW`/`RESET TIME ZONE`/`TRANSACTION ISOLATION LEVEL`/`SESSION AUTHORIZATION` · `COPY` options from before 9.0 |
153+
| **PostgreSQL foreign schema import** | `IMPORT FOREIGN SCHEMA … [LIMIT TO \| EXCEPT (…)] FROM SERVER … INTO … [OPTIONS (…)]` |
154+
| **PostgreSQL cursors** | `DECLARE … CURSOR` · `FETCH` · `MOVE` · `CLOSE` |
155+
| **PostgreSQL RLS** | `CREATE POLICY` · `ALTER TABLE … ENABLE`/`DISABLE`/`FORCE`/`NO FORCE ROW LEVEL SECURITY` |
156+
| **MySQL table maintenance** | `ANALYZE` · `CHECK` · `CHECKSUM` · `OPTIMIZE` · `REPAIR TABLE` |
153157
| **MySQL locks** | `LOCK TABLES` · `UNLOCK TABLES` · `LOCK INSTANCE FOR BACKUP` · `UNLOCK INSTANCE` |
154158
| **MySQL resource groups** | `CREATE`/`ALTER`/`DROP RESOURCE GROUP` · `SET RESOURCE GROUP` |
155159
| **MySQL loadable functions** | `CREATE [AGGREGATE] FUNCTION … RETURNS … SONAME` |
156160
| **MySQL servers, plugins and components** | `CREATE SERVER` · `ALTER SERVER` with `OPTIONS (… PORT 3306)` · `INSTALL`/`UNINSTALL PLUGIN` · `INSTALL`/`UNINSTALL COMPONENT` |
157-
| **PostgreSQL session statements** | `DISCARD` · `LISTEN` · `UNLISTEN` · `SHOW`/`RESET TIME ZONE`/`TRANSACTION ISOLATION LEVEL`/`SESSION AUTHORIZATION` · `COPY` options from before 9.0 |
158161
| **MySQL server administration** | `FLUSH` · `KILL` · `RESET PERSIST` · `HANDLER … OPEN`/`READ`/`CLOSE` |
159162
| **MySQL replication** | `CHANGE REPLICATION SOURCE TO` · `CHANGE REPLICATION FILTER` · `START`/`STOP REPLICA` · `RESET REPLICA` · `RESET BINARY LOGS AND GTIDS` · `PURGE BINARY LOGS` |
160163
| **MySQL data loading** | `LOAD DATA` · `LOAD XML` · `SELECT … INTO OUTFILE` with the same `FIELDS` and `LINES` clauses |
161-
| **PostgreSQL cursors** | `DECLARE … CURSOR` · `FETCH` · `MOVE` · `CLOSE` |
162164
| **MySQL account management** | `CREATE`/`ALTER`/`RENAME USER` · `CREATE ROLE` · `SET ROLE` · `SET DEFAULT ROLE` · `SET PASSWORD` |
163165
| **Tablespaces** | MySQL and PostgreSQL `CREATE TABLESPACE` · `ALTER TABLESPACE` |
164166
| **MySQL spatial reference systems and instance** | `CREATE`/`DROP SPATIAL REFERENCE SYSTEM` · `ALTER INSTANCE` |
165-
| **PostgreSQL RLS** | `CREATE POLICY` · `ALTER TABLE … ENABLE`/`DISABLE`/`FORCE`/`NO FORCE ROW LEVEL SECURITY` |
166167
| **Routine changes** | `ALTER FUNCTION` · `ALTER PROCEDURE` · `ALTER ROUTINE` · PostgreSQL `ALTER TRIGGER` |
167168
| **MySQL metadata statements** | `SHOW [FULL] COLUMNS`/`FIELDS` · `SHOW INDEX`/`INDEXES`/`KEYS` · `SHOW TABLE STATUS` · `SHOW CREATE TABLE`/`VIEW`/`PROCEDURE`/`FUNCTION`/`TRIGGER`/`EVENT`/`DATABASE`/`USER` · `SHOW [GLOBAL | SESSION] VARIABLES`/`STATUS` · `SHOW [FULL] PROCESSLIST` · `SHOW GRANTS [FOR …]` · `SHOW WARNINGS`/`ERRORS` · `SHOW BINARY LOGS`/`BINLOG EVENTS`/`RELAYLOG EVENTS` · `SHOW ENGINE …` · `SHOW PROFILE` · `SHOW DATABASES`/`TRIGGERS`/`EVENTS`/`OPEN TABLES`/`CHARACTER SET`/`COLLATION` · `SHOW PROCEDURE`/`FUNCTION STATUS`/`CODE` · `SHOW REPLICA STATUS`/`REPLICAS` |
168169
| **MySQL utility statements** | `EXPLAIN`/`DESCRIBE [ANALYZE] FORMAT = …` · `EXPLAIN FOR CONNECTION` · `DESCRIBE t col` · `PREPARE … FROM` · `EXECUTE … USING` · `DROP PREPARE` · `DO` |

‎src/main/java/net/sf/jsqlparser/parser/feature/Feature.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,12 @@ public enum Feature {
724724
createSequence, createAccessMethod, createAccessMethodTable,
725725
/** Publication and subscription definitions. */
726726
createForeignDataWrapper, alterForeignDataWrapper, createServer, alterServer, createUserMapping, alterUserMapping,
727+
/**
728+
* PostgreSQL "IMPORT FOREIGN SCHEMA" statement is allowed
729+
*
730+
* @see net.sf.jsqlparser.statement.foreign.ImportForeignSchema
731+
*/
732+
importForeignSchema,
727733

728734
createRule, notifyStatement, createTextSearchConfiguration, alterTextSearchConfiguration, createCollation, alterCollation,
729735

‎src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import net.sf.jsqlparser.statement.drop.DropUserMapping;
109109
import net.sf.jsqlparser.statement.execute.Execute;
110110
import net.sf.jsqlparser.statement.export.Export;
111+
import net.sf.jsqlparser.statement.foreign.ImportForeignSchema;
111112
import net.sf.jsqlparser.statement.export.ExportDataStatement;
112113
import net.sf.jsqlparser.statement.grant.AlterDefaultPrivileges;
113114
import net.sf.jsqlparser.statement.grant.Grant;
@@ -1649,6 +1650,11 @@ public <S> Void visit(AlterUserMapping statement, S context) {
16491650
return schemaOnly();
16501651
}
16511652

1653+
@Override
1654+
public <S> Void visit(ImportForeignSchema statement, S context) {
1655+
return schemaOnly();
1656+
}
1657+
16521658
@Override
16531659
public <S> Void visit(DropUserMapping statement, S context) {
16541660
return schemaOnly();

‎src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import net.sf.jsqlparser.statement.drop.Drop;
102102
import net.sf.jsqlparser.statement.execute.Execute;
103103
import net.sf.jsqlparser.statement.export.Export;
104+
import net.sf.jsqlparser.statement.foreign.ImportForeignSchema;
104105
import net.sf.jsqlparser.statement.grant.Grant;
105106
import net.sf.jsqlparser.statement.imprt.Import;
106107
import net.sf.jsqlparser.statement.insert.Insert;
@@ -1207,6 +1208,14 @@ default void visit(AlterUserMapping statement) {
12071208
visit(statement, null);
12081209
}
12091210

1211+
default <S> T visit(ImportForeignSchema statement, S context) {
1212+
return null;
1213+
}
1214+
1215+
default void visit(ImportForeignSchema statement) {
1216+
visit(statement, null);
1217+
}
1218+
12101219
default <S> T visit(DropUserMapping statement, S context) {
12111220
return null;
12121221
}

‎src/main/java/net/sf/jsqlparser/statement/StatementVisitorAdapter.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import net.sf.jsqlparser.statement.notify.ListenStatement;
3434
import net.sf.jsqlparser.statement.notify.NotifyStatement;
3535
import net.sf.jsqlparser.statement.foreign.ForeignDataStatement;
36+
import net.sf.jsqlparser.statement.foreign.ImportForeignSchema;
3637

3738
import net.sf.jsqlparser.statement.create.accessmethod.CreateAccessMethod;
3839

@@ -1275,6 +1276,11 @@ public <S> T visit(AlterUserMapping statement, S context) {
12751276
return visitForeignDataStatement(statement, context);
12761277
}
12771278

1279+
@Override
1280+
public <S> T visit(ImportForeignSchema statement, S context) {
1281+
return visitForeignDataStatement(statement, context);
1282+
}
1283+
12781284
@Override
12791285
public <S> T visit(DropUserMapping statement, S context) {
12801286
return null;
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2026 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.foreign;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.Objects;
15+
import java.util.function.Consumer;
16+
import net.sf.jsqlparser.expression.Expression;
17+
import net.sf.jsqlparser.schema.TableReference;
18+
import net.sf.jsqlparser.statement.StatementVisitor;
19+
20+
/**
21+
* PostgreSQL {@code IMPORT FOREIGN SCHEMA remote_schema [{LIMIT TO | EXCEPT} (table [, ...])] FROM
22+
* SERVER server INTO local_schema [OPTIONS (option 'value' [, ...])]}, which creates foreign tables
23+
* in the local schema for the tables of a schema on the foreign server.
24+
*
25+
* @see <a href="https://www.postgresql.org/docs/18/sql-importforeignschema.html">IMPORT FOREIGN
26+
* SCHEMA</a>
27+
*/
28+
public class ImportForeignSchema extends ForeignDataStatement {
29+
private String remoteSchema;
30+
private Qualification qualification;
31+
private final List<TableReference> tables = new ArrayList<>();
32+
private String server;
33+
private String localSchema;
34+
35+
/** How the table list restricts the import. */
36+
public enum Qualification {
37+
/** Import only the listed tables. */
38+
LIMIT_TO("LIMIT TO"),
39+
/** Import every table but the listed ones. */
40+
EXCEPT("EXCEPT");
41+
42+
private final String sql;
43+
44+
Qualification(String sql) {
45+
this.sql = sql;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return sql;
51+
}
52+
}
53+
54+
public String getRemoteSchema() {
55+
return remoteSchema;
56+
}
57+
58+
public void setRemoteSchema(String remoteSchema) {
59+
this.remoteSchema = remoteSchema;
60+
}
61+
62+
public ImportForeignSchema withRemoteSchema(String remoteSchema) {
63+
setRemoteSchema(remoteSchema);
64+
return this;
65+
}
66+
67+
/** The kind of the table list, or null when the whole remote schema is imported. */
68+
public Qualification getQualification() {
69+
return qualification;
70+
}
71+
72+
/**
73+
* The tables of the remote schema that LIMIT TO or EXCEPT lists; empty without a qualification.
74+
* The list is a read-only snapshot, change it with {@link #setTables(Qualification, List)}.
75+
*/
76+
public List<TableReference> getTables() {
77+
return List.copyOf(tables);
78+
}
79+
80+
/**
81+
* Restricts the import to the listed tables or excludes them.
82+
*
83+
* @param qualification the kind of the list, null to import the whole remote schema
84+
* @param tables the listed tables; at least one with a qualification, none without
85+
*/
86+
public void setTables(Qualification qualification, List<TableReference> tables) {
87+
List<TableReference> copy = tables == null ? List.of() : List.copyOf(tables);
88+
if (qualification == null != copy.isEmpty()) {
89+
throw new IllegalArgumentException(
90+
"LIMIT TO and EXCEPT need a table list, and a table list needs one of them");
91+
}
92+
this.qualification = qualification;
93+
this.tables.clear();
94+
this.tables.addAll(copy);
95+
}
96+
97+
public ImportForeignSchema withTables(Qualification qualification,
98+
List<TableReference> tables) {
99+
setTables(qualification, tables);
100+
return this;
101+
}
102+
103+
public String getServer() {
104+
return server;
105+
}
106+
107+
public void setServer(String server) {
108+
this.server = server;
109+
}
110+
111+
public ImportForeignSchema withServer(String server) {
112+
setServer(server);
113+
return this;
114+
}
115+
116+
public String getLocalSchema() {
117+
return localSchema;
118+
}
119+
120+
public void setLocalSchema(String localSchema) {
121+
this.localSchema = localSchema;
122+
}
123+
124+
public ImportForeignSchema withLocalSchema(String localSchema) {
125+
setLocalSchema(localSchema);
126+
return this;
127+
}
128+
129+
@Override
130+
public <T, S> T accept(StatementVisitor<T> visitor, S context) {
131+
return visitor.visit(this, context);
132+
}
133+
134+
@Override
135+
public StringBuilder appendTo(StringBuilder sql, Consumer<Expression> printer) {
136+
sql.append("IMPORT FOREIGN SCHEMA ").append(Objects.requireNonNull(remoteSchema));
137+
if (qualification != null) {
138+
sql.append(' ').append(qualification).append(" (");
139+
for (int i = 0; i < tables.size(); i++) {
140+
if (i > 0) {
141+
sql.append(", ");
142+
}
143+
tables.get(i).appendTo(sql);
144+
}
145+
sql.append(')');
146+
}
147+
sql.append(" FROM SERVER ").append(Objects.requireNonNull(server));
148+
sql.append(" INTO ").append(Objects.requireNonNull(localSchema));
149+
appendOptionsTo(sql, printer);
150+
return sql;
151+
}
152+
}

‎src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
import net.sf.jsqlparser.statement.execute.Execute;
208208
import net.sf.jsqlparser.statement.execute.ExecuteArgument;
209209
import net.sf.jsqlparser.statement.export.Export;
210+
import net.sf.jsqlparser.statement.foreign.ImportForeignSchema;
210211
import net.sf.jsqlparser.statement.export.ExportDataStatement;
211212
import net.sf.jsqlparser.statement.grant.AlterDefaultPrivileges;
212213
import net.sf.jsqlparser.statement.grant.Grant;
@@ -3580,6 +3581,13 @@ public <S> Void visit(AlterUserMapping statement, S context) {
35803581
return null;
35813582
}
35823583

3584+
@Override
3585+
public <S> Void visit(ImportForeignSchema statement, S context) {
3586+
// LIMIT TO and EXCEPT name tables of the remote schema, not relations of this database.
3587+
statement.visitExpressions(expression -> expression.accept(this, context));
3588+
return null;
3589+
}
3590+
35833591
@Override
35843592
public <S> Void visit(DropUserMapping statement, S context) {
35853593
// User and server names do not refer to tables.

‎src/main/java/net/sf/jsqlparser/util/deparser/StatementDeParser.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
import net.sf.jsqlparser.statement.drop.Drop;
137137
import net.sf.jsqlparser.statement.execute.Execute;
138138
import net.sf.jsqlparser.statement.export.Export;
139+
import net.sf.jsqlparser.statement.foreign.ImportForeignSchema;
139140
import net.sf.jsqlparser.statement.grant.Grant;
140141
import net.sf.jsqlparser.statement.imprt.Import;
141142
import net.sf.jsqlparser.statement.insert.Insert;
@@ -1183,6 +1184,12 @@ public <S> StringBuilder visit(AlterUserMapping statement, S context) {
11831184
expression -> expression.accept(expressionDeParser, context));
11841185
}
11851186

1187+
@Override
1188+
public <S> StringBuilder visit(ImportForeignSchema statement, S context) {
1189+
return statement.appendTo(builder,
1190+
expression -> expression.accept(expressionDeParser, context));
1191+
}
1192+
11861193
@Override
11871194
public <S> StringBuilder visit(DropUserMapping statement, S context) {
11881195
return statement.appendTo(builder);

‎src/main/java/net/sf/jsqlparser/util/validation/feature/FeaturesAllowed.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ public class FeaturesAllowed implements FeatureSetValidation, ModifyableFeatureS
183183
* all DDL {@link Feature}'s
184184
*/
185185
public static final FeaturesAllowed DDL =
186-
new FeaturesAllowed("DDL").add(CREATE, ALTER, DROP).unmodifyable();
186+
new FeaturesAllowed("DDL", Feature.importForeignSchema).add(CREATE, ALTER, DROP)
187+
.unmodifyable();
187188
private static final String SEPERATOR = " + ";
188189
private Set<String> names = new LinkedHashSet<>();
189190
private Set<Feature> features = new HashSet<>();

‎src/main/java/net/sf/jsqlparser/util/validation/feature/PostgresqlVersion.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ public enum PostgresqlVersion implements Version {
119119
Feature.createTablespace, Feature.alterTablespace,
120120
Feature.createForeignDataWrapper, Feature.alterForeignDataWrapper,
121121
Feature.createServer, Feature.alterServer, Feature.createUserMapping,
122-
Feature.alterUserMapping,
122+
Feature.alterUserMapping, // https://www.postgresql.org/docs/current/sql-importforeignschema.html
123+
Feature.importForeignSchema,
123124
Feature.createTextSearchConfiguration, Feature.alterTextSearchConfiguration,
124125
Feature.createRule, Feature.notifyStatement,
125126
Feature.createCollation, Feature.alterCollation,
@@ -129,8 +130,7 @@ public enum PostgresqlVersion implements Version {
129130
// https://www.postgresql.org/docs/current/sql-createconversion.html
130131
// https://www.postgresql.org/docs/current/sql-alterconversion.html
131132
Feature.createCast, Feature.createTransform, Feature.createConversion,
132-
Feature.alterConversion,
133-
// https://www.postgresql.org/docs/current/sql-createoperator.html
133+
Feature.alterConversion, // https://www.postgresql.org/docs/current/sql-createoperator.html
134134
// https://www.postgresql.org/docs/current/sql-createopclass.html
135135
// https://www.postgresql.org/docs/current/sql-createopfamily.html
136136
// https://www.postgresql.org/docs/current/sql-createaggregate.html

0 commit comments

Comments
 (0)