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-11080: SqlCompiler crashes when a query is made of 100% comments #464

Merged
merged 3 commits into from
Jul 10, 2024
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 @@ -367,20 +367,23 @@ class NamedParametersPreparedStatement(
}

// The query output type is obtained using JDBC's `metadata`
private val queryOutputType: PostgresRowType = {
private val queryOutputType: Either[String, PostgresRowType] = {
val metadata = stmt.getMetaData // SQLException at that point would be a bug.
val columns = (1 to metadata.getColumnCount).map { i =>
val name = metadata.getColumnName(i)
val tipe = metadata.getColumnType(i)
val typeName = metadata.getColumnTypeName(i)
val nullability = metadata.isNullable(i)
val nullable = {
// report nullable if it's advertised as such, or unknown (when unknown it can be nullable).
nullability == ResultSetMetaData.columnNullable || nullability == ResultSetMetaData.columnNullableUnknown
if (metadata == null) Left("non-executable code")
else {
val columns = (1 to metadata.getColumnCount).map { i =>
val name = metadata.getColumnName(i)
val tipe = metadata.getColumnType(i)
val typeName = metadata.getColumnTypeName(i)
val nullability = metadata.isNullable(i)
val nullable = {
// report nullable if it's advertised as such, or unknown (when unknown it can be nullable).
nullability == ResultSetMetaData.columnNullable || nullability == ResultSetMetaData.columnNullableUnknown
}
PostgresColumn(name, PostgresType(tipe, nullable, typeName))
}
PostgresColumn(name, PostgresType(tipe, nullable, typeName))
Right(PostgresRowType(columns))
}
PostgresRowType(columns)
}

// helper for 'hover'
Expand All @@ -395,7 +398,12 @@ class NamedParametersPreparedStatement(
if (errors.nonEmpty) Left(errors.flatten)
else {
val typeInfo = declaredTypeInfo.mapValues(_.right.get).toMap
Right(QueryInfo(typeInfo, queryOutputType))
queryOutputType.left
.map(highlightError(parsedTree.tree))
.left
.map(List(_))
.right
.map(outputType => QueryInfo(typeInfo, outputType))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,4 +935,25 @@ class TestSqlCompilerServiceAirports
== """[{"trip_id":0,"departure_date":"2016-02-27","arrival_date":"2016-03-06"}]"""
)
}

test("""-- @param p
|-- @type p integer
|-- SELECT :p + 10;
|""".stripMargin) { t =>
val v = compilerService.validate(t.q, asJson())
assert(v.messages.size == 1)
assert(v.messages(0).message == "non-executable code")
}

test("""CREATE TABLE Persons (
| ID int NOT NULL,
| LastName varchar(255) NOT NULL,
| FirstName varchar(255),
| Age int,
| PRIMARY KEY (ID)
|);""".stripMargin) { t =>
val v = compilerService.validate(t.q, asJson())
assert(v.messages.size == 1)
assert(v.messages(0).message == "non-executable code")
}
}