Skip to content

Commit 9bf8bd4

Browse files
committed
SQL: Tweak pattern matching in SYS TABLES (#41243)
Yet another improvement to SYS TABLES on differentiating between table types specified as '%' and '' while maintaining legacy support for null Fix #40775 (cherry picked from commit 6dbca5e)
1 parent 4bff26e commit 9bf8bd4

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public SysTables visitSysTables(SysTablesContext ctx) {
146146
boolean legacyTableType = false;
147147
for (StringContext string : ctx.string()) {
148148
String value = string(string);
149-
if (value != null) {
149+
if (value != null && value.isEmpty() == false) {
150150
// check special ODBC wildcard case
151151
if (value.equals(StringUtils.SQL_WILDCARD) && ctx.string().size() == 1) {
152152
// treat % as null

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

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,30 @@ public class SysTablesTests extends ESTestCase {
5454
//
5555
// catalog enumeration
5656
//
57-
public void testSysTablesCatalogEnumeration() throws Exception {
57+
public void testSysTablesCatalogEnumerationWithEmptyType() throws Exception {
58+
executeCommand("SYS TABLES CATALOG LIKE '%' LIKE '' TYPE ''", r -> {
59+
assertEquals(1, r.size());
60+
assertEquals(CLUSTER_NAME, r.column(0));
61+
// everything else should be null
62+
for (int i = 1; i < 10; i++) {
63+
assertNull(r.column(i));
64+
}
65+
}, index);
66+
}
67+
68+
public void testSysTablesCatalogAllTypes() throws Exception {
69+
executeCommand("SYS TABLES CATALOG LIKE '%' LIKE '' TYPE '%'", r -> {
70+
assertEquals(1, r.size());
71+
assertEquals(CLUSTER_NAME, r.column(0));
72+
// everything else should be null
73+
for (int i = 1; i < 10; i++) {
74+
assertNull(r.column(i));
75+
}
76+
}, new IndexInfo[0]);
77+
}
78+
79+
// when types are null, consider them equivalent to '' for compatibility reasons
80+
public void testSysTablesCatalogNoTypes() throws Exception {
5881
executeCommand("SYS TABLES CATALOG LIKE '%' LIKE ''", r -> {
5982
assertEquals(1, r.size());
6083
assertEquals(CLUSTER_NAME, r.column(0));
@@ -65,24 +88,18 @@ public void testSysTablesCatalogEnumeration() throws Exception {
6588
}, index);
6689
}
6790

91+
6892
//
6993
// table types enumeration
7094
//
95+
96+
// missing type means pattern
7197
public void testSysTablesTypesEnumerationWoString() throws Exception {
7298
executeCommand("SYS TABLES CATALOG LIKE '' LIKE '' ", r -> {
7399
assertEquals(2, r.size());
74100
assertEquals("BASE TABLE", r.column(3));
75101
assertTrue(r.advanceRow());
76102
assertEquals("VIEW", r.column(3));
77-
}, new IndexInfo[0]);
78-
}
79-
80-
public void testSysTablesEnumerateTypes() throws Exception {
81-
executeCommand("SYS TABLES CATALOG LIKE '' LIKE '' TYPE '%'", r -> {
82-
assertEquals(2, r.size());
83-
assertEquals("BASE TABLE", r.column(3));
84-
assertTrue(r.advanceRow());
85-
assertEquals("VIEW", r.column(3));
86103
}, alias, index);
87104
}
88105

@@ -107,6 +124,13 @@ public void testSysTablesTypesEnumeration() throws Exception {
107124
}, new IndexInfo[0]);
108125
}
109126

127+
// when a type is specified, apply filtering
128+
public void testSysTablesTypesEnumerationAllCatalogsAndSpecifiedView() throws Exception {
129+
executeCommand("SYS TABLES CATALOG LIKE '%' LIKE '' TYPE 'VIEW'", r -> {
130+
assertEquals(0, r.size());
131+
}, new IndexInfo[0]);
132+
}
133+
110134
public void testSysTablesDifferentCatalog() throws Exception {
111135
executeCommand("SYS TABLES CATALOG LIKE 'foo'", r -> {
112136
assertEquals(0, r.size());
@@ -262,6 +286,12 @@ public void testSysTablesWithCatalogOnlyAliases() throws Exception {
262286
}, alias);
263287
}
264288

289+
public void testSysTablesWithEmptyCatalogOnlyAliases() throws Exception {
290+
executeCommand("SYS TABLES CATALOG LIKE '' LIKE 'test' TYPE 'VIEW'", r -> {
291+
assertEquals(0, r.size());
292+
}, alias);
293+
}
294+
265295
public void testSysTablesWithInvalidType() throws Exception {
266296
executeCommand("SYS TABLES LIKE 'test' TYPE 'QUE HORA ES'", r -> {
267297
assertEquals(0, r.size());

0 commit comments

Comments
 (0)