Skip to content

SQL: Improve compatibility with MS query #30516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public Object visitSysCatalogs(SysCatalogsContext ctx) {
@Override
public SysTables visitSysTables(SysTablesContext ctx) {
List<IndexType> types = new ArrayList<>();
boolean legacyTableType = false;
for (StringContext string : ctx.string()) {
String value = string(string);
if (value != null) {
Expand All @@ -156,6 +157,12 @@ public SysTables visitSysTables(SysTablesContext ctx) {
// since % is the same as not specifying a value, choose
// https://docs.microsoft.com/en-us/sql/odbc/reference/develop-app/value-list-arguments?view=ssdt-18vs2017
// that is skip the value
}
// special case for legacy apps (like msquery) that always asks for 'TABLE'
// which we manually map to all concrete tables supported
else if (value.toUpperCase(Locale.ROOT).equals("TABLE")) {
legacyTableType = true;
types.add(IndexType.INDEX);
} else {
IndexType type = IndexType.from(value);
types.add(type);
Expand All @@ -165,7 +172,7 @@ public SysTables visitSysTables(SysTablesContext ctx) {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,21 @@ public class SysTables extends Command {
private final LikePattern pattern;
private final LikePattern clusterPattern;
private final EnumSet<IndexType> types;
// flag indicating whether tables are reported as `TABLE` or `BASE TABLE`
private final boolean legacyTableTypes;

public SysTables(Location location, LikePattern clusterPattern, LikePattern pattern, EnumSet<IndexType> types) {
public SysTables(Location location, LikePattern clusterPattern, LikePattern pattern, EnumSet<IndexType> types,
boolean legacyTableTypes) {
super(location);
this.clusterPattern = clusterPattern;
this.pattern = pattern;
this.types = types;
this.legacyTableTypes = legacyTableTypes;
}

@Override
protected NodeInfo<SysTables> info() {
return NodeInfo.create(this, SysTables::new, clusterPattern, pattern, types);
return NodeInfo.create(this, SysTables::new, clusterPattern, pattern, types, legacyTableTypes);
}

@Override
Expand Down Expand Up @@ -111,7 +115,7 @@ public final void execute(SqlSession session, ActionListener<SchemaRowSet> liste
.map(t -> asList(cluster,
EMPTY,
t.name(),
t.type().toSql(),
legacyName(t.type()),
EMPTY,
null,
null,
Expand All @@ -122,6 +126,10 @@ public final void execute(SqlSession session, ActionListener<SchemaRowSet> liste
, listener::onFailure));
}

private String legacyName(IndexType indexType) {
return legacyTableTypes && indexType == IndexType.INDEX ? "TABLE" : indexType.toSql();
}

@Override
public int hashCode() {
return Objects.hash(clusterPattern, pattern, types);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ public void testSysTablesOnlyIndices() throws Exception {
}, index);
}

public void testSysTablesOnlyIndicesInLegacyMode() throws Exception {
executeCommand("SYS TABLES LIKE 'test' TYPE 'TABLE'", r -> {
assertEquals(1, r.size());
assertEquals("test", r.column(2));
assertEquals("TABLE", r.column(3));
}, index);

}

public void testSysTablesOnlyIndicesLegacyModeParameterized() throws Exception {
executeCommand("SYS TABLES LIKE 'test' TYPE ?", asList(param("TABLE")), r -> {
assertEquals(1, r.size());
assertEquals("test", r.column(2));
assertEquals("TABLE", r.column(3));
}, index);
}

public void testSysTablesOnlyIndicesParameterized() throws Exception {
executeCommand("SYS TABLES LIKE 'test' TYPE ?", asList(param("ALIAS")), r -> {
assertEquals(1, r.size());
Expand All @@ -131,6 +148,18 @@ public void testSysTablesOnlyIndicesAndAliasesParameterized() throws Exception {
}, index, alias);
}

public void testSysTablesOnlyIndicesLegacyAndAliasesParameterized() throws Exception {
List<SqlTypedParamValue> params = asList(param("ALIAS"), param("TABLE"));
executeCommand("SYS TABLES LIKE 'test' TYPE ?, ?", params, r -> {
assertEquals(2, r.size());
assertEquals("test", r.column(2));
assertEquals("TABLE", r.column(3));
assertTrue(r.advanceRow());
assertEquals("alias", r.column(2));
assertEquals("ALIAS", r.column(3));
}, index, alias);
}

public void testSysTablesWithCatalogOnlyAliases() throws Exception {
executeCommand("SYS TABLES CATALOG LIKE '%' LIKE 'test' TYPE 'ALIAS'", r -> {
assertEquals(1, r.size());
Expand Down