Skip to content

Fix #3986 #3990

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

Merged
merged 2 commits into from
Feb 23, 2023
Merged

Fix #3986 #3990

merged 2 commits into from
Feb 23, 2023

Conversation

jods4
Copy link
Contributor

@jods4 jods4 commented Feb 22, 2023

Fix #3986

OR xx IS NULL should be wrapped with parens like xx IN () OR xx IN () is, as it might be part of a larger AND expression.

`OR xx IS NULL` should be wrapped with parens like `xx IN () OR xx IN ()` is, as it might be part of a larger AND expression.
viceroypenguin
viceroypenguin previously approved these changes Feb 22, 2023
@jods4 jods4 changed the title Fix #3986 [WIP] Fix #3986 Feb 22, 2023
@viceroypenguin viceroypenguin self-requested a review February 22, 2023 13:13
@MaceWindu
Copy link
Contributor

MaceWindu commented Feb 22, 2023

That's something for SqlOptimizer to do. I'm currently trying to implement it in SqlOptimizer and if I will fail - we will use SqlBuilder fix for release

@jods4
Copy link
Contributor Author

jods4 commented Feb 22, 2023

I thought my fix was incorrect because of the WithNull clause that always add a OR but it turns out it's ok.
This is not obvious, so here's the full explanation why we don't need more parens:

A positive (IN) case that has multiple clauses, or contains null, or has WithNull true will end up like the following expression, which is fine to wrap in parens in case it's connected with AND to something else (fixes #3986):

x IN (..) OR x IN (..) OR x IS NULL

A negative (NOT IN) will codegen like one of those:

-- When null is part of the list:
x NOT IN (..) AND x NOT IN (..) AND x IS NOT NULL
-- When WithNull is true:
x NOT IN (..) AND x NOT IN (..) OR x IS NULL

Notice the second case is a mix of AND/OR, which made me think I might have introduced a bug with this fix.
But as OR has a lower precedence than AND it parses correctly as (x NOT IN (..) AND x NOT IN (..)) OR x IS NULL.
These expression can then be wrapped in parens (esp. the second one which has OR precedence and won't combine correctly with an outside AND.

@jods4 jods4 changed the title [WIP] Fix #3986 Fix #3986 Feb 22, 2023
@MaceWindu
Copy link
Contributor

/azp run test-all

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MaceWindu
Copy link
Contributor

/azp run test-all

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MaceWindu MaceWindu added this to the 5.0.0 milestone Feb 23, 2023
@linq2dbot
Copy link

Test baselines changed by this PR. Don't forget to merge/close baselines PR after this pr merged/closed.

@MaceWindu MaceWindu merged commit 2dc064f into master Feb 23, 2023
@MaceWindu MaceWindu deleted the issues/3986 branch February 23, 2023 16:36
MaceWindu pushed a commit to linq2db/linq2db.baselines that referenced this pull request Feb 23, 2023
* [Windows / SQLite (both providers)] baselines

* [Windows / SQLite (specialized tests)] baselines

* [Windows / SQLite (both providers)] baselines

* [Windows / Access MDB (Jet/ODBC)] baselines

* [Windows / SQL CE] baselines

* [Windows / Access MDB (Jet/ODBC)] baselines

* [Windows / SQL Server 2005] baselines

* [Windows / SQL Server 2005] baselines

* [Windows / SQL Server 2008] baselines

* [Windows / SQL Server 2012] baselines

* [Windows / SQLite (specialized tests)] baselines

* [Windows / SQL Server 2017] baselines

* [Windows / SQL Server 2008] baselines

* [Windows / SQL Server 2014] baselines

* [Windows / SQL Server 2014] baselines

* [Windows / SQL Server 2012] baselines

* [Windows / SQL Server 2019] baselines

* [Linux / DB2 LUW 11.5] baselines

* [Linux / Firebird 2.5] baselines

* [Linux / Firebird 3.0] baselines

* [Windows / SQL Server EXTRAS] baselines

* [Windows / SQL Server 2017] baselines

* [Linux / Informix 14.10] baselines

* [Linux / Firebird 4.0] baselines

* [Linux / MariaDB] baselines

* [Windows / SQL Server EXTRAS] baselines

* [Linux / ClickHouse] baselines

* [Linux / MySQL 8 (both providers)] baselines

* [Windows / SQL Server 2022] baselines

* [Windows / SQL Server 2019] baselines

* [Linux / MySQL 5.5 (both providers)] baselines

* [Linux / Oracle 11g XE] baselines

* [Linux / PostgreSQL 10] baselines

* [Linux / PostgreSQL 11] baselines

* [Linux / PostgreSQL 12] baselines

* [Linux / PostgreSQL 13] baselines

* [Linux / Oracle 12c] baselines

* [Linux / PostgreSQL 15] baselines

* [Linux / PostgreSQL 14] baselines

* [Linux / Oracle 21c] baselines

* [Linux / Oracle 18c] baselines

* [Linux / Sybase ASE 16] baselines

* [Linux / SAP HANA 2] baselines

* [Linux / SQL Server 2019] baselines

* [Windows / SQL Server 2016] baselines

* [Windows / Access ACE (ODBC-only) x64] baselines

---------

Co-authored-by: Azure Pipelines Bot <azp@linq2db.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect SQL generated over 'Contains' in array having null element
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy