suneet-s commented on a change in pull request #9449:
URL: https://github.com/apache/druid/pull/9449#discussion_r434787284



##########
File path: 
server/src/main/java/org/apache/druid/metadata/input/SqlInputSource.java
##########
@@ -0,0 +1,151 @@
+/*
+ * 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.druid.metadata.input;
+
+import com.fasterxml.jackson.annotation.JacksonInject;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Preconditions;
+import org.apache.druid.data.input.AbstractInputSource;
+import org.apache.druid.data.input.InputFormat;
+import org.apache.druid.data.input.InputRowSchema;
+import org.apache.druid.data.input.InputSourceReader;
+import org.apache.druid.data.input.InputSplit;
+import org.apache.druid.data.input.SplitHintSpec;
+import org.apache.druid.data.input.impl.InputEntityIteratingReader;
+import org.apache.druid.data.input.impl.SplittableInputSource;
+import org.apache.druid.guice.annotations.Smile;
+import org.apache.druid.metadata.SQLFirehoseDatabaseConnector;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
+
+public class SqlInputSource extends AbstractInputSource implements 
SplittableInputSource<String>
+{
+  private final List<String> sqls;
+  private final SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector;
+  private final ObjectMapper objectMapper;
+  private final boolean foldCase;
+
+  @JsonCreator
+  public SqlInputSource(
+      @JsonProperty("sqls") List<String> sqls,
+      @JsonProperty("foldCase") boolean foldCase,
+      @JsonProperty("database") SQLFirehoseDatabaseConnector 
sqlFirehoseDatabaseConnector,
+      @JacksonInject @Smile ObjectMapper objectMapper
+  )
+  {
+    Preconditions.checkArgument(sqls.size() > 0, "No SQL queries provided");
+
+    this.sqls = sqls;
+    this.foldCase = foldCase;
+    this.sqlFirehoseDatabaseConnector = Preconditions.checkNotNull(
+        sqlFirehoseDatabaseConnector,
+        "SQL Metadata Connector not configured!"
+    );
+    this.objectMapper = objectMapper;
+  }
+
+  @JsonProperty
+  public List<String> getSqls()
+  {
+    return sqls;
+  }
+
+  @JsonProperty
+  public boolean isFoldCase()
+  {
+    return foldCase;
+  }
+
+  @JsonProperty("database")
+  public SQLFirehoseDatabaseConnector getSQLFirehoseDatabaseConnector()
+  {
+    return sqlFirehoseDatabaseConnector;
+  }
+
+  @Override
+  public Stream<InputSplit<String>> createSplits(InputFormat inputFormat, 
@Nullable SplitHintSpec splitHintSpec)
+  {
+    return sqls.stream().map(InputSplit::new);

Review comment:
       Does this need to check either inputFormat or splitHintSpec to create 
the Stream of InputSplits? SImilar comment for function below. Not sure if I 
understand how these should be used.

##########
File path: 
server/src/main/java/org/apache/druid/metadata/input/InputSourceModule.java
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.druid.metadata.input;
+
+import com.fasterxml.jackson.databind.Module;
+import com.fasterxml.jackson.databind.jsontype.NamedType;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.google.common.collect.ImmutableList;
+import com.google.inject.Binder;
+import org.apache.druid.initialization.DruidModule;
+
+import java.util.List;
+
+public class InputSourceModule implements DruidModule
+{
+  @Override
+  public List<? extends Module> getJacksonModules()

Review comment:
       I was thinking of a test where we verify that the "sql" named type is 
registered to the SqlInputSource class correctly. Here's an example of a module 
test I've added for a custom extension 
https://github.com/implydata/indexed-table-loader/blob/master/src/test/java/io/imply/druid/indextable/loader/IndexedTableLoaderDruidModuleTest.java

##########
File path: server/src/main/java/org/apache/druid/metadata/input/SqlEntity.java
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.druid.metadata.input;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Preconditions;
+import org.apache.druid.data.input.InputEntity;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.metadata.SQLFirehoseDatabaseConnector;
+import org.skife.jdbi.v2.ResultIterator;
+import org.skife.jdbi.v2.exceptions.CallbackFailedException;
+import org.skife.jdbi.v2.exceptions.ResultSetException;
+import org.skife.jdbi.v2.exceptions.StatementException;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Represents a rdbms based input resource and knows how to read query results 
from the resource using SQL queries.
+ */
+public class SqlEntity implements InputEntity
+{
+  private static final Logger LOG = new Logger(SqlEntity.class);
+
+  private final String sql;
+  private final ObjectMapper objectMapper;
+  private final SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector;
+  private final boolean foldCase;
+
+  public SqlEntity(
+      String sql,
+      SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector,
+      boolean foldCase,
+      ObjectMapper objectMapper
+  )
+  {
+    this.sql = sql;
+    this.sqlFirehoseDatabaseConnector = Preconditions.checkNotNull(
+        sqlFirehoseDatabaseConnector,
+        "SQL Metadata Connector not configured!"
+    );
+    this.foldCase = foldCase;
+    this.objectMapper = objectMapper;
+  }
+
+  public String getSql()
+  {
+    return sql;
+  }
+
+  @Nullable
+  @Override
+  public URI getUri()
+  {
+    return null;
+  }
+
+  @Override
+  public InputStream open()
+  {
+    throw new UnsupportedOperationException("Please use fetch() instead");
+  }
+
+  @Override
+  public CleanableFile fetch(File temporaryDirectory, byte[] fetchBuffer) 
throws IOException
+  {
+    final File tempFile = File.createTempFile("druid-sql-entity", ".tmp", 
temporaryDirectory);
+    return openCleanableFile(sql, sqlFirehoseDatabaseConnector, objectMapper, 
foldCase, tempFile);
+
+  }
+
+  /**
+   * Executes a SQL query on the specified database and fetches the result 
into the given file.
+   * The result file is deleted if the query execution or the file write fails.
+   *
+   * @param sql                          The SQL query to be executed
+   * @param sqlFirehoseDatabaseConnector The database connector
+   * @param objectMapper                 An object mapper, used for 
deserialization
+   * @param foldCase                     A boolean flag used to enable or 
disabling case sensitivity while handling database column names
+   *
+   * @return A {@link InputEntity.CleanableFile} object that wraps the file 
containing the SQL results
+   */
+
+  public static CleanableFile openCleanableFile(
+      String sql,
+      SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector,
+      ObjectMapper objectMapper,
+      boolean foldCase,
+      File tempFile
+  )
+      throws IOException
+  {
+    try (FileOutputStream fos = new FileOutputStream(tempFile);
+         final JsonGenerator jg = 
objectMapper.getFactory().createGenerator(fos);) {
+
+      // Execute the sql query and lazily retrieve the results into the file 
in json format.
+      // foldCase is useful to handle differences in case sensitivity behavior 
across databases.
+      sqlFirehoseDatabaseConnector.retryWithHandle(
+          (handle) -> {
+            ResultIterator<Map<String, Object>> resultIterator = 
handle.createQuery(
+                sql
+            ).map(
+                (index, r, ctx) -> {
+                  Map<String, Object> resultRow = foldCase ? new 
CaseFoldedMap() : new HashMap<>();
+                  ResultSetMetaData resultMetadata;
+                  try {
+                    resultMetadata = r.getMetaData();
+                  }
+                  catch (SQLException e) {
+                    throw new ResultSetException("Unable to obtain metadata 
from result set", e, ctx);
+                  }
+                  try {
+                    for (int i = 1; i <= resultMetadata.getColumnCount(); i++) 
{
+                      String key = resultMetadata.getColumnName(i);
+                      String alias = resultMetadata.getColumnLabel(i);
+                      Object value = r.getObject(i);
+                      resultRow.put(alias != null ? alias : key, value);
+                    }
+                  }
+                  catch (SQLException e) {
+                    throw new ResultSetException("Unable to access specific 
metadata from " +
+                                                 "result set metadata", e, 
ctx);
+                  }
+                  return resultRow;
+                }
+            ).iterator();
+            jg.writeStartArray();
+            while (resultIterator.hasNext()) {
+              jg.writeObject(resultIterator.next());

Review comment:
       Sorry I missed this earlier: Why did we chose json serialization? What 
happens if the object returned from the result set can not be serialized by the 
json generator?

##########
File path: docs/ingestion/native-batch.md
##########
@@ -1310,6 +1311,56 @@ A spec that applies a filter and reads a subset of the 
original datasource's col
 This spec above will only return the `page`, `user` dimensions and `added` 
metric.
 Only rows where `page` = `Druid` will be returned.
 
+### SQL Input Source
+
+The SQL input source is used to read data directly from RDBMS.
+The SQL input source is _splittable_ and can be used by the [Parallel 
task](#parallel-task), where each worker task will read from one SQL query from 
the list of queries.
+Since this input source has a fixed input format for reading events, no 
`inputFormat` field needs to be specified in the ingestion spec when using this 
input source.
+
+|property|description|required?|
+|--------|-----------|---------|
+|type|This should be "sql".|Yes|
+|database|Specifies the database connection details. The database type 
corresponds to the extension that supplies the `connectorConfig` support and 
this extension must be loaded into Druid. For database types `mysql` and 
`postgresql`, the `connectorConfig` support is provided by 
[mysql-metadata-storage](../development/extensions-core/mysql.md) and 
[postgresql-metadata-storage](../development/extensions-core/postgresql.md) 
extensions respectively.|Yes|
+|foldCase|Toggle case folding of database column names. This may be enabled in 
cases where the database returns case insensitive column names in query 
results.|No|
+|sqls|List of SQL queries where each SQL query would retrieve the data to be 
indexed.|Yes|
+
+An example SqlInputSource spec is shown below:
+
+```json
+...
+    "ioConfig": {
+      "type": "index_parallel",
+      "inputSource": {
+        "type": "sql",
+        "database": {
+            "type": "mysql",
+            "connectorConfig": {
+                "connectURI": "jdbc:mysql://host:port/schema",
+                "user": "user",
+                "password": "password"
+            }
+        },
+        "sqls": ["SELECT * FROM table1 WHERE timestamp BETWEEN '2013-01-01 
00:00:00' AND '2013-01-01 11:59:59'", "SELECT * FROM table2 WHERE timestamp 
BETWEEN '2013-01-01 00:00:00' AND '2013-01-01 11:59:59'"]
+    },
+...
+```
+
+The spec above will read all events from two separate SQLs for the interval 
`2013-01-01/2013-01-02`.
+Each of the SQL queries will be run in its own sub-task and thus for the above 
example, there would be two sub-tasks.
+
+Compared to the other native batch InputSources, SQL InputSource behaves 
differently in terms of reading the input data and so it would be helpful to 
consider the following points before using this InputSource in a production 
environment:

Review comment:
       Can we mark this as a big warning in the docs? I think a similar warning 
should be made on line 1316 indicating that this functionality is experimental 
and not yet recommended for production use.

##########
File path: server/src/main/java/org/apache/druid/metadata/input/SqlEntity.java
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.druid.metadata.input;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Preconditions;
+import org.apache.druid.data.input.InputEntity;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.metadata.SQLFirehoseDatabaseConnector;
+import org.skife.jdbi.v2.ResultIterator;
+import org.skife.jdbi.v2.exceptions.CallbackFailedException;
+import org.skife.jdbi.v2.exceptions.ResultSetException;
+import org.skife.jdbi.v2.exceptions.StatementException;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Represents a rdbms based input resource and knows how to read query results 
from the resource using SQL queries.
+ */
+public class SqlEntity implements InputEntity
+{
+  private static final Logger LOG = new Logger(SqlEntity.class);
+
+  private final String sql;
+  private final ObjectMapper objectMapper;
+  private final SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector;
+  private final boolean foldCase;
+
+  public SqlEntity(
+      String sql,
+      SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector,
+      boolean foldCase,
+      ObjectMapper objectMapper
+  )
+  {
+    this.sql = sql;
+    this.sqlFirehoseDatabaseConnector = Preconditions.checkNotNull(
+        sqlFirehoseDatabaseConnector,
+        "SQL Metadata Connector not configured!"
+    );
+    this.foldCase = foldCase;
+    this.objectMapper = objectMapper;
+  }
+
+  public String getSql()
+  {
+    return sql;
+  }
+
+  @Nullable
+  @Override
+  public URI getUri()
+  {
+    return null;
+  }
+
+  @Override
+  public InputStream open()
+  {
+    throw new UnsupportedOperationException("Please use fetch() instead");
+  }
+
+  @Override
+  public CleanableFile fetch(File temporaryDirectory, byte[] fetchBuffer) 
throws IOException
+  {
+    final File tempFile = File.createTempFile("druid-sql-entity", ".tmp", 
temporaryDirectory);
+    return openCleanableFile(sql, sqlFirehoseDatabaseConnector, objectMapper, 
foldCase, tempFile);
+
+  }
+
+  /**
+   * Executes a SQL query on the specified database and fetches the result 
into the given file.
+   * The result file is deleted if the query execution or the file write fails.
+   *
+   * @param sql                          The SQL query to be executed
+   * @param sqlFirehoseDatabaseConnector The database connector
+   * @param objectMapper                 An object mapper, used for 
deserialization
+   * @param foldCase                     A boolean flag used to enable or 
disabling case sensitivity while handling database column names
+   *
+   * @return A {@link InputEntity.CleanableFile} object that wraps the file 
containing the SQL results
+   */
+
+  public static CleanableFile openCleanableFile(
+      String sql,
+      SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector,
+      ObjectMapper objectMapper,
+      boolean foldCase,
+      File tempFile
+  )
+      throws IOException
+  {
+    try (FileOutputStream fos = new FileOutputStream(tempFile);
+         final JsonGenerator jg = 
objectMapper.getFactory().createGenerator(fos);) {
+
+      // Execute the sql query and lazily retrieve the results into the file 
in json format.
+      // foldCase is useful to handle differences in case sensitivity behavior 
across databases.
+      sqlFirehoseDatabaseConnector.retryWithHandle(
+          (handle) -> {
+            ResultIterator<Map<String, Object>> resultIterator = 
handle.createQuery(
+                sql
+            ).map(
+                (index, r, ctx) -> {
+                  Map<String, Object> resultRow = foldCase ? new 
CaseFoldedMap() : new HashMap<>();
+                  ResultSetMetaData resultMetadata;
+                  try {
+                    resultMetadata = r.getMetaData();
+                  }
+                  catch (SQLException e) {
+                    throw new ResultSetException("Unable to obtain metadata 
from result set", e, ctx);
+                  }
+                  try {
+                    for (int i = 1; i <= resultMetadata.getColumnCount(); i++) 
{
+                      String key = resultMetadata.getColumnName(i);
+                      String alias = resultMetadata.getColumnLabel(i);
+                      Object value = r.getObject(i);
+                      resultRow.put(alias != null ? alias : key, value);
+                    }
+                  }
+                  catch (SQLException e) {
+                    throw new ResultSetException("Unable to access specific 
metadata from " +
+                                                 "result set metadata", e, 
ctx);
+                  }
+                  return resultRow;
+                }
+            ).iterator();
+            jg.writeStartArray();
+            while (resultIterator.hasNext()) {
+              jg.writeObject(resultIterator.next());
+            }
+            jg.writeEndArray();
+            jg.close();
+            return null;
+          },
+          (exception) -> {
+            final boolean isStatementException = exception instanceof 
StatementException ||

Review comment:
       It looks like this logic is duplicated from 
`SQLMetadataStorageActionHandler#isStatementException`. Can you consolidate 
them please

##########
File path: server/src/main/java/org/apache/druid/metadata/input/SqlEntity.java
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.druid.metadata.input;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Preconditions;
+import org.apache.druid.data.input.InputEntity;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.metadata.SQLFirehoseDatabaseConnector;
+import org.skife.jdbi.v2.ResultIterator;
+import org.skife.jdbi.v2.exceptions.CallbackFailedException;
+import org.skife.jdbi.v2.exceptions.ResultSetException;
+import org.skife.jdbi.v2.exceptions.StatementException;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Represents a rdbms based input resource and knows how to read query results 
from the resource using SQL queries.
+ */
+public class SqlEntity implements InputEntity
+{
+  private static final Logger LOG = new Logger(SqlEntity.class);
+
+  private final String sql;
+  private final ObjectMapper objectMapper;
+  private final SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector;
+  private final boolean foldCase;
+
+  public SqlEntity(
+      String sql,
+      SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector,
+      boolean foldCase,
+      ObjectMapper objectMapper
+  )
+  {
+    this.sql = sql;
+    this.sqlFirehoseDatabaseConnector = Preconditions.checkNotNull(
+        sqlFirehoseDatabaseConnector,
+        "SQL Metadata Connector not configured!"
+    );
+    this.foldCase = foldCase;
+    this.objectMapper = objectMapper;
+  }
+
+  public String getSql()
+  {
+    return sql;
+  }
+
+  @Nullable
+  @Override
+  public URI getUri()
+  {
+    return null;
+  }
+
+  @Override
+  public InputStream open()
+  {
+    throw new UnsupportedOperationException("Please use fetch() instead");
+  }
+
+  @Override
+  public CleanableFile fetch(File temporaryDirectory, byte[] fetchBuffer) 
throws IOException
+  {
+    final File tempFile = File.createTempFile("druid-sql-entity", ".tmp", 
temporaryDirectory);
+    return openCleanableFile(sql, sqlFirehoseDatabaseConnector, objectMapper, 
foldCase, tempFile);
+
+  }
+
+  /**
+   * Executes a SQL query on the specified database and fetches the result 
into the given file.
+   * The result file is deleted if the query execution or the file write fails.
+   *
+   * @param sql                          The SQL query to be executed
+   * @param sqlFirehoseDatabaseConnector The database connector
+   * @param objectMapper                 An object mapper, used for 
deserialization
+   * @param foldCase                     A boolean flag used to enable or 
disabling case sensitivity while handling database column names
+   *
+   * @return A {@link InputEntity.CleanableFile} object that wraps the file 
containing the SQL results
+   */
+
+  public static CleanableFile openCleanableFile(
+      String sql,
+      SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector,
+      ObjectMapper objectMapper,
+      boolean foldCase,
+      File tempFile
+  )
+      throws IOException
+  {
+    try (FileOutputStream fos = new FileOutputStream(tempFile);
+         final JsonGenerator jg = 
objectMapper.getFactory().createGenerator(fos);) {
+
+      // Execute the sql query and lazily retrieve the results into the file 
in json format.
+      // foldCase is useful to handle differences in case sensitivity behavior 
across databases.
+      sqlFirehoseDatabaseConnector.retryWithHandle(
+          (handle) -> {
+            ResultIterator<Map<String, Object>> resultIterator = 
handle.createQuery(
+                sql
+            ).map(
+                (index, r, ctx) -> {
+                  Map<String, Object> resultRow = foldCase ? new 
CaseFoldedMap() : new HashMap<>();
+                  ResultSetMetaData resultMetadata;
+                  try {
+                    resultMetadata = r.getMetaData();
+                  }
+                  catch (SQLException e) {
+                    throw new ResultSetException("Unable to obtain metadata 
from result set", e, ctx);
+                  }
+                  try {
+                    for (int i = 1; i <= resultMetadata.getColumnCount(); i++) 
{
+                      String key = resultMetadata.getColumnName(i);
+                      String alias = resultMetadata.getColumnLabel(i);
+                      Object value = r.getObject(i);
+                      resultRow.put(alias != null ? alias : key, value);
+                    }
+                  }
+                  catch (SQLException e) {
+                    throw new ResultSetException("Unable to access specific 
metadata from " +
+                                                 "result set metadata", e, 
ctx);
+                  }
+                  return resultRow;
+                }
+            ).iterator();
+            jg.writeStartArray();
+            while (resultIterator.hasNext()) {
+              jg.writeObject(resultIterator.next());
+            }
+            jg.writeEndArray();
+            jg.close();
+            return null;
+          },
+          (exception) -> {
+            final boolean isStatementException = exception instanceof 
StatementException ||
+                                                 (exception instanceof 
CallbackFailedException
+                                                  && exception.getCause() 
instanceof StatementException);
+            return 
sqlFirehoseDatabaseConnector.isTransientException(exception) && 
!(isStatementException);
+          }
+      );
+      return new CleanableFile()
+      {
+        @Override
+        public File file()
+        {
+          return tempFile;
+        }
+
+        @Override
+        public void close()
+        {
+          if (!tempFile.delete()) {
+            LOG.warn("Failed to remove file[%s]", tempFile.getAbsolutePath());
+          }
+        }
+      };
+    }
+    catch (Exception e) {
+      if (!tempFile.delete()) {
+        LOG.warn("Failed to remove file[%s]", tempFile.getAbsolutePath());
+      }
+      throw new IOException(e);
+    }
+  }
+
+  private static class CaseFoldedMap extends HashMap<String, Object>
+  {
+    public static final long serialVersionUID = 1L;
+
+    @Override
+    public Object get(Object obj)
+    {
+      return super.get(StringUtils.toLowerCase((String) obj));

Review comment:
       This breaks the contract of HashMap which allows the key to be null. 
StringUtils.toLowerCase(null) will throw an NPE

##########
File path: 
server/src/main/java/org/apache/druid/metadata/input/SqlInputSource.java
##########
@@ -0,0 +1,151 @@
+/*
+ * 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.druid.metadata.input;
+
+import com.fasterxml.jackson.annotation.JacksonInject;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Preconditions;
+import org.apache.druid.data.input.AbstractInputSource;
+import org.apache.druid.data.input.InputFormat;
+import org.apache.druid.data.input.InputRowSchema;
+import org.apache.druid.data.input.InputSourceReader;
+import org.apache.druid.data.input.InputSplit;
+import org.apache.druid.data.input.SplitHintSpec;
+import org.apache.druid.data.input.impl.InputEntityIteratingReader;
+import org.apache.druid.data.input.impl.SplittableInputSource;
+import org.apache.druid.guice.annotations.Smile;
+import org.apache.druid.metadata.SQLFirehoseDatabaseConnector;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
+
+public class SqlInputSource extends AbstractInputSource implements 
SplittableInputSource<String>
+{
+  private final List<String> sqls;
+  private final SQLFirehoseDatabaseConnector sqlFirehoseDatabaseConnector;
+  private final ObjectMapper objectMapper;
+  private final boolean foldCase;
+
+  @JsonCreator
+  public SqlInputSource(
+      @JsonProperty("sqls") List<String> sqls,
+      @JsonProperty("foldCase") boolean foldCase,
+      @JsonProperty("database") SQLFirehoseDatabaseConnector 
sqlFirehoseDatabaseConnector,
+      @JacksonInject @Smile ObjectMapper objectMapper

Review comment:
       Why do we need a smile object mapper instead of just the default object 
mapper




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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to