Skip to content

feat: Support Union type in approx_distinct#23714

Open
mkleen wants to merge 2 commits into
apache:mainfrom
mkleen:union_approx_distinct
Open

feat: Support Union type in approx_distinct#23714
mkleen wants to merge 2 commits into
apache:mainfrom
mkleen:union_approx_distinct

Conversation

@mkleen

@mkleen mkleen commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

  • Support the Arrow type Union for approx_distinct

What changes are included in this PR?

  • Enable HLLAccumulator and HllGroupsAccumulator to support Union
  • Tests for the non-grouped and grouped path as part of approx_distinct.rs

Are these changes tested?

Yes

Are there any user-facing changes?

Yes, approx_distinct supports now Union but no breaking changes.

@github-actions github-actions Bot added the functions Changes to functions implementation label Jul 20, 2026
@mkleen
mkleen marked this pull request as ready for review July 20, 2026 09:18
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.69%. Comparing base (3b1ce16) to head (9096c52).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #23714   +/-   ##
=======================================
  Coverage   80.69%   80.69%           
=======================================
  Files        1088     1088           
  Lines      367766   367813   +47     
  Branches   367766   367813   +47     
=======================================
+ Hits       296781   296820   +39     
- Misses      53276    53279    +3     
- Partials    17709    17714    +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

}

#[test]
fn union_support_hll_acc_and_group_acc() {

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.

not possible to do via SLT?

@mkleen mkleen Jul 20, 2026

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.

It's tricky via SLT. There is no SQL union type but only union operations in datafusion.

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.

what if we add more rows here:

fn register_union_table(ctx: &SessionContext) {
let union = UnionArray::try_new(
UnionFields::try_new(
// typeids: 3 for int, 1 for string
vec![3, 1],
vec![
Field::new("int", DataType::Int32, false),
Field::new("string", DataType::Utf8, false),
],
)
.unwrap(),
ScalarBuffer::from(vec![3, 1, 3]),
None,
vec![
Arc::new(Int32Array::from(vec![1, 2, 3])),
Arc::new(StringArray::from(vec![
Some("foo"),
Some("bar"),
Some("baz"),
])),
],
)
.unwrap();
let schema = Schema::new(vec![Field::new(
"union_column",
union.data_type().clone(),
false,
)]);
let batch =
RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(union)]).unwrap();
ctx.register_batch("union_table", batch).unwrap();
}

then can use in slts like here

# Note: union_table is registered via Rust code in the sqllogictest test harness
# because there is no way to create a union type in SQL today
##########
## UNION DataType Tests
##########
query ?I
select union_column, union_extract(union_column, 'int') from union_table;
----
{int=1} 1
{string=bar} NULL
{int=3} 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants