Skip to content

Support millisecond span#4672

Merged
yuancu merged 2 commits intoopensearch-project:mainfrom
yuancu:issues/4550
Oct 30, 2025
Merged

Support millisecond span#4672
yuancu merged 2 commits intoopensearch-project:mainfrom
yuancu:issues/4550

Conversation

@yuancu
Copy link
Copy Markdown
Collaborator

@yuancu yuancu commented Oct 27, 2025

Description

Milliseconds had been improperly converted to microseconds. This PR fixes the issue by incorporating support to milliseconds.

Additionally, this PR correct a minor flaw of per function implementations so that they work properly with milliseconds.

Related Issues

Resolves #4550

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
@yuancu yuancu added the bug Something isn't working label Oct 27, 2025
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
function(
"timestampdiff",
stringLiteral("SECOND"),
stringLiteral("MILLISECOND"),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add some tests for MICROSECOND

Content-Type: 'application/json'
ppl:
body:
query: source=test_data_2023 | timechart span=500ms count()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can you add a test for MICROSECOND:

source=test_data_2023 | timechart span=500000us count()

Copy link
Copy Markdown
Collaborator Author

@yuancu yuancu Oct 28, 2025

Choose a reason for hiding this comment

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

It seems the minimum date time unit supportted in span function is millisecond:

  • SpanFunction.java
    public static Object evalTimestamp(
        @Parameter(name = "value") String value,
        @Parameter(name = "interval") int interval,
        @Parameter(name = "unit") String unit) {
      ExprValue exprInterval = ExprValueUtils.fromObjectValue(interval, ExprCoreType.INTEGER);
      ExprValue exprValue = ExprValueUtils.fromObjectValue(value, ExprCoreType.TIMESTAMP);
      Rounding<?> rounding = new TimestampRounding(exprInterval, unit);
      return rounding.round(exprValue).valueForCalcite();
    }
  • then in Rounding.java
    DateTimeUnit.resolve(unit)
    
    ...
    
    public enum DateTimeUnit {
        @Getter private final int id;
        @Getter private final String name;
        protected final boolean isMillisBased;
        protected final long ratio;
    
        MILLISECOND(1, "ms", true, ChronoField.MILLI_OF_SECOND.getBaseUnit().getDuration().toMillis()) {
          @Override
          public long round(long utcMillis, int interval) {
            return DateTimeUtils.roundFloor(utcMillis, ratio * interval);
          }
        },
    
        SECOND(2, "s", true, ChronoField.SECOND_OF_MINUTE.getBaseUnit().getDuration().toMillis()) {
          @Override
          public long round(long utcMillis, int interval) {
            return DateTimeUtils.roundFloor(utcMillis, ratio * interval);
          }
        },
    ...
    }

DateTimeUnit is built based on milliseconds. Its ration attribute if of type long, thus can not represent a fraction of millisecond.

Should I refactor this part to extend its support to microsecond and nanosecond?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What does the current user doc say?

Copy link
Copy Markdown
Collaborator Author

@yuancu yuancu Oct 29, 2025

Choose a reason for hiding this comment

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

In the doc for stats command, it claims that up to milliseconds is supported:

Span Interval Units
millisecond (ms)
second (s)
minute (m, case sensitive)
hour (h)
day (d)
week (w)
month (M, case sensitive)
quarter (q)
year (y)

Copy link
Copy Markdown
Member

@LantaoJin LantaoJin Oct 29, 2025

Choose a reason for hiding this comment

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

Ok, we can file a new PR to support MICROSECOND , as well as source=test_data_2023 | timechart span=500000us count()

SPANLENGTH: [0-9]+ (
'US'|'MS'|'CS'|'DS'
'US' |'CS'|'DS'
|'MS'|'MILLISECOND'|'MILLISECONDS'
Copy link
Copy Markdown
Collaborator

@dai-chen dai-chen Oct 28, 2025

Choose a reason for hiding this comment

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

If we want to expose microsecond span support, we need to update all docs related? Also we have to change per_* function to microsecond-based, otherwise timechart span=1us per_second... will break?

I'm thinking shall we separate the PR, e.g., fix mismatch of microsecond and millisecond in this PR and decide whether to expose all subsecond to PPL span clause later?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It seems microseconds are not supported by span yet.

I'm wondering if we are going to expand the support to microseconds, as nanoseconds are also supported in PPL, should we also do it for nanoseconds?

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'm under the same impression for span with timechart command. Probably we can track this as separate task and don't need to make more changes for span and per_* function here.

UNKNOWN("unknown"),
NONE(""),
MICROSECOND("us"),
US("us"),
Copy link
Copy Markdown
Collaborator

@Swiddis Swiddis Oct 29, 2025

Choose a reason for hiding this comment

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

question: μs? Why fear unicode in the strings?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That's a good point. I didn't think of it. I used us because it was already used to represent microseconds in the grammar file:

'US'|'MS'|'CS'|'DS'

Besides, us has the merit in its convenience of typing; SPL also adopts us for microseconds:

Error in 'timechart' command: The value for option span (1μs) is invalid. When span is expressed using a sub-second unit (ds, cs, ms, us), the span value needs to be < 1 second, and 1 second must be evenly divisible by the span value. 

Therefore, I think it's acceptable to adopt us for our use case. But of course, we can extend the support to μs.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It comes to me that cs and ds are not supported well in span as well. I'll raise another issue for it.

Copy link
Copy Markdown
Collaborator

@dai-chen dai-chen left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@yuancu yuancu merged commit b224750 into opensearch-project:main Oct 30, 2025
33 checks passed
@yuancu yuancu deleted the issues/4550 branch October 30, 2025 02:07
@opensearch-trigger-bot
Copy link
Copy Markdown
Contributor

The backport to 2.19-dev failed:

The process '/usr/bin/git' failed with exit code 128

To backport manually, run these commands in your terminal:

# Navigate to the root of your repository
cd $(git rev-parse --show-toplevel)
# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add ../.worktrees/sql/backport-2.19-dev 2.19-dev
# Navigate to the new working tree
pushd ../.worktrees/sql/backport-2.19-dev
# Create a new branch
git switch --create backport/backport-4672-to-2.19-dev
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 b224750d9bb865aa6695055c6ec4246485f597c2
# Push it to GitHub
git push --set-upstream origin backport/backport-4672-to-2.19-dev
# Go back to the original working tree
popd
# Delete the working tree
git worktree remove ../.worktrees/sql/backport-2.19-dev

Then, create a pull request where the base branch is 2.19-dev and the compare/head branch is backport/backport-4672-to-2.19-dev.

yuancu added a commit to yuancu/sql-plugin that referenced this pull request Oct 30, 2025
* Support millisecond span

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update per funciton tests

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

---------

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
(cherry picked from commit b224750)
@LantaoJin LantaoJin added the backport-manually Filed a PR to backport manually. label Oct 30, 2025
yuancu added a commit that referenced this pull request Oct 30, 2025
* Support millisecond span



* Update per funciton tests



---------


(cherry picked from commit b224750)

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
expani pushed a commit to vinaykpud/sql that referenced this pull request Nov 4, 2025
* Support millisecond span

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

* Update per funciton tests

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>

---------

Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 2.19-dev backport-failed backport-manually Filed a PR to backport manually. bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Span millisecond incorrectly converted to microsecond interval

4 participants