You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/05/08 09:58:33 UTC

[GitHub] [arrow-datafusion] alamb commented on a change in pull request #289: Add csv mode to datafusion cli

alamb commented on a change in pull request #289:
URL: https://github.com/apache/arrow-datafusion/pull/289#discussion_r628730249



##########
File path: datafusion-cli/src/format/print_format.rs
##########
@@ -0,0 +1,57 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Print format variants
+use arrow::csv::writer::WriterBuilder;
+use datafusion::arrow::record_batch::RecordBatch;
+use datafusion::arrow::util::pretty;
+use datafusion::error::Result;
+use std::io::{Read, Seek, SeekFrom};
+use tempfile::tempfile;
+
+/// Allow records to be printed in different formats
+#[derive(Debug, Clone)]
+pub enum PrintFormat {
+    Csv,
+    Aligned,
+}
+
+impl PrintFormat {
+    /// print the batches to stdout using the specified format
+    pub fn print_batches(&self, batches: &[RecordBatch]) -> Result<()> {
+        match self {
+            PrintFormat::Csv => {
+                // utilizing a temp file so that we can leverage the arrow csv writer

Review comment:
       I don't think you need to use an actual `tempfile` here. You can probably print directly to a `Vec<u8>` in memory. Here is an example of how to do so from IOx:
   
   https://github.com/influxdata/influxdb_iox/blob/411cf134e9087198f74fe78ccb3c62e16344bf2f/influxdb_iox_client/src/format.rs#L133-L143

##########
File path: datafusion-cli/src/format/print_format.rs
##########
@@ -0,0 +1,57 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Print format variants
+use arrow::csv::writer::WriterBuilder;
+use datafusion::arrow::record_batch::RecordBatch;
+use datafusion::arrow::util::pretty;
+use datafusion::error::Result;
+use std::io::{Read, Seek, SeekFrom};
+use tempfile::tempfile;
+
+/// Allow records to be printed in different formats
+#[derive(Debug, Clone)]
+pub enum PrintFormat {
+    Csv,
+    Aligned,
+}
+
+impl PrintFormat {
+    /// print the batches to stdout using the specified format
+    pub fn print_batches(&self, batches: &[RecordBatch]) -> Result<()> {
+        match self {
+            PrintFormat::Csv => {
+                // utilizing a temp file so that we can leverage the arrow csv writer
+                // ideally the upstream shall take a more generic trait
+                let mut file = tempfile()?;
+                {
+                    let builder = WriterBuilder::new().has_headers(true);
+                    let mut writer = builder.build(&file);
+                    batches
+                        .iter()
+                        .for_each(|batch| writer.write(batch).unwrap());

Review comment:
       I think it would be good to return the error here rather than calling `unwrap()` 

##########
File path: datafusion-cli/src/format/print_format.rs
##########
@@ -0,0 +1,57 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Print format variants
+use arrow::csv::writer::WriterBuilder;
+use datafusion::arrow::record_batch::RecordBatch;
+use datafusion::arrow::util::pretty;
+use datafusion::error::Result;
+use std::io::{Read, Seek, SeekFrom};
+use tempfile::tempfile;
+
+/// Allow records to be printed in different formats
+#[derive(Debug, Clone)]
+pub enum PrintFormat {
+    Csv,
+    Aligned,
+}
+
+impl PrintFormat {
+    /// print the batches to stdout using the specified format
+    pub fn print_batches(&self, batches: &[RecordBatch]) -> Result<()> {
+        match self {
+            PrintFormat::Csv => {
+                // utilizing a temp file so that we can leverage the arrow csv writer
+                // ideally the upstream shall take a more generic trait
+                let mut file = tempfile()?;
+                {
+                    let builder = WriterBuilder::new().has_headers(true);
+                    let mut writer = builder.build(&file);
+                    batches
+                        .iter()
+                        .for_each(|batch| writer.write(batch).unwrap());
+                }
+                let mut data = String::new();
+                file.seek(SeekFrom::Start(0))?;
+                file.read_to_string(&mut data)?;
+                println!("{}", data);
+            }
+            PrintFormat::Aligned => pretty::print_batches(batches)?,

Review comment:
       FWIW I called this format `Pretty` when I was doing something similar in IOx -- I don't think it is really important, however




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org