Skip to content

Commit 1347c7c

Browse files
authored
Merge pull request #30 from hasura/gavin/snowflake-native-query-support
Add Native Query support to Snowflake
2 parents c2bba32 + b4afe2b commit 1347c7c

File tree

2 files changed

+9
-43
lines changed

2 files changed

+9
-43
lines changed

ndc-connector-snowflake/src/main/kotlin/io/hasura/snowflake/CTEQueryGenerator.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.hasura
22

3+
import io.hasura.ndc.common.ConnectorConfiguration
34
import io.hasura.ndc.ir.*
45
import io.hasura.ndc.ir.extensions.isVariablesRequest
56
import io.hasura.ndc.sqlgen.BaseQueryGenerator
@@ -18,9 +19,9 @@ object CTEQueryGenerator : BaseQueryGenerator() {
1819
override fun queryRequestToSQL(
1920
request: QueryRequest
2021
): Select<*> {
21-
return buildCTEs(request)
22-
.select(DSL.jsonArrayAgg(DSL.field(DSL.name(listOf("data", ROWS_AND_AGGREGATES)))))
23-
.from(buildSelections(request).asTable("data"))
22+
return buildCTEs(request)
23+
.select(DSL.jsonArrayAgg(DSL.field(DSL.name(listOf("data", ROWS_AND_AGGREGATES)))))
24+
.from(buildSelections(request).asTable("data"))
2425
}
2526

2627

@@ -44,10 +45,11 @@ object CTEQueryGenerator : BaseQueryGenerator() {
4445
}
4546

4647
private fun buildCTEs(request: QueryRequest, varCTE: List<CommonTableExpression<*>> = emptyList()): WithStep {
47-
return DSL.with(
48-
varCTE +
49-
forEachQueryLevelRecursively(request, CTEQueryGenerator::buildCTE).distinct()
50-
)
48+
val withStep = mkNativeQueryCTEs(request)
49+
.with(varCTE)
50+
.with(forEachQueryLevelRecursively(request, CTEQueryGenerator::buildCTE).distinct())
51+
52+
return withStep
5153
}
5254

5355
private fun getCollectionAsjOOQName(collection: String): Name {

ndc-sqlgen/src/main/kotlin/io/hasura/ndc/sqlgen/BaseQueryGenerator.kt

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -128,42 +128,6 @@ abstract class BaseQueryGenerator : BaseGenerator {
128128
return withStep
129129
}
130130

131-
fun mkNativeQueryCTE(
132-
request: QueryRequest
133-
): org.jooq.WithStep {
134-
val config = ConnectorConfiguration.Loader.config
135-
136-
fun renderNativeQuerySQL(
137-
nativeQuery: NativeQueryInfo,
138-
arguments: Map<String, Argument>
139-
): String {
140-
val sql = nativeQuery.sql
141-
val parts = sql.parts
142-
143-
return parts.joinToString("") { part ->
144-
when (part) {
145-
is NativeQueryPart.Text -> part.value
146-
is NativeQueryPart.Parameter -> {
147-
val argument = arguments[part.value] ?: error("Argument ${part.value} not found")
148-
when (argument) {
149-
is Argument.Literal -> argument.value.toString()
150-
else -> error("Only literals are supported in Native Queries in this version")
151-
}
152-
}
153-
}
154-
}
155-
}
156-
157-
val nativeQuery = config.nativeQueries[request.collection]!!
158-
val nativeQuerySQL = renderNativeQuerySQL(nativeQuery, request.arguments)
159-
160-
return DSL.with(
161-
DSL.name(request.collection)
162-
).`as`(
163-
DSL.resultQuery(nativeQuerySQL)
164-
)
165-
}
166-
167131
fun getQueryColumnFields(fields: Map<String, IRField>): Map<String, IRField.ColumnField> {
168132
return fields
169133
.filterValues { it is IRField.ColumnField }

0 commit comments

Comments
 (0)