Skip to content

Commit

Permalink
Remove Record::from_raw_cols_vals_unchecked (nushell#11810)
Browse files Browse the repository at this point in the history
# Description
Follows from nushell#11718 and replaces all usages of
`Record::from_raw_cols_vals_unchecked` with iterator or `record!`
equivalents.
  • Loading branch information
IanManske authored and dmatos2012 committed Feb 20, 2024
1 parent 09b390d commit cf15799
Show file tree
Hide file tree
Showing 18 changed files with 268 additions and 360 deletions.
24 changes: 9 additions & 15 deletions crates/nu-cmd-dataframe/src/dataframe/eager/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use polars::prelude::*;

Expand Down Expand Up @@ -52,27 +52,21 @@ impl Command for CastDF {
description: "Cast a column in a dataframe to a different dtype",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr cast u8 a | dfr schema",
result: Some(Value::record(
Record::from_raw_cols_vals_unchecked(
vec!["a".to_string(), "b".to_string()],
vec![
Value::string("u8", Span::test_data()),
Value::string("i64", Span::test_data()),
],
),
record! {
"a" => Value::string("u8", Span::test_data()),
"b" => Value::string("i64", Span::test_data()),
},
Span::test_data(),
)),
},
Example {
description: "Cast a column in a lazy dataframe to a different dtype",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr into-lazy | dfr cast u8 a | dfr schema",
result: Some(Value::record(
Record::from_raw_cols_vals_unchecked(
vec!["a".to_string(), "b".to_string()],
vec![
Value::string("u8", Span::test_data()),
Value::string("i64", Span::test_data()),
],
),
record! {
"a" => Value::string("u8", Span::test_data()),
"b" => Value::string("i64", Span::test_data()),
},
Span::test_data(),
)),
},
Expand Down
22 changes: 10 additions & 12 deletions crates/nu-cmd-dataframe/src/dataframe/eager/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, Type, Value,
record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};

