Skip to content

Fix to unwrap PSObject when setting data to DataRow and DataRowView#19071

Open
daxian-dbw wants to merge 2 commits into
PowerShell:masterfrom
daxian-dbw:datarow
Open

Fix to unwrap PSObject when setting data to DataRow and DataRowView#19071
daxian-dbw wants to merge 2 commits into
PowerShell:masterfrom
daxian-dbw:datarow

Conversation

@daxian-dbw
Copy link
Copy Markdown
Member

PR Summary

Fix #17627

Fix to unwrap PSObject when setting data to DataRow and DataRowView.

For set-index operation on DataRow and DataRowView, we unwrap its argument value if it's appropriate.
We treat those 2 types specially because their indexers accept 'object' arguments in the signatures, but actually check the data type internally and will fail if the argument is wrapped in PSObject.

This fixes a regression introduced in PowerShell v7.2.0, see #17627 (comment) for details.

PR Checklist

{
DataRowView dataRowView = (DataRowView)property.baseObject;
dataRowView[(string)property.adapterData] = setValue;
dataRowView[(string)property.adapterData] = PSObject.Base(setValue);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could we make PSObject more flexible/smart with IConvertable?
The error is raised in https://github.com/dotnet/runtime/blob/1c442fcb36e73eb3d8be10423a45389ce2a468b9/src/libraries/System.Data.Common/src/System/Data/Common/DateTimeStorage.cs#L165
The pattern looks as commonly used. So perhaps we could benefit from implementation IConvertable in PSObject.

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.

This sounds like a good idea, better than special-casing the DataRow and DataRowView in the binder code.

Copy link
Copy Markdown
Contributor

@SeeminglyScience SeeminglyScience left a comment

Choose a reason for hiding this comment

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

LGTM!

@ili101
Copy link
Copy Markdown

ili101 commented Feb 1, 2023

By chance I encountered a similar error today that exist even in 5.1
Will this also fix this scenario? Thank you

$MyObject = [PSCustomObject]@{
    Param = 1
}
$Select = $MyObject | Select-Object -Property ('Param')
$SelectExpression = 1 | Select-Object -Property (
    @{ Name = 'Param'; Expression = { 1 } }
)

$Dt = [System.Data.DataTable]::new()
$Dt.Columns.Add('Param', [int]) | Out-Null
$Row = $Dt.Rows.Add()
$Row['Param'] = $MyObject.Param # 👍
$Row['Param'] = $Select.Param # 👍
$Row['Param'] = $SelectExpression.Param # Error
# Unable to cast object of type 'System.Management.Automation.PSObject' to type 'System.IConvertible'.Couldn't store <1> in Param Column.  Expected type is Int32.

@ili101
Copy link
Copy Markdown

ili101 commented Feb 2, 2023

Another one I see now with multiple items

$MyObject = [PSCustomObject]@{
    Param = 1
    Param2 = 'Foo'
}
$Select = $MyObject | Select-Object -Property ('Param', 'Param2')
$SelectExpression = 1 | Select-Object -Property (
    @{ Name = 'Param'; Expression = { 1 } },
    @{ Name = 'Param2'; Expression = { 'Foo' } }
)
$Dt = [System.Data.DataTable]::new()
$Dt.Columns.Add('Param', [int]) | Out-Null
$Dt.Columns.Add('Param2', [string]) | Out-Null
$Row = $Dt.Rows.Add()
$Row['Param'] = $MyObject.Param # 👍
$Row['Param'] = $Select.Param # 👍
$Row['Param'] = $SelectExpression.Param # Error
$Row = $Dt.Rows.Add($SelectExpression.Param, $SelectExpression.Param2) # Individual 👍
$Row = $Dt.Rows.Add($MyObject.PSObject.Properties.Value) # Array exact types 👍
$Row = $Dt.Rows.Add(('1', 'Foo')) # Array string casted automatically 👍
$Row = $Dt.Rows.Add($SelectExpression.PSObject.Properties.Value) # Array PSObject error, not casted

@daxian-dbw
Copy link
Copy Markdown
Member Author

daxian-dbw commented Feb 3, 2023

Closing this PR, as #19071 (comment) proposes a better solution to the problem.
@ili101 I think all those issues will be resolved if PSObject properly implements System.IConvertible.

The idea to implement IConvertible for PSObject is rejected by the engine working group (see the details in #19170). So, reopening this PR.

@pull-request-quantifier-deprecated
Copy link
Copy Markdown

This PR has 41 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +34 -7
Percentile : 16.4%

Total files changed: 3

Change summary by file extension:
.cs : +12 -7
.ps1 : +22 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Mar 30, 2023
@ghost ghost added the Review - Needed The PR is being reviewed label Apr 6, 2023
@ghost
Copy link
Copy Markdown

ghost commented Apr 6, 2023

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@daxian-dbw daxian-dbw added WG-Engine core PowerShell engine, interpreter, and runtime Needs-Triage The issue is new and needs to be triaged by a work group. labels May 1, 2023
@JamesWTruher JamesWTruher removed the Needs-Triage The issue is new and needs to be triaged by a work group. label Aug 21, 2023
@JamesWTruher
Copy link
Copy Markdown
Collaborator

Engine WG discussed this again and we still think it's a good idea.

@rkeithhill rkeithhill added the WG-Reviewed A Working Group has reviewed this and made a recommendation label Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small Review - Needed The PR is being reviewed WG-Engine core PowerShell engine, interpreter, and runtime WG-Reviewed A Working Group has reviewed this and made a recommendation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression bug 7.2.0 - Unable to cast DateTime into DataRow DateTime column

6 participants