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

fix: EXPOSED-287 Wrong parenthesis with advanced use of isDistinctFrom #1990

Merged
merged 1 commit into from
Feb 13, 2024

Conversation

bog-walk
Copy link
Member

Attempting to use either operator isDistinctFrom() or isNotDistinctFrom() with a left- or right-side value that is a complex expression results in either a syntax exception or incorrect results (depending on the db):

Table
    .selectAll()
    .where { Table.number1.isNotNull() isDistinctFrom Table.number2.isNull() }

// generates SQL:
// WHERE number_1 IS NOT NULL IS DISTINCT FROM number_2 IS NULL

// which is actually evaluated as:
// WHERE (number_1 IS NOT NULL IS DISTINCT FROM number_2) IS NULL

isNull() and isNotNull() are already marked as ComplexExpression.
This fix ensures that any such expressions are identified properly when appending to the query builder, by using appendExpression() instead of base append().

// new SQL:
// WHERE (number_1 IS NOT NULL) IS DISTINCT FROM (number_2 IS NULL)

// non-complex expressions retain their original SQL:
// WHERE number_1 IS DISTINCT FROM number_2

Attempting to use either operator isDistinctFrom() or isNotDistinctFrom() with a
left-or right-side value that is a complex expression results in either a syntax
exception or incorrect results (depending on the database).

Complex expressions like isNull() and isNotNull() are already marked, so this fix
ensures that they are identified as such and wrapped in parentheses when appending
to a query builder, by using appendExpression() instead of base append().
@bog-walk bog-walk requested review from e5l and joc-a February 13, 2024 01:02
Copy link
Member

@e5l e5l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, looks good!

@@ -29,7 +29,7 @@ class ConditionsTests : DatabaseTestsBase() {
val number2 = integer("number_2").nullable()
}
// remove SQL Server exclusion once test container supports SQL Server 2022
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create a YouTrack issue for this?

@bog-walk bog-walk merged commit d6cd99c into main Feb 13, 2024
5 checks passed
@bog-walk bog-walk deleted the bog-walk/fix-is-distinct-from-op branch February 13, 2024 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants