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: Crash in use of aliased OpBoolean #1631

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

daemon23
Copy link

Selecting aliased comparison results blows up (in mariadb/mysql, anyway) trying to cast an int to a boolean. This corrects that issue.

@daemon23
Copy link
Author

After fussing with the code I have an alternative which would simplify this significantly, but it involves restructuring Expression and ExpressionWithColumnType to be interfaces; I can easily see it being a breaking change for users.

@Tapac
Copy link
Contributor

Tapac commented Nov 17, 2022

Thank you for a PR. I think it would be better to resolve it in a more generic way for all ExpressionAliases by making a recursive call to rawToColumnValue with delegate as a value. WDYT?

@daemon23
Copy link
Author

daemon23 commented Nov 20, 2022

That actually makes a lot of sense as well and you're probably right. It might be worth performing a check in ExpressionAlias's init block that the delegate is not itself an alias; the only downside I could think of is some sort of bizarre multiple nesting aliases scenario, but I can't think of an SQL where that would be legal anyway.

@daemon23
Copy link
Author

btw just want to note that I really like what you've done here--I'm just sorry that I probably can't use it in my job because we have a number of devs who are opposed to any ORM tool that doesn't prioritize working with raw SQL.

@joc-a joc-a changed the title Fix for use of aliased OpBooleans fix: Crash in use of aliased OpBoolean Jul 18, 2023
@joc-a
Copy link
Collaborator

joc-a commented Jul 18, 2023

Thank you for a PR. I think it would be better to resolve it in a more generic way for all ExpressionAliases by making a recursive call to rawToColumnValue with delegate as a value. WDYT?

@Tapac Something like this?

@Suppress("UNCHECKED_CAST")
    private fun <T> rawToColumnValue(raw: T?, expression: Expression<T>): T {
        return when {
            raw == null -> null
            raw == NotInitializedValue -> error("$expression is not initialized yet")
            expression is ExpressionAlias<T> && expression.delegate is ExpressionWithColumnType<T> -> expression.delegate.columnType.valueFromDB(raw)
            expression is ExpressionWithColumnType<T> -> expression.columnType.valueFromDB(raw)
            expression is Op.OpBoolean -> BooleanColumnType.INSTANCE.valueFromDB(raw)
            expression is ExpressionAlias<T> -> rawToColumnValue(raw, expression.delegate)
            else -> raw
        } as T
    }

@joc-a joc-a linked an issue Jul 19, 2023 that may be closed by this pull request
@joc-a joc-a self-assigned this Jul 19, 2023
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.

Using exists in subquery
3 participants