[ 
https://issues.apache.org/jira/browse/GEODE-3938?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16254193#comment-16254193
 ] 

ASF GitHub Bot commented on GEODE-3938:
---------------------------------------

jhuynh1 closed pull request #1003: Fix for GEODE-3938
URL: https://github.com/apache/geode/pull/1003
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/geode-core/src/main/antlr/org/apache/geode/cache/query/internal/parse/oql.g 
b/geode-core/src/main/antlr/org/apache/geode/cache/query/internal/parse/oql.g
index a743752743..c2913663b5 100644
--- 
a/geode-core/src/main/antlr/org/apache/geode/cache/query/internal/parse/oql.g
+++ 
b/geode-core/src/main/antlr/org/apache/geode/cache/query/internal/parse/oql.g
@@ -917,7 +917,7 @@ conversionExpr :
                
"to_date"^<AST=org.apache.geode.cache.query.internal.parse.ASTConversionExpr>
               )
               TOK_LPAREN!
-              stringLiteral TOK_COMMA! stringLiteral
+              (stringLiteral | queryParam) TOK_COMMA! stringLiteral
               TOK_RPAREN! 
            )    
        )
diff --git 
a/geode-core/src/test/java/org/apache/geode/cache/query/QueryServiceJUnitTest.java
 
b/geode-core/src/test/java/org/apache/geode/cache/query/QueryServiceJUnitTest.java
index a360fe7ac2..45658fbe7e 100644
--- 
a/geode-core/src/test/java/org/apache/geode/cache/query/QueryServiceJUnitTest.java
+++ 
b/geode-core/src/test/java/org/apache/geode/cache/query/QueryServiceJUnitTest.java
@@ -71,6 +71,54 @@ public void testNewQuery() throws Exception {
   }
 
   @Test
+  public void toDateWithPresetDateShouldExecuteWithoutExceptions() throws 
Exception {
+    String testDate = "01/01/2000";
+    QueryService queryService = CacheUtils.getQueryService();
+    Query query = queryService.newQuery(
+        "SELECT * FROM /Portfolios WHERE createDate >= to_date('" + testDate + 
"', 'MM/dd/yyyy')");
+    query.execute();
+  }
+
+  @Test
+  public void toDateWithValidBindParameterDateShouldExecuteWithoutExceptions() 
throws Exception {
+    String testDate = "01/01/2000";
+    QueryService queryService = CacheUtils.getQueryService();
+    Query query = queryService
+        .newQuery("SELECT * FROM /Portfolios WHERE createDate >= to_date($1, 
'MM/dd/yyyy')");
+    query.execute(testDate);
+  }
+
+  @Test
+  public void 
toDateWithInValidStringBindParameterDateShouldThrowQueryInvalidException()
+      throws Exception {
+    String invalid = "someInvalidString";
+    QueryService queryService = CacheUtils.getQueryService();
+    Query query = queryService
+        .newQuery("SELECT * FROM /Portfolios WHERE createDate >= to_date($1, 
'MM/dd/yyyy')");
+    try {
+      query.execute(invalid);
+      fail();
+    } catch (QueryInvalidException e) {
+      // expected
+    }
+  }
+
+  @Test
+  public void 
toDateWithRegionAsBindParameterDateShouldThrowQueryInvalidException()
+      throws Exception {
+    Object invalid = CacheUtils.getRegion("Portfolios");
+    QueryService queryService = CacheUtils.getQueryService();
+    Query query = queryService
+        .newQuery("SELECT * FROM /Portfolios WHERE createDate >= to_date($1, 
'MM/dd/yyyy')");
+    try {
+      query.execute(invalid);
+      fail();
+    } catch (QueryInvalidException e) {
+      // expected
+    }
+  }
+
+  @Test
   public void testCreateIndex() throws Exception {
     CacheUtils.log("testCreateIndex");
     QueryService qs = CacheUtils.getQueryService();
diff --git 
a/geode-core/src/test/java/org/apache/geode/cache/query/internal/parse/OQLParserTest.java
 
b/geode-core/src/test/java/org/apache/geode/cache/query/internal/parse/OQLParserTest.java
new file mode 100644
index 0000000000..636726b11d
--- /dev/null
+++ 
b/geode-core/src/test/java/org/apache/geode/cache/query/internal/parse/OQLParserTest.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache 
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the 
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ * or implied. See the License for the specific language governing permissions 
and limitations under
+ * the License.
+ */
+package org.apache.geode.cache.query.internal.parse;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.StringReader;
+
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.geode.test.junit.categories.UnitTest;
+
+/**
+ * TODO: class created to fix GEODE-3938. Add more tests for other queries, 
parameters, etc.
+ */
+@Category(UnitTest.class)
+public class OQLParserTest {
+
+  @Test
+  public void testToDatePresetQueryFunction() throws Exception {
+    String oqlSource =
+        "SELECT * FROM /Portfolios WHERE createDate >= to_date('01/01/2000', 
'MM/dd/yyyy')";
+    OQLLexer lexer = new OQLLexer(new StringReader(oqlSource));
+    OQLParser parser = new OQLParser(lexer);
+
+    parser.queryProgram();
+    assertThat(parser.getAST()).isNotNull();
+  }
+
+  @Test
+  public void testToDatePresetQueryFunctionWithQueryParameter() throws 
Exception {
+    String oqlSource = "SELECT * FROM /Portfolios WHERE createDate >= 
to_date($1, 'MM/dd/yyyy')";
+    OQLLexer lexer = new OQLLexer(new StringReader(oqlSource));
+    OQLParser parser = new OQLParser(lexer);
+
+    parser.queryProgram();
+    assertThat(parser.getAST()).isNotNull();
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Allow query parameters within the to_date preset query function
> ---------------------------------------------------------------
>
>                 Key: GEODE-3938
>                 URL: https://issues.apache.org/jira/browse/GEODE-3938
>             Project: Geode
>          Issue Type: New Feature
>          Components: querying
>            Reporter: Juan José Ramos Cassella
>            Assignee: Juan José Ramos Cassella
>            Priority: Trivial
>
> Currently the {{to_date}} preset query function doesn't allow the user to 
> pass the date to format as a query parameter, which forces the user 
> application to add boilerplate code to manually parse the date.
> Apparently there's no internal blockers preventing such a feature to be 
> enabled, and the change just implies a small change in {{oql.g}}:
> {code}
> conversionExpr :
>   (
>     ...
>         |
>            (  
>               (
>                
> "to_date"^<AST=org.apache.geode.cache.query.internal.parse.ASTConversionExpr>
>               )
>               TOK_LPAREN!
>               (stringLiteral | queryParam) TOK_COMMA! stringLiteral
>               TOK_RPAREN! 
>            )    
>     ...
>   )
>     ;
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to