Skip to content

Commit

Permalink
BUG: Filters on values with spaces don't work on symbol columns
Browse files Browse the repository at this point in the history
Spaces in symbols need to be escaped such `$"with spaces".
  • Loading branch information
tuor713 committed Feb 18, 2022
1 parent 45c83c1 commit 3191242
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.uwh</groupId>
<artifactId>trino-kdb-plugin</artifactId>
<version>0.0.9-${trino.version}</version>
<version>0.0.10-${trino.version}</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/uwh/trino/kdb/KDBTableHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class KDBTableHandle implements ConnectorTableHandle {
Expand Down Expand Up @@ -147,6 +149,9 @@ public Optional<String> getWhereClause() {
return Optional.empty();
}

// https://code.kx.com/q/basics/syntax/#names-and-namespaces
private static final Predicate<String> VALID_NAME = Pattern.compile("^[.a-zA-Z][._a-zA-Z0-9]*$").asPredicate();

private static String formatKDBValue(KDBType type, Object value) {
if (type == KDBType.String) {
String s = ((Slice) value).toStringUtf8();
Expand All @@ -160,7 +165,7 @@ private static String formatKDBValue(KDBType type, Object value) {
return date.format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
} else if (value instanceof Slice) {
Slice s = (Slice) value;
return "`" + s.toStringUtf8();
return VALID_NAME.test(s.toStringUtf8()) ? "`" + s.toStringUtf8() : "`$\"" + s.toStringUtf8() + "\"";
} else {
return value.toString();
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/uwh/trino/kdb/TestKDBPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,15 @@ public void testPartitionedTable() {

}

@Test
public void testSymbolWithSpaces() {
query("select sym from \"([] sym:(`with; `space; `$\"\"with space\"\"))\" where sym = 'with space'", 1);
assertEquals(res.getOnlyColumnAsSet(), Set.of("with space"));

query("select sym from \"([] sym:(`with; `space; `$\"\"with space\"\"))\" where sym in ('with', 'with space')", 2);
assertEquals(res.getOnlyColumnAsSet(), Set.of("with", "with space"));
}

@Test
public void testPartitionedTableQuerySplit() {
query("select * from partition_table", 12);
Expand Down

0 comments on commit 3191242

Please sign in to comment.