Skip to content

Commit d119b29

Browse files
authored
[SQL] add SHOW command (#36509)
* add SHOW command * add SHOW CURRENT * add SHOW CURRENT * add LIKE pattern * spotless * use SqlCall; remove @nullable * spotless * minor fixes * use PrintWriter and flush all at once * correct PrintWriter usage * cleanup * fix name * address comments * spotless
1 parent a3890c6 commit d119b29

File tree

18 files changed

+1134
-13
lines changed

18 files changed

+1134
-13
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"https://github.com/apache/beam/pull/36890": "fixing some null errors"
2+
"comment": "Modify this file in a trivial way to cause this test suite to run ",
3+
"modification": 3
34
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"comment": "Modify this file in a trivial way to cause this test suite to run.",
3-
"modification": 1
3+
"modification": 3
44
}

sdks/java/extensions/sql/iceberg/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/iceberg/IcebergCatalog.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
2121

22+
import java.util.Collection;
2223
import java.util.HashMap;
2324
import java.util.Map;
2425
import org.apache.beam.sdk.extensions.sql.meta.catalog.InMemoryCatalog;
@@ -71,6 +72,11 @@ public boolean createDatabase(String database) {
7172
return catalogConfig.createNamespace(database);
7273
}
7374

75+
@Override
76+
public Collection<String> databases() {
77+
return catalogConfig.listNamespaces();
78+
}
79+
7480
@Override
7581
public void useDatabase(String database) {
7682
checkArgument(databaseExists(database), "Database '%s' does not exist.");
Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.beam.sdk.extensions.sql.jdbc;
19+
20+
import static org.apache.beam.sdk.extensions.sql.jdbc.BeamSqlLineTestingUtils.buildArgs;
21+
import static org.hamcrest.CoreMatchers.everyItem;
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.Matchers.is;
24+
import static org.hamcrest.Matchers.oneOf;
25+
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.IOException;
28+
import java.util.Arrays;
29+
import java.util.List;
30+
import org.junit.Test;
31+
32+
public class BeamSqlLineShowTest {
33+
@Test
34+
public void testShowTables() throws IOException {
35+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
36+
String[] args =
37+
buildArgs(
38+
"CREATE DATABASE other_db",
39+
"CREATE EXTERNAL TABLE other_db.should_not_show_up (id int, name varchar) TYPE 'text'",
40+
"CREATE CATALOG my_catalog TYPE 'local'",
41+
"CREATE DATABASE my_catalog.my_db",
42+
"USE DATABASE my_catalog.my_db",
43+
"CREATE EXTERNAL TABLE my_table (id int, name varchar) TYPE 'text'",
44+
"CREATE EXTERNAL TABLE my_other_table (col1 int, col2 timestamp) TYPE 'text'",
45+
"CREATE EXTERNAL TABLE my_other_table_with_a_long_name (foo varchar, bar boolean) TYPE 'test'",
46+
"SHOW TABLES");
47+
48+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
49+
50+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
51+
System.out.println(byteArrayOutputStream.toString("UTF-8"));
52+
assertThat(
53+
Arrays.asList(
54+
"+------+------+",
55+
"| NAME | TYPE |",
56+
"+------+------+",
57+
"| my_other_table | text |",
58+
"| my_other_table_with_a_long_name | test |",
59+
"| my_table | text |",
60+
"+------+------+"),
61+
everyItem(is(oneOf(lines.toArray()))));
62+
}
63+
64+
@Test
65+
public void testShowTablesInOtherDatabase() throws IOException {
66+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
67+
String[] args =
68+
buildArgs(
69+
"CREATE DATABASE my_db",
70+
"USE DATABASE my_db",
71+
"CREATE EXTERNAL TABLE should_not_show_up (id int, name varchar) TYPE 'text'",
72+
"CREATE CATALOG other_catalog TYPE 'local'",
73+
"CREATE DATABASE other_catalog.other_db",
74+
"CREATE EXTERNAL TABLE other_catalog.other_db.other_table (id int, name varchar) TYPE 'text'",
75+
"SHOW TABLES IN other_catalog.other_db");
76+
77+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
78+
79+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
80+
assertThat(
81+
Arrays.asList(
82+
"+------+------+",
83+
"| NAME | TYPE |",
84+
"+------+------+",
85+
"| other_table | text |",
86+
"+------+------+"),
87+
everyItem(is(oneOf(lines.toArray()))));
88+
}
89+
90+
@Test
91+
public void testShowTablesWithPattern() throws IOException {
92+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
93+
String[] args =
94+
buildArgs(
95+
"CREATE DATABASE my_db",
96+
"USE DATABASE my_db",
97+
"CREATE EXTERNAL TABLE my_table (id int, name varchar) TYPE 'text'",
98+
"CREATE EXTERNAL TABLE my_table_2 (id int, name varchar) TYPE 'text'",
99+
"CREATE EXTERNAL TABLE my_foo_table_1 (id int, name varchar) TYPE 'text'",
100+
"CREATE EXTERNAL TABLE my_foo_table_2 (id int, name varchar) TYPE 'text'",
101+
"SHOW TABLES LIKE '%foo%'");
102+
103+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
104+
105+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
106+
assertThat(
107+
Arrays.asList(
108+
"+------+------+",
109+
"| NAME | TYPE |",
110+
"+------+------+",
111+
"| my_foo_table_1 | text |",
112+
"| my_foo_table_2 | text |",
113+
"+------+------+"),
114+
everyItem(is(oneOf(lines.toArray()))));
115+
}
116+
117+
@Test
118+
public void testShowCurrentDatabase() throws IOException {
119+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
120+
String[] args =
121+
buildArgs(
122+
"CREATE DATABASE should_not_show_up",
123+
"CREATE CATALOG my_catalog TYPE 'local'",
124+
"USE CATALOG my_catalog",
125+
"CREATE DATABASE my_db",
126+
"CREATE DATABASE my_other_db",
127+
"CREATE DATABASE my_database_that_has_a_very_long_name",
128+
"USE DATABASE my_other_db",
129+
"SHOW CURRENT database");
130+
131+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
132+
133+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
134+
assertThat(
135+
Arrays.asList("+------+", "| NAME |", "+------+", "| my_other_db |", "+------+"),
136+
everyItem(is(oneOf(lines.toArray()))));
137+
}
138+
139+
@Test
140+
public void testShowCurrentDatabaseWithNoneSet() throws IOException {
141+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
142+
String[] args =
143+
buildArgs(
144+
"CREATE DATABASE should_not_show_up",
145+
"CREATE CATALOG my_catalog TYPE 'local'",
146+
"USE CATALOG my_catalog",
147+
"DROP DATABASE `default`",
148+
"SHOW CURRENT DATABASE");
149+
150+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
151+
152+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
153+
assertThat(
154+
Arrays.asList("+------+", "| NAME |", "+------+", "+------+"),
155+
everyItem(is(oneOf(lines.toArray()))));
156+
}
157+
158+
@Test
159+
public void testShowDatabases() throws IOException {
160+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
161+
String[] args =
162+
buildArgs(
163+
"CREATE DATABASE should_not_show_up",
164+
"CREATE CATALOG my_catalog TYPE 'local'",
165+
"USE CATALOG my_catalog",
166+
"CREATE DATABASE my_db",
167+
"CREATE DATABASE my_other_db",
168+
"CREATE DATABASE my_database_that_has_a_very_long_name",
169+
"SHOW DATABASES");
170+
171+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
172+
173+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
174+
System.out.println(byteArrayOutputStream.toString("UTF-8"));
175+
assertThat(
176+
Arrays.asList(
177+
"+------+",
178+
"| NAME |",
179+
"+------+",
180+
"| default |",
181+
"| my_database_that_has_a_very_long_name |",
182+
"| my_db |",
183+
"| my_other_db |",
184+
"+------+"),
185+
everyItem(is(oneOf(lines.toArray()))));
186+
}
187+
188+
@Test
189+
public void testShowDatabasesInOtherCatalog() throws IOException {
190+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
191+
String[] args =
192+
buildArgs(
193+
"CREATE DATABASE should_not_show_up",
194+
"CREATE CATALOG my_catalog TYPE 'local'",
195+
"USE CATALOG my_catalog",
196+
"CREATE DATABASE my_db",
197+
"CREATE CATALOG my_other_catalog TYPE 'local'",
198+
"CREATE DATABASE my_other_catalog.other_db",
199+
"SHOW DATABASES FROM my_other_catalog");
200+
201+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
202+
203+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
204+
assertThat(
205+
Arrays.asList(
206+
"+------+", "| NAME |", "+------+", "| default |", "| other_db |", "+------+"),
207+
everyItem(is(oneOf(lines.toArray()))));
208+
}
209+
210+
@Test
211+
public void testShowDatabasesWithPattern() throws IOException {
212+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
213+
String[] args =
214+
buildArgs(
215+
"CREATE CATALOG my_catalog TYPE 'local'",
216+
"CREATE DATABASE my_catalog.my_db",
217+
"CREATE DATABASE my_catalog.other_db",
218+
"CREATE DATABASE my_catalog.some_foo_db",
219+
"CREATE DATABASE my_catalog.some_other_foo_db",
220+
"SHOW DATABASES FROM my_catalog LIKE '%foo%'");
221+
222+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
223+
224+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
225+
assertThat(
226+
Arrays.asList(
227+
"+------+",
228+
"| NAME |",
229+
"+------+",
230+
"| some_foo_db |",
231+
"| some_other_foo_db |",
232+
"+------+"),
233+
everyItem(is(oneOf(lines.toArray()))));
234+
}
235+
236+
@Test
237+
public void testShowCurrentCatalog() throws IOException {
238+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
239+
String[] args =
240+
buildArgs(
241+
"CREATE CATALOG my_catalog TYPE 'local'",
242+
"CREATE CATALOG my_very_long_catalog_name TYPE 'local'",
243+
"SHOW CURRENT CATALOG");
244+
245+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
246+
247+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
248+
assertThat(
249+
Arrays.asList(
250+
"+------+------+",
251+
"| NAME | TYPE |",
252+
"+------+------+",
253+
"| default | local |",
254+
"+------+------+"),
255+
everyItem(is(oneOf(lines.toArray()))));
256+
}
257+
258+
@Test
259+
public void testShowCatalogs() throws IOException {
260+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
261+
String[] args =
262+
buildArgs(
263+
"CREATE CATALOG my_catalog TYPE 'local'",
264+
"CREATE CATALOG my_very_long_catalog_name TYPE 'local'",
265+
"SHOW CATALOGS");
266+
267+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
268+
269+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
270+
System.out.println(byteArrayOutputStream.toString("UTF-8"));
271+
assertThat(
272+
Arrays.asList(
273+
"+------+------+",
274+
"| NAME | TYPE |",
275+
"+------+------+",
276+
"| default | local |",
277+
"| my_catalog | local |",
278+
"| my_very_long_catalog_name | local |",
279+
"+------+------+"),
280+
everyItem(is(oneOf(lines.toArray()))));
281+
}
282+
283+
@Test
284+
public void testShowCatalogsWithPattern() throws IOException {
285+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
286+
String[] args =
287+
buildArgs(
288+
"CREATE CATALOG my_catalog TYPE 'local'",
289+
"CREATE CATALOG my_catalog_2 TYPE 'local'",
290+
"CREATE CATALOG my_very_long_catalog_name TYPE 'local'",
291+
"SHOW CATALOGS LIKE 'my_catalog%'");
292+
293+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
294+
295+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
296+
assertThat(
297+
Arrays.asList(
298+
"+------+------+",
299+
"| NAME | TYPE |",
300+
"+------+------+",
301+
"| my_catalog | local |",
302+
"| my_catalog_2 | local |",
303+
"+------+------+"),
304+
everyItem(is(oneOf(lines.toArray()))));
305+
}
306+
}

sdks/java/extensions/sql/src/main/codegen/config.fmpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ data: {
5050
"TBLPROPERTIES"
5151
"PROPERTIES"
5252
"PARTITIONED"
53+
"CATALOGS"
54+
"DATABASES"
55+
"TABLES"
5356
"USE"
5457
]
5558

@@ -422,6 +425,10 @@ data: {
422425
# Return type of method implementation should be 'SqlNode'.
423426
# Example: SqlShowDatabases(), SqlShowTables().
424427
statementParserMethods: [
428+
"SqlShowTables(Span.of())"
429+
"SqlShowDatabases(Span.of())"
430+
"SqlShowCatalogs(Span.of())"
431+
"SqlShowCurrent(Span.of())"
425432
"SqlUseCatalog(Span.of(), null)"
426433
"SqlUseDatabase(Span.of(), null)"
427434
"SqlSetOptionBeam(Span.of(), null)"

0 commit comments

Comments
 (0)