Исключение
org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.
... возникает только в том случае, если в запрос ниже добавлена интерполяция строк, например:
[...]('SRID=4326;Point(${geoPoint.longitude} ${geoPoint.latitude})'), location)[...]
Если переменные удалены (значения жестко закодированы ), он работает просто отлично:
def fetchBy(geoPoint: GeoPoint, radius: Int): Query0[Merchant] = {
sql"""SELECT data->>'name' AS name, data->>'address' AS address, data->>'postCode' AS postCode, st_distance(ST_GeogFromText('SRID=4326;Point(-0.233226 51.5060804)'), location) AS distance FROM merchant WHERE ST_DWithin(location, ST_GeogFromText('SRID=4326;Point(-0.233226 51.5060804)'), 1000) ORDER BY distance;"""
.query[Merchant]
}
Моя настройка
Postgres ГИС-схема:
CREATE EXTENSION postgis;
CREATE TABLE merchant (
id varchar NOT NULL,
location geography,
data jsonb
);
Класс case для декодирования результата запроса до:
final case class Merchant(name: String, address: Option[Address] = None, postCode: String, distance: Option[String] = None)
object Merchant {
implicit val merchantEncoder: Encoder[Merchant] = deriveEncoder[Merchant]
implicit val merchantDecoder: Decoder[Merchant] = deriveDecoder[Merchant]
implicit val decoder: EntityDecoder[IO, Merchant] = jsonOf[IO, Merchant]
}
Stacktrace:
org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.
at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:64)
at org.postgresql.core.v3.SimpleParameterList.setBinaryParameter(SimpleParameterList.java:131)
at org.postgresql.jdbc.PgPreparedStatement.bindBytes(PgPreparedStatement.java:1005)
at org.postgresql.jdbc.PgPreparedStatement.setDouble(PgPreparedStatement.java:323)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setDouble(HikariProxyPreparedStatement.java)
at doobie.util.meta.MetaInstances.$anonfun$DoubleMeta$2(meta.scala:197)
at doobie.util.meta.MetaInstances.$anonfun$DoubleMeta$2$adapted(meta.scala:197)
at doobie.util.Put.unsafeSetNonNullable(put.scala:40)
at doobie.util.fragment$Fragment.$anonfun$write$5(fragment.scala:55)
at doobie.util.fragment$Fragment.$anonfun$write$5$adapted(fragment.scala:53)
at scala.collection.Iterator.foreach(Iterator.scala:941)
at scala.collection.Iterator.foreach$(Iterator.scala:941)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1429)
at doobie.util.fragment$Fragment.$anonfun$write$4(fragment.scala:53)
at doobie.util.fragment$Fragment.$anonfun$write$4$adapted(fragment.scala:51)
at doobie.util.Write.$anonfun$set$1(write.scala:26)
at doobie.util.Write.$anonfun$set$1$adapted(write.scala:26)
at doobie.free.KleisliInterpreter.$anonfun$primitive$2(kleisliinterpreter.scala:112)
at cats.effect.internals.IORunLoop$.cats$effect$internals$IORunLoop$$loop(IORunLoop.scala:87)
at cats.effect.internals.IORunLoop$.startCancelable(IORunLoop.scala:41)
at cats.effect.internals.IOBracket$BracketStart.run(IOBracket.scala:88)
at cats.effect.internals.Trampoline.cats$effect$internals$Trampoline$$immediateLoop(Trampoline.scala:67)
at cats.effect.internals.Trampoline.startLoop(Trampoline.scala:35)
at cats.effect.internals.TrampolineEC$JVMTrampoline.super$startLoop(TrampolineEC.scala:89)
at cats.effect.internals.TrampolineEC$JVMTrampoline.$anonfun$startLoop$1(TrampolineEC.scala:89)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:85)
at cats.effect.internals.TrampolineEC$JVMTrampoline.startLoop(TrampolineEC.scala:89)
at cats.effect.internals.Trampoline.execute(Trampoline.scala:43)
at cats.effect.internals.TrampolineEC.execute(TrampolineEC.scala:42)
at cats.effect.internals.IOBracket$BracketStart.apply(IOBracket.scala:69)
at cats.effect.internals.IOBracket$BracketStart.apply(IOBracket.scala:49)
at cats.effect.internals.IORunLoop$.cats$effect$internals$IORunLoop$$loop(IORunLoop.scala:139)
at cats.effect.internals.IORunLoop$RestartCallback.signal(IORunLoop.scala:359)
at cats.effect.internals.IORunLoop$RestartCallback.apply(IORunLoop.scala:380)
at cats.effect.internals.IORunLoop$RestartCallback.apply(IORunLoop.scala:323)
at cats.effect.internals.IOShift$Tick.run(IOShift.scala:35)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)