%3CLINGO-SUB%20id%3D%22lingo-sub-1634393%22%20slang%3D%22en-US%22%3EMining%20for%20logic%20bugs%20in%20the%20Citus%20extension%20to%20Postgres%20with%20SQLancer%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1634393%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EMany%20of%20you%20rely%20on%20databases%20to%20return%20correct%20results%20for%20your%20SQL%20queries%2C%20however%20complex%20your%20queries%20might%20be.%20And%20you%20probably%20place%20your%20trust%20with%20no%20questions%20asked%E2%80%94since%20you%20know%20relational%20databases%20are%20built%20on%20top%20of%20proven%20mathematical%20foundations%2C%20and%20since%20there%20is%20no%20practical%20way%20to%20manually%20verify%20your%20SQL%20query%20output%20anyway.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESince%20it%20is%20possible%20that%20a%20database's%20implementation%20of%20the%20SQL%20logic%20could%20have%20a%20few%20errors%2C%20database%20developers%20apply%20extensive%20testing%20methods%20to%20avoid%20such%20flaws.%20For%20instance%2C%20the%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fcitusdata%2Fcitus%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ECitus%20open%20source%20repo%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20on%20GitHub%20has%20more%20than%20twice%20as%20many%20lines%20related%20to%20automated%20testing%20than%20lines%20of%20database%20code.%20However%2C%20checking%20correctness%20for%20all%20possible%20SQL%20queries%20is%20challenging%20because%20of%20the%20lack%20of%20a%20%E2%80%9Cground%20truth%E2%80%9D%20to%20compare%20their%20outputs%20against%2C%20and%20the%20infinite%20number%20of%20possible%20SQL%20queries.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EEven%20when%20we%20do%20know%20the%20result%20a%20SQL%20query%20is%20supposed%20to%20give%20and%20we%20get%20that%20result%20as%20expected%2C%20that%20does%20not%20mean%20the%20implementation%20is%20100%25%20correct%20in%20all%20possible%20variants%20of%20the%20query%20and%20input%20data.%20The%20challenge%20for%20a%20database%20team%20is%20in%20finding%20subtle%20edge%20cases%20where%20bugs%20might%20be%20introduced.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1270427058%22%20id%3D%22toc-hId--1270427736%22%3E%3CSPAN%3EWhat%20are%20logic%20bugs%20in%20SQL%2C%20and%20why%20are%20they%20hard%20to%20detect%3F%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3E%3CBR%20%2F%3EThere%20are%202%20primary%20types%20of%20errors%20in%20databases%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3E%E2%80%9Craised%E2%80%9D%20errors%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3Elogic%20errors%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%E2%80%9CRaised%E2%80%9D%20errors%3C%2FSTRONG%3E%3CSPAN%3E%20include%20syntax%20errors%2C%20panics%2C%20faults%2C%20and%20other%20crashes.%20Raised%20errors%20terminate%20the%20process%20abnormally%20or%20give%20you%20some%20signal%20of%20undesired%20behavior%2C%20making%20them%20immediately%20recognizable%20to%20you.%20In%20order%20to%20automatically%20detect%20(and%20then%20fix)%20raised%20errors%2C%20database%20providers%20widely%20use%20fuzzing%20tools%20for%20SQL.%20Fuzzing%20tools%20are%20random%20SQL%20query%20generators%20that%20strain%20the%20target%20database%20with%20complex%20commands%20until%20an%20error%20is%20raised.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CBR%20%2F%3E%3CSTRONG%3ELogic%20errors%3C%2FSTRONG%3E%2C%20on%20the%20other%20hand%2C%20are%20latent%20(or%20silent)%20errors%20that%20cause%20the%20database%20to%20produce%20inaccurate%20results%20without%20emitting%20any%20sign%20of%20unexpected%20behavior.%20Logic%20bugs%20are%20dangerous%E2%80%94neither%20the%20user%2C%20nor%20the%20database%20provider%2C%20nor%20the%20fuzzing%20tools%20might%20be%20aware%20of%20incorrect%20results%20being%20fetched%20from%20the%20database.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Raised-errors-vs-logic-errors-in-databases-Fig1.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F216514i31E79F9A119600C0%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Raised-errors-vs-logic-errors-in-databases-Fig1.png%22%20alt%3D%22Fig.%201%3A%20%E2%80%9CRaised%E2%80%9D%20errors%20vs.%20logic%20errors%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFig.%201%3A%20%E2%80%9CRaised%E2%80%9D%20errors%20vs.%20logic%20errors%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1217085775%22%20id%3D%22toc-hId-1217085097%22%3E%3CSPAN%3EWhat%20if%20we%20could%20find%20a%20way%20to%20test%20the%20validity%20of%20SQL%20query%20output%20for%20a%20DBMS%3F%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20recently%20launched%20open%20source%20%3C%2FSPAN%3E%3CA%20href%3D%22http%3A%2F%2Fwww.sqlancer.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ESQLancer%20(Synthesized%20Query%20Lancer)%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20tool%20gives%20you%20a%20way%20to%20test%20the%20validity%20of%20a%20database%E2%80%99s%20query%20responses.%20SQLancer%20is%20an%20automated%20Database%20Management%20System%20(DBMS)%20testing%20tool%20for%20detecting%20logic%20bugs.%20SQLancer%E2%80%99s%20testing%20approaches%20substitute%20the%20need%20for%20a%20ground%20truth%20in%20their%20correctness%20checks%2C%20by%20probing%20databases%20for%20self-consistency%20rather%20than%20agreement%20with%20some%20known%20behavior.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWhile%20SQLancer%20is%20still%20in%20the%20research%20prototype%20stage%2C%20it%20was%20released%20early%20on%20due%20to%20high%20demand%20from%20different%20companies%2C%20organizations%2C%20and%20developers.%20I%20first%20learned%20about%20SQLancer%20in%20June%202020%20through%20a%20Quarantine%202020%20Database%20Talk%20organized%20by%20Carnegie%20Mellon%20Professor%20of%20Databases%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fandy_pavlo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EAndy%20Pavlo%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E.%20%3C%2FSPAN%3E%3CSPAN%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3EThe%20CMU%20talk%20by%20SQLancer%20creator%20Manuel%20Rigger%20was%20about%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fyoutu.be%2FNp46NQ6lqP8%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EFinding%20Logic%20Bugs%20in%20Database%20Management%20Systems%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E.%20At%20the%20time%20of%20Manuel%20Rigger%E2%80%99s%20talk%2C%20SQLancer%20had%20already%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.manuelrigger.at%2Fdbms-bugs%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Eidentified%20more%20than%20175%20bugs%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20in%205%20different%20SQL%20database%20management%20systems%20by%20using%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.manuelrigger.at%2Fpreprints%2FTLP.pdf%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ETernary%20Logic%20Partitioning%3C%2FSPAN%3E%3CSPAN%3E%20(TLP).%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20Hence%2C%20Ternary%20Logic%20Partitioning%20became%20the%20SQLancer%20testing%20approach%20that%20was%20of%20most%20interest%20to%20our%20team%20at%20Citus.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIf%20you%20are%20not%20familiar%2C%20Citus%20is%20an%20open%20source%20extension%20to%20Postgres%20that%20transforms%20Postgres%20into%20a%20distributed%20database.%20Since%20the%20Citus%20team%20was%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.citusdata.com%2Fblog%2F2019%2F01%2F24%2Fmicrosoft-acquires-citus-data%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Eacquired%20by%20Microsoft%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20last%20year%2C%20Citus%20is%20now%20available%20on%20Microsoft%20Azure%20as%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fquickstart-create-hyperscale-portal%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EHyperscale%20(Citus)%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%2C%20a%20built-in%20deployment%20option%20on%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fservices%2Fpostgresql%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EAzure%20Database%20for%20PostgreSQL%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EDuring%20my%20software%20engineering%20internship%20the%20summer%20after%20my%20second%20year%20as%20an%20undergraduate%20at%20Stanford%2C%20I%20worked%20on%20the%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.citusdata.com%2Fdownload%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ECitus%20open%20source%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20team%20at%20Microsoft.%20My%20project%20was%20about%20incorporating%20correctness%20checks%20into%20our%20automated%20testing%20mechanisms%2C%20which%20led%20me%20to%20the%20CMU%20database%20talk%20on%20SQLancer.%20Keep%20reading%20to%20find%20out%20what%20excited%20us%20about%20SQLancer%2C%20how%20I%20developed%20an%20implementation%20of%20SQLancer%20that%20supports%20the%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fcitusdata%2Fcitus%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ECitus%20extension%20to%20Postgres%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%2C%20and%20the%20rewarding%20outcomes%20of%20my%20project.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--590368688%22%20id%3D%22toc-hId--590369366%22%3E%3CSPAN%3ESQLancer%E2%80%99s%20performance%20on%20Citus%20(%26amp%3B%20Postgres)%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3E%3CBR%20%2F%3ESQLancer%20proved%20itself%20useful%20for%20detecting%20logic%20bugs%20in%20Citus%20early%20on%20in%20my%20project%2C%20with%20the%20first%20error%20found%20within%20seconds%20of%20our%20initial%20SQLancer%20run%20after%20I%20had%20just%20finished%20implementing%20basic%20Citus%20support.%20And%20I%20found%20the%20first%20SQL%20logic%20bug%20within%20the%20first%20week.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%3EBy%20the%20end%20of%20my%20summer%20internship%20on%20the%20Postgres%20team%20at%20Microsoft%2C%20we%20had%20opened%20more%20than%2010%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fcitusdata%2Fcitus%2Fissues%3Fq%3Dis%253Aissue%2Blabel%253Asqlancer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Eissues%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20in%20the%20Citus%20GitHub%20page%20for%20errors%20found%20by%20SQLancer%2C%20at%20least%203%20of%20which%20qualify%20as%20SQL%20logic%20bugs.%20Moreover%2C%20I%20was%20also%20able%20to%20identify%20an%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fmessage-id%2FE1jvD5P-0007pL-Gx%2540gemulon.postgresql.org%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Eerror%20in%20PostgreSQL%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%2C%20which%20was%20then%20fixed%20by%20David%20Rowley%20and%20referenced%20by%20Thomas%20Munro%E2%80%94both%20of%20whom%20are%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fmicrosoft-azure-welcomes-postgresql-committers%2Fba-p%2F1207612%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3E%3CSPAN%3EPostgreSQL%20committers%20and%20part%20of%20the%20Postgres%20team%20at%20Microsoft%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2FMengTangmu%2Fstatus%2F1282909653530103813%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Thomas-Munro-tweet-about-SQLancer-and-Postgres.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F216525i7A7EE7ECBCA8E5FE%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Thomas-Munro-tweet-about-SQLancer-and-Postgres.png%22%20alt%3D%22https%3A%2F%2Ftwitter.com%2FMengTangmu%2Fstatus%2F1282909653530103813%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Ehttps%3A%2F%2Ftwitter.com%2FMengTangmu%2Fstatus%2F1282909653530103813%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWith%20the%20Citus%20implementation%20of%20SQLancer%2C%20we%20were%20able%20to%20test%20more%20than%203%20million%20SQL%20queries%20in%203%20days.%20While%20logic%20bugs%20in%20SQL%20databases%20are%20rare%2C%20the%20speed%20of%20the%20SQLancer%20tool%20and%20its%20broad%2C%20complex%20test%20space%20allowed%20us%20to%20detect%20logic%20errors%20that%20exist%20in%20the%20DBMS%20implementation%20(or%20that%20might%20be%20introduced%20in%20the%20future%20with%20the%20addition%20of%20new%20features).%20Whether%20part%20of%20Continuous%20Integration%20(CI)%20tests%20or%20part%20of%20regular%20lengthy%20background%20runs%2C%20incorporating%20SQLancer%20into%20the%20automated%20testing%20procedures%20for%20the%20Citus%20extension%20to%20PostgreSQL%20will%20significantly%20improve%20the%20reliability%20and%20stability%20of%20Citus%20for%20our%20users.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1897144145%22%20id%3D%22toc-hId-1897143467%22%3E%3CSPAN%3EA%20technical%20overview%20of%20SQLancer%E2%80%99s%20testing%20approach%3A%20Ternary%20Logic%20Partitioning%20(TLP)%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAmong%20three%20of%20the%20test%20oracles%20adopted%20by%20SQLancer%2C%20my%20project%20focused%20on%20the%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.manuelrigger.at%2Fpreprints%2FTLP.pdf%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ETernary%20Logic%20Partitioning%20(TLP)%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20approach%2C%20which%20can%20be%20used%20to%20test%20%3C%2FSPAN%3E%3CSPAN%3EWHERE%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3EGROUP%20BY%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3EHAVING%3C%2FSPAN%3E%3CSPAN%3E%20clauses%2C%20aggregate%20functions%2C%20and%20%3C%2FSPAN%3E%3CSPAN%3EDISTINCT%3C%2FSPAN%3E%3CSPAN%3E%20queries.%20TLP%20compares%20the%20result%20sets%20of%20two%20semantically%20equivalent%20yet%20syntactically%20different%20%3C%2FSPAN%3E%3CSPAN%3ESELECT%3C%2FSPAN%3E%3CSPAN%3E%20queries%20that%20are%20expected%20to%20be%20equal.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ELet%E2%80%99s%20make%20an%20apples%20analogy%3A%20Say%2C%20you%20want%20to%20%3C%2FSPAN%3E%E2%80%98%3CSTRONG%3E%3CI%3Efetch%20all%20apples%3C%2FI%3E%3C%2FSTRONG%3E%E2%80%99%3CSPAN%3E.%20You%20could%20also%20say%20that%20you%20want%20to%20%3C%2FSPAN%3E%E2%80%98%3CSTRONG%3E%3CI%3Efetch%20all%20apples%20that%20are%20red%3C%2FI%3E%3C%2FSTRONG%3E%2C%E2%80%99%26nbsp%3B%E2%80%98%3CSTRONG%3E%3CI%3Efetch%20all%20apples%20that%20are%20not%20red%3C%2FI%3E%3C%2FSTRONG%3E%2C%E2%80%99%3CSPAN%3E%26nbsp%3Bor%3C%2FSPAN%3E%20%E2%80%98%3CSTRONG%3E%3CI%3Efetch%20all%20apples%20where%20the%20color%20is%20unknown%3C%2FI%3E%3C%2FSTRONG%3E.%E2%80%99%3CSPAN%3E%26nbsp%3BBoth%20versions%20would%20be%20equivalent%E2%80%94the%20second%20one%20is%20only%20a%20more%20verbose%20way%20of%20saying%20the%20same%20thing%20by%20adding%20conditions%20related%20to%20%E2%80%9Credness%E2%80%9D%20and%20combining%20them%20in%20a%20way%20that%20makes%20the%20condition%20trivial.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Ternary-logic-partitioning-to-fetch-all-apples-Fig2.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F216515iC4E50B8D56014DC2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Ternary-logic-partitioning-to-fetch-all-apples-Fig2.png%22%20alt%3D%22Fig%202.%20Ternary%20partitioning%20of%20a%20predicate%2C%20hand-drawn%20by%20Nazli%20Ugur%20Koyluoglu%2C%20inspired%20by%20TLP%20paper%20by%20Rigger%20%26amp%3B%20Su%20(https%3A%2F%2Fwww.manuelrigger.at%2Fpreprints%2FTLP.pdf)%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFig%202.%20Ternary%20partitioning%20of%20a%20predicate%2C%20hand-drawn%20by%20Nazli%20Ugur%20Koyluoglu%2C%20inspired%20by%20TLP%20paper%20by%20Rigger%20%26amp%3B%20Su%20(%3CA%20href%3D%22https%3A%2F%2Fwww.manuelrigger.at%2Fpreprints%2FTLP.pdf%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.manuelrigger.at%2Fpreprints%2FTLP.pdf%3C%2FA%3E)%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EFor%20those%20of%20you%20who%20want%20to%20see%20the%20mathematical%20explanation%20for%20how%20TLP%20works%2C%20rather%20than%20rely%20on%20my%20apple%20analogy%2C%20here%20it%20goes%3A%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ELet%20%3C%2FSPAN%3E%3CSTRONG%3EQ%3C%2FSTRONG%3E%3CSPAN%3E%20be%20the%20original%20query.%20The%20semantically%20equivalent%20query%20%3C%2FSPAN%3E%3CSTRONG%3EQ%E2%80%99%3C%2FSTRONG%3E%3CSPAN%3E%20would%20be%20a%20%3C%2FSPAN%3E%3CSPAN%3EUNION%3C%2FSPAN%3E%3CSPAN%3E%20of%203%20queries%20that%20partition%20%3C%2FSPAN%3E%3CSTRONG%3EQ%3C%2FSTRONG%3E%3CSPAN%3E%20using%20ternary%20logic.%20These%203%20queries%20are%20generated%20by%20randomly%20generating%20a%20boolean%20predicate%20%3C%2FSPAN%3E%3CSTRONG%3E%CF%95%3C%2FSTRONG%3E%3CSPAN%3E%2C%20and%20appending%20a%20version%20of%20%3C%2FSPAN%3E%3CSTRONG%3E%CF%95%3C%2FSTRONG%3E%3CSPAN%3E%20evaluated%20to%20%3C%2FSPAN%3E%3CSPAN%3ETRUE%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3EFALSE%3C%2FSPAN%3E%3CSPAN%3E%2C%20and%20%3C%2FSPAN%3E%3CSPAN%3ENULL%3C%2FSPAN%3E%3CSPAN%3E%20to%20the%20end%20of%20%3C%2FSPAN%3E%3CSTRONG%3EQ%3C%2FSTRONG%3E%3CSPAN%3E%2C%20each%20in%20the%20form%20of%20a%20%3C%2FSPAN%3E%3CSPAN%3EWHERE%3C%2FSPAN%3E%3CSPAN%3E%20or%20%3C%2FSPAN%3E%3CSPAN%3EHAVING%3C%2FSPAN%3E%3CSPAN%3E%20clause.%20Since%20these%203%20boolean%20values%20cover%20the%20universal%20set%20of%20states%20associated%20with%20the%20predicate%2C%20their%20union%2C%20%3C%2FSPAN%3E%3CSTRONG%3EQ%E2%80%99%3C%2FSTRONG%3E%3CSPAN%3E%2C%20must%20return%20the%20same%20result%20set%20as%20that%20returned%20by%20the%20query%20without%20the%20predicate%2C%20%3C%2FSPAN%3E%3CSTRONG%3EQ%3C%2FSTRONG%3E%3CSPAN%3E%2C%20i.e.%20%3C%2FSPAN%3E%3CSTRONG%3EResultSet(Q)%20%3D%20ResultSet(Q%E2%80%99)%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22SQLancer-implementation-steps-for-Ternary-Logic_Partitioning-test-Fig3.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F216516i9741B4C0271D5D4E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22SQLancer-implementation-steps-for-Ternary-Logic_Partitioning-test-Fig3.png%22%20alt%3D%22Fig%203.%20Implementation%20steps%20for%20Ternary%20Logic%20Partitioning%2C%20inspired%20by%20Manuel%20Rigger%E2%80%99s%20SQLancer%20talk%20at%20the%20CMU%20Quarantine%202020%20Database%20talks%3A%20https%3A%2F%2Fyoutu.be%2F_LLHssTadKA%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFig%203.%20Implementation%20steps%20for%20Ternary%20Logic%20Partitioning%2C%20inspired%20by%20Manuel%20Rigger%E2%80%99s%20SQLancer%20talk%20at%20the%20CMU%20Quarantine%202020%20Database%20talks%3A%20%3CA%20href%3D%22https%3A%2F%2Fyoutu.be%2F_LLHssTadKA%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fyoutu.be%2F_LLHssTadKA%3C%2FA%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20steps%20for%20implementing%20the%20test%20oracle%20are%20as%20follows%3A%3C%2FSPAN%3E%3CSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CSPAN%3ERandomly%20generate%20the%20original%20query%20%3C%2FSPAN%3E%3CSTRONG%3EQ%3C%2FSTRONG%3E%3CSPAN%3E%2C%20in%20a%20similar%20fashion%20to%20random%20queries%20generated%20by%20fuzzers.%20To%20continue%20our%20apple%20analogy%2C%20%3C%2FSPAN%3E%3CSTRONG%3EQ%3C%2FSTRONG%3E%3CSPAN%3E%20would%20be%20%E2%80%98%3C%2FSPAN%3E%3CSTRONG%3E%3CI%3Efetch%20all%20apples%3C%2FI%3E%3C%2FSTRONG%3E.%3CSPAN%3E%E2%80%99%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3ERandomly%20generate%20the%20boolean%20predicate%20%3C%2FSPAN%3E%3CSTRONG%3E%CF%95%3C%2FSTRONG%3E%3CSPAN%3E.%20%3C%2FSPAN%3E%3CSTRONG%3E%CF%95%3C%2FSTRONG%3E%3CSPAN%3E%20corresponds%20to%20the%20condition%20in%20our%20analogy%2C%20%E2%80%98%3C%2FSPAN%3E%3CSTRONG%3E%3CEM%3Eto%20be%20red%3C%2FEM%3E%3C%2FSTRONG%3E%3CSPAN%3E.%E2%80%99%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3ECreate%20the%20semantically%20equivalent%20query%20%3C%2FSPAN%3E%3CSPAN%3E%3CSTRONG%3EQ%E2%80%99%3C%2FSTRONG%3E%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Eusing%20the%20Ternary%20Logic%20Partitioning%20(TLP)%20approach.%20In%20our%20analogy%2C%20%3C%2FSPAN%3E%3CSTRONG%3EQ%E2%80%99%3C%2FSTRONG%3E%3CSPAN%3E%20corresponds%20to%20the%20verbose%20version%20of%20the%20command%2C%20to%20%E2%80%98%3C%2FSPAN%3E%3CSTRONG%3E%3CEM%3Efetch%20apples%20that%20are%20red%3C%2FEM%3E%3C%2FSTRONG%3E%2C%3CSPAN%3E%E2%80%99%20%E2%80%98%3C%2FSPAN%3E%3CSTRONG%3E%3CEM%3Ethat%20are%20not%20red%3C%2FEM%3E%3C%2FSTRONG%3E%2C%E2%80%99%20or%20%E2%80%98%3CSTRONG%3E%3CEM%3Ewhere%20the%20color%20is%20unknown%3C%2FEM%3E%3C%2FSTRONG%3E%3CSPAN%3E.%E2%80%99%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EFetch%20and%20compare%20result%20sets%20for%20%3C%2FSPAN%3E%3CSTRONG%3EQ%3C%2FSTRONG%3E%3CSPAN%3E%20and%20%3C%2FSPAN%3E%3CSTRONG%3EQ%E2%80%99%3C%2FSTRONG%3E%3CSPAN%3E.%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EReport%20a%20logic%20bug%20if%20there%20is%20a%20mismatch.%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ETernary%20Logic%20Partitioning%20in%20SQLancer%20allows%20us%20to%20test%20a%20database%20like%20Postgres%20or%20Citus%20against%20itself%2C%20first%20by%20generating%20a%20(relatively)%20simple%20and%20a%20more%20complex%20version%20of%20a%20SQL%20query%2C%20and%20then%20by%20checking%20whether%20the%20added%20complexity%20introduces%20a%20logic%20bug.%20In%20other%20words%2C%20SQLancer%E2%80%99s%20TLP%20test%20oracle%20eliminates%20the%20need%20for%20a%20ground%20truth%20by%20designating%20the%20simpler%20SQL%20query%20as%20a%20heuristic%20for%20the%20expected%20output%20of%20the%20more%20complex%20query.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWhile%20there%E2%80%99s%20no%20guarantee%20that%20we%20can%20detect%20all%20logic%20bugs%20in%20a%20database%20like%20Postgres%20or%20Citus%20using%20query%20partitioning%2C%20SQLancer%E2%80%99s%20TLP%20has%20proven%20to%20be%20successful%20with%20the%20Citus%20extension%20to%20Postgres%20and%20with%20other%20databases%20as%20well.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-89689682%22%20id%3D%22toc-hId-89689004%22%3E%3CSPAN%3EWhy%20care%20about%20the%20logic%20bugs%20SQLancer%20finds%3F%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20might%20think%2C%20if%20SQLancer%20is%20testing%20super%20complex%20SQL%20queries%20that%20developers%20wouldn%E2%80%99t%20usually%20come%20up%20with%20(let%20alone%20users)%2C%20why%20are%20these%20SQL%20queries%20important%3F%20Good%20question.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWell%2C%20while%20the%20logic%20errors%20are%20revealed%20by%20complex%20machine-generated%20SQL%20queries%2C%20it%E2%80%99s%20possible%20that%20the%20logic%20bugs%20themselves%20might%20lie%20in%20integral%20operations%20that%20affect%20behavior%20generally.%20For%20the%20Citus%20extension%20to%20Postgres%20in%20particular%2C%20the%20source%20of%20logic%20bugs%20has%20mostly%20been%20the%20parsing%20and%20deparsing%20of%20SQL%20queries%20during%20pushdown%20to%20the%20Citus%20worker%20nodes.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAnother%20use%20case%20that%20highlights%20the%20usefulness%20of%20SQLancer%3A%20many%20of%20you%20probably%20use%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fblog.bitsrc.io%2Fwhat-is-an-orm-and-why-you-should-use-it-b2b6f75f5e2a%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Eobject-relational%20mappers%20(ORMs)%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20to%20generate%20SQL%20queries%20rather%20than%20manually%20writing%20them%2C%20which%20makes%20it%20more%20likely%20that%20your%20computer-generated%20queries%20might%20wander%20into%20the%20error-prone%20zone%20tested%20by%20SQLancer.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1717764781%22%20id%3D%22toc-hId--1717765459%22%3E%3CSPAN%3ELogic%20bug%20detection%20in%20Citus%2C%20at%20a%20glance%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ERunning%20SQLancer%20to%20find%20logic%20bugs%20in%20your%20SQL%20involves%202%20different%20phases%20of%20execution%3A%3C%2FSPAN%3E%3CSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CSPAN%3EThe%20preparation%20phase.%20SQLancer%20initializes%20the%20test%20database%20with%20Citus%20support%2C%20creates%20%3C%2FSPAN%3E%3CA%20href%3D%22http%3A%2F%2Fdocs.citusdata.com%2Fen%2Fv9.4%2Fget_started%2Fconcepts.html%3Fhighlight%3Dreference%2520tables%23table-types%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Elocal%2C%20distributed%2C%20and%20reference%20tables%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20inside%20the%20database%2C%20and%20performs%20insertions%2C%20updates%2C%20deletes%2C%20and%20other%20modifications%20to%20the%20tables%20via%20the%20randomly%20generated%20SQL%20commands.%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EThe%20testing%20phase.%20SQLancer%20generates%2C%20executes%2C%20and%20compares%20the%20outputs%20of%20pairs%20of%20original%20and%20partitioned%20SELECT%20queries%2C%20in%20line%20with%20the%20TLP%20approach%E2%80%94until%20it%20finds%20a%20mismatch%2C%20i.e.%20logic%20bug.%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Pair-of-original-and-partitioned-queries-generated-by-SQLancer-for-Citus-Fig4.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F216517i3CC10F8474AFA8D8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Pair-of-original-and-partitioned-queries-generated-by-SQLancer-for-Citus-Fig4.png%22%20alt%3D%22Fig.%204%3A%20A%20sample%20pair%20of%20original%20and%20partitioned%20queries%20generated%20by%20the%20Citus%20implementation%20of%20SQLancer.%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFig.%204%3A%20A%20sample%20pair%20of%20original%20and%20partitioned%20queries%20generated%20by%20the%20Citus%20implementation%20of%20SQLancer.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20Figure%204%20above%2C%20you%20can%20see%20the%20original%20SQL%20query%20and%20the%20partitioned%20query%E2%80%94a%20union%20of%20the%203%20components%20with%20the%20predicates%20evaluated%20to%20%3C%2FSPAN%3E%3CSPAN%3ETRUE%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3EFALSE%3C%2FSPAN%3E%3CSPAN%3E%2C%20and%20%3C%2FSPAN%3E%3CSPAN%3ENULL%3C%2FSPAN%3E%3CSPAN%3E%2C%20respectively%E2%80%94that%20failed%20to%20return%20identical%20result%20sets.%20As%20it%20turned%20out%2C%20Citus%20had%20a%20bug%20in%20its%20handling%20of%20explicit%20%3C%2FSPAN%3E%3CSPAN%3ECROSS%20JOIN%20%3C%2FSPAN%3E%3CSPAN%3Esyntax%20when%20used%20in%20a%20complex%20join%20tree.%20Since%20cross%20joins%20are%20more%20commonly%20used%20in%20the%20implicit%20form%20(%3C%2FSPAN%3E%3CSPAN%3EFROM%20t1%2C%20t4%3C%2FSPAN%3E%3CSPAN%3E)%2C%20this%20type%20of%20join%20tree%20had%20not%20come%20up%20in%20manual%20testing.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-769748052%22%20id%3D%22toc-hId-769747374%22%3E%3CSPAN%3EHow%20I%20implemented%20SQLancer%20for%20the%20Citus%20extension%20to%20Postgres%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EI%20created%20the%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fsqlancer%2Fsqlancer%2Ftree%2Fmaster%2Fsrc%2Fsqlancer%2Fcitus%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ECitus%20implementation%20of%20SQLancer%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20by%20reusing%20and%20extending%20components%20of%20the%20existing%20SQLancer%20support%20for%20PostgreSQL.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CBR%20%2F%3EProgramming%20a%20SQLancer%20implementation%20for%20Citus%20involved%20incorporating%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fdatabase-sharding-explained-in-plain-english%2Fba-p%2F868774%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Esharding%3C%2FA%3E%20across%20multiple%20nodes%20in%20the%20creation%20of%20test%20databases%20by%20SQLancer%2C%20expanding%20the%20test%20space%20by%20integrating%20%3CA%20href%3D%22https%3A%2F%2Fdocs.citusdata.com%2Fen%2Fstable%2Fget_started%2Fconcepts.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECitus%20concepts%20and%20features%3C%2FA%3E%20such%20as%20distributed%20and%20reference%20tables%2C%20colocation%2C%20distribution%20key%20etc.%20into%20the%20database%20preparation%20phase%2C%20configuring%20the%20different%20types%20of%20JOINs%20supported%20by%20Citus%20in%20the%20SELECT%20statements%20generated%20during%20the%20testing%20phase%2C%20and%20adjusting%20the%20expected%20behavior%20to%20reflect%20Citus%E2%80%99%20SQL%20coverage.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2FMengTangmu%2Fstatus%2F1282909653530103813%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22AzureDBPostgres-tweet-about-1000th-commit-to-SQLancer.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F216528i8FF55C65FBCA29AC%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22AzureDBPostgres-tweet-about-1000th-commit-to-SQLancer.png%22%20alt%3D%22https%3A%2F%2Ftwitter.com%2FAzureDBPostgres%2Fstatus%2F1293663559801438208%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Ehttps%3A%2F%2Ftwitter.com%2FAzureDBPostgres%2Fstatus%2F1293663559801438208%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EA%20potential%20positive%20side%20effect%20of%20my%20project%20to%20the%20Postgres%20community%3A%20Building%20Citus%20support%20into%20SQLancer%20on%20top%20of%20the%20existing%20Postgres%20support%20required%20reworking%20the%20Postgres%20support%20to%20prepare%20its%20Java%20classes%20to%20be%20extended%20by%20new%20implementations.%20The%20improvements%20and%20modifications%20I%20contributed%20to%20the%20Postgres%20implementation%20of%20SQLancer%20have%20thus%20%3C%2FSPAN%3E%3CSTRONG%3Epaved%20the%20way%20for%20future%20SQLancer%20applications%20for%20other%20PostgreSQL%20extensions%3C%2FSTRONG%3E%3CSPAN%3E%2C%20as%20well.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1037706411%22%20id%3D%22toc-hId--1037707089%22%3E%3CSPAN%3ESpecial%20Thanks%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EI%20had%20an%20incredible%20experience%20working%20on%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fcitusdata%2Fcitus%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECitus%20extension%20to%20Postgres%3C%2FA%3E%20during%20my%20summer%20internship%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fservices%2Fpostgresql%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPostgres%20team%20at%20Microsoft%3C%2FA%3E.%20I%20had%20the%20chance%20to%20join%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.citusdata.com%2Fdownload%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECitus%20open%20source%3C%2FA%3E%20project%20and%20work%20with%20some%20pretty%20amazing%20people.%20I%20would%20like%20to%20thank%20my%20mentor%2C%20Nils%20Dijk%2C%20for%20his%20commitment%20to%20my%20growth%2C%20especially%20in%20a%20remote%20setting%20with%20limitations%20imposed%20by%20COVID-19.%20I%20would%20like%20to%20thank%20my%20manager%2C%20Utku%20Azman%2C%20for%20his%20constant%20encouragement%20to%20take%20initiative.%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThank%20you%20to%20Onder%20Kalaci%20and%20Marco%20Slot%2C%20whose%20contributions%20to%20the%20vision%20for%20our%20implementation%20of%20SQLancer%20helped%20tailor%20my%20project%20to%20make%20my%20work%20even%20more%20useful%20for%20the%20Citus%20team.%20%3C%2FSPAN%3E%3CSPAN%3EAnd%20I%20want%20to%20thank%20the%20Citus%20team%20as%20a%20whole%20for%20welcoming%20me%20with%20their%20unsparing%20help.%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESpecial%20thanks%20to%20Manuel%20Rigger%2C%20the%20creator%20of%20%3CA%20href%3D%22http%3A%2F%2Fwww.sqlancer.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESQLancer%3C%2FA%3E%2C%20for%20sharing%20our%20excitement%20about%20the%20development%20of%20a%20SQLancer%20implementation%20for%20Citus%20and%20his%20willingness%20to%20collaborate.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fsqlancer_dbms%2Fstatus%2F1293277358783434752%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22sqlancer_dbms-tweet-about-Nazli-SQLancer-contribution-for-Citus.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F216531i27C62E8F34F7DDB8%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22sqlancer_dbms-tweet-about-Nazli-SQLancer-contribution-for-Citus.png%22%20alt%3D%22https%3A%2F%2Ftwitter.com%2Fsqlancer_dbms%2Fstatus%2F1293277358783434752%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Ehttps%3A%2F%2Ftwitter.com%2Fsqlancer_dbms%2Fstatus%2F1293277358783434752%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fsqlancer_dbms%2Fstatus%2F1293178256108081152%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22sqlancer_dbms-tweet-about-Citus-use-of-SQLancer.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F216532i1F0BF8F252AE9BCD%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22sqlancer_dbms-tweet-about-Citus-use-of-SQLancer.png%22%20alt%3D%22https%3A%2F%2Ftwitter.com%2Fsqlancer_dbms%2Fstatus%2F1293178256108081152%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Ehttps%3A%2F%2Ftwitter.com%2Fsqlancer_dbms%2Fstatus%2F1293178256108081152%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1634393%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Citus-SQLancer-Postgres-Blog-with-three-apples-to-represent-Ternary-logic-partitioning.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F216513iB875708B050DD207%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Citus-SQLancer-Postgres-Blog-with-three-apples-to-represent-Ternary-logic-partitioning.png%22%20alt%3D%22Citus-SQLancer-Postgres-Blog-with-three-apples-to-represent-Ternary-logic-partitioning.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EStory%20of%20how%20to%20use%20newly-released%20SQLancer%20to%20hunt%20for%20logic%20errors%20in%20databases%20using%20Ternary%20Logic%20Partitioning.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1634393%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Database%20for%20PostgreSQL%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ECitus%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgres%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Regular Visitor

