Skip to content

Commit

Permalink
Merge pull request #35 from hasura/build-comparison-cleanup
Browse files Browse the repository at this point in the history
cleaned up comparison handling around IN operator
  • Loading branch information
gneeri authored Dec 11, 2024
2 parents 902b24d + 41ca23e commit 72b6f52
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions ndc-sqlgen/src/main/kotlin/io/hasura/ndc/sqlgen/BaseGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,26 @@ sealed interface BaseGenerator {
fun buildComparison(
col: Field<Any>,
operator: ApplyBinaryComparisonOperator,
value: Field<Any>
listVal: List<Field<Any>>
): Condition {
if (operator != ApplyBinaryComparisonOperator.IN && listVal.size != 1) {
error("Only the IN operator supports multiple values")
}

// unwrap single value for use in all but the IN operator
// OR return falseCondition if listVal is empty
val singleVal = listVal.firstOrNull() ?: return DSL.falseCondition()

return when (operator) {
ApplyBinaryComparisonOperator.EQ -> col.eq(value)
ApplyBinaryComparisonOperator.GT -> col.gt(value)
ApplyBinaryComparisonOperator.GTE -> col.ge(value)
ApplyBinaryComparisonOperator.LT -> col.lt(value)
ApplyBinaryComparisonOperator.LTE -> col.le(value)
ApplyBinaryComparisonOperator.IN -> DSL.nullCondition()
ApplyBinaryComparisonOperator.EQ -> col.eq(singleVal)
ApplyBinaryComparisonOperator.GT -> col.gt(singleVal)
ApplyBinaryComparisonOperator.GTE -> col.ge(singleVal)
ApplyBinaryComparisonOperator.LT -> col.lt(singleVal)
ApplyBinaryComparisonOperator.LTE -> col.le(singleVal)
ApplyBinaryComparisonOperator.IN -> col.`in`(listVal)
ApplyBinaryComparisonOperator.IS_NULL -> col.isNull
ApplyBinaryComparisonOperator.LIKE -> col.like(value as Field<String>)
ApplyBinaryComparisonOperator.CONTAINS -> col.contains(value as Field<String>)
ApplyBinaryComparisonOperator.LIKE -> col.like(singleVal as Field<String>)
ApplyBinaryComparisonOperator.CONTAINS -> col.contains(singleVal as Field<String>)
}
}

Expand Down Expand Up @@ -124,15 +132,16 @@ sealed interface BaseGenerator {
val comparisonValue = when (val v = e.value) {
is ComparisonValue.ColumnComp -> {
val col = splitCollectionName(getCollectionForCompCol(v.column, request))
DSL.field(DSL.name(col + v.column.name))
listOf(DSL.field(DSL.name(col + v.column.name)))
}

is ComparisonValue.ScalarComp ->
if(e.operator == ApplyBinaryComparisonOperator.IN)
return handleInComp(column, v)
else DSL.inline(v.value)
when (val scalarValue = v.value) {
is List<*> -> (scalarValue as List<Any>).map { DSL.inline(it) }
else -> listOf(DSL.inline(scalarValue))
}

is ComparisonValue.VariableComp -> DSL.field(DSL.name(listOf("vars", v.name)))
is ComparisonValue.VariableComp -> listOf(DSL.field(DSL.name(listOf("vars", v.name))))
}
return buildComparison(column, e.operator, comparisonValue)
}
Expand Down Expand Up @@ -212,17 +221,6 @@ sealed interface BaseGenerator {
}
}

fun handleInComp(column: Field<Any>, value: ComparisonValue.ScalarComp): Condition {
return when (val scalarValue = value.value) {
is List<*> -> {
if (scalarValue.isEmpty()) DSL.falseCondition()
else column.`in`(scalarValue.map { DSL.inline(it) })
}
// Handle non-array scalar value
else -> column.eq(DSL.inline(scalarValue))
}
}

fun splitCollectionName(collectionName: String): List<String> {
return collectionName.split(".")
}
Expand Down

0 comments on commit 72b6f52

Please sign in to comment.