#[derive(Clone)]
Expand Down Expand Up @@ -33,13 +33,10 @@ impl Command for SchemaDF {
description: "Dataframe schema",
example: r#"[[a b]; [1 "foo"] [3 "bar"]] | dfr into-df | dfr schema"#,
result: Some(Value::record(
Record::from_raw_cols_vals_unchecked(
vec!["a".to_string(), "b".to_string()],
vec![
Value::string("i64", Span::test_data()),
Value::string("str", Span::test_data()),
],
),
record! {
"a" => Value::string("i64", Span::test_data()),
"b" => Value::string("str", Span::test_data()),
},
Span::test_data(),
)),
}]
Expand Down Expand Up @@ -98,10 +95,11 @@ fn datatype_list(span: Span) -> Value {
]
.iter()
.map(|(dtype, note)| {
Value::record(Record::from_raw_cols_vals_unchecked(
vec!["dtype".to_string(), "note".to_string()],
vec![Value::string(*dtype, span), Value::string(*note, span)],
),span)
Value::record(record! {
"dtype" => Value::string(*dtype, span),
"note" => Value::string(*note, span),
},
span)
})
.collect();
Value::list(types, span)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,15 +1033,14 @@ fn series_to_values(
Either::Right(it)
}
.map(|any_values| {
let vals: Result<Vec<Value>, ShellError> = any_values
let record = polar_fields
.iter()
.map(|v| any_value_to_value(v, span))
.collect();
let cols: Vec<String> = polar_fields
.iter()
.map(|field| field.name.to_string())
.collect();
let record = Record::from_raw_cols_vals_unchecked(cols, vals?);
.zip(any_values)
.map(|(field, val)| {
any_value_to_value(val, span).map(|val| (field.name.to_string(), val))
})
.collect::<Result<_, _>>()?;

Ok(Value::record(record, span))
})
.collect();
Expand Down Expand Up @@ -1138,20 +1137,16 @@ fn any_value_to_value(any_value: &AnyValue, span: Span) -> Result<Value, ShellEr
any_value_to_value(&static_value, span)
}
AnyValue::StructOwned(struct_tuple) => {
let values: Result<Vec<Value>, ShellError> = struct_tuple
.0
.iter()
.map(|s| any_value_to_value(s, span))
.collect();
let fields = struct_tuple
let record = struct_tuple
.1
.iter()
.map(|f| f.name().to_string())
.collect();
Ok(Value::Record {
val: Record::from_raw_cols_vals_unchecked(fields, values?),
internal_span: span,
})
.zip(&struct_tuple.0)
.map(|(field, val)| {
any_value_to_value(val, span).map(|val| (field.name.to_string(), val))
})
.collect::<Result<_, _>>()?;

Ok(Value::record(record, span))
}
AnyValue::StringOwned(s) => Ok(Value::string(s.to_string(), span)),
AnyValue::Binary(bytes) => Ok(Value::binary(*bytes, span)),
Expand Down
16 changes: 7 additions & 9 deletions crates/nu-cmd-dataframe/src/dataframe/values/nu_dataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,13 @@ impl NuDataFrame {
match value {
Value::CustomValue { .. } => return Self::try_from_value(value),
Value::List { vals, .. } => {
let cols = (0..vals.len())
.map(|i| format!("{i}"))
.collect::<Vec<String>>();

conversion::insert_record(
&mut column_values,
Record::from_raw_cols_vals_unchecked(cols, vals),
&maybe_schema,
)?
let record = vals
.into_iter()
.enumerate()
.map(|(i, val)| (format!("{i}"), val))
.collect();

conversion::insert_record(&mut column_values, record, &maybe_schema)?
}
Value::Record { val: record, .. } => {
conversion::insert_record(&mut column_values, record, &maybe_schema)?
Expand Down
52 changes: 16 additions & 36 deletions crates/nu-cmd-dataframe/src/dataframe/values/nu_schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use nu_protocol::{Record, ShellError, Span, Value};
use nu_protocol::{ShellError, Span, Value};
use polars::prelude::{DataType, Field, Schema, SchemaRef, TimeUnit};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -37,15 +37,14 @@ impl From<NuSchema> for SchemaRef {
}

fn fields_to_value(fields: impl Iterator<Item = Field>, span: Span) -> Value {
let (cols, vals) = fields
let record = fields
.map(|field| {
let val = dtype_to_value(field.data_type(), span);
let col = field.name().to_string();
let val = dtype_to_value(field.data_type(), span);
(col, val)
})
.unzip();
.collect();

let record = Record::from_raw_cols_vals_unchecked(cols, vals);
Value::record(record, Span::unknown())
}

Expand Down Expand Up @@ -188,42 +187,23 @@ fn str_to_time_unit(ts_string: &str, span: Span) -> Result<TimeUnit, ShellError>
#[cfg(test)]
mod test {

use nu_protocol::record;

use super::*;

#[test]
fn test_value_to_schema() {
let value = Value::Record {
val: Record::from_raw_cols_vals_unchecked(
vec!["name".into(), "age".into(), "address".into()],
vec![
Value::String {
val: "str".into(),
internal_span: Span::test_data(),
},
Value::String {
val: "i32".into(),
internal_span: Span::test_data(),
},
Value::Record {
val: Record::from_raw_cols_vals_unchecked(
vec!["street".into(), "city".into()],
vec![
Value::String {
val: "str".into(),
internal_span: Span::test_data(),
},
Value::String {
val: "str".into(),
internal_span: Span::test_data(),
},
],
),
internal_span: Span::test_data(),
},
],
),
internal_span: Span::test_data(),
let address = record! {
"street" => Value::test_string("str"),
"city" => Value::test_string("str"),
};

let value = Value::test_record(record! {
"name" => Value::test_string("str"),
"age" => Value::test_string("i32"),
"address" => Value::test_record(address)
});

let schema = value_to_schema(&value, Span::unknown()).unwrap();
let expected = Schema::from_iter(vec![
Field::new("name", DataType::String),
Expand Down
8 changes: 3 additions & 5 deletions crates/nu-cmd-extra/src/extra/filters/roll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod roll_left;
mod roll_right;
mod roll_up;

use nu_protocol::{Record, ShellError, Value};
use nu_protocol::{ShellError, Value};
pub use roll_::Roll;
pub use roll_down::RollDown;
pub use roll_left::RollLeft;
Expand Down Expand Up @@ -70,10 +70,8 @@ fn horizontal_rotate_value(
HorizontalDirection::Left => vals.rotate_left(rotations),
}

Ok(Value::record(
Record::from_raw_cols_vals_unchecked(cols, vals),
span,
))
let record = cols.into_iter().zip(vals).collect();
Ok(Value::record(record, span))
}
Value::List { vals, .. } => {
let values = vals
Expand Down
26 changes: 8 additions & 18 deletions crates/nu-command/src/charting/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
Spanned, SyntaxShape, Type, Value,
};
use std::collections::HashMap;
Expand Down Expand Up @@ -239,13 +239,6 @@ fn histogram_impl(
}

let mut result = vec![];
let result_cols = vec![
value_column_name.to_string(),
"count".to_string(),
"quantile".to_string(),
"percentage".to_string(),
freq_column.to_string(),
];
const MAX_FREQ_COUNT: f64 = 100.0;
for (val, count) in counter.into_iter().sorted() {
let quantile = match calc_method {
Expand All @@ -259,16 +252,13 @@ fn histogram_impl(
result.push((
count, // attach count first for easily sorting.
Value::record(
Record::from_raw_cols_vals_unchecked(
result_cols.clone(),
vec![
val.into_value(),
Value::int(count, span),
Value::float(quantile, span),
Value::string(percentage, span),
Value::string(freq, span),
],
),
record! {
value_column_name => val.into_value(),
"count" => Value::int(count, span),
"quantile" => Value::float(quantile, span),
"percentage" => Value::string(percentage, span),
freq_column => Value::string(freq, span),
},
span,
),
));
Expand Down
29 changes: 15 additions & 14 deletions crates/nu-command/src/database/values/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,15 @@ fn prepared_statement_to_nu_list(
) -> Result<Value, SqliteError> {
let column_names = stmt
.column_names()
.iter()
.map(|c| c.to_string())
.into_iter()
.map(String::from)
.collect::<Vec<String>>();

let row_results = stmt.query_map([], |row| {
Ok(convert_sqlite_row_to_nu_value(
row,
call_span,
column_names.clone(),
&column_names,
))
})?;

Expand Down Expand Up @@ -491,18 +491,19 @@ fn read_entire_sqlite_db(
Ok(Value::record(tables, call_span))
}

pub fn convert_sqlite_row_to_nu_value(row: &Row, span: Span, column_names: Vec<String>) -> Value {
let mut vals = Vec::with_capacity(column_names.len());

for i in 0..column_names.len() {
let val = convert_sqlite_value_to_nu_value(row.get_ref_unwrap(i), span);
vals.push(val);
}
pub fn convert_sqlite_row_to_nu_value(row: &Row, span: Span, column_names: &[String]) -> Value {
let record = column_names
.iter()
.enumerate()
.map(|(i, col)| {
(
col.clone(),
convert_sqlite_value_to_nu_value(row.get_ref_unwrap(i), span),
)
})
.collect();

Value::record(
Record::from_raw_cols_vals_unchecked(column_names, vals),
span,
)
Value::record(record, span)
}

pub fn convert_sqlite_value_to_nu_value(value: ValueRef, span: Span) -> Value {
Expand Down

0 comments on commit cf15799

Please sign in to comment.