1616import com .facebook .presto .common .function .OperatorType ;
1717import com .facebook .presto .common .type .DecimalType ;
1818import com .facebook .presto .common .type .RowType ;
19- import com .facebook .presto .common .type .TimestampType ;
2019import com .facebook .presto .common .type .Type ;
2120import com .facebook .presto .common .type .VarcharType ;
2221import com .facebook .presto .plugin .clp .ClpColumnHandle ;
5857import static com .facebook .presto .common .type .IntegerType .INTEGER ;
5958import static com .facebook .presto .common .type .RealType .REAL ;
6059import static com .facebook .presto .common .type .SmallintType .SMALLINT ;
61- import static com .facebook .presto .common .type .TimestampType .TIMESTAMP ;
62- import static com .facebook .presto .common .type .TimestampType .TIMESTAMP_MICROSECONDS ;
6360import static com .facebook .presto .common .type .TinyintType .TINYINT ;
6461import static com .facebook .presto .plugin .clp .ClpErrorCode .CLP_PUSHDOWN_UNSUPPORTED_EXPRESSION ;
6562import static com .facebook .presto .spi .relation .SpecialFormExpression .Form .AND ;
6663import static java .lang .Integer .parseInt ;
6764import static java .lang .String .format ;
6865import static java .util .Objects .requireNonNull ;
69- import static java .util .concurrent .TimeUnit .SECONDS ;
7066
7167/**
7268 * A translator to translate Presto {@link RowExpression}s into:
@@ -260,10 +256,8 @@ private ClpExpression handleBetween(CallExpression node)
260256 }
261257
262258 String variable = variableOpt .get ();
263- Type lowerBoundType = second .getType ();
264- String lowerBound = tryEnsureNanosecondTimestamp (lowerBoundType , getLiteralString ((ConstantExpression ) second ));
265- Type upperBoundType = third .getType ();
266- String upperBound = tryEnsureNanosecondTimestamp (upperBoundType , getLiteralString ((ConstantExpression ) third ));
259+ String lowerBound = getLiteralString ((ConstantExpression ) second );
260+ String upperBound = getLiteralString ((ConstantExpression ) third );
267261 String kql = String .format ("%s >= %s AND %s <= %s" , variable , lowerBound , variable , upperBound );
268262 String metadataSqlQuery = metadataFilterColumns .contains (variable )
269263 ? String .format ("\" %s\" >= %s AND \" %s\" <= %s" , variable , lowerBound , variable , upperBound )
@@ -446,7 +440,6 @@ private ClpExpression buildClpExpression(
446440 RowExpression originalNode )
447441 {
448442 String metadataSqlQuery = null ;
449- literalString = tryEnsureNanosecondTimestamp (literalType , literalString );
450443 if (operator .equals (EQUAL )) {
451444 if (literalType instanceof VarcharType ) {
452445 return new ClpExpression (format ("%s: \" %s\" " , variableName , escapeKqlSpecialCharsForStringValue (literalString )));
@@ -493,7 +486,7 @@ private Optional<ClpExpression> tryInterpretSubstringEquality(
493486 RowExpression possibleSubstring ,
494487 RowExpression possibleLiteral )
495488 {
496- if (!operator .equals (EQUAL ) && !( operator . equals ( NOT_EQUAL )) ) {
489+ if (!operator .equals (EQUAL )) {
497490 return Optional .empty ();
498491 }
499492
@@ -508,7 +501,7 @@ private Optional<ClpExpression> tryInterpretSubstringEquality(
508501 }
509502
510503 String targetString = getLiteralString ((ConstantExpression ) possibleLiteral );
511- return interpretSubstringEquality (maybeSubstringCall .get (), targetString , operator . equals ( EQUAL ) );
504+ return interpretSubstringEquality (maybeSubstringCall .get (), targetString );
512505 }
513506
514507 /**
@@ -561,12 +554,8 @@ private Optional<SubstrInfo> parseSubstringCall(CallExpression callExpression)
561554 * @param targetString the literal string being compared to
562555 * @return an Optional containing either a ClpExpression with the equivalent KQL query
563556 */
564- private Optional <ClpExpression > interpretSubstringEquality (SubstrInfo info , String targetString , boolean isEqual )
557+ private Optional <ClpExpression > interpretSubstringEquality (SubstrInfo info , String targetString )
565558 {
566- StringBuilder result = new StringBuilder ();
567- if (!isEqual ) {
568- result .append ("NOT " );
569- }
570559 if (info .lengthExpression != null ) {
571560 Optional <Integer > maybeStart = parseIntValue (info .startExpression );
572561 Optional <Integer > maybeLen = parseLengthLiteral (info .lengthExpression , targetString );
@@ -575,6 +564,7 @@ private Optional<ClpExpression> interpretSubstringEquality(SubstrInfo info, Stri
575564 int start = maybeStart .get ();
576565 int len = maybeLen .get ();
577566 if (start > 0 && len == targetString .length ()) {
567+ StringBuilder result = new StringBuilder ();
578568 result .append (info .variableName ).append (": \" " );
579569 for (int i = 1 ; i < start ; i ++) {
580570 result .append ("?" );
@@ -589,6 +579,7 @@ private Optional<ClpExpression> interpretSubstringEquality(SubstrInfo info, Stri
589579 if (maybeStart .isPresent ()) {
590580 int start = maybeStart .get ();
591581 if (start > 0 ) {
582+ StringBuilder result = new StringBuilder ();
592583 result .append (info .variableName ).append (": \" " );
593584 for (int i = 1 ; i < start ; i ++) {
594585 result .append ("?" );
@@ -597,8 +588,7 @@ private Optional<ClpExpression> interpretSubstringEquality(SubstrInfo info, Stri
597588 return Optional .of (new ClpExpression (result .toString ()));
598589 }
599590 if (start == -targetString .length ()) {
600- result .append (format ("%s: \" *%s\" " , info .variableName , targetString ));
601- return Optional .of (new ClpExpression (result .toString ()));
591+ return Optional .of (new ClpExpression (format ("%s: \" *%s\" " , info .variableName , targetString )));
602592 }
603593 }
604594 }
@@ -924,31 +914,9 @@ public static boolean isClpCompatibleNumericType(Type type)
924914 || type .equals (TINYINT )
925915 || type .equals (DOUBLE )
926916 || type .equals (REAL )
927- || type .equals (TIMESTAMP )
928- || type .equals (TIMESTAMP_MICROSECONDS )
929917 || type instanceof DecimalType ;
930918 }
931919
932- private static String tryEnsureNanosecondTimestamp (Type type , String literalString )
933- {
934- if (type == TIMESTAMP ) {
935- return ensureNanosecondTimestamp (TIMESTAMP , literalString );
936- }
937- else if (type == TIMESTAMP_MICROSECONDS ) {
938- return ensureNanosecondTimestamp (TIMESTAMP_MICROSECONDS , literalString );
939- }
940- return literalString ;
941- }
942-
943- private static String ensureNanosecondTimestamp (TimestampType type , String literalString )
944- {
945- long literalNumber = Long .parseLong (literalString );
946- long seconds = type .getEpochSecond (literalNumber );
947- long nanosecondFraction = type .getNanos (literalNumber );
948- long nanoseconds = SECONDS .toNanos (seconds ) + nanosecondFraction ;
949- return Long .toString (nanoseconds );
950- }
951-
952920 private static class SubstrInfo
953921 {
954922 String variableName ;
0 commit comments