Skip to content

Commit 51b16e6

Browse files
committed
Fix one of ray comment
1 parent b0e35cc commit 51b16e6

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

presto-clp/src/main/java/com/facebook/presto/plugin/clp/ClpFilterToKqlConverter.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
import static com.facebook.presto.common.function.OperatorType.flip;
5151
import static com.facebook.presto.common.type.BooleanType.BOOLEAN;
5252
import static com.facebook.presto.plugin.clp.ClpErrorCode.CLP_PUSHDOWN_UNSUPPORTED_EXPRESSION;
53-
import static com.facebook.presto.plugin.clp.ClpUtils.KqlUtils.escapeKqlSpecialCharsForStringValue;
53+
import static com.facebook.presto.plugin.clp.ClpUtils.escapeKqlSpecialCharsForStringValue;
54+
import static com.facebook.presto.plugin.clp.ClpUtils.isNumericType;
5455
import static com.facebook.presto.spi.relation.SpecialFormExpression.Form.AND;
5556
import static java.lang.Integer.parseInt;
5657
import static java.lang.String.format;
@@ -128,9 +129,9 @@ public ClpExpression visitCall(CallExpression node, Void context)
128129
}
129130

130131
/**
131-
* Handles the BETWEEN expression for numeric range, the range must be numeric. If the first
132-
* argument is not a variable reference expression or either range boundaries is not constant
133-
* expressions it won't translate the expression.
132+
* Handles the BETWEEN expression for numeric range, all arguments must be numeric. If the first
133+
* argument is not a variable reference expression or either the second or the third is not
134+
* constant expressions it won't translate the expression.
134135
*
135136
* <p></p>
136137
* Example: <code>col1 BETWEEN 0 AND 5</code> → <code>col1 >= 0 AND col1 <= 5</code>
@@ -150,6 +151,10 @@ private ClpExpression handleBetween(CallExpression node)
150151
|| !(node.getArguments().get(2) instanceof ConstantExpression)) {
151152
return new ClpExpression(node);
152153
}
154+
if (!isNumericType(node.getArguments().get(0).getType())) {
155+
// Let the Presto SQL analyzer throw exception
156+
return new ClpExpression(node);
157+
}
153158
Optional<String> variableReferencePushDownExpression = node.getArguments().get(0).accept(this, null).getPushDownExpression();
154159
if (!variableReferencePushDownExpression.isPresent()) {
155160
return new ClpExpression(node);

presto-clp/src/main/java/com/facebook/presto/plugin/clp/ClpUtils.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,42 @@
1414
package com.facebook.presto.plugin.clp;
1515

1616
import com.facebook.airlift.log.Logger;
17+
import com.facebook.presto.common.type.Type;
18+
19+
import static com.facebook.presto.common.type.BigintType.BIGINT;
20+
import static com.facebook.presto.common.type.DoubleType.DOUBLE;
1721

1822
public class ClpUtils
1923
{
20-
public static class KqlUtils
24+
private static final Logger log = Logger.get(ClpUtils.class);
25+
26+
/**
27+
* Refer to
28+
* <a href="https://docs.yscope.com/clp/main/user-guide/reference-json-search-syntax">here
29+
* </a> for all special chars in the string value that need to be escaped.
30+
*
31+
* @param literalString the target string to escape special chars '\', '"', ;?' and '*'
32+
* @return the escaped string
33+
*/
34+
public static String escapeKqlSpecialCharsForStringValue(String literalString)
2135
{
22-
private static final Logger log = Logger.get(KqlUtils.class);
36+
String escaped = literalString;
37+
escaped = escaped.replace("\\", "\\\\");
38+
escaped = escaped.replace("\"", "\\\"");
39+
escaped = escaped.replace("?", "\\?");
40+
escaped = escaped.replace("*", "\\*");
41+
return escaped;
42+
}
2343

24-
public static String escapeKqlSpecialCharsForStringValue(String literalString)
25-
{
26-
String escaped = literalString;
27-
escaped = escaped.replace("\\", "\\\\");
28-
escaped = escaped.replace("\"", "\\\"");
29-
escaped = escaped.replace("?", "\\?");
30-
escaped = escaped.replace("*", "\\*");
31-
return escaped;
32-
}
44+
/**
45+
* Check if the type is one of the numeric types that CLP will handle. Refer to
46+
* {@code ClpSchemaTree} for all types that CLP will handle.
47+
*
48+
* @param type the type to check
49+
* @return is the type numeric or not
50+
*/
51+
public static boolean isNumericType(Type type)
52+
{
53+
return BIGINT == type || DOUBLE == type;
3354
}
3455
}

presto-clp/src/test/java/com/facebook/presto/plugin/clp/TestClpFilterToKql.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,8 @@ public void testBetweenPushDown()
106106
testFilter(tryPushDown("0 BETWEEN NULL AND 5", sessionHolder, ImmutableSet.of()), null, "BETWEEN(0, null, 5)", sessionHolder, false);
107107

108108
// Illegal cases
109-
assertThrows(SemanticException.class, () -> {
110-
testFilter(
111-
tryPushDown("fare BETWEEN 'Apple' AND 'Pear'", sessionHolder, ImmutableSet.of()),
112-
"fare >= 0 AND fare <= 5",
113-
null,
114-
sessionHolder,
115-
true);
116-
});
109+
assertThrows(SemanticException.class, () -> tryPushDown("fare BETWEEN 'Apple' AND 'Pear'", sessionHolder, ImmutableSet.of()));
110+
assertThrows(SemanticException.class, () -> tryPushDown("city.Name BETWEEN 0 AND 5", sessionHolder, ImmutableSet.of()));
117111
}
118112

119113
@Test
@@ -288,6 +282,12 @@ public void testMetadataSqlGeneration()
288282
sessionHolder,
289283
"(fare > 0 AND city.Name: \"b*\")",
290284
"(\"fare\" > 0)");
285+
testFilterWithMetadataSql(
286+
tryPushDown("((fare BETWEEN 0 AND 5) AND city.Name like 'b%')",
287+
sessionHolder, ImmutableSet.of("fare")),
288+
sessionHolder,
289+
"(fare >= 0 AND fare <= 5 AND city.Name: \"b*\")",
290+
"(\"fare\" >= 0 AND \"fare\" <= 5)");
291291
testFilterWithMetadataSql(
292292
tryPushDown("(fare > 0 OR city.Name like 'b%')",
293293
sessionHolder, ImmutableSet.of("fare")),

0 commit comments

Comments
 (0)