You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/01/06 02:37:55 UTC

[GitHub] [iotdb] JackieTien97 commented on a change in pull request #2417: [IOTDB-1073] Built-in UDTFs

JackieTien97 commented on a change in pull request #2417:
URL: https://github.com/apache/iotdb/pull/2417#discussion_r552327267



##########
File path: server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFMatches.java
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.iotdb.db.query.udf.builtin;
+
+import org.apache.iotdb.db.query.udf.api.UDTF;
+import org.apache.iotdb.db.query.udf.api.access.Row;
+import org.apache.iotdb.db.query.udf.api.collector.PointCollector;
+import org.apache.iotdb.db.query.udf.api.customizer.config.UDTFConfigurations;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameterValidator;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameters;
+import org.apache.iotdb.db.query.udf.api.customizer.strategy.RowByRowAccessStrategy;
+import org.apache.iotdb.db.query.udf.api.exception.UDFException;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+public class UDTFMatches implements UDTF {
+
+  private String regex;

Review comment:
       It's better to compile the regex in the contructor function to get a Pattern object, like::
   ```
   Pattern pattern = compile(regex);
   ```
   And then reuse the Pattern object to avoid construct a new one each time in String.match() function, like:
   ```
   Matcher match = pattern.matcher(row.getString(0));
   return match.matches();
   ``` 

##########
File path: server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFContains.java
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.iotdb.db.query.udf.builtin;
+
+import org.apache.iotdb.db.query.udf.api.UDTF;
+import org.apache.iotdb.db.query.udf.api.access.Row;
+import org.apache.iotdb.db.query.udf.api.collector.PointCollector;
+import org.apache.iotdb.db.query.udf.api.customizer.config.UDTFConfigurations;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameterValidator;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameters;
+import org.apache.iotdb.db.query.udf.api.customizer.strategy.RowByRowAccessStrategy;
+import org.apache.iotdb.db.query.udf.api.exception.UDFException;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+public class UDTFContains implements UDTF {

Review comment:
       Actually, I think this function can be optimized. Because the pattern string is fixed(we can it pattern), say ing its length is m, and the query string is different, saying its average length is n.
   If you use the String.contains() function, time complexity will be O(n * m), you can consider to use KMP algorithm whose time complexity is O(n).

##########
File path: server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFContains.java
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.iotdb.db.query.udf.builtin;
+
+import org.apache.iotdb.db.query.udf.api.UDTF;
+import org.apache.iotdb.db.query.udf.api.access.Row;
+import org.apache.iotdb.db.query.udf.api.collector.PointCollector;
+import org.apache.iotdb.db.query.udf.api.customizer.config.UDTFConfigurations;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameterValidator;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameters;
+import org.apache.iotdb.db.query.udf.api.customizer.strategy.RowByRowAccessStrategy;
+import org.apache.iotdb.db.query.udf.api.exception.UDFException;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+public class UDTFContains implements UDTF {

Review comment:
       You can refer the KMP algorithm I've written before, as following:
   
   ```java
   public class KMP {
   
       private int[] next;
       private String pattern;
   
       public KMP(String pattern) {
           this.pattern = pattern;
           next = new int[pattern.length()];
           Arrays.fill(next, -1);
           for (int i = 1; i < pattern.length(); i++) {
               int j = next[i - 1];
               while (j != -1 && pattern.charAt(j + 1) != pattern.charAt(i)) {
                   j = next[j];
               }
               if (pattern.charAt(j + 1) == pattern.charAt(i)) {
                   next[i] = j + 1;
               }
           }
       }
   
       public boolean contains(String query) {
           int match = -1;
           for (int i = 0; i < query.length(); i++) {
               while (match != -1 && pattern.charAt(match + 1) != query.charAt(i)) {
                   match = next[match];
               }
               if (pattern.charAt(match + 1) == query.charAt(i)) {
                   match++;
                   if (match == next.length - 1) {
                       return true;
                   }
               }
           }
           return false;
       }
   }
   ```




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