Skip to content

Conversation

@cloud-fan
Copy link
Contributor

@cloud-fan cloud-fan commented Oct 28, 2019

What changes were proposed in this pull request?

When parsing a string to interval, do not allow the leading "interval".

This PR also removes the leading "interval" from CalendarInterval.toString, to be consistent.

Why are the changes needed?

When parsing string to interval, we already know it's an interval and the leading "interval" in the string is kind of duplicated. The behavior is also consistent with pgsql

cloud0fan=# select 'interval 1 day'::interval;
ERROR:  invalid input syntax for type interval: "interval 1 day"
LINE 1: select 'interval 1 day'::interval;
               ^
cloud0fan=# select '1 day'::interval;
 interval 
----------
 1 day
(1 row)

cloud0fan=# select interval'interval 1 year';
ERROR:  invalid input syntax for type interval: " interval 1 year"
LINE 1: select interval' interval 1 year';

cloud0fan=# select interval'1 year';
 interval 
----------
 1 year
(1 row)

Does this PR introduce any user-facing change?

Yes. The INTERVAL'...' syntax is newly introduced in the master branch, so it's OK to change. The cast behavior in 2.4 is pretty weird: the leading "interval" in the string is required. I think it's better to change it (with a migration guide and legacy config).

How was this patch tested?

updated tests

validate(intervals.nonEmpty, "at least one time unit should be given for interval literal", ctx)
Literal(intervals.reduce(_.add(_)))
}
override def visitMultiUnitsInterval(ctx: MultiUnitsIntervalContext): CalendarInterval = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is mostly the original visitSingleInterval, with some methods inlined.

val value = string(ctx.STRING())
try {
val interval = (ctx.from.getText, ctx.to.getText) match {
case ("YEAR", "MONTH") =>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these are copied from the original visitIntervalField.


def legacyCastStringToInterval(str: String): CalendarInterval = {
val trimmed = str.trim
if (trimmed.startsWith("interval")) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be case insensitive check. You can use regionMatches() for that.

@MaxGekk
Copy link
Member

MaxGekk commented Oct 28, 2019

If we don't allow 'interval' in an input strings, shouldn't we output it too:

StringBuilder sb = new StringBuilder("interval");

Just in case, could you write a round trip test if such test doesn't exist. Otherwise it looks strange that Spark cannot parse back its output.

@SparkQA
Copy link

SparkQA commented Oct 28, 2019

Test build #112773 has finished for PR 26283 at commit ce43b9a.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@cloud-fan
Copy link
Contributor Author

The parser cleanup has been extracted to #26285

@dongjoon-hyun
Copy link
Member

Thank you, @cloud-fan !

@MaxGekk
Copy link
Member

MaxGekk commented Nov 4, 2019

@cloud-fan Please, rebase this on the master.

@SparkQA
Copy link

SparkQA commented Nov 4, 2019

Test build #113214 has finished for PR 26283 at commit 03e2a53.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@cloud-fan cloud-fan force-pushed the follow branch 3 times, most recently from a8c23a3 to 135f8e9 Compare November 5, 2019 04:21
@SparkQA
Copy link

SparkQA commented Nov 5, 2019

Test build #113236 has finished for PR 26283 at commit 135f8e9.

  • This patch fails to build.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Nov 5, 2019

Test build #113235 has finished for PR 26283 at commit 14e0239.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Nov 5, 2019

Test build #113239 has finished for PR 26283 at commit a4ffd00.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@cloud-fan
Copy link
Contributor Author

This has bigger impacts than I expect, e.g. it breaks functions.window which is a streaming-related public API.

I'm closing it to avoid breaking public APIs.

@cloud-fan cloud-fan closed this Nov 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants