Skip to content

Convert to Datetime when encountering Date - #607

Merged
thomasp85 merged 7 commits into
mainfrom
issue-250-smooth-temporal
Dec 4, 2026
Merged

thomasp85 merged 7 commits into
mainfrom
issue-250-smooth-temporal

Conversation

@thomasp85

Copy link
Copy Markdown
Collaborator

Fix #250

We now support doing smoothing (density/smooth/violin) over temporal values such as Date by upcasting to microseconds since epoch

@thomasp85
thomasp85 requested a review from teunbrand November 3, 2026 14:42
Comment thread src/plot/layer/geom/density.rs Outdated
let expr = temporal_as_epoch_micros(dialect, &dialect.quote_ident(&value), kind);
(
format!(
"SELECT *, {expr} AS {val_alias} FROM ({query}) AS {src_alias}",

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.

Don't we need to use the Select dialect infrastructure you introduced?

Comment thread src/execute/scale.rs Outdated
.collect();
if !column_dtypes.is_empty() {
if let Ok(common_dtype) = crate::plot::scale::coerce_dtypes(&column_dtypes) {
let kind = infer_transform(&spec.scales[idx], &common_dtype, is_map);

@teunbrand teunbrand Nov 4, 2026 •

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.

Map projections stamp their position scales with TransformKind::Geographic, so these cannot have temporal scales. Because if temporal_transform then is mutually exclusive with is_map, we can hardcode false for is_map (and remove the variable here, maybe commenting on why we hardcode it to false).

Arguably a user can specify SCALE lon VIA date or something, which would be irregular and kicks all conventional map wisdom out the door, so the exact re-inference of the scale shouldn't matter too much even in this fringe case.

Also this kicks in on smooth layers, which may have its own problems with map projections.

data_cte: &str,
grid_cte: &str,
dialect: &dyn SqlDialect,
value_temporal: Option<crate::plot::types::CastTargetType>,

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.

IIUC we only cast to datetime when using smooth, but density/violin also maybe ought to support temporal data. If you agree, then we should remove this argument and always cast a date value.

@thomasp85
thomasp85 requested a review from teunbrand December 1, 2026 13:22

@teunbrand teunbrand left a comment

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.

I still think #607 (comment) is worth making a decision about.
In the current state, using density on a date column gives an unsightly error.
If we decide that we want date-enabled density, I'd be happy. If not, then we should error more gracefully.

Here is a test query that I think should work:

SELECT * FROM (VALUES
    ('2024-01-01'::DATE, 'A', 10),
    ('2024-01-02'::DATE, 'A', 20),
    ('2024-01-03'::DATE, 'A', 30),
    ('2024-01-04'::DATE, 'A', 40),
    ('2024-01-05'::DATE, 'A', 50),
    ('2024-03-10'::DATE, 'B', 15),
    ('2024-03-11'::DATE, 'B', 25),
    ('2024-03-12'::DATE, 'B', 35),
    ('2024-03-13'::DATE, 'B', 45),
    ('2024-03-14'::DATE, 'B', 55)
) AS t(date, grp, value)

VISUALISE date AS x, grp AS colour
DRAW density

@thomasp85

Copy link
Copy Markdown
Collaborator Author

hmm I think a decision has been made and it is to support it... I can't reproduce your issue. Are you looking at the latest commit?

@teunbrand

teunbrand commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Oh yeah you're right something was stale.
It renders now, but I think there is a different bug going on with density now, and it is not date(time) specific.
This:

VISUALISE bill_dep AS x, species AS colour FROM ggsql:penguins
DRAW density

Now renders such:

image

Which feels like a grouping concern. If you can't reproduce it is probably a me-problem and I'll approve

@thomasp85

Copy link
Copy Markdown
Collaborator Author

Good catch - it was a preexisting bug that this work surfaced - fixed now

@teunbrand teunbrand left a comment

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.

LGTM thanks for fixing that ordering bug!

@thomasp85
thomasp85 merged commit dbe2751 into main Dec 4, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Smooth should accept date/time axes

2 participants