Skip to content

Optimize conditional uniqueness rules to partial unique indexes#316

Open
thgreasi wants to merge 1 commit into
masterfrom
autogen-partial-unique-indexes
Open

Optimize conditional uniqueness rules to partial unique indexes#316
thgreasi wants to merge 1 commit into
masterfrom
autogen-partial-unique-indexes

Conversation

@thgreasi
Copy link
Copy Markdown
Contributor

@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch 2 times, most recently from 323d546 to 2ea06f7 Compare January 27, 2026 15:21
@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch 3 times, most recently from 90c06d3 to 071fcd6 Compare February 1, 2026 22:29
thgreasi added a commit to balena-io/pinejs that referenced this pull request Feb 2, 2026
thgreasi added a commit to balena-io/pinejs that referenced this pull request Feb 5, 2026
thgreasi added a commit to balena-io/pinejs that referenced this pull request Feb 5, 2026
thgreasi added a commit to balena-io/pinejs that referenced this pull request Feb 5, 2026
@thgreasi thgreasi requested a review from a team February 5, 2026 10:19
thgreasi added a commit to balena-io/pinejs that referenced this pull request Feb 5, 2026
thgreasi added a commit to balena-io/pinejs that referenced this pull request Feb 5, 2026
@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch from 071fcd6 to da3da0e Compare February 11, 2026 08:11
@thgreasi
Copy link
Copy Markdown
Contributor Author

Removed es-toolkit

Comment thread src/abstract-sql-schema-optimizer.ts Outdated
Comment on lines +136 to +137
booleanNode[1][0] === 'ReferencedField' &&
booleanNode[2][0] === 'ReferencedField';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
booleanNode[1][0] === 'ReferencedField' &&
booleanNode[2][0] === 'ReferencedField';
isReferencedFieldNode(booleanNode[1]) &&
isReferencedFieldNode(booleanNode[2]);

