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 2022/06/23 23:51:53 UTC

[GitHub] [pinot] jackjlli commented on a diff in pull request #8965: Support JSON column type in recommender.

jackjlli commented on code in PR #8965:
URL: https://github.com/apache/pinot/pull/8965#discussion_r905595876


##########
pinot-controller/src/main/java/org/apache/pinot/controller/recommender/data/generator/JsonGenerator.java:
##########
@@ -0,0 +1,61 @@
+/**
+ * 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.controller.recommender.data.generator;
+
+import java.util.Random;
+
+
+public class JsonGenerator implements Generator {
+  // Length of each key-value pair int the JSON string.
+  static final int DEFAULT_JSON_ELEMENT_LENGTH = 5;
+
+  private int _jsonStringLength = 0;
+  private Random _random;

Review Comment:
   Same here.



##########
pinot-controller/src/main/java/org/apache/pinot/controller/recommender/data/generator/JsonGenerator.java:
##########
@@ -0,0 +1,61 @@
+/**
+ * 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.controller.recommender.data.generator;
+
+import java.util.Random;
+
+
+public class JsonGenerator implements Generator {
+  // Length of each key-value pair int the JSON string.
+  static final int DEFAULT_JSON_ELEMENT_LENGTH = 5;
+
+  private int _jsonStringLength = 0;

Review Comment:
   make it final?



##########
pinot-controller/src/test/java/org/apache/pinot/controller/recommender/data/generator/JsonGeneratorTest.java:
##########
@@ -0,0 +1,59 @@
+/**
+ * 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.controller.recommender.data.generator;
+
+import java.io.IOException;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class JsonGeneratorTest {
+  @Test
+  public void testNext()
+      throws IOException {
+    // JsonGenerator generates empty json when size is  less than JsonGenerator.DEFAULT_JSON_ELEMENT_LENGTH)
+    JsonGenerator jsonGenerator1 = new JsonGenerator(0);
+    Assert.assertEquals(jsonGenerator1.next(), "{}");
+
+    JsonGenerator jsonGenerator2 = new JsonGenerator(4);
+    Assert.assertEquals(jsonGenerator2.next(), "{}");
+
+    JsonGenerator jsonGenerator3 = new JsonGenerator(8);
+    checkJson((String) jsonGenerator3.next(), 8);
+
+    JsonGenerator jsonGenerator4 = new JsonGenerator(20);
+    checkJson((String) jsonGenerator4.next(), 20);
+  }
+
+  public static void checkJson(String jsonString, int desiredLength)
+      throws IOException {
+
+    // Number of expected elements in the generated JSON string
+    int elementCount = desiredLength / JsonGenerator.DEFAULT_JSON_ELEMENT_LENGTH;
+
+    // Make sure we are generating JSON string that is close to the desired length. Length of JSON string should be 2
+    // (length of opening and closing parentheses) + number of commas + size of all the elements.
+    Assert.assertEquals(jsonString.length(),
+        "{}".length() + (elementCount - 1) + elementCount * JsonGenerator.DEFAULT_JSON_ELEMENT_LENGTH);
+
+    // Check if json string is valid json.
+    JsonUtils.stringToJsonNode(jsonString);

Review Comment:
   Wrap with try catch block, and make it fail if exception is thrown.



##########
pinot-controller/src/test/java/org/apache/pinot/controller/recommender/data/generator/JsonGeneratorTest.java:
##########
@@ -0,0 +1,59 @@
+/**
+ * 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.controller.recommender.data.generator;
+
+import java.io.IOException;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class JsonGeneratorTest {
+  @Test

Review Comment:
   Put an empty line before `@Test`.



##########
pinot-controller/src/test/java/org/apache/pinot/controller/recommender/data/generator/JsonGeneratorTest.java:
##########
@@ -0,0 +1,59 @@
+/**
+ * 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.controller.recommender.data.generator;
+
+import java.io.IOException;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class JsonGeneratorTest {
+  @Test
+  public void testNext()
+      throws IOException {
+    // JsonGenerator generates empty json when size is  less than JsonGenerator.DEFAULT_JSON_ELEMENT_LENGTH)

Review Comment:
   Extra space in the sentence.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org

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