Many of you rely on databases to return correct results for your SQL queries, however complex your queries might be. And you probably place your trust with no questions asked—since you know relational databases are built on top of proven mathematical foundations, and since there is no practical way to manually verify your SQL query output anyway. 

 

Since it is possible that a database's implementation of the SQL logic could have a few errors, database developers apply extensive testing methods to avoid such flaws. For instance, the Citus open source repo on GitHub has more than twice as many lines related to automated testing than lines of database code. However, checking correctness for all possible SQL queries is challenging because of the lack of a “ground truth” to compare their outputs against, and the infinite number of possible SQL queries. 

 

Even when we do know the result a SQL query is supposed to give and we get that result as expected, that does not mean the implementation is 100% correct in all possible variants of the query and input data. The challenge for a database team is in finding subtle edge cases where bugs might be introduced.

 

What are logic bugs in SQL, and why are they hard to detect?


There are 2 primary types of errors in databases:

  • “raised” errors
  • logic errors 

 

“Raised” errors include syntax errors, panics, faults, and other crashes. Raised errors terminate the process abnormally or give you some signal of undesired behavior, making them immediately recognizable to you. In order to automatically detect (and then fix) raised errors, database providers widely use fuzzing tools for SQL. Fuzzing tools are random SQL query generators that strain the target database with complex commands until an error is raised.


