You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2021/06/10 16:37:39 UTC

[GitHub] [incubator-pinot] mcvsubbu commented on a change in pull request #6998: Support json path expressions in query.

mcvsubbu commented on a change in pull request #6998:
URL: https://github.com/apache/incubator-pinot/pull/6998#discussion_r649350918



##########
File path: pinot-core/src/test/java/org/apache/pinot/core/query/optimizer/statement/JsonStatementOptimizerTest.java
##########
@@ -0,0 +1,201 @@
+/**
+ * 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.pinot.core.query.optimizer.statement;
+
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.common.request.PinotQuery;
+import org.apache.pinot.core.query.optimizer.QueryOptimizer;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.sql.parsers.CalciteSqlCompiler;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class JsonStatementOptimizerTest {
+  private static final QueryOptimizer OPTIMIZER = new QueryOptimizer();
+  private static final CalciteSqlCompiler SQL_COMPILER = new CalciteSqlCompiler();
+  private static final Schema SCHEMA =
+      new Schema.SchemaBuilder().setSchemaName("testTable").addSingleValueDimension("intColumn", FieldSpec.DataType.INT)
+          .addSingleValueDimension("longColumn", FieldSpec.DataType.LONG)
+          .addSingleValueDimension("stringColumn", FieldSpec.DataType.STRING)
+          .addMultiValueDimension("jsonColumn", FieldSpec.DataType.JSON).build();
+
+  /** Test that a json path expression in SELECT list is properly converted to a JSON_EXTRACT_SCALAR function within an AS function. */
+  @Test
+  public void testJsonSelect() {
+    // SELECT using a simple json path expression.
+    BrokerRequest sqlBrokerRequest1 = SQL_COMPILER.compileToBrokerRequest("SELECT jsonColumn.x FROM testTable");
+    PinotQuery pinotQuery1 = sqlBrokerRequest1.getPinotQuery();
+    OPTIMIZER.optimize(pinotQuery1, SCHEMA);
+
+    Assert.assertEquals(pinotQuery1.getSelectList().get(0).toString(),
+        "Expression(type:FUNCTION, functionCall:Function(operator:AS, operands:[Expression(type:FUNCTION, functionCall:Function(operator:JSON_EXTRACT_SCALAR, operands:[Expression(type:IDENTIFIER, identifier:Identifier(name:jsonColumn)), Expression(type:LITERAL, literal:<Literal stringValue:$.x>), Expression(type:LITERAL, literal:<Literal stringValue:STRING>), Expression(type:LITERAL, literal:<Literal stringValue:null>)])), Expression(type:IDENTIFIER, identifier:Identifier(name:jsonColumn.x))]))");
+
+    // SELECT using json path expressions with array addressing.
+    BrokerRequest sqlBrokerRequest2 = SQL_COMPILER.compileToBrokerRequest("SELECT jsonColumn.data[0][1].a.b[0] FROM testTable");
+    PinotQuery pinotQuery2 = sqlBrokerRequest2.getPinotQuery();
+    OPTIMIZER.optimize(pinotQuery2, SCHEMA);
+
+    Assert.assertEquals(pinotQuery2.getSelectList().get(0).toString(),
+        "Expression(type:FUNCTION, functionCall:Function(operator:AS, operands:[Expression(type:FUNCTION, functionCall:Function(operator:JSON_EXTRACT_SCALAR, operands:[Expression(type:IDENTIFIER, identifier:Identifier(name:jsonColumn)), Expression(type:LITERAL, literal:<Literal stringValue:$.data[0][1].a.b[0]>), Expression(type:LITERAL, literal:<Literal stringValue:STRING>), Expression(type:LITERAL, literal:<Literal stringValue:null>)])), Expression(type:IDENTIFIER, identifier:Identifier(name:jsonColumn.data[0][1].a.b[0]))]))");
+
+    // SELECT using json path expressions within double quotes.
+    BrokerRequest sqlBrokerRequest3 = SQL_COMPILER.compileToBrokerRequest("SELECT \"jsonColumn.a.b.c[0][1][2][3].d.e.f[0].g\" FROM testTable");
+    PinotQuery pinotQuery3 = sqlBrokerRequest3.getPinotQuery();
+    OPTIMIZER.optimize(pinotQuery2, SCHEMA);
+
+    Assert.assertEquals(pinotQuery2.getSelectList().get(0).toString(),
+        "Expression(type:FUNCTION, functionCall:Function(operator:AS, operands:[Expression(type:FUNCTION, functionCall:Function(operator:JSON_EXTRACT_SCALAR, operands:[Expression(type:IDENTIFIER, identifier:Identifier(name:jsonColumn)), Expression(type:LITERAL, literal:<Literal stringValue:$.data[0][1].a.b[0]>), Expression(type:LITERAL, literal:<Literal stringValue:STRING>), Expression(type:LITERAL, literal:<Literal stringValue:null>)])), Expression(type:FUNCTION, functionCall:Function(operator:JSON_EXTRACT_SCALAR, operands:[Expression(type:IDENTIFIER, identifier:Identifier(name:jsonColumn)), Expression(type:LITERAL, literal:<Literal stringValue:$.data[0][1].a.b[0]>), Expression(type:LITERAL, literal:<Literal stringValue:STRING>), Expression(type:LITERAL, literal:<Literal stringValue:null>)]))]))");
+  }
+
+  /** Test that a predicate comparing a json path expression with literal is properly converted into a JSON_MATCH function. */
+  @Test
+  public void testJsonFilter() {
+    // String literal
+    BrokerRequest sqlBrokerRequest1 =
+        SQL_COMPILER.compileToBrokerRequest("SELECT * FROM testTable WHERE jsonColumn.name.first = 'daffy'");
+    PinotQuery pinotQuery1 = sqlBrokerRequest1.getPinotQuery();
+    OPTIMIZER.optimize(pinotQuery1, SCHEMA);
+
+    Assert.assertEquals(pinotQuery1.getFilterExpression().toString(),
+        "Expression(type:FUNCTION, functionCall:Function(operator:JSON_MATCH, operands:[Expression(type:IDENTIFIER, identifier:Identifier(name:jsonColumn)), Expression(type:LITERAL, literal:<Literal stringValue:\"$.name.first\" = 'daffy'>)]))");
+
+    // Numerical literal
+    BrokerRequest sqlBrokerRequest2 =
+        SQL_COMPILER.compileToBrokerRequest("SELECT * FROM testTable WHERE jsonColumn.id = 101");
+    PinotQuery pinotQuery2 = sqlBrokerRequest2.getPinotQuery();
+    OPTIMIZER.optimize(pinotQuery2, SCHEMA);
+
+    Assert.assertEquals(pinotQuery2.getFilterExpression().toString(),
+        "Expression(type:FUNCTION, functionCall:Function(operator:JSON_MATCH, operands:[Expression(type:IDENTIFIER, identifier:Identifier(name:jsonColumn)), Expression(type:LITERAL, literal:<Literal stringValue:\"$.id\" = 101>)]))");
+  }
+
+  /** Test that a json path expression in GROUP BY clause is properly converted into a JSON_EXTRACT_SCALAR function. */
+  @Test
+  public void testJsonGroupBy() {
+    BrokerRequest sqlBrokerRequest =
+        SQL_COMPILER.compileToBrokerRequest("SELECT jsonColumn.id, count(*) FROM testTable GROUP BY jsonColumn.id");
+    PinotQuery pinotQuery = sqlBrokerRequest.getPinotQuery();
+    OPTIMIZER.optimize(pinotQuery, SCHEMA);
+

Review comment:
       Better yet, compile the hard-coded translated query and assert that they generate the same expression list? 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org