Comment thread src/abstract-sql-schema-optimizer.ts Outdated
if (fromNode[1][0] !== 'Alias') {
return;
}
if (fromNode[1][1][0] !== 'Table') {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (fromNode[1][1][0] !== 'Table') {
if (!isTableNode(fromNode[1][1])) {

Comment thread src/abstract-sql-schema-optimizer.ts Outdated
};

function extractTableAndAlias(fromNode: FromNode) {
if (fromNode[1][0] !== 'Alias') {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (fromNode[1][0] !== 'Alias') {
if (!isAliasNode(fromNode[1])) {

@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch 3 times, most recently from 11d170e to a1e7407 Compare February 17, 2026 09:50
@thgreasi thgreasi requested a review from Page- February 17, 2026 22:13
Comment thread src/abstract-sql-schema-optimizer.ts Outdated
) {
const outerSelectQuery = ruleBody[1];
// Check if the query is of one of the following forms:
const queryChecksAbsenseOfRows =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
const queryChecksAbsenseOfRows =
const queryChecksAbsenceOfRows =

Comment thread src/abstract-sql-schema-optimizer.ts Outdated
* ) >= 2
* ) = 0 AS "result";
*/
function convertRuleToPartialUniqueIndex(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think at this point it's probably worth creating a separate file to host individual optimizations (eg schema-optimizations/partial-unique-index.ts) with a shared code folder as well, since reviewing this PR has been very tricky and that's whilst being able to see the things related to this optimization in isolation, looking at it later when it's mixed in with other optimizations would make it even even harder which I don't want to imagine trying

@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch from a1e7407 to fd2e2f9 Compare February 26, 2026 13:20
@thgreasi thgreasi marked this pull request as ready for review February 27, 2026 14:13
@thgreasi thgreasi marked this pull request as draft February 27, 2026 14:13
@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch from fd2e2f9 to 9b9590f Compare February 27, 2026 16:44
@thgreasi thgreasi requested a review from Page- February 27, 2026 16:53
@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch from 9b9590f to 35eaa60 Compare February 27, 2026 16:53
Comment thread src/schema-optimizations/utils.ts Outdated
return result;
}

export const convertReferencedFieldsToFields = (nodes: AbstractSqlType[]) => {
Copy link
Copy Markdown
Contributor Author

@thgreasi thgreasi Feb 27, 2026

Choose a reason for hiding this comment

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

Moved as is from src/abstract-sql-schema-optimizer.ts

Copy link
Copy Markdown
Contributor Author

@thgreasi thgreasi Feb 27, 2026

Choose a reason for hiding this comment

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

Extracted mostly as is from src/abstract-sql-schema-optimizer.ts

@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch 2 times, most recently from 0093b33 to 524e13e Compare February 28, 2026 11:36
@thgreasi thgreasi marked this pull request as ready for review March 2, 2026 13:35
Comment thread src/schema-optimizations/utils.ts Outdated
return result;
}

export const convertReferencedFieldsToFields = (nodes: AbstractSqlType[]) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seeing this now makes me realize it should probably accept a table name to strip explicit references for, as a safety measure in case the ReferencedField is referencing something we don't expect

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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


// Find on the outer FROM the table matching the one in the inner FROM
const tableWithUniquenessCheck = nestedFromInfo.tableName;
const [targetTableInfos, parentTableInfos] = partition(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can this/other cases like this become

Suggested change
const [targetTableInfos, parentTableInfos] = partition(
const { true: targetTableInfos, false: parentTableInfos } = groupBy(

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's how I had it initially, but at the end a didn't like how it looked in the already complex code and wanted to automatically do the ?? [], so that we can have simpler checks from that point onward.
But that's pretty much how I implemented partition() it in utils.ts.
If you insist I can inline it, but I prefer not to, to keep the sources simpler.

Copy link
Copy Markdown
Contributor Author

@thgreasi thgreasi Mar 11, 2026

Choose a reason for hiding this comment

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

Oh, more importantly!!!
Having a dedicated partition allowed me to use any return type assertions that the provided fn does, to automatically narrow down the typing of the true group.

export function partition<T, U extends T>(
	entries: T[],
	iteratee: (item: T) => item is U,
): [U[], T[]];

Comment on lines +64 to +82
const result: BooleanTypeNodes[] = [];
for (const n of nodes) {
if (Array.isArray(n) && n[0] === 'Exists' && Array.isArray(n[1])) {
const maybeFieldTypeNode = n[1];
const fieldName =
isReferencedFieldNode(maybeFieldTypeNode) &&
maybeFieldTypeNode[1] === resourceAlias
? maybeFieldTypeNode[2]
: undefined;
if (
typeof fieldName === 'string' &&
fieldsByFieldName[fieldName]?.required === true
) {
continue;
}
}
result.push(n);
}
return result;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe I'm missing something but I think this can be:

Suggested change
const result: BooleanTypeNodes[] = [];
for (const n of nodes) {
if (Array.isArray(n) && n[0] === 'Exists' && Array.isArray(n[1])) {
const maybeFieldTypeNode = n[1];
const fieldName =
isReferencedFieldNode(maybeFieldTypeNode) &&
maybeFieldTypeNode[1] === resourceAlias
? maybeFieldTypeNode[2]
: undefined;
if (
typeof fieldName === 'string' &&
fieldsByFieldName[fieldName]?.required === true
) {
continue;
}
}
result.push(n);
}
return result;
return nodes.map((n) => {
if (!Array.isArray(n) && n[0] === 'Exists' && Array.isArray(n[1])) {
const maybeFieldTypeNode = n[1];
const fieldName =
isReferencedFieldNode(maybeFieldTypeNode) &&
maybeFieldTypeNode[1] === resourceAlias
? maybeFieldTypeNode[2]
: undefined;
if (
typeof fieldName === 'string' &&
fieldsByFieldName[fieldName]?.required === true
) {
return false;
}
}
return true;
});

(with further simplification via changing where the returns are, but I left it as similar to current as possible for the example)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I guess you mean .filter() instead of .map() and you also accidentally added a ! in from of the Array.isArray(n), right?
Yes it seems so 👍

@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch from 524e13e to a07b7f5 Compare March 11, 2026 15:59
@flowzone-app flowzone-app Bot enabled auto-merge March 11, 2026 16:03
@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch from a07b7f5 to 02a6692 Compare March 17, 2026 15:44
@thgreasi thgreasi requested a review from Page- March 31, 2026 06:32
@thgreasi thgreasi force-pushed the autogen-partial-unique-indexes branch from 02a6692 to 116cc78 Compare May 3, 2026 12:44
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.

2 participants