Skip to content

Commit 636d35e

Browse files
committed
apply review suggestion
1 parent 7dcc7fc commit 636d35e

File tree

1 file changed

+55
-47
lines changed

1 file changed

+55
-47
lines changed

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

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@
6565
* Supported translations include:
6666
* <ul>
6767
* <li>Comparisons between variables and constants (e.g., =, !=, <, >, <=, >=).</li>
68-
* <li>String pattern matches using LIKE with constant patterns only. <code>"^%[^%_]*%$"</code>
69-
* is not supported.</li>
70-
* <li>Membership checks using IN with a list of constants only</li>
71-
* <li>NULL checks via IS NULL</li>
72-
* <li>Substring comparisons (e.g., <code>SUBSTR(x, start, len) = "val"</code>) are supported
73-
* only when compared against a constant.</li>
74-
* <li>Dereferencing fields from row-typed variables</li>
75-
* <li>Logical operators AND, OR, and NOT</li>
68+
* <li>String pattern matches using LIKE with constant patterns only. Patterns that begin and
69+
* end with <code>%</code> (i.e., <code>"^%[^%_]*%$"</code>) are not supported.</li>
70+
* <li>Membership checks using IN with a list of constants only.</li>
71+
* <li>NULL checks via IS NULL.</li>
72+
* <li>Substring comparisons (e.g., <code>SUBSTR(x, start, len) = "val"</code>) against a
73+
* constant.</li>
74+
* <li>Dereferencing fields from row-typed variables.</li>
75+
* <li>Logical operators AND, OR, and NOT.</li>
7676
* </ul>
7777
*/
7878
public class ClpFilterToKqlConverter
@@ -188,8 +188,8 @@ private String getVariableName(VariableReferenceExpression variable)
188188
* Example: <code>NOT (col1 = 5)</code> → <code>NOT col1: 5</code>
189189
*
190190
* @param node the NOT call expression
191-
* @return a ClpExpression containing the equivalent KQL query or the original expression if it
192-
* couldn't be translated
191+
* @return a ClpExpression containing either the equivalent KQL query, or the original
192+
* expression if it couldn't be translated
193193
*/
194194
private ClpExpression handleNot(CallExpression node)
195195
{
@@ -209,14 +209,15 @@ private ClpExpression handleNot(CallExpression node)
209209
/**
210210
* Handles LIKE expressions.
211211
* <p></p>
212-
* Converts SQL LIKE patterns into equivalent KQL queries using <code>*</code> (for <code>%</code>)
213-
* and <code>?</code> (for <code>_</code>). Only supports constant or casted constant patterns.
212+
* Converts SQL LIKE patterns into equivalent KQL queries using <code>*</code> (for
213+
* <code>%</code>) and <code>?</code> (for <code>_</code>). Only supports constant or casted
214+
* constant patterns.
214215
* <p></p>
215216
* Example: <code>col1 LIKE 'a_bc%'</code> → <code>col1: "a?bc*"</code>
216217
*
217218
* @param node the LIKE call expression
218-
* @return a ClpExpression containing the equivalent KQL query, or the original expression if it
219-
* couldn't be translated
219+
* @return a ClpExpression containing either the equivalent KQL query, or the original
220+
* expression if it couldn't be translated
220221
*/
221222
private ClpExpression handleLike(CallExpression node)
222223
{
@@ -259,13 +260,13 @@ else if (argument instanceof CallExpression) {
259260
/**
260261
* Handles logical binary operators (e.g., <code>=, !=, <, ></code>) between two expressions.
261262
* <p></p>
262-
* Supports constant values on either side and flips the operator if necessary. Also delegates to a
263-
* substring handler for <code>SUBSTR(x, ...) = 'value'</code> patterns.
263+
* Supports constant values on either side and flips the operator if necessary. Also delegates
264+
* to a substring handler for <code>SUBSTR(x, ...) = 'value'</code> patterns.
264265
*
265266
* @param operator the binary operator (e.g., EQUAL, NOT_EQUAL)
266267
* @param node the call expression representing the binary operation
267-
* @return a ClpExpression containing the equivalent KQL query or the original expression if it
268-
* couldn't be translated
268+
* @return a ClpExpression containing either the equivalent KQL query, or the original
269+
* expression if it couldn't be translated
269270
*/
270271
private ClpExpression handleLogicalBinary(OperatorType operator, CallExpression node)
271272
{
@@ -339,8 +340,8 @@ else if (leftIsConstant) {
339340
* @param operator the comparison operator
340341
* @param literalType the type of the literal
341342
* @param originalNode the original RowExpression node
342-
* @return a ClpExpression containing the equivalent KQL query or the original expression if it
343-
* couldn't be translated
343+
* @return a ClpExpression containing either the equivalent KQL query, or the original
344+
* expression if it couldn't be translated
344345
*/
345346
private ClpExpression buildClpExpression(
346347
String variableName,
@@ -372,13 +373,15 @@ else if (LOGICAL_BINARY_OPS_FILTER.contains(operator) && !(literalType instanceo
372373
}
373374

374375
/**
375-
* Checks whether the given expression matches the pattern SUBSTR(x, ...) = 'someString',
376-
* and if so, attempts to convert it into a KQL query using wildcards and construct a CLP expression.
376+
* Checks whether the given expression matches the pattern
377+
* <code>SUBSTR(x, ...) = 'someString'</code>, and if so, attempts to convert it into a KQL
378+
* query using wildcards and constructs a CLP expression.
377379
*
378380
* @param operator the comparison operator (should be EQUAL)
379381
* @param possibleSubstring the left or right expression, possibly a SUBSTR call
380382
* @param possibleLiteral the opposite expression, possibly a string constant
381-
* @return a ClpExpression containing the translated KQL filter or an empty one if conversion fails
383+
* @return a ClpExpression containing either the equivalent KQL query, or nothing if it couldn't
384+
* be translated
382385
*/
383386
private ClpExpression tryInterpretSubstringEquality(
384387
OperatorType operator,
@@ -407,7 +410,7 @@ private ClpExpression tryInterpretSubstringEquality(
407410
* Parses a <code>SUBSTR(x, start [, length])</code> call into a SubstrInfo object if valid.
408411
*
409412
* @param callExpression the call expression to inspect
410-
* @return an Optional containing SubstrInfo if the expression is a valid SUBSTR call, otherwise empty
413+
* @return an Optional containing SubstrInfo if the expression is a valid SUBSTR call
411414
*/
412415
private Optional<SubstrInfo> parseSubstringCall(CallExpression callExpression)
413416
{
@@ -438,7 +441,8 @@ private Optional<SubstrInfo> parseSubstringCall(CallExpression callExpression)
438441
}
439442

440443
/**
441-
* Converts a <code>SUBSTR(x, start [, length]) = 'someString'</code> into a KQL-style wildcard query.
444+
* Converts a <code>SUBSTR(x, start [, length]) = 'someString'</code> into a KQL-style wildcard
445+
* query.
442446
* <p></p>
443447
* Examples:
444448
* <ul>
@@ -450,7 +454,8 @@ private Optional<SubstrInfo> parseSubstringCall(CallExpression callExpression)
450454
*
451455
* @param info parsed SUBSTR call info
452456
* @param targetString the literal string being compared to
453-
* @return a ClpExpression containing the translated KQL query if successful; otherwise, an empty ClpExpression
457+
* @return a ClpExpression containing either the equivalent KQL query, or nothing if it couldn't
458+
* be translated
454459
*/
455460
private ClpExpression interpretSubstringEquality(SubstrInfo info, String targetString)
456461
{
@@ -498,7 +503,7 @@ private ClpExpression interpretSubstringEquality(SubstrInfo info, String targetS
498503
* Attempts to parse a RowExpression as an integer constant.
499504
*
500505
* @param expression the row expression to parse
501-
* @return an Optional containing the parsed integer value, if successful
506+
* @return an Optional containing the integer value if it could be parsed
502507
*/
503508
private Optional<Integer> parseIntValue(RowExpression expression)
504509
{
@@ -532,7 +537,7 @@ else if (expression instanceof CallExpression) {
532537
*
533538
* @param lengthExpression the expression representing the length parameter
534539
* @param targetString the target string to compare length against
535-
* @return an Optional containing the length if it matches targetString.length(), otherwise empty
540+
* @return an Optional containing the length if it matches targetString.length()
536541
*/
537542
private Optional<Integer> parseLengthLiteral(RowExpression lengthExpression, String targetString)
538543
{
@@ -551,14 +556,14 @@ private Optional<Integer> parseLengthLiteral(RowExpression lengthExpression, Str
551556
}
552557

553558
/**
554-
* Handles the logical AND expression.
559+
* Handles the logical <code>AND</code> expression.
555560
* <p></p>
556-
* Combines all definable child expressions into a single KQL query joined by AND.
557-
* Any unsupported children are collected into a remaining expression.
561+
* Combines all definable child expressions into a single KQL query joined by AND. Any
562+
* unsupported children are collected into the remaining expression.
558563
* <p></p>
559564
* Example: <code>col1 = 5 AND col2 = 'abc'</code> → <code>(col1: 5 AND col2: "abc")</code>
560565
*
561-
* @param node the AND special form expression
566+
* @param node the <code>AND</code> special form expression
562567
* @return a ClpExpression containing the KQL query and any remaining sub-expressions
563568
*/
564569
private ClpExpression handleAnd(SpecialFormExpression node)
@@ -596,15 +601,16 @@ else if (!remainingExpressions.isEmpty()) {
596601
}
597602

598603
/**
599-
* Handles the logical OR expression.
604+
* Handles the logical <code>OR</code> expression.
600605
* <p></p>
601606
* Combines all fully convertible child expressions into a single KQL query joined by OR.
602607
* Falls back to the original node if any child cannot be converted.
603608
* <p></p>
604609
* Example: <code>col1 = 5 OR col1 = 10</code> → <code>(col1: 5 OR col1: 10)</code>
605610
*
606-
* @param node the OR special form expression
607-
* @return a ClpExpression containing the OR-based KQL string, or the original expression if not fully convertible
611+
* @param node the <code>OR</code> special form expression
612+
* @return a ClpExpression containing either the equivalent KQL query, or the original
613+
* expression if it couldn't be fully translated
608614
*/
609615
private ClpExpression handleOr(SpecialFormExpression node)
610616
{
@@ -623,13 +629,13 @@ private ClpExpression handleOr(SpecialFormExpression node)
623629
}
624630

625631
/**
626-
* Handles the IN predicate.
632+
* Handles the <code>IN</code> predicate.
627633
* <p></p>
628634
* Example: <code>col1 IN (1, 2, 3)</code> → <code>(col1: 1 OR col1: 2 OR col1: 3)</code>
629635
*
630-
* @param node the IN special form expression
631-
* @return a ClpExpression containing the equivalent KQL query or the original expression if it
632-
* couldn't be translated
636+
* @param node the <code>IN</code> special form expression
637+
* @return a ClpExpression containing either the equivalent KQL query, or the original
638+
* expression if it couldn't be translated
633639
*/
634640
private ClpExpression handleIn(SpecialFormExpression node)
635641
{
@@ -655,18 +661,19 @@ private ClpExpression handleIn(SpecialFormExpression node)
655661
}
656662
queryBuilder.append(" OR ");
657663
}
664+
658665
// Remove the last " OR " from the query
659666
return new ClpExpression(queryBuilder.substring(0, queryBuilder.length() - 4) + ")");
660667
}
661668

662669
/**
663-
* Handles the IS NULL predicate.
670+
* Handles the <code>IS NULL</code> predicate.
664671
* <p></p>
665672
* Example: <code>col1 IS NULL</code> → <code>NOT col1: *</code>
666673
*
667-
* @param node the IS_NULL special form expression
668-
* @return a ClpExpression containing the equivalent KQL query or the original expression if it
669-
* couldn't be translated
674+
* @param node the <code>IS_NULL</code> special form expression
675+
* @return a ClpExpression containing either the equivalent KQL query, or the original
676+
* expression if it couldn't be translated
670677
*/
671678
private ClpExpression handleIsNull(SpecialFormExpression node)
672679
{
@@ -687,13 +694,14 @@ private ClpExpression handleIsNull(SpecialFormExpression node)
687694
/**
688695
* Handles dereference expressions on RowTypes (e.g., <code>col.row_field</code>).
689696
* <p></p>
690-
* Converts nested row field access into dot-separated KQL-compatible field names.
697+
* Converts nested row field accesses into dot-separated KQL-compatible field names.
691698
* <p></p>
692699
* Example: <code>address.city</code> (from a RowType 'address') → <code>address.city</code>
693700
*
694-
* @param expression the dereference expression (SpecialFormExpression or VariableReferenceExpression)
695-
* @return a ClpExpression containing the dot-separated field name or the original expression if it
696-
* couldn't be translated
701+
* @param expression the dereference expression ({@link SpecialFormExpression} or
702+
* {@link VariableReferenceExpression})
703+
* @return a ClpExpression containing either the dot-separated field name, or the original
704+
* expression if it couldn't be translated
697705
*/
698706
private ClpExpression handleDereference(RowExpression expression)
699707
{

0 commit comments

Comments
 (0)