Skip to content

Commit dedb59d

Browse files
committed
manage tables that track system metadata; feed SHOW results straight into SqlLine
1 parent 6fde9c9 commit dedb59d

File tree

12 files changed

+1020
-292
lines changed

12 files changed

+1020
-292
lines changed
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
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 BeamSqlLineShowTests {
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+
assertThat(
52+
Arrays.asList(
53+
"+------+------+",
54+
"| NAME | TYPE |",
55+
"+------+------+",
56+
"| my_other_table | text |",
57+
"| my_other_table_with_a_long_name | test |",
58+
"| my_table | text |",
59+
"+------+------+"),
60+
everyItem(is(oneOf(lines.toArray()))));
61+
}
62+
63+
@Test
64+
public void testShowTablesInOtherDatabase() throws IOException {
65+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
66+
String[] args =
67+
buildArgs(
68+
"CREATE DATABASE my_db",
69+
"USE DATABASE my_db",
70+
"CREATE EXTERNAL TABLE should_not_show_up (id int, name varchar) TYPE 'text'",
71+
"CREATE CATALOG other_catalog TYPE 'local'",
72+
"CREATE DATABASE other_catalog.other_db",
73+
"CREATE EXTERNAL TABLE other_catalog.other_db.other_table (id int, name varchar) TYPE 'text'",
74+
"SHOW TABLES IN other_catalog.other_db");
75+
76+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
77+
78+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
79+
assertThat(
80+
Arrays.asList(
81+
"+------+------+",
82+
"| NAME | TYPE |",
83+
"+------+------+",
84+
"| other_table | text |",
85+
"+------+------+"),
86+
everyItem(is(oneOf(lines.toArray()))));
87+
}
88+
89+
@Test
90+
public void testShowTablesWithPattern() throws IOException {
91+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
92+
String[] args =
93+
buildArgs(
94+
"CREATE DATABASE my_db",
95+
"USE DATABASE my_db",
96+
"CREATE EXTERNAL TABLE my_table (id int, name varchar) TYPE 'text'",
97+
"CREATE EXTERNAL TABLE my_table_2 (id int, name varchar) TYPE 'text'",
98+
"CREATE EXTERNAL TABLE my_foo_table_1 (id int, name varchar) TYPE 'text'",
99+
"CREATE EXTERNAL TABLE my_foo_table_2 (id int, name varchar) TYPE 'text'",
100+
"SHOW TABLES LIKE '%foo%'");
101+
102+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
103+
104+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
105+
assertThat(
106+
Arrays.asList(
107+
"+------+------+",
108+
"| NAME | TYPE |",
109+
"+------+------+",
110+
"| my_foo_table_1 | text |",
111+
"| my_foo_table_2 | text |",
112+
"+------+------+"),
113+
everyItem(is(oneOf(lines.toArray()))));
114+
}
115+
116+
@Test
117+
public void testShowCurrentDatabase() throws IOException {
118+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
119+
String[] args =
120+
buildArgs(
121+
"CREATE DATABASE should_not_show_up",
122+
"CREATE CATALOG my_catalog TYPE 'local'",
123+
"USE CATALOG my_catalog",
124+
"CREATE DATABASE my_db",
125+
"CREATE DATABASE my_other_db",
126+
"CREATE DATABASE my_database_that_has_a_very_long_name",
127+
"USE DATABASE my_other_db",
128+
"SHOW CURRENT database");
129+
130+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
131+
132+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
133+
assertThat(
134+
Arrays.asList("+------+", "| NAME |", "+------+", "| my_other_db |", "+------+"),
135+
everyItem(is(oneOf(lines.toArray()))));
136+
}
137+
138+
@Test
139+
public void testShowCurrentDatabaseWithNoneSet() throws IOException {
140+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
141+
String[] args =
142+
buildArgs(
143+
"CREATE DATABASE should_not_show_up",
144+
"CREATE CATALOG my_catalog TYPE 'local'",
145+
"USE CATALOG my_catalog",
146+
"DROP DATABASE `default`",
147+
"SHOW CURRENT DATABASE");
148+
149+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
150+
151+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
152+
assertThat(
153+
Arrays.asList("+------+", "| NAME |", "+------+", "+------+"),
154+
everyItem(is(oneOf(lines.toArray()))));
155+
}
156+
157+
@Test
158+
public void testShowDatabases() throws IOException {
159+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
160+
String[] args =
161+
buildArgs(
162+
"CREATE DATABASE should_not_show_up",
163+
"CREATE CATALOG my_catalog TYPE 'local'",
164+
"USE CATALOG my_catalog",
165+
"CREATE DATABASE my_db",
166+
"CREATE DATABASE my_other_db",
167+
"CREATE DATABASE my_database_that_has_a_very_long_name",
168+
"SHOW DATABASES");
169+
170+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
171+
172+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
173+
assertThat(
174+
Arrays.asList(
175+
"+------+",
176+
"| NAME |",
177+
"+------+",
178+
"| default |",
179+
"| my_database_that_has_a_very_long_name |",
180+
"| my_db |",
181+
"| my_other_db |",
182+
"+------+"),
183+
everyItem(is(oneOf(lines.toArray()))));
184+
}
185+
186+
@Test
187+
public void testShowDatabasesInOtherCatalog() throws IOException {
188+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
189+
String[] args =
190+
buildArgs(
191+
"CREATE DATABASE should_not_show_up",
192+
"CREATE CATALOG my_catalog TYPE 'local'",
193+
"USE CATALOG my_catalog",
194+
"CREATE DATABASE my_db",
195+
"CREATE CATALOG my_other_catalog TYPE 'local'",
196+
"CREATE DATABASE my_other_catalog.other_db",
197+
"SHOW DATABASES FROM my_other_catalog");
198+
199+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
200+
201+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
202+
assertThat(
203+
Arrays.asList(
204+
"+------+", "| NAME |", "+------+", "| default |", "| other_db |", "+------+"),
205+
everyItem(is(oneOf(lines.toArray()))));
206+
}
207+
208+
@Test
209+
public void testShowDatabasesWithPattern() throws IOException {
210+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
211+
String[] args =
212+
buildArgs(
213+
"CREATE CATALOG my_catalog TYPE 'local'",
214+
"CREATE DATABASE my_catalog.my_db",
215+
"CREATE DATABASE my_catalog.other_db",
216+
"CREATE DATABASE my_catalog.some_foo_db",
217+
"CREATE DATABASE my_catalog.some_other_foo_db",
218+
"SHOW DATABASES FROM my_catalog LIKE '%foo%'");
219+
220+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
221+
222+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
223+
assertThat(
224+
Arrays.asList(
225+
"+------+",
226+
"| NAME |",
227+
"+------+",
228+
"| some_foo_db |",
229+
"| some_other_foo_db |",
230+
"+------+"),
231+
everyItem(is(oneOf(lines.toArray()))));
232+
}
233+
234+
@Test
235+
public void testShowCurrentCatalog() throws IOException {
236+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
237+
String[] args =
238+
buildArgs(
239+
"CREATE CATALOG my_catalog TYPE 'local'",
240+
"CREATE CATALOG my_very_long_catalog_name TYPE 'local'",
241+
"SHOW CURRENT CATALOG");
242+
243+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
244+
245+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
246+
assertThat(
247+
Arrays.asList(
248+
"+------+------+",
249+
"| NAME | TYPE |",
250+
"+------+------+",
251+
"| default | local |",
252+
"+------+------+"),
253+
everyItem(is(oneOf(lines.toArray()))));
254+
}
255+
256+
@Test
257+
public void testShowCatalogs() throws IOException {
258+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
259+
String[] args =
260+
buildArgs(
261+
"CREATE CATALOG my_catalog TYPE 'local'",
262+
"CREATE CATALOG my_very_long_catalog_name TYPE 'local'",
263+
"SHOW CATALOGS");
264+
265+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
266+
267+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
268+
assertThat(
269+
Arrays.asList(
270+
"+------+------+",
271+
"| NAME | TYPE |",
272+
"+------+------+",
273+
"| default | local |",
274+
"| my_catalog | local |",
275+
"| my_very_long_catalog_name | local |",
276+
"+------+------+"),
277+
everyItem(is(oneOf(lines.toArray()))));
278+
}
279+
280+
@Test
281+
public void testShowCatalogsWithPattern() throws IOException {
282+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
283+
String[] args =
284+
buildArgs(
285+
"CREATE CATALOG my_catalog TYPE 'local'",
286+
"CREATE CATALOG my_catalog_2 TYPE 'local'",
287+
"CREATE CATALOG my_very_long_catalog_name TYPE 'local'",
288+
"SHOW CATALOGS LIKE 'my_catalog%'");
289+
290+
BeamSqlLine.runSqlLine(args, null, byteArrayOutputStream, null);
291+
292+
List<String> lines = Arrays.asList(byteArrayOutputStream.toString("UTF-8").split("\n"));
293+
assertThat(
294+
Arrays.asList(
295+
"+------+------+",
296+
"| NAME | TYPE |",
297+
"+------+------+",
298+
"| my_catalog | local |",
299+
"| my_catalog_2 | local |",
300+
"+------+------+"),
301+
everyItem(is(oneOf(lines.toArray()))));
302+
}
303+
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ data: {
3535
"org.apache.beam.sdk.extensions.sql.impl.parser.SqlDdlNodes"
3636
"org.apache.beam.sdk.extensions.sql.impl.parser.SqlUseCatalog"
3737
"org.apache.beam.sdk.extensions.sql.impl.parser.SqlUseDatabase"
38-
"org.apache.beam.sdk.extensions.sql.impl.parser.SqlShowCatalogs"
39-
"org.apache.beam.sdk.extensions.sql.impl.parser.SqlShowDatabases"
40-
"org.apache.beam.sdk.extensions.sql.impl.parser.SqlShowTables"
4138
"org.apache.beam.sdk.extensions.sql.impl.parser.SqlSetOptionBeam"
4239
"org.apache.beam.sdk.extensions.sql.impl.utils.CalciteUtils"
4340
"org.apache.beam.sdk.schemas.Schema"
@@ -428,10 +425,6 @@ data: {
428425
# Return type of method implementation should be 'SqlNode'.
429426
# Example: SqlShowDatabases(), SqlShowTables().
430427
statementParserMethods: [
431-
"SqlShowTables(Span.of())"
432-
"SqlShowDatabases(Span.of())"
433-
"SqlShowCatalogs(Span.of())"
434-
"SqlShowCurrent(Span.of())"
435428
"SqlUseCatalog(Span.of(), null)"
436429
"SqlUseDatabase(Span.of(), null)"
437430
"SqlSetOptionBeam(Span.of(), null)"

0 commit comments

Comments
 (0)