Logic errors, on the other hand, are latent (or silent) errors that cause the database to produce inaccurate results without emitting any sign of unexpected behavior. Logic bugs are dangerous—neither the user, nor the database provider, nor the fuzzing tools might be aware of incorrect results being fetched from the database.

 

Fig. 1: “Raised” errors vs. logic errorsFig. 1: “Raised” errors vs. logic errors

 

What if we could find a way to test the validity of SQL query output for a DBMS?

 

The recently launched open source SQLancer (Synthesized Query Lancer) tool gives you a way to test the validity of a database’s query responses. SQLancer is an automated Database Management System (DBMS) testing tool for detecting logic bugs. SQLancer’s testing approaches substitute the need for a ground truth in their correctness checks, by probing databases for self-consistency rather than agreement with some known behavior. 

 

While SQLancer is still in the research prototype stage, it was released early on due to high demand from different companies, organizations, and developers. I first learned about SQLancer in June 2020 through a Quarantine 2020 Database Talk organized by Carnegie Mellon Professor of Databases Andy Pavlo.

The CMU talk by SQLancer creator Manuel Rigger was about Finding Logic Bugs in Database Management Systems. At the time of Manuel Rigger’s talk, SQLancer had already identified more than 175 bugs in 5 different SQL database management systems by using Ternary Logic Partitioning (TLP). Hence, Ternary Logic Partitioning became the SQLancer testing approach that was of most interest to our team at Citus.

 

