Skip to content

More granular type widening and stricter type checking - #16912

Merged
Bahex merged 8 commits into
nushell:mainfrom
Bahex:Type-OneOf
Oct 27, 2025
Merged

Bahex merged 8 commits into
nushell:mainfrom
Bahex:Type-OneOf

Conversation

@Bahex

@Bahex Bahex commented Oct 22, 2025

Copy link
Copy Markdown
Member
  • Added Type::OneOf corresponding to Syntax::OneOf. It's a super type of all of its type parameters.
  • Type::widen(T, U):
    • only falls back to any when one of its inputs is any
    • if no common super type exists, the result is oneof<T, U>
    • when input types are not direct subtypes of one another, a common super type is found:
      • Previously this would be any as int and float are not subtypes of each other:
        int + float => number
        
      • In case of records and tables, only columns common to both are preserved:
        {a: int, b: string} + {a: int, c: string} => {a: int}
        
      • Common columns with differing types are also subject to Type::widen
        {key: string, value: string} + {key: string, value: int}
        => {key: string, value: oneof<string, int>}
        
      • Nesting oneofs and redundant type parameters are avoided.
        oneof<string, int> + oneof<float, nothing>
        => oneof<string, number, nothing>
        
  • cell-path is now type checked as oneof<cell-path, string, int> instead of any. Permitting string and int is to make using get, select and other similar commands with variables convenient. Also avoid a breaking change.

Release notes summary - What our users need to know

TODO

Tasks after submitting

@Bahex
Bahex requested a review from 132ikl October 22, 2025 09:42
@Bahex Bahex added A:type-system Problems or features related to nushell's type system notes:breaking-changes Noted in Breaking Changes section labels Oct 22, 2025
@github-actions github-actions Bot added A:parser Issues related to parsing A:std-library Defining and improving the standard library written in Nu A:LSP Language Server Protocol (nu-lsp) labels Oct 22, 2025
@Bahex
Bahex requested a review from sholderbach October 22, 2025 09:44
Comment thread crates/nu-protocol/src/value/mod.rs Resolved
Comment on lines +154 to +162
small
.into_iter()
.filter_map(|(col, typ)| {
big.iter()
.find_map(|(b_col, b_typ)| (&col == b_col).then(|| b_typ.clone()))
.map(|b_typ| (col, typ, b_typ))
})
.map(|(col, t, u)| (col, t.widen(u)))
.collect()

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.

How about internally sorting those fields for collection types? Then we can make this O(n).

Probably we can do it for oneof type/sytaxshape too, #16445 should be easy to fix then.

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.

@Bahex Any response to this? How about some TODO comments for the potential optimization and maybe the cellpath type checking as well?

Sorry to infest on this topic, the fact that a table is just a list of records in nushell makes it very sensitive to the perf of this kind of basic building blocks to scale on large datasets.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sorry, I forgot about this one.

I think it might be better as an issue rather than a TODO since it needs some discussion.

Also, I don't expect a significant difference, since getting a type like table<a: string, b: int> from a value is pretty much the same before and after this PR since checking all rows match type-wise was necessary to get a table<> type.

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 it might be better as an issue rather than a TODO since it needs some discussion.

I can manage that, but which one do you mean?

  1. Potential optimization of this part
  2. A new internal data structure for table
  3. Cellpath type other than a OneOf mixture with int and string

Also, I don't expect a significant difference, since getting a type like table<a: string, b: int> from a value is pretty much the same before and after this PR since checking all rows match type-wise was necessary to get a table<> type.

Probably true, I don't have a clear view of previous implementation of this.

Sorry for not making it clear, I'm just saying that there's plenty room of optimization here given this O(RC^2) algo.

TBC, personally I don't do any serious data analysis in nushell, so O(RC^2) is good enough for me. But actually it's quite easy to make it O(RC), that's why I suggested a TODO here.

Easier for new comers to spot, if they find it laggy for some really large datasets and want to improve.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For the potential optimization. Table data structure is a recurring discussion with no clear answer.

@blindFS

blindFS commented Oct 22, 2025 •

Copy link
Copy Markdown
Contributor

cell-path is now type checked as oneof<cell-path, string, int> instead of any. Permitting string and int is to make using get, select and other similar commands with variables convenient. Also avoid a breaking change.

It seems more natural to me to change the SyntaxShape of the cellpath argument of get/select to OneOf(Int, String, CellPath) instead.

However, it will be a breaking change for cellpath typed user defined arguments, a mild one, I hope.

@Bahex

Bahex commented Oct 22, 2025

Copy link
Copy Markdown
Member Author

@blindFS changing the command signature itself to allow int and string arguments does seem like a better solution in the long term, but i think we should stick with the relaxed type checking for now.

Comment on lines +88 to +90
(Type::OneOf(oneof), Type::CellPath) => {
oneof.iter().all(|t| t.is_subtype_of(&Type::CellPath))
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

shouldn't this already be handled by the general case OneOf arm below?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The general case below has the subtyping relation reversed:

match (self, other) {
	(this, Type::OneOf(that)) => that.iter().any(|t| this.is_subtype_of(t)),
	...
}

If this == Type::CellPath, it would be checking if cell-path is subtype of the oneof<..>.

	(Type::OneOf(oneof), Type::CellPath) => { .. }

The relation here is the opposite, cell-path is the supertype, oneof is being checked to see if it consist solely of cell-path's subtypes (cell-path, string, int).

It's a little strange, but we need it to avoid breaking get $col/get $row kind of uses.

Comment thread crates/nu-std/std-rfc/iter/mod.nu Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:LSP Language Server Protocol (nu-lsp) A:parser Issues related to parsing A:std-library Defining and improving the standard library written in Nu A:type-system Problems or features related to nushell's type system notes:breaking-changes Noted in Breaking Changes section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants