Github user chenghao-intel commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1612#discussion_r15509316
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcResultSetRDD.scala ---
    @@ -0,0 +1,70 @@
    +/*
    + * 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.spark.sql.jdbc
    +
    +import java.sql.ResultSet
    +
    +import org.apache.spark.rdd.JdbcRDD
    +import org.apache.spark.sql.catalyst.expressions._
    +import org.apache.spark.sql.catalyst.plans.logical._
    +import org.apache.spark.sql.catalyst.types._
    +import org.apache.spark.sql.execution.{ExistingRdd, SparkLogicalPlan}
    +import org.apache.spark.sql.Logging
    +
    +private[sql] object JdbcResultSetRDD extends Logging {
    +
    +  private[sql] def inferSchema(
    +      jdbcResultSet: JdbcRDD[ResultSet]): LogicalPlan = {
    +    val schema = createSchema(jdbcResultSet.getSchema)
    +
    +    SparkLogicalPlan(ExistingRdd(asAttributes(schema), 
jdbcResultSet.map(asRow(_, schema))))
    +  }
    +
    +  private def createSchema(metaSchema: Seq[(String, Int, Boolean)]): 
Seq[StructField] = {
    +    metaSchema.map(e => StructField(e._1, 
JdbcTypes.toPrimitiveDataType(e._2), e._3))
    +  }
    +
    +  private def asRow(rs: ResultSet, schema: Seq[StructField]): Row = {
    +    val row = new GenericMutableRow(schema.length)
    +    schema.zipWithIndex.foreach {
    +      case (StructField(name, dataType, nullable), i) => {
    +        dataType match {
    +          case StringType  => row.update(i, rs.getString(i+1))
    --- End diff --
    
    This probably contains 2 bugs:
    * Not consistent with `JdbcTypes.toPrimitiveDataType`
    
    As `java.sql.Types.DATE`, `java.sql.Types.TIME` and `java.sql.Types.CLOB` 
all of them will cast into `StringType` in  `JdbcTypes.toPrimitiveDataType`, 
`rs.getString` for `java.sql.Types.TIME` here may cause exception.
    * Null value checking
    
    According to the document of ResultSet, the primitive type getter methods 
will return a default value if null value found. For example the API `int 
getInt(int columnIndex)` will returns `0` if the actual value is `null`.
    We can write the code like:
    ```
    val iVal = rs.getInt("ID_PARENT")
    if (rs.wasNull()) {
      row.update(i, null)
    } else {
      row.update(i, iVal)
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to