Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,103 changes: 713 additions & 390 deletions Cargo.lock.msrv

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions docs/source/statements/displaying-results.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Displaying Query Results

This guide explains how to display query results from the database using the `RowsDisplayer` utility. The `RowsDisplayer` provides a flexible way to format and visualize query results as tables with various customization options, it tries to copy the behavior of the `cqlsh` utility.

## Basic Usage

To display query results, create a `RowsDisplayer` instance and configure its display settings:

```rust
let result: QueryRowsResult = session
.query_unpaged("SELECT * FROM examples_ks.basic1", &[])
.await?
.into_rows_result()?;
let displayer = result.rows_displayer();
println!("{}", displayer);
```

## Display Settings

### Terminal Width

Control the width of the output table:

```rust
displayer.set_terminal_width(80);
```

- Setting width to 0 (default) disables wrapping
- Table will attempt to wrap at specified width while maintaining readability
- Columns are adjusted proportionally when wrapping

### Color Output

Enable or disable colored output:

```rust
displayer.use_color(true); // Enable colors (default)
displayer.use_color(false); // Disable colors
```

When enabled, different data types are displayed in distinct colors:
- Numbers (integers, decimals, floats): Green
- Text and strings: Yellow
- Collections (lists, sets, maps): Blue
- Errors: Red
- Binary data: Magenta

### Binary Data Display

Configure how BLOB data is displayed using `ByteDisplaying`:

```rust
displayer.set_blob_displaying(ByteDisplaying::Hex); // Default
displayer.set_blob_displaying(ByteDisplaying::Ascii);
displayer.set_blob_displaying(ByteDisplaying::Dec);
```

Options:
- `Hex`: Display as hexadecimal values (e.g., "0A FF 42")
- `Ascii`: Display as ASCII characters where possible
- `Dec`: Display as decimal values (e.g., "213 7 66")

Comment on lines +58 to +62
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is that correct? In the implementation I did not see spaces between values. In your screenshot it also looked differently than here.

### Number Formatting

#### Integer Display

Control scientific notation for integers:

```rust
displayer.set_exponent_displaying_integers(true); // Enable scientific notation
displayer.set_exponent_displaying_integers(false); // Disable (default)
```

#### Floating Point Precision

Set the number of decimal places for floating point numbers:

```rust
displayer.set_floating_point_precision(6); // Show 6 decimal places (default)
```

## Example Output

Here's an example of how the output might look with default settings:

```
+----------+-------------+----------------+-------------+
| id | name | values | created_at |
+----------+-------------+----------------+-------------+
| 1 | Example | [1, 2, 3] | 2024-01-06 |
| 2 | Test Data | [4, 5, 6] | 2024-01-06 |
+----------+-------------+----------------+-------------+
```

## Best Practices

1. **Terminal Width**
- Set appropriate terminal width for better readability
- Consider using terminal width detection if available
- Use 0 width for untruncated output

2. **Color Usage**
- Enable colors for better type distinction
- Disable colors when outputting to files or non-terminal destinations
- Consider user accessibility settings

3. **Binary Data**
- Choose appropriate blob display format based on data content
- Use Hex for general binary data
- Use ASCII when data is known to be text
- Use Dec for byte-oriented analysis

4. **Number Formatting**
- Adjust floating point precision based on data requirements
- Enable scientific notation for very large/small numbers
- Consider locale-specific formatting needs

Comment on lines +95 to +117
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this section LLM-generated? It contains a lot of text, yet not a single useful information.

## Implementation Details

The displayer uses the following traits internally:
- `Display` for converting values to strings
- Custom formatting traits for specific types

Output is generated using Rust's formatting system (`fmt::Display`), ensuring efficient memory usage and proper error handling.
Comment on lines +118 to +124
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why would a user care about implementation details?
Why are we talking about efficient memory usage if the formatter uses Row and CqlValue, allocated all the rows at once, and boxes all the values again?

1 change: 1 addition & 0 deletions docs/source/statements/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,6 @@ There is a special functionality to enable [USE keyspace](usekeyspace.md).
schema-agreement
lwt
timeouts
displaying-results
timestamp-generators
```
6 changes: 6 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ scylla = { path = "../scylla", features = [
"num-bigint-04",
"bigdecimal-04",
"metrics",
"result-displayer",
] }
tokio = { version = "1.34", features = ["full"] }
tracing = { version = "0.1.25", features = ["log"] }
Expand Down Expand Up @@ -137,3 +138,8 @@ path = "tls-rustls.rs"
[[example]]
name = "execution_profile"
path = "execution_profile.rs"


[[example]]
name = "displayer"
path = "displayer.rs"
Comment on lines +141 to +145
Copy link
Collaborator

Choose a reason for hiding this comment

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

⛏ Missing newline

Loading
Loading