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-14922: Added HSTORE support to SqlCompilerService #518

Merged
merged 1 commit into from
Sep 20, 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 @@ -138,7 +138,8 @@ object SqlTypesUtils {
val otherTypeMap: Map[String, RawType] = Map(
"interval" -> RawIntervalType(false, false),
"json" -> RawAnyType(),
"jsonb" -> RawAnyType()
"jsonb" -> RawAnyType(),
"hstore" -> RawAnyType()
)
otherTypeMap.get(pgTypeName) match {
case Some(t) => Right(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.rawlabs.sql.compiler.writers

import com.fasterxml.jackson.core.{JsonEncoding, JsonFactory, JsonParser}
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import com.rawlabs.compiler.{
RawAnyType,
Expand Down Expand Up @@ -129,6 +130,14 @@ class TypedResultSetJsonWriter(os: OutputStream, maxRows: Option[Long]) {
val json = mapper.readTree(data)
writeRawJson(json)
}
case "hstore" =>
val hstoreMap = v.getObject(i).asInstanceOf[java.util.Map[String, String]]
if (v.wasNull()) gen.writeNull()
else {
// Convert hstore to JSON-like structure
val json = mapper.valueToTree[ObjectNode](hstoreMap)
writeRawJson(json)
}
case _ => throw new IOException("unsupported type")
}
case _: RawDateType =>
Expand Down
2 changes: 2 additions & 0 deletions sql-compiler/src/test/resources/example.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
CREATE SCHEMA example;

CREATE EXTENSION hstore;

CREATE TABLE example.airports (
airport_id integer,
name character varying(256),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1153,4 +1153,61 @@ class TestSqlCompilerServiceAirports
}
}

test("SELECT 'a=>1,b=>tralala'::hstore AS r -- JSON") { t =>
val baos = new ByteArrayOutputStream()
assert(
compilerService.execute(
t.q,
asJson(),
None,
baos
) == ExecutionSuccess(true)
)
assert(baos.toString() === """[{"r":{"a":"1","b":"tralala"}}]""")
}

test("SELECT NULL::hstore AS r -- JSON") { t =>
val baos = new ByteArrayOutputStream()
assert(
compilerService.execute(
t.q,
asJson(),
None,
baos
) == ExecutionSuccess(true)
)
assert(baos.toString() === """[{"r":null}]""")
}

// TODO What do we do in CSV?
ignore("SELECT 'a=>1,b=>tralala'::hstore AS r -- CSV") { t =>
val baos = new ByteArrayOutputStream()
assert(
compilerService.execute(
t.q,
asCsv(),
None,
baos
) == ExecutionSuccess(true)
)
assert(
baos.toString() ===
"""r
|{"a":"1","b":"tralala"}
|""".stripMargin
)
}

ignore("SELECT NULL::hstore AS r -- CSV") { t =>
val baos = new ByteArrayOutputStream()
assert(
compilerService.execute(
t.q,
asCsv(),
None,
baos
) == ExecutionSuccess(true)
)
assert(baos.toString() === """[{"r":{"a":"1","b":"tralala"}}]""")
}
}