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

ASF GitHub Bot commented on DRILL-8169:
---------------------------------------

jnturton commented on a change in pull request #2496:
URL: https://github.com/apache/drill/pull/2496#discussion_r828820399



##########
File path: 
contrib/storage-http/src/main/java/org/apache/drill/exec/store/http/udfs/HttpHelperFunctions.java
##########
@@ -0,0 +1,166 @@
+/*
+ * 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.drill.exec.store.http.udfs;
+
+import io.netty.buffer.DrillBuf;
+import org.apache.drill.exec.expr.DrillSimpleFunc;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate;
+import org.apache.drill.exec.expr.annotations.Output;
+import org.apache.drill.exec.expr.annotations.Param;
+import org.apache.drill.exec.expr.annotations.Workspace;
+import org.apache.drill.exec.server.DrillbitContext;
+import org.apache.drill.exec.server.options.OptionManager;
+import org.apache.drill.exec.vector.complex.reader.FieldReader;
+import org.apache.drill.exec.vector.complex.writer.BaseWriter.ComplexWriter;
+
+import javax.inject.Inject;
+
+public class HttpHelperFunctions {
+
+  @FunctionTemplate(names = {"http_get_url", "httpGetUrl"},
+    scope = FunctionTemplate.FunctionScope.SIMPLE,
+    isVarArg = true)
+  public static class HttpGetFunction implements DrillSimpleFunc {
+
+    @Param
+    FieldReader[] inputReaders;
+
+    @Output
+    ComplexWriter writer;
+
+    @Inject
+    OptionManager options;
+
+    @Inject
+    DrillBuf buffer;
+
+    @Workspace
+    org.apache.drill.exec.vector.complex.fn.JsonReader jsonReader;
+
+    @Override
+    public void setup() {
+      jsonReader = new 
org.apache.drill.exec.vector.complex.fn.JsonReader.Builder(buffer)
+        .defaultSchemaPathColumns()
+        
.readNumbersAsDouble(options.getOption(org.apache.drill.exec.ExecConstants.JSON_READ_NUMBERS_AS_DOUBLE).bool_val)
+        
.allTextMode(options.getOption(org.apache.drill.exec.ExecConstants.JSON_ALL_TEXT_MODE).bool_val)
+        
.enableNanInf(options.getOption(org.apache.drill.exec.ExecConstants.JSON_READER_NAN_INF_NUMBERS).bool_val)
+        .build();
+    }
+
+    @Override
+    public void eval() {
+      if (inputReaders.length > 0) {
+        // Get the URL
+        FieldReader urlReader = inputReaders[0];
+        String url = urlReader.readObject().toString();
+
+        // Process Positional Arguments
+        java.util.List args = 
org.apache.drill.exec.store.http.udfs.HttpHelperUtils.buildParameterList(inputReaders);
+        String finalUrl = 
org.apache.drill.exec.util.HttpUtils.mapPositionalParameters(url, args);
+
+        // Make the API call
+        String results = 
org.apache.drill.exec.util.HttpUtils.makeSimpleGetRequest(finalUrl);
+
+        // If the result string is null or empty, return an empty map
+        if (results == null || results.length() == 0) {
+          // Return empty map
+          org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter 
mapWriter = writer.rootAsMap();
+          mapWriter.start();
+          mapWriter.end();
+          return;
+        }
+
+        try {
+          jsonReader.setSource(results);
+          jsonReader.setIgnoreJSONParseErrors(true);  // Reduce number of 
errors
+          jsonReader.write(writer);
+          buffer = jsonReader.getWorkBuf();
+        } catch (Exception e) {
+          throw new 
org.apache.drill.common.exceptions.DrillRuntimeException("Error while 
converting from JSON. ", e);
+        }
+      }
+    }
+  }
+
+
+  @FunctionTemplate(names = {"http_get", "httpGet"},

Review comment:
       If this UDF is not only for for GET requests, but for whatever request 
type is configured in the named storage config then can we name it differently? 
 `http_req_storage` or something?

##########
File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/util/HttpUtils.java
##########
@@ -0,0 +1,168 @@
+/*
+ * 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.drill.exec.util;

Review comment:
       This class looks like it belongs in contrib/storage-http.  Is there a 
reason why it's here?




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


> Add UDFs to HTTP Plugin to Facilitate Joins
> -------------------------------------------
>
>                 Key: DRILL-8169
>                 URL: https://issues.apache.org/jira/browse/DRILL-8169
>             Project: Apache Drill
>          Issue Type: Improvement
>          Components: Storage - Other
>    Affects Versions: 1.20.0
>            Reporter: Charles Givre
>            Assignee: Charles Givre
>            Priority: Major
>             Fix For: 2.0.0
>
>
> There are some situations where a user might want to join data with an API 
> result and the pushdowns prevent that from happening. The main situation 
> where this happens is when 
> an API has parameters which are part of the URL AND these parameters are 
> dynamically populated via a join. 
> In this case, there are two functions `http_get_url` and `http_get` which you 
> can use to faciliate these joins. 
> * `http_get('<storage_plugin_name>', <params>)`: This function accepts a 
> storage plugin as input and an optional list of parameters to include in a 
> URL.
> * `http_get_url(<url>, <params>)`: This function works in the same way except 
> that it does not pull any configuration information from existing storage 
> plugins.
> ### Example Queries
> Let's say that you have a storage plugin called `github` with an endpoint 
> called `repos` which points to the url: https://github.com/orgs/\{org}/repos. 
> It is easy enough to 
> write a query like this:
> ```sql
> SELECT * 
> FROM github.repos
> WHERE org='apache'
> ```
> However, if you had a file with organizations and wanted to join this with 
> the API, the query would fail. Using the functions listed above you could get 
> this data as follows:
> ```sql
> SELECT http_get('github.repos', `org`)
> FROM dfs.`some_data.csvh`
> ```
> or
> ```sql
> SELECT http_get('https://github.com/orgs/\{org}/repos', `org`)
> FROM dfs.`some_data.csvh`
> ```
> ** WARNING: This functionality will execute an HTTP Request FOR EVERY ROW IN 
> YOUR DATA. Use with caution. **
>  



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to