Skip to content

Commit 4a5905c

Browse files
authored
Fix: Snowflake prevent exponential growth in CTE vars query result size (#81)
* Fix: Snowflake prevent exponential growth in CTE vars query result size * Add tests for fix
1 parent 21a8138 commit 4a5905c

4 files changed

Lines changed: 1128 additions & 12 deletions

File tree

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ object CTEQueryGenerator : BaseQueryGenerator() {
5656
)
5757
.orderBy(DSL.field(DSL.name(listOf(VARS, INDEX))))
5858
)
59-
.from(buildSelections(request).asTable("data"))
60-
.rightJoin(DSL.name(VARS))
59+
.from(DSL.name(VARS))
60+
.leftJoin(buildSelections(request).asTable("data"))
6161
.on(
6262
DSL.field(DSL.name("data", INDEX))
6363
.eq(DSL.field(DSL.name(VARS, INDEX)))
@@ -184,7 +184,7 @@ object CTEQueryGenerator : BaseQueryGenerator() {
184184
).orderBy(
185185
run {
186186
val orderByFields = translateIROrderByField(request) +
187-
if (request.isVariablesRequest()) listOf(
187+
if (request.isVariablesRequest() && request.root_collection == request.collection) listOf(
188188
DSL.field(
189189
DSL.name(
190190
listOf(
@@ -201,8 +201,19 @@ object CTEQueryGenerator : BaseQueryGenerator() {
201201

202202
)
203203
.apply {
204-
if (request.isVariablesRequest())
205-
this.select(DSL.table(DSL.name(VARS)).asterisk())
204+
if (request.isVariablesRequest()) {
205+
if (relationship == null) {
206+
// Root: bring vars.* (includes idx)
207+
this.select(DSL.table(DSL.name(VARS)).asterisk())
208+
} else {
209+
// Nested: propagate parent's idx as idx to keep per-var partitioning
210+
this.select(
211+
DSL.field(
212+
DSL.name(listOf(genCTEName(relSource ?: request.collection), INDEX))
213+
).`as`(DSL.name(INDEX))
214+
)
215+
}
216+
}
206217
}
207218
.apply {
208219
if (relationship != null
@@ -225,8 +236,8 @@ object CTEQueryGenerator : BaseQueryGenerator() {
225236
sourceCollection = request.collection
226237
)
227238
}
228-
.apply {// cross join "vars" if this request contains variables
229-
if (request.isVariablesRequest())
239+
.apply {// only the root level should see VARS; nested levels should be scoped by parent
240+
if (request.isVariablesRequest() && relationship == null)
230241
(this as SelectJoinStep<*>).crossJoin(DSL.name(VARS))
231242
}
232243
.apply {
@@ -454,11 +465,19 @@ object CTEQueryGenerator : BaseQueryGenerator() {
454465
innerSelect.asTable(innerAlias)
455466
)
456467
.on(
457-
mkSQLJoin(
458-
relationship,
459-
sourceCollection = genCTEName(currentRequest.collection),
460-
targetTableNameTransform = { innerAlias }
461-
)
468+
run {
469+
val baseJoin = mkSQLJoin(
470+
relationship,
471+
sourceCollection = genCTEName(currentRequest.collection),
472+
targetTableNameTransform = { innerAlias }
473+
)
474+
if (currentRequest.isVariablesRequest()) {
475+
baseJoin.and(
476+
DSL.field(DSL.name(listOf(genCTEName(currentRequest.collection), INDEX)))
477+
.eq(DSL.field(DSL.name(listOf(innerAlias, INDEX))))
478+
)
479+
} else baseJoin
480+
}
462481
)
463482
joinedAliases.add(innerAlias)
464483
}

0 commit comments

Comments
 (0)