Skip to content

Commit 971443a

Browse files
authored
feat: Add CLP_WILDCARD_* functions. (#64)
1 parent 02fe345 commit 971443a

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,36 @@ public static Block clpGetStringArray(@SqlType(StandardTypes.VARCHAR) Slice json
6565
{
6666
throw new UnsupportedOperationException("CLP_GET_STRING_ARRAY is a placeholder function without implementation.");
6767
}
68+
69+
@ScalarFunction(value = "CLP_WILDCARD_STRING_COLUMN", deterministic = false)
70+
@Description("Used in filter expressions to allow comparisons with any string column in the log record.")
71+
@SqlType(StandardTypes.VARCHAR)
72+
public static Slice clpWildcardStringColumn()
73+
{
74+
throw new UnsupportedOperationException("CLP_WILDCARD_STRING_COLUMN is a placeholder function without implementation.");
75+
}
76+
77+
@ScalarFunction(value = "CLP_WILDCARD_INT_COLUMN", deterministic = false)
78+
@Description("Used in filter expressions to allow comparisons with any integer column in the log record.")
79+
@SqlType(StandardTypes.BIGINT)
80+
public static long clpWildcardIntColumn()
81+
{
82+
throw new UnsupportedOperationException("CLP_WILDCARD_INT_COLUMN is a placeholder function without implementation.");
83+
}
84+
85+
@ScalarFunction(value = "CLP_WILDCARD_FLOAT_COLUMN", deterministic = false)
86+
@Description("Used in filter expressions to allow comparisons with any floating point column in the log record.")
87+
@SqlType(StandardTypes.DOUBLE)
88+
public static double clpWildcardFloatColumn()
89+
{
90+
throw new UnsupportedOperationException("CLP_WILDCARD_FLOAT_COLUMN is a placeholder function without implementation.");
91+
}
92+
93+
@ScalarFunction(value = "CLP_WILDCARD_BOOL_COLUMN", deterministic = false)
94+
@Description("Used in filter expressions to allow comparisons with any boolean column in the log record.")
95+
@SqlType(StandardTypes.BOOLEAN)
96+
public static boolean clpWildcardBoolColumn()
97+
{
98+
throw new UnsupportedOperationException("CLP_WILDCARD_BOOL_COLUMN is a placeholder function without implementation.");
99+
}
68100
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ public ClpExpression visitCall(CallExpression node, Void context)
144144
}
145145
}
146146

147+
String functionName = functionMetadata.getName().getObjectName().toUpperCase();
148+
if (functionName.startsWith("CLP_WILDCARD_")) {
149+
return new ClpExpression("*");
150+
}
151+
147152
return new ClpExpression(node);
148153
}
149154

@@ -233,23 +238,23 @@ private ClpExpression handleBetween(CallExpression node)
233238
throw new PrestoException(CLP_PUSHDOWN_UNSUPPORTED_EXPRESSION,
234239
"BETWEEN operator must have exactly three arguments. Received: " + node);
235240
}
241+
236242
RowExpression first = arguments.get(0);
237243
RowExpression second = arguments.get(1);
238244
RowExpression third = arguments.get(2);
239-
if (!(first instanceof VariableReferenceExpression)
240-
|| !(second instanceof ConstantExpression)
241-
|| !(third instanceof ConstantExpression)) {
242-
return new ClpExpression(node);
243-
}
244245
if (!isClpCompatibleNumericType(first.getType())
245246
|| !isClpCompatibleNumericType(second.getType())
246247
|| !isClpCompatibleNumericType(third.getType())) {
247248
return new ClpExpression(node);
248249
}
250+
249251
Optional<String> variableOpt = first.accept(this, null).getPushDownExpression();
250-
if (!variableOpt.isPresent()) {
252+
if (!variableOpt.isPresent()
253+
|| !(second instanceof ConstantExpression)
254+
|| !(third instanceof ConstantExpression)) {
251255
return new ClpExpression(node);
252256
}
257+
253258
String variable = variableOpt.get();
254259
String lowerBound = getLiteralString((ConstantExpression) second);
255260
String upperBound = getLiteralString((ConstantExpression) third);

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,39 @@ public void testMetadataSqlGeneration()
271271
testMetadataFilterColumns);
272272
}
273273

274+
@Test
275+
public void testClpWildcardUdf()
276+
{
277+
SessionHolder sessionHolder = new SessionHolder();
278+
279+
testPushDown(sessionHolder, "CLP_WILDCARD_STRING_COLUMN() = 'Beijing'", "*: \"Beijing\"", null);
280+
testPushDown(sessionHolder, "CLP_WILDCARD_INT_COLUMN() = 1", "*: 1", null);
281+
testPushDown(sessionHolder, "CLP_WILDCARD_FLOAT_COLUMN() > 0", "* > 0", null);
282+
testPushDown(sessionHolder, "CLP_WILDCARD_BOOL_COLUMN() = true", "*: true", null);
283+
284+
testPushDown(sessionHolder, "CLP_WILDCARD_STRING_COLUMN() like 'hello%'", "*: \"hello*\"", null);
285+
testPushDown(sessionHolder, "substr(CLP_WILDCARD_STRING_COLUMN(), 1, 2) = 'he'", "*: \"he*\"", null);
286+
testPushDown(sessionHolder, "CLP_WILDCARD_INT_COLUMN() BETWEEN 0 AND 5", "* >= 0 AND * <= 5", null);
287+
testPushDown(sessionHolder, "CLP_WILDCARD_STRING_COLUMN() IN ('hello world', 'hello world 2')", "(*: \"hello world\" OR *: \"hello world 2\")", null);
288+
289+
testPushDown(sessionHolder, "NOT CLP_WILDCARD_FLOAT_COLUMN() > 0", "NOT * > 0", null);
290+
testPushDown(
291+
sessionHolder,
292+
"CLP_WILDCARD_STRING_COLUMN() = 'Beijing' AND CLP_WILDCARD_INT_COLUMN() = 1 AND city.Region.Id = 1",
293+
"((*: \"Beijing\" AND *: 1) AND city.Region.Id: 1)",
294+
null);
295+
testPushDown(
296+
sessionHolder,
297+
"CLP_WILDCARD_STRING_COLUMN() = 'Toronto' OR CLP_WILDCARD_INT_COLUMN() = 2",
298+
"(*: \"Toronto\" OR *: 2)",
299+
null);
300+
testPushDown(
301+
sessionHolder,
302+
"CLP_WILDCARD_STRING_COLUMN() = 'Shanghai' AND (CLP_WILDCARD_INT_COLUMN() = 3 OR city.Region.Id = 5)",
303+
"(*: \"Shanghai\" AND (*: 3 OR city.Region.Id: 5))",
304+
null);
305+
}
306+
274307
private void testPushDown(SessionHolder sessionHolder, String sql, String expectedKql, String expectedRemaining)
275308
{
276309
ClpExpression clpExpression = tryPushDown(sql, sessionHolder, ImmutableSet.of());

0 commit comments

Comments
 (0)