Skip to content

Commit

Permalink
SQL: Remove slightly used meta commands (#37506)
Browse files Browse the repository at this point in the history
Remove SYS CATALOGS and SYS TABLE TYPES as they are a subset of SYS
TABLES (and thus somewhat redundant) and used only by JDBC.

Close #37409

(cherry picked from commit 023bb2f)
  • Loading branch information
costin committed Jan 16, 2019
1 parent 52a8a73 commit 282ce36
Show file tree
Hide file tree
Showing 17 changed files with 788 additions and 1,141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,16 @@ public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLExce

@Override
public ResultSet getCatalogs() throws SQLException {
return con.createStatement().executeQuery("SYS CATALOGS");
// TABLE_CAT is the first column
Object[][] data = queryColumn(con, "SYS TABLES CATALOG LIKE '%'", 1);
return memorySet(con.cfg, columnInfo("", "TABLE_CAT"), data);
}

@Override
public ResultSet getTableTypes() throws SQLException {
return con.createStatement().executeQuery("SYS TABLE TYPES");
// TABLE_TYPE (4)
Object[][] data = queryColumn(con, "SYS TABLES TYPE '%'", 4);
return memorySet(con.cfg, columnInfo("", "TABLE_TYPE"), data);
}

@Override
Expand Down Expand Up @@ -1114,6 +1118,26 @@ public boolean generatedKeyAlwaysReturned() throws SQLException {
return false;
}

//
// Utility methods
//

private static Object[][] queryColumn(JdbcConnection con, String query, int... cols) throws SQLException {
List<Object[]> data = new ArrayList<>();
try (ResultSet rs = con.createStatement().executeQuery(query)) {
while (rs.next()) {
Object[] row = new Object[cols.length];
for (int i = 0; i < cols.length; i++) {
row[i] = rs.getObject(cols[i]);
}
data.add(row);
}
}

return data.toArray(new Object[][] {});
}


private static List<JdbcColumnInfo> columnInfo(String tableName, Object... cols) throws JdbcSQLException {
List<JdbcColumnInfo> columns = new ArrayList<>();

Expand Down Expand Up @@ -1156,7 +1180,7 @@ private static ResultSet memorySet(JdbcConfiguration cfg, List<JdbcColumnInfo> c
return new JdbcResultSet(cfg, null, new InMemoryCursor(columns, data));
}

static class InMemoryCursor implements Cursor {
private static class InMemoryCursor implements Cursor {

private final List<JdbcColumnInfo> columns;
private final Object[][] data;
Expand Down Expand Up @@ -1197,4 +1221,4 @@ public void close() throws SQLException {
// this cursor doesn't hold any resource - no need to clean up
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ public void testGetTables() throws Exception {
}
}

public void testGetTableTypes() throws Exception {
public void testGetTypeOfTables() throws Exception {
index("test1", body -> body.field("name", "bob"));
index("test2", body -> body.field("name", "bob"));

try (Connection h2 = LocalH2.anonymousDb(); Connection es = esJdbc()) {
h2.createStatement().executeUpdate("RUNSCRIPT FROM 'classpath:/setup_mock_metadata_get_table_types.sql'");
h2.createStatement().executeUpdate("RUNSCRIPT FROM 'classpath:/setup_mock_metadata_get_types_of_table.sql'");

CheckedSupplier<ResultSet, SQLException> all = () -> h2.createStatement()
.executeQuery("SELECT '" + clusterName() + "' AS TABLE_CAT, * FROM mock");
Expand All @@ -88,6 +88,23 @@ public void testGetTableTypes() throws Exception {
}
}

public void testGetTableTypes() throws Exception {
index("test1", body -> body.field("name", "bob"));
index("test2", body -> body.field("name", "bob"));

try (Connection h2 = LocalH2.anonymousDb(); Connection es = esJdbc()) {
h2.createStatement().executeUpdate("RUNSCRIPT FROM 'classpath:/setup_mock_metadata_get_table_types.sql'");
assertResultSets(h2.createStatement().executeQuery("SELECT * FROM mock"), es.getMetaData().getTableTypes());
}
}

public void testGetCatalogs() throws Exception {
try (Connection h2 = LocalH2.anonymousDb(); Connection es = esJdbc()) {
assertResultSets(h2.createStatement().executeQuery("SELECT '" + clusterName() + "' AS TABLE_CAT"),
es.getMetaData().getCatalogs());
}
}

public void testColumns() throws Exception {
index("test1", body -> body.field("name", "bob"));
index("test2", body -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public void testSqlRestUsage() throws IOException {
allTotalQueries += randomCommandExecutions;
for (int i = 0; i < randomCommandExecutions; i++) {
runSql(randomFrom("SHOW FUNCTIONS", "SHOW COLUMNS FROM library", "SHOW SCHEMAS",
"SHOW TABLES", "SYS CATALOGS", "SYS COLUMNS LIKE '%name'",
"SYS TABLES", "SYS TYPES"));
"SHOW TABLES", "SYS TABLES", "SYS COLUMNS LIKE '%name'",
"SYS TABLES TYPE '%'", "SYS TYPES"));
}
responseAsMap = getStats();
assertFeatureMetric(baseMetrics.get("command") + randomCommandExecutions, responseAsMap, "command");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
CREATE TABLE mock (
TABLE_SCHEM VARCHAR,
TABLE_NAME VARCHAR,
TABLE_TYPE VARCHAR,
REMARKS VARCHAR,
TYPE_CAT VARCHAR,
TYPE_SCHEM VARCHAR,
TYPE_NAME VARCHAR,
SELF_REFERENCING_COL_NAME VARCHAR,
REF_GENERATION VARCHAR
) AS
SELECT '', 'test1', 'BASE TABLE', '', null, null, null, null, null FROM DUAL
SELECT 'ALIAS' FROM DUAL
UNION ALL
SELECT '', 'test2', 'BASE TABLE', '', null, null, null, null, null FROM DUAL
SELECT 'BASE TABLE' FROM DUAL
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE mock (
TABLE_SCHEM VARCHAR,
TABLE_NAME VARCHAR,
TABLE_TYPE VARCHAR,
REMARKS VARCHAR,
TYPE_CAT VARCHAR,
TYPE_SCHEM VARCHAR,
TYPE_NAME VARCHAR,
SELF_REFERENCING_COL_NAME VARCHAR,
REF_GENERATION VARCHAR
) AS
SELECT '', 'test1', 'BASE TABLE', '', null, null, null, null, null FROM DUAL
UNION ALL
SELECT '', 'test2', 'BASE TABLE', '', null, null, null, null, null FROM DUAL
;
2 changes: 0 additions & 2 deletions x-pack/plugin/sql/src/main/antlr/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ statement
| (DESCRIBE | DESC) (tableLike=likePattern | tableIdent=tableIdentifier) #showColumns
| SHOW FUNCTIONS (likePattern)? #showFunctions
| SHOW SCHEMAS #showSchemas
| SYS CATALOGS #sysCatalogs
| SYS TABLES (CATALOG clusterLike=likePattern)?
(tableLike=likePattern | tableIdent=tableIdentifier)?
(TYPE string (',' string)* )? #sysTables
| SYS COLUMNS (CATALOG cluster=string)?
(TABLE tableLike=likePattern | tableIdent=tableIdentifier)?
(columnPattern=likePattern)? #sysColumns
| SYS TYPES ((PLUS | MINUS)? type=number)? #sysTypes
| SYS TABLE TYPES #sysTableTypes
;

query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.ShowSchemasContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.ShowTablesContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.StringContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.SysCatalogsContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.SysColumnsContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.SysTableTypesContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.SysTablesContext;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.SysTypesContext;
import org.elasticsearch.xpack.sql.plan.TableIdentifier;
Expand All @@ -29,9 +27,7 @@
import org.elasticsearch.xpack.sql.plan.logical.command.ShowFunctions;
import org.elasticsearch.xpack.sql.plan.logical.command.ShowSchemas;
import org.elasticsearch.xpack.sql.plan.logical.command.ShowTables;
import org.elasticsearch.xpack.sql.plan.logical.command.sys.SysCatalogs;
import org.elasticsearch.xpack.sql.plan.logical.command.sys.SysColumns;
import org.elasticsearch.xpack.sql.plan.logical.command.sys.SysTableTypes;
import org.elasticsearch.xpack.sql.plan.logical.command.sys.SysTables;
import org.elasticsearch.xpack.sql.plan.logical.command.sys.SysTypes;
import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue;
Expand Down Expand Up @@ -144,11 +140,6 @@ public Object visitShowColumns(ShowColumnsContext ctx) {
return new ShowColumns(source(ctx), index, visitLikePattern(ctx.likePattern()));
}

@Override
public Object visitSysCatalogs(SysCatalogsContext ctx) {
return new SysCatalogs(source(ctx));
}

@Override
public SysTables visitSysTables(SysTablesContext ctx) {
List<IndexType> types = new ArrayList<>();
Expand Down Expand Up @@ -199,9 +190,4 @@ public SysTypes visitSysTypes(SysTypesContext ctx) {

return new SysTypes(source(ctx), Integer.valueOf(type));
}

@Override
public Object visitSysTableTypes(SysTableTypesContext ctx) {
return new SysTableTypes(source(ctx));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,6 @@ class SqlBaseBaseListener implements SqlBaseListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitShowSchemas(SqlBaseParser.ShowSchemasContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterSysCatalogs(SqlBaseParser.SysCatalogsContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitSysCatalogs(SqlBaseParser.SysCatalogsContext ctx) { }
/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -167,18 +155,6 @@ class SqlBaseBaseListener implements SqlBaseListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitSysTypes(SqlBaseParser.SysTypesContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterSysTableTypes(SqlBaseParser.SysTableTypesContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitSysTableTypes(SqlBaseParser.SysTableTypesContext ctx) { }
/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ class SqlBaseBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements SqlBa
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitShowSchemas(SqlBaseParser.ShowSchemasContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitSysCatalogs(SqlBaseParser.SysCatalogsContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
Expand All @@ -102,13 +95,6 @@ class SqlBaseBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements SqlBa
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitSysTypes(SqlBaseParser.SysTypesContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitSysTableTypes(SqlBaseParser.SysTableTypesContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,6 @@ interface SqlBaseListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitShowSchemas(SqlBaseParser.ShowSchemasContext ctx);
/**
* Enter a parse tree produced by the {@code sysCatalogs}
* labeled alternative in {@link SqlBaseParser#statement}.
* @param ctx the parse tree
*/
void enterSysCatalogs(SqlBaseParser.SysCatalogsContext ctx);
/**
* Exit a parse tree produced by the {@code sysCatalogs}
* labeled alternative in {@link SqlBaseParser#statement}.
* @param ctx the parse tree
*/
void exitSysCatalogs(SqlBaseParser.SysCatalogsContext ctx);
/**
* Enter a parse tree produced by the {@code sysTables}
* labeled alternative in {@link SqlBaseParser#statement}.
Expand Down Expand Up @@ -159,18 +147,6 @@ interface SqlBaseListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitSysTypes(SqlBaseParser.SysTypesContext ctx);
/**
* Enter a parse tree produced by the {@code sysTableTypes}
* labeled alternative in {@link SqlBaseParser#statement}.
* @param ctx the parse tree
*/
void enterSysTableTypes(SqlBaseParser.SysTableTypesContext ctx);
/**
* Exit a parse tree produced by the {@code sysTableTypes}
* labeled alternative in {@link SqlBaseParser#statement}.
* @param ctx the parse tree
*/
void exitSysTableTypes(SqlBaseParser.SysTableTypesContext ctx);
/**
* Enter a parse tree produced by {@link SqlBaseParser#query}.
* @param ctx the parse tree
Expand Down
Loading

0 comments on commit 282ce36

Please sign in to comment.