Skip to content

Commit 5047087

Browse files
committed
SQL: Improve compatibility with MS query (elastic#30516)
Support TABLE as a legacy argument for SYS TABLE commands Fix elastic#30398
1 parent 31c6616 commit 5047087

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/CommandBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public Object visitSysCatalogs(SysCatalogsContext ctx) {
148148
@Override
149149
public SysTables visitSysTables(SysTablesContext ctx) {
150150
List<IndexType> types = new ArrayList<>();
151+
boolean legacyTableType = false;
151152
for (StringContext string : ctx.string()) {
152153
String value = string(string);
153154
if (value != null) {
@@ -156,6 +157,12 @@ public SysTables visitSysTables(SysTablesContext ctx) {
156157
// since % is the same as not specifying a value, choose
157158
// https://docs.microsoft.com/en-us/sql/odbc/reference/develop-app/value-list-arguments?view=ssdt-18vs2017
158159
// that is skip the value
160+
}
161+
// special case for legacy apps (like msquery) that always asks for 'TABLE'
162+
// which we manually map to all concrete tables supported
163+
else if (value.toUpperCase(Locale.ROOT).equals("TABLE")) {
164+
legacyTableType = true;
165+
types.add(IndexType.INDEX);
159166
} else {
160167
IndexType type = IndexType.from(value);
161168
types.add(type);
@@ -165,7 +172,7 @@ public SysTables visitSysTables(SysTablesContext ctx) {
165172

166173
// if the ODBC enumeration is specified, skip validation
167174
EnumSet<IndexType> set = types.isEmpty() ? null : EnumSet.copyOf(types);
168-
return new SysTables(source(ctx), visitPattern(ctx.clusterPattern), visitPattern(ctx.tablePattern), set);
175+
return new SysTables(source(ctx), visitPattern(ctx.clusterPattern), visitPattern(ctx.tablePattern), set, legacyTableType);
169176
}
170177

171178
@Override

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTables.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,21 @@ public class SysTables extends Command {
3333
private final LikePattern pattern;
3434
private final LikePattern clusterPattern;
3535
private final EnumSet<IndexType> types;
36+
// flag indicating whether tables are reported as `TABLE` or `BASE TABLE`
37+
private final boolean legacyTableTypes;
3638

37-
public SysTables(Location location, LikePattern clusterPattern, LikePattern pattern, EnumSet<IndexType> types) {
39+
public SysTables(Location location, LikePattern clusterPattern, LikePattern pattern, EnumSet<IndexType> types,
40+
boolean legacyTableTypes) {
3841
super(location);
3942
this.clusterPattern = clusterPattern;
4043
this.pattern = pattern;
4144
this.types = types;
45+
this.legacyTableTypes = legacyTableTypes;
4246
}
4347

4448
@Override
4549
protected NodeInfo<SysTables> info() {
46-
return NodeInfo.create(this, SysTables::new, clusterPattern, pattern, types);
50+
return NodeInfo.create(this, SysTables::new, clusterPattern, pattern, types, legacyTableTypes);
4751
}
4852

4953
@Override
@@ -111,7 +115,7 @@ public final void execute(SqlSession session, ActionListener<SchemaRowSet> liste
111115
.map(t -> asList(cluster,
112116
EMPTY,
113117
t.name(),
114-
t.type().toSql(),
118+
legacyName(t.type()),
115119
EMPTY,
116120
null,
117121
null,
@@ -122,6 +126,10 @@ public final void execute(SqlSession session, ActionListener<SchemaRowSet> liste
122126
, listener::onFailure));
123127
}
124128

129+
private String legacyName(IndexType indexType) {
130+
return legacyTableTypes && indexType == IndexType.INDEX ? "TABLE" : indexType.toSql();
131+
}
132+
125133
@Override
126134
public int hashCode() {
127135
return Objects.hash(clusterPattern, pattern, types);

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTablesTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ public void testSysTablesOnlyIndices() throws Exception {
105105
}, index);
106106
}
107107

108+
public void testSysTablesOnlyIndicesInLegacyMode() throws Exception {
109+
executeCommand("SYS TABLES LIKE 'test' TYPE 'TABLE'", r -> {
110+
assertEquals(1, r.size());
111+
assertEquals("test", r.column(2));
112+
assertEquals("TABLE", r.column(3));
113+
}, index);
114+
115+
}
116+
117+
public void testSysTablesOnlyIndicesLegacyModeParameterized() throws Exception {
118+
executeCommand("SYS TABLES LIKE 'test' TYPE ?", asList(param("TABLE")), r -> {
119+
assertEquals(1, r.size());
120+
assertEquals("test", r.column(2));
121+
assertEquals("TABLE", r.column(3));
122+
}, index);
123+
}
124+
108125
public void testSysTablesOnlyIndicesParameterized() throws Exception {
109126
executeCommand("SYS TABLES LIKE 'test' TYPE ?", asList(param("ALIAS")), r -> {
110127
assertEquals(1, r.size());
@@ -131,6 +148,18 @@ public void testSysTablesOnlyIndicesAndAliasesParameterized() throws Exception {
131148
}, index, alias);
132149
}
133150

151+
public void testSysTablesOnlyIndicesLegacyAndAliasesParameterized() throws Exception {
152+
List<SqlTypedParamValue> params = asList(param("ALIAS"), param("TABLE"));
153+
executeCommand("SYS TABLES LIKE 'test' TYPE ?, ?", params, r -> {
154+
assertEquals(2, r.size());
155+
assertEquals("test", r.column(2));
156+
assertEquals("TABLE", r.column(3));
157+
assertTrue(r.advanceRow());
158+
assertEquals("alias", r.column(2));
159+
assertEquals("ALIAS", r.column(3));
160+
}, index, alias);
161+
}
162+
134163
public void testSysTablesWithCatalogOnlyAliases() throws Exception {
135164
executeCommand("SYS TABLES CATALOG LIKE '%' LIKE 'test' TYPE 'ALIAS'", r -> {
136165
assertEquals(1, r.size());

0 commit comments

Comments
 (0)