If you are not familiar, Citus is an open source extension to Postgres that transforms Postgres into a distributed database. Since the Citus team was acquired by Microsoft last year, Citus is now available on Microsoft Azure as Hyperscale (Citus), a built-in deployment option on Azure Database for PostgreSQL

 

During my software engineering internship the summer after my second year as an undergraduate at Stanford, I worked on the Citus open source team at Microsoft. My project was about incorporating correctness checks into our automated testing mechanisms, which led me to the CMU database talk on SQLancer. Keep reading to find out what excited us about SQLancer, how I developed an implementation of SQLancer that supports the Citus extension to Postgres, and the rewarding outcomes of my project.

 

SQLancer’s performance on Citus (& Postgres)


SQLancer proved itself useful for detecting logic bugs in Citus early on in my project, with the first error found within seconds of our initial SQLancer run after I had just finished implementing basic Citus support. And I found the first SQL logic bug within the first week.


By the end of my summer internship on the Postgres team at Microsoft, we had opened more than 10 issues in the Citus GitHub page for errors found by SQLancer, at least 3 of which qualify as SQL logic bugs. Moreover, I was also able to identify an error in PostgreSQL, which was then fixed by David Rowley and referenced by Thomas Munro—both of whom are PostgreSQL committers and part of the Postgres team at Microsoft.

 

