Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RD-14762: SqlCompilerService crashes on default null date parameter #514

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -617,25 +617,41 @@ class NamedParametersPreparedStatement(

private def getDefaultValue(rs: ResultSet, postgresType: PostgresType): DefaultValue = {
assert(rs.next(), "rs.next() was false")
val attempt = postgresType.jdbcType match {
case java.sql.Types.SMALLINT => RawShort(rs.getShort(1))
case java.sql.Types.INTEGER => RawInt(rs.getInt(1))
case java.sql.Types.BIGINT => RawLong(rs.getLong(1))
case java.sql.Types.DECIMAL => RawDecimal(rs.getBigDecimal(1))
case java.sql.Types.FLOAT => RawFloat(rs.getFloat(1))
case java.sql.Types.DOUBLE => RawDouble(rs.getDouble(1))
case java.sql.Types.DATE => RawDate(rs.getDate(1).toLocalDate)
case java.sql.Types.TIME => RawTime(LocalTime.ofNanoOfDay(rs.getTime(1).getTime * 1000000))
case java.sql.Types.TIMESTAMP => RawTimestamp(rs.getTimestamp(1).toLocalDateTime)
case java.sql.Types.BOOLEAN => RawBool(rs.getBoolean(1))
case java.sql.Types.VARCHAR => RawString(rs.getString(1))
val value = postgresType.jdbcType match {
case java.sql.Types.SMALLINT =>
val sqlValue = rs.getShort(1)
if (rs.wasNull()) RawNull() else RawShort(sqlValue)
case java.sql.Types.INTEGER =>
val sqlValue = rs.getInt(1)
if (rs.wasNull()) RawNull() else RawInt(sqlValue)
case java.sql.Types.BIGINT =>
val sqlValue = rs.getLong(1)
if (rs.wasNull()) RawNull() else RawLong(sqlValue)
case java.sql.Types.DECIMAL =>
val sqlValue = rs.getBigDecimal(1)
if (rs.wasNull()) RawNull() else RawDecimal(sqlValue)
case java.sql.Types.FLOAT =>
val sqlValue = rs.getFloat(1)
if (rs.wasNull()) RawNull() else RawFloat(sqlValue)
case java.sql.Types.DOUBLE =>
val sqlValue = rs.getDouble(1)
if (rs.wasNull()) RawNull() else RawDouble(sqlValue)
case java.sql.Types.DATE =>
val sqlValue = rs.getDate(1)
if (rs.wasNull()) RawNull() else RawDate(sqlValue.toLocalDate)
case java.sql.Types.TIME =>
val sqlValue = rs.getTime(1)
if (rs.wasNull()) RawNull() else RawTime(LocalTime.ofNanoOfDay(sqlValue.getTime * 1000000))
case java.sql.Types.TIMESTAMP =>
val sqlValue = rs.getTimestamp(1)
if (rs.wasNull()) RawNull() else RawTimestamp(sqlValue.toLocalDateTime)
case java.sql.Types.BOOLEAN =>
val sqlValue = rs.getBoolean(1)
if (rs.wasNull()) RawNull() else RawBool(sqlValue)
case java.sql.Types.VARCHAR =>
val sqlValue = rs.getString(1)
if (rs.wasNull()) RawNull() else RawString(sqlValue)
}
val value =
if (rs.wasNull()) {
RawNull()
} else {
attempt
}
DefaultValue(value, postgresType)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.rawlabs.compiler.{
Pos,
ProgramEnvironment,
RawAttrType,
RawDate,
RawDateType,
RawDecimalType,
RawInt,
Expand All @@ -46,6 +47,7 @@ import com.rawlabs.utils.core._

import java.io.ByteArrayOutputStream
import java.sql.DriverManager
import java.time.LocalDate
import scala.io.Source

class TestSqlCompilerServiceAirports
Expand Down Expand Up @@ -1081,4 +1083,45 @@ class TestSqlCompilerServiceAirports
}
}

test("""-- @type x integer
|-- @default x null
|SELECT :x AS x""".stripMargin) { t =>
val baos = new ByteArrayOutputStream()
baos.reset()
assert(compilerService.execute(t.q, asJson(), None, baos) == ExecutionSuccess(true))
assert(baos.toString() === """[{"x":null}]""")
baos.reset()
assert(compilerService.execute(t.q, asJson(Map("x" -> RawInt(12))), None, baos) == ExecutionSuccess(true))
assert(baos.toString() === """[{"x":12}]""")
}

test("""-- @type x varchar
|-- @default x null
|SELECT :x AS x""".stripMargin) { t =>
val baos = new ByteArrayOutputStream()
assert(compilerService.execute(t.q, asJson(), None, baos) == ExecutionSuccess(true))
assert(baos.toString() === """[{"x":null}]""")
baos.reset()
assert(compilerService.execute(t.q, asJson(Map("x" -> RawString("tralala"))), None, baos) == ExecutionSuccess(true))
assert(baos.toString() === """[{"x":"tralala"}]""")
}

test("""-- @type x date
|-- @default x null
|SELECT :x AS x""".stripMargin) { t =>
val baos = new ByteArrayOutputStream()
assert(compilerService.execute(t.q, asJson(), None, baos) == ExecutionSuccess(true))
assert(baos.toString() === """[{"x":null}]""")
baos.reset()
assert(
compilerService.execute(
t.q,
asJson(Map("x" -> RawDate(LocalDate.of(2008, 9, 29)))),
None,
baos
) == ExecutionSuccess(true)
)
assert(baos.toString() === """[{"x":"2008-09-29"}]""")
}

}