Skip to content

Conversation

@alpass163
Copy link
Contributor

This PR implements the EXCEPT DISTINCT and EXCEPT ALL operators. This is achieved by introducing two new optimizer rules that translate the ExceptNode into a more fundamental execution plan:

(1)ImplementExceptDistinctAsUnion (EXCEPT DISTINCT)

This rule translates EXCEPT DISTINCT into a Union + Aggregation + Filter plan.

It utilizes SetOperationNodeTranslator to append marker columns, then uses Aggregation (GROUP BY) to count the occurrences of each row in each sub-relation.

Finally, a FilterNode selects rows that appear in the first relation (count1 >= 1) but not in any of the subsequent relations (count2...n == 0).

(2)ImplementExceptAllAsUnion (EXCEPT ALL)

This rule translates EXCEPT ALL into a Union + Window + Filter plan.

It uses a Window function to compute the exact occurrence count for each row from each sub-relation (count1, count2...) along with a ROW_NUMBER().

A FilterNode then applies the expression row_number <= greatest(greatest(count1 - count2, 0) - count3...) to precisely implement the bag semantics subtraction required by EXCEPT ALL.

The SetOperationNodeTranslator class contains the core, shared logic used by both translation rules.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements the SQL EXCEPT operator with both DISTINCT and ALL variants for the table model. The implementation follows the same pattern as the existing INTERSECT operator, translating EXCEPT operations into lower-level execution plans using unions, aggregations, and window functions.

Key Changes:

  • Introduces ExceptNode as a new logical plan node that extends SetOperationNode
  • Adds two optimizer rules: ImplementExceptDistinctAsUnion (translates EXCEPT DISTINCT using Union + Aggregation + Filter) and ImplementExceptAll (translates EXCEPT ALL using Union + Window + Filter)
  • Extends existing infrastructure to support ExceptNode (visitors, pattern matchers, optimizers)

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
ExceptNode.java New logical plan node for EXCEPT operations, mirrors IntersectNode structure
ImplementExceptDistinctAsUnion.java Optimizer rule that translates EXCEPT DISTINCT to Union + Aggregation + Filter plan
ImplementExceptAll.java Optimizer rule that translates EXCEPT ALL to Union + Window + Filter plan
SetOperationNodeTranslator.java Updated comment to reflect usage by both INTERSECT and EXCEPT operations
LogicalOptimizeFactory.java Registers the new EXCEPT optimizer rules alongside existing set operation rules
UnaliasSymbolReferences.java Adds visitExcept method to handle symbol aliasing for ExceptNode
Patterns.java Adds pattern matching support for ExceptNode and removes old commented code
RelationPlanner.java Implements visitExcept to create ExceptNode during query planning, removes placeholder exception
PlanVisitor.java Adds visitExcept visitor method for ExceptNode traversal
PlanGraphPrinter.java Adds visitExcept for plan visualization
PlanNodeType.java Registers TABLE_EXCEPT_NODE type (1036) for serialization infrastructure
PruneDistinctAggregation.java Extends optimization to handle EXCEPT DISTINCT nodes
ImplementIntersectDistinctAsUnion.java Updates GenericLiteral creation to use LongType.INT64.getDisplayName()
TestMetadata.java Updates SUBTRACT operator handling to use SubtractionResolver for proper type resolution
ExceptTest.java Unit tests verifying logical plan structure for EXCEPT operations
IoTDBExceptTableIT.java Integration tests covering EXCEPT DISTINCT, EXCEPT ALL, type compatibility, and error cases

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

@JackieTien97 JackieTien97 merged commit 85b8f16 into apache:master Nov 28, 2025
27 of 28 checks passed
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