https://twitter.com/MengTangmu/status/1282909653530103813https://twitter.com/MengTangmu/status/1282909653530103813

With the Citus implementation of SQLancer, we were able to test more than 3 million SQL queries in 3 days. While logic bugs in SQL databases are rare, the speed of the SQLancer tool and its broad, complex test space allowed us to detect logic errors that exist in the DBMS implementation (or that might be introduced in the future with the addition of new features). Whether part of Continuous Integration (CI) tests or part of regular lengthy background runs, incorporating SQLancer into the automated testing procedures for the Citus extension to PostgreSQL will significantly improve the reliability and stability of Citus for our users. 

 

A technical overview of SQLancer’s testing approach: Ternary Logic Partitioning (TLP)

 

Among three of the test oracles adopted by SQLancer, my project focused on the Ternary Logic Partitioning (TLP) approach, which can be used to test WHERE, GROUP BY, HAVING clauses, aggregate functions, and DISTINCT queries. TLP compares the result sets of two semantically equivalent yet syntactically different SELECT queries that are expected to be equal.

 

Let’s make an apples analogy: Say, you want to fetch all apples. You could also say that you want to fetch all apples that are red,’ ‘fetch all apples that are not red,’ orfetch all apples where the color is unknown.’ Both versions would be equivalent—the second one is only a more verbose way of saying the same thing by adding conditions related to “redness” and combining them in a way that makes the condition trivial.

 

Fig 2. Ternary partitioning of a predicate, hand-drawn by Nazli Ugur Koyluoglu, inspired by TLP paper by Rigger & Su (https://www.manuelrigger.at/preprints/TLP.pdf)Fig 2. Ternary partitioning of a predicate, hand-drawn by Nazli Ugur Koyluoglu, inspired by TLP paper by Rigger & Su (https://www.manuelrigger.at/preprints/TLP.pdf)

 

For those of you who want to see the mathematical explanation for how TLP works, rather than rely on my apple analogy, here it goes: 

 

Let Q be the original query. The semantically equivalent query Q’ would be a UNION of 3 queries that partition Q using ternary logic. These 3 queries are generated by randomly generating a boolean predicate ϕ, and appending a version of ϕ evaluated to TRUE, FALSE, and NULL to the end of Q, each in the form of a WHERE or HAVING clause. Since these 3 boolean values cover the universal set of states associated with the predicate, their union, Q’, must return the same result set as that returned by the query without the predicate, Q, i.e. ResultSet(Q) = ResultSet(Q’).

 

Fig 3. Implementation steps for Ternary Logic Partitioning, inspired by Manuel Rigger’s SQLancer talk at the CMU Quarantine 2020 Database talks: https://youtu.be/_LLHssTadKAFig 3. Implementation steps for Ternary Logic Partitioning, inspired by Manuel Rigger’s SQLancer talk at the CMU Quarantine 2020 Database talks: https://youtu.be/_LLHssTadKA

 

The steps for implementing the test oracle are as follows:

  1. Randomly generate the original query Q, in a similar fashion to random queries generated by fuzzers. To continue our apple analogy, Q would be ‘fetch all apples.
  2. Randomly generate the boolean predicate ϕ. ϕ corresponds to the condition in our analogy, ‘to be red.’
  3. Create the semantically equivalent query Q’ using the Ternary Logic Partitioning (TLP) approach. In our analogy, Q’ corresponds to the verbose version of the command, to ‘fetch apples that are red,’ ‘that are not red,’ or ‘where the color is unknown.’
  4. Fetch and compare result sets for Q and Q’
  5. Report a logic bug if there is a mismatch. 

 

Ternary Logic Partitioning in SQLancer allows us to test a database like Postgres or Citus against itself, first by generating a (relatively) simple and a more complex version of a SQL query, and then by checking whether the added complexity introduces a logic bug. In other words, SQLancer’s TLP test oracle eliminates the need for a ground truth by designating the simpler SQL query as a heuristic for the expected output of the more complex query. 

 

While there’s no guarantee that we can detect all logic bugs in a database like Postgres or Citus using query partitioning, SQLancer’s TLP has proven to be successful with the Citus extension to Postgres and with other databases as well. 

 

Why care about the logic bugs SQLancer finds?

 

You might think, if SQLancer is testing super complex SQL queries that developers wouldn’t usually come up with (let alone users), why are these SQL queries important? Good question.

 

Well, while the logic errors are revealed by complex machine-generated SQL queries, it’s possible that the logic bugs themselves might lie in integral operations that affect behavior generally. For the Citus extension to Postgres in particular, the source of logic bugs has mostly been the parsing and deparsing of SQL queries during pushdown to the Citus worker nodes. 

 

Another use case that highlights the usefulness of SQLancer: many of you probably use object-relational mappers (ORMs) to generate SQL queries rather than manually writing them, which makes it more likely that your computer-generated queries might wander into the error-prone zone tested by SQLancer. 

 

Logic bug detection in Citus, at a glance

 

Running SQLancer to find logic bugs in your SQL involves 2 different phases of execution:

  1. The preparation phase. SQLancer initializes the test database with Citus support, creates local, distributed, and reference tables inside the database, and performs insertions, updates, deletes, and other modifications to the tables via the randomly generated SQL commands.
  2. The testing phase. SQLancer generates, executes, and compares the outputs of pairs of original and partitioned SELECT queries, in line with the TLP approach—until it finds a mismatch, i.e. logic bug.

 

Fig. 4: A sample pair of original and partitioned queries generated by the Citus implementation of SQLancer.Fig. 4: A sample pair of original and partitioned queries generated by the Citus implementation of SQLancer.

 

In Figure 4 above, you can see the original SQL query and the partitioned query—a union of the 3 components with the predicates evaluated to TRUE, FALSE, and NULL, respectively—that failed to return identical result sets. As it turned out, Citus had a bug in its handling of explicit CROSS JOIN syntax when used in a complex join tree. Since cross joins are more commonly used in the implicit form (FROM t1, t4), this type of join tree had not come up in manual testing.

 

How I implemented SQLancer for the Citus extension to Postgres

 

I created the Citus implementation of SQLancer by reusing and extending components of the existing SQLancer support for PostgreSQL. 


Programming a SQLancer implementation for Citus involved incorporating sharding across multiple nodes in the creation of test databases by SQLancer, expanding the test space by integrating Citus concepts and features such as distributed and reference tables, colocation, distribution key etc. into the database preparation phase, configuring the different types of JOINs supported by Citus in the SELECT statements generated during the testing phase, and adjusting the expected behavior to reflect Citus’ SQL coverage.

 

https://twitter.com/AzureDBPostgres/status/1293663559801438208https://twitter.com/AzureDBPostgres/status/1293663559801438208

A potential positive side effect of my project to the Postgres community: Building Citus support into SQLancer on top of the existing Postgres support required reworking the Postgres support to prepare its Java classes to be extended by new implementations. The improvements and modifications I contributed to the Postgres implementation of SQLancer have thus paved the way for future SQLancer applications for other PostgreSQL extensions, as well.

 

Special Thanks

 

I had an incredible experience working on the Citus extension to Postgres during my summer internship in the Postgres team at Microsoft. I had the chance to join the Citus open source project and work with some pretty amazing people. I would like to thank my mentor, Nils Dijk, for his commitment to my growth, especially in a remote setting with limitations imposed by COVID-19. I would like to thank my manager, Utku Azman, for his constant encouragement to take initiative.

 

Thank you to Onder Kalaci and Marco Slot, whose contributions to the vision for our implementation of SQLancer helped tailor my project to make my work even more useful for the Citus team. And I want to thank the Citus team as a whole for welcoming me with their unsparing help.

 

Special thanks to Manuel Rigger, the creator of SQLancer, for sharing our excitement about the development of a SQLancer implementation for Citus and his willingness to collaborate.

 

https://twitter.com/sqlancer_dbms/status/1293277358783434752https://twitter.com/sqlancer_dbms/status/1293277358783434752

https://twitter.com/sqlancer_dbms/status/1293178256108081152https://twitter.com/sqlancer_dbms/status/1293178256108081152