Skip to content
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 @@ -376,7 +376,7 @@ protected Node visitShowCatalogs(ShowCatalogs node, Void context)
Optional<Expression> predicate = Optional.empty();
Optional<String> likePattern = node.getLikePattern();
if (likePattern.isPresent()) {
predicate = Optional.of(new LikePredicate(identifier("Catalog"), new StringLiteral(likePattern.get()), Optional.empty()));
predicate = Optional.of(new LikePredicate(identifier("Catalog"), new StringLiteral(likePattern.get()), node.getEscape().map(StringLiteral::new)));
}

return simpleQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ statement
(LIKE pattern=string (ESCAPE escape=string)?)? #showTables
| SHOW SCHEMAS ((FROM | IN) identifier)?
(LIKE pattern=string (ESCAPE escape=string)?)? #showSchemas
| SHOW CATALOGS (LIKE pattern=string)? #showCatalogs
| SHOW CATALOGS
(LIKE pattern=string (ESCAPE escape=string)?)? #showCatalogs
| SHOW COLUMNS (FROM | IN) qualifiedName #showColumns
| SHOW STATS FOR qualifiedName #showStats
| SHOW STATS FOR '(' querySpecification ')' #showStatsForQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,10 @@ protected Void visitShowCatalogs(ShowCatalogs node, Integer context)
builder.append(" LIKE ")
.append(formatStringLiteral(value)));

node.getEscape().ifPresent((value) ->
builder.append(" ESCAPE ")
.append(formatStringLiteral(value)));

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,8 @@ public Node visitShowCatalogs(SqlBaseParser.ShowCatalogsContext context)
{
return new ShowCatalogs(getLocation(context),
getTextIfPresent(context.pattern)
.map(AstBuilder::unquote),
getTextIfPresent(context.escape)
.map(AstBuilder::unquote));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.google.common.base.MoreObjects.toStringHelper;
Expand All @@ -25,28 +26,35 @@ public final class ShowCatalogs
extends Statement
{
private final Optional<String> likePattern;
private final Optional<String> escape;

public ShowCatalogs(Optional<String> likePattern)
public ShowCatalogs(Optional<String> likePattern, Optional<String> escape)
{
this(Optional.empty(), likePattern);
this(Optional.empty(), likePattern, escape);
}

public ShowCatalogs(NodeLocation location, Optional<String> likePattern)
public ShowCatalogs(NodeLocation location, Optional<String> likePattern, Optional<String> escape)
{
this(Optional.of(location), likePattern);
this(Optional.of(location), likePattern, escape);
}

public ShowCatalogs(Optional<NodeLocation> location, Optional<String> likePattern)
public ShowCatalogs(Optional<NodeLocation> location, Optional<String> likePattern, Optional<String> escape)
{
super(location);
this.likePattern = requireNonNull(likePattern, "likePattern is null");
this.escape = requireNonNull(escape, "escape is null");
}

public Optional<String> getLikePattern()
{
return likePattern;
}

public Optional<String> getEscape()
{
return escape;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
{
Expand All @@ -62,7 +70,7 @@ public List<Node> getChildren()
@Override
public int hashCode()
{
return 0;
return Objects.hash(likePattern, escape);
}

@Override
Expand All @@ -71,12 +79,20 @@ public boolean equals(Object obj)
if (this == obj) {
return true;
}
return (obj != null) && (getClass() == obj.getClass());
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
ShowCatalogs o = (ShowCatalogs) obj;
return Objects.equals(likePattern, o.likePattern) &&
Objects.equals(escape, o.escape);
}

@Override
public String toString()
{
return toStringHelper(this).toString();
return toStringHelper(this)
.add("likePattern", likePattern)
.add("escape", escape)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,9 @@ public void testShowSession()
@Test
public void testShowCatalogs()
{
assertStatement("SHOW CATALOGS", new ShowCatalogs(Optional.empty()));
assertStatement("SHOW CATALOGS LIKE '%'", new ShowCatalogs(Optional.of("%")));
assertStatement("SHOW CATALOGS", new ShowCatalogs(Optional.empty(), Optional.empty()));
assertStatement("SHOW CATALOGS LIKE '%'", new ShowCatalogs(Optional.of("%"), Optional.empty()));
assertStatement("SHOW CATALOGS LIKE '%$_%' ESCAPE '$'", new ShowCatalogs(Optional.of("%$_%"), Optional.of("$")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ public Object[][] getStatements()
{"SELECT * FROM t WHERE EXISTS (",
"line 1:31: mismatched input '<EOF>'. Expecting: <query>"},
{"SHOW SESSION LIKE '%$_%' ESCAPE",
"line 1:32: mismatched input '<EOF>'. Expecting: <string>"}};
"line 1:32: mismatched input '<EOF>'. Expecting: <string>"},
{"SHOW CATALOGS LIKE '%$_%' ESCAPE",
"line 1:33: mismatched input '<EOF>'. Expecting: <string>"}};
}

@Test(dataProvider = "statements")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2565,6 +2565,32 @@ public void testShowCatalogsLike()
assertEquals(result.getOnlyColumnAsSet(), ImmutableSet.of(getSession().getCatalog().get()));
}

@Test
public void testShowCatalogsLikeWithEscape()
{
try {
MaterializedResult result = computeActual(getSession(), "SHOW CATALOGS LIKE 't$_%' ESCAPE ''");
assertTrue(false);
}
catch (Exception e) {
assertEquals("Escape string must be a single character", e.getMessage());
}

try {
MaterializedResult result = computeActual(getSession(), "SHOW CATALOGS LIKE 't$_%' ESCAPE '$$'");
assertTrue(false);
}
catch (Exception e) {
assertEquals("Escape string must be a single character", e.getMessage());
}

MaterializedResult result = computeActual(getSession(), "SHOW CATALOGS LIKE '%testing$_%' ESCAPE '$'");
assertEquals("[[testing_catalog]]", result.getMaterializedRows().toString());

result = computeActual(getSession(), "SHOW CATALOGS LIKE '$_%' ESCAPE '$'");
assertEquals("[]", result.getMaterializedRows().toString());
}

@Test
public void testShowSchemas()
{
Expand Down