Skip to content

Commit

Permalink
Merge pull request #33 from hasura/in-update-variation
Browse files Browse the repository at this point in the history
In update variation
  • Loading branch information
gneeri authored Dec 5, 2024
2 parents 1347c7c + e98b709 commit 2131c44
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ object MySQLJDBCSchemaGenerator : JDBCSchemaGenerator() {
"_eq" to ComparisonOperatorDefinition.Equal,
"_contains" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.STRING.name)),
"_like" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.STRING.name)),
"_in" to ComparisonOperatorDefinition.In
),
aggregate_functions = mapOf(
"min" to AggregateFunctionDefinition(result_type = Type.Named(NDCScalar.STRING.name)),
Expand All @@ -76,7 +77,8 @@ object MySQLJDBCSchemaGenerator : JDBCSchemaGenerator() {
"_lt" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.DATETIME.name)),
"_gte" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.DATETIME.name)),
"_lte" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.DATETIME.name)),
"_eq" to ComparisonOperatorDefinition.Equal
"_eq" to ComparisonOperatorDefinition.Equal,
"_in" to ComparisonOperatorDefinition.In
),
aggregate_functions = emptyMap()
),
Expand All @@ -86,7 +88,8 @@ object MySQLJDBCSchemaGenerator : JDBCSchemaGenerator() {
"_lt" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.DATE.name)),
"_gte" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.DATE.name)),
"_lte" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.DATE.name)),
"_eq" to ComparisonOperatorDefinition.Equal
"_eq" to ComparisonOperatorDefinition.Equal,
"_in" to ComparisonOperatorDefinition.In
),
aggregate_functions = emptyMap()
),
Expand All @@ -96,7 +99,8 @@ object MySQLJDBCSchemaGenerator : JDBCSchemaGenerator() {
"_lt" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.TIME.name)),
"_gte" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.TIME.name)),
"_lte" to ComparisonOperatorDefinition.Custom(argument_type = Type.Named(NDCScalar.TIME.name)),
"_eq" to ComparisonOperatorDefinition.Equal
"_eq" to ComparisonOperatorDefinition.Equal,
"_in" to ComparisonOperatorDefinition.In
),
aggregate_functions = emptyMap()
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,6 @@ object CTEQueryGenerator : BaseQueryGenerator() {
is Expression.And -> e.expressions.flatMap { getVars(it) }
is Expression.Or -> e.expressions.flatMap { getVars(it) }
is Expression.Not -> getVars(e.expression)
is Expression.ApplyBinaryArrayComparison ->
if (e.values.filterIsInstance<ComparisonValue.VariableComp>().isNotEmpty())
listOf(DSL.field(DSL.name(e.column.name)))
else emptyList()

is Expression.ApplyBinaryComparison ->
if (e.value is ComparisonValue.VariableComp)
listOf(DSL.field(DSL.name(e.column.name)))
Expand Down
24 changes: 6 additions & 18 deletions ndc-ir/src/main/kotlin/io/hasura/ndc/ir/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,6 @@ enum class ApplyUnaryComparisonOperator {
IS_NULL
}

enum class ApplyBinaryArrayComparisonOperator {
@JsonProperty("in")
IN
}

// /////////////////////////////////////////////////////////////////////////
// EXPRESSIONS
// /////////////////////////////////////////////////////////////////////////
Expand All @@ -212,26 +207,19 @@ sealed interface Expression {
@JsonTypeName("not")
data class Not(val expression: Expression) : Expression

@JsonTypeName("unary_comparison_operator")
data class ApplyUnaryComparison(
val operator: ApplyUnaryComparisonOperator,
val column: String
) : Expression

@JsonTypeName("binary_comparison_operator")
data class ApplyBinaryComparison(
val operator: ApplyBinaryComparisonOperator,
override val column: ComparisonColumn,
val value: ComparisonValue
) : Expression, ExpressionOnColumn

@JsonTypeName("binary_array_comparison_operator")
data class ApplyBinaryArrayComparison(
val operator: ApplyBinaryArrayComparisonOperator,
override val column: ComparisonColumn,
val values: List<ComparisonValue>
) : Expression, ExpressionOnColumn

@JsonTypeName("unary_op")
data class ApplyUnaryComparison(
val operator: ApplyUnaryComparisonOperator,
val column: String
) : Expression

// Test if a row exists that matches the where subexpression in the specified table (in_table)
@JsonTypeName("exists")
data class Exists(
Expand Down
56 changes: 21 additions & 35 deletions ndc-sqlgen/src/main/kotlin/io/hasura/ndc/sqlgen/BaseGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ sealed interface BaseGenerator {
ApplyBinaryComparisonOperator.GTE -> col.ge(value)
ApplyBinaryComparisonOperator.LT -> col.lt(value)
ApplyBinaryComparisonOperator.LTE -> col.le(value)
ApplyBinaryComparisonOperator.IN -> col.`in`(value)
ApplyBinaryComparisonOperator.IN -> DSL.nullCondition()
ApplyBinaryComparisonOperator.IS_NULL -> col.isNull
ApplyBinaryComparisonOperator.LIKE -> col.like(value as Field<String>)
ApplyBinaryComparisonOperator.CONTAINS -> col.contains(value as Field<String>)
Expand Down Expand Up @@ -102,10 +102,6 @@ sealed interface BaseGenerator {
request: QueryRequest
): Condition {

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

return when (e) {
is Expression.Not -> DSL.not(expressionToCondition(e.expression, request))

Expand All @@ -131,7 +127,11 @@ sealed interface BaseGenerator {
DSL.field(DSL.name(col + v.column.name))
}

is ComparisonValue.ScalarComp -> DSL.inline(v.value)
is ComparisonValue.ScalarComp ->
if(e.operator == ApplyBinaryComparisonOperator.IN)
return handleInComp(column, v)
else DSL.inline(v.value)

is ComparisonValue.VariableComp -> DSL.field(DSL.name(listOf("vars", v.name)))
}
return buildComparison(column, e.operator, comparisonValue)
Expand All @@ -144,35 +144,6 @@ sealed interface BaseGenerator {
}
}

is Expression.ApplyBinaryArrayComparison -> {
val column = DSL.field(
DSL.name(
splitCollectionName(getCollectionForCompCol(e.column, request)) + e.column.name
)
)
when (e.operator) {
ApplyBinaryArrayComparisonOperator.IN -> {
when {
e.values.isEmpty() -> column.`in`(
DSL.select(DSL.nullCondition())
.where(DSL.inline(1).eq(DSL.inline(0)))
)

else -> column.`in`(DSL.list(e.values.map {
when (it) {
is ComparisonValue.ScalarComp -> DSL.inline(it.value)
is ComparisonValue.VariableComp -> DSL.field(DSL.name(listOf("vars", it.name)))
is ComparisonValue.ColumnComp -> {
val col = splitCollectionName(getCollectionForCompCol(it.column, request))
DSL.field(DSL.name(col + it.column.name))
}
}
}))
}
}
}
}

is Expression.Exists -> {
when (val inTable = e.in_collection) {
is ExistsInCollection.Related -> {
Expand Down Expand Up @@ -241,4 +212,19 @@ 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(".")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,6 @@ abstract class BaseQueryGenerator : BaseGenerator {
}
}

is Expression.ApplyBinaryArrayComparison -> {
addForColumn(where.column)
where.values.filterIsInstance<ComparisonValue.ColumnComp>()
.forEach { addForColumn(it.column) }
}

is Expression.ApplyUnaryComparison -> {} // no-op
is Expression.Exists -> addJoinsRequiredForPredicate(request, select, where.predicate, seenRelations)
}
Expand Down

0 comments on commit 2131c44

Please sign in to comment.