Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: implement IntervalStyle #67000

Merged
merged 6 commits into from
Jul 2, 2021
Merged

Conversation

otan
Copy link
Contributor

@otan otan commented Jun 29, 2021

See individual commits for details.

This is supremely messy, and I hope one day we clean this up. We do conversions differently for casts, pgwire and JSON. ctx.Location and sessiondatapb.DataConversionConfig should probably be unified, and be universally applies from the FmtCtx instead of ad-hocly applied throughout the stack (extra_float_digits and bytes_encode_format suffers the same problem and is probably incorrect in a few places). I'll file an issue for this after it merges.

Supercedes #62313
Resolves #65724
Resolves #61167

@otan otan requested review from rafiss and a team June 29, 2021 09:06
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

my comments are all pretty minor. this is great work navigating the maze!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @otan)


pkg/sql/vars.go, line 802 at r2 (raw file):

	// See https://www.postgresql.org/docs/10/static/runtime-config-client.html#GUC-INTERVALSTYLE
	`intervalstyle`: {

are the session var names case insensitive?


pkg/sql/sem/tree/casts.go, line 903 at r5 (raw file):

				FmtDataConversionConfig(ctx.SessionData.DataConversionConfig),
			)
		case *DInterval:

i didn't even realize that casts were affected. nice catch


pkg/sql/sessiondatapb/session_data.proto, line 66 at r2 (raw file):

// DataConversionConfig contains the parameters that influence the conversion
// between SQL data types and strings/byte arrays.

maybe update this comment?


pkg/sql/sessiondatapb/session_data.proto, line 76 at r2 (raw file):

  int32 extra_float_digits = 2;
  // IntervalStyle indicates the style to display intervals as.
  util.duration.IntervalStyle interval_style = 13;

why is it = 13?


pkg/util/duration/duration.go, line 596 at r1 (raw file):

}

// extractAbsTime returns signed amount of hours, minutes, seconds,

nit: comment is for extractTime


pkg/util/duration/duration.proto, line 16 at r1 (raw file):

// IntervalStyle matches the PostgreSQL IntervalStyle session parameter.
enum IntervalStyle {

dumb Q: why did we want this in a proto?


pkg/util/duration/duration_test.go, line 605 at r1 (raw file):

}

func TestFormat(t *testing.T) {

great test -- is there a way we can confirm that this matches the postgres output?

Copy link
Contributor Author

@otan otan left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mneverov and @rafiss)


pkg/sql/vars.go, line 802 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

are the session var names case insensitive?

yep!


pkg/sql/sem/tree/casts.go, line 903 at r5 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

i didn't even realize that casts were affected. nice catch

Done.


pkg/sql/sessiondatapb/session_data.proto, line 66 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

maybe update this comment?

Done.


pkg/sql/sessiondatapb/session_data.proto, line 76 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

why is it = 13?

oops.


pkg/util/duration/duration.proto, line 16 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

dumb Q: why did we want this in a proto?

for storing this into the SessionData proto, which can be serialized.


pkg/util/duration/duration_test.go, line 605 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

great test -- is there a way we can confirm that this matches the postgres output?

credits go to @mneverov for this - i only improved the readability.
i've verified these cases to match, but outside of randomised testing it's hard

mneverov and others added 6 commits July 1, 2021 12:45
Release note (sql change): Support iso_8601 and sql_standard as usable
session variables in IntervalStyle. Also add a
`sql.defaults.intervalstyle` cluster setting to be used as the default
interval style.
First, we place some session data into FmtCtx which are relevant for
printing.

Plumb the sessiondata into FmtCtx in many places by making evalCtx have
a FmtCtx option, which creates a FmtCtx with the session data from the
evalCtx. Unfortunately, this turned out to not heavily help, as casts,
JSON and pgwire do not go through this codepath...

Release note: None
This commit outputs intervals with the the IntervalStyle
session variable over pgwire.

Release note: None
Apply IntervalStyle to casts from intervals (arrays and tuples included)
to string.

Release note: None
This enable JSON based operations to correctly apply the IntervalStyle
session setting.

Release note: None
@otan otan force-pushed the intervalstyle branch from 49272c7 to e4239ab Compare July 1, 2021 02:48
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

lgtm!

what do you think about writing a CompareTest that is just focused on comparing IntervalStyle? it would mean adding a new test to the configs in compare_test.go, then a sqlsmith.SmitherOption that just generates SELECT interval and SET intervalstyle queries. Can be a later PR or a backlog issue if you think it's a reasonable idea!

bigger priority than that would be to implement DateStyle next: #41773

Reviewed 4 of 5 files at r1, 31 of 31 files at r7.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mneverov and @rafiss)

@otan
Copy link
Contributor Author

otan commented Jul 1, 2021

bigger priority than that would be to implement DateStyle next: #41773

that one is going to be an even bigger PITA ...

what do you think about writing a CompareTest that is just focused on comparing IntervalStyle? it would mean adding a new test to the configs in compare_test.go, then a sqlsmith.SmitherOption that just generates SELECT interval and SET intervalstyle queries. Can be a later PR or a backlog issue if you think it's a reasonable idea!

i think it's a fine idea. will write an issue.

@otan
Copy link
Contributor Author

otan commented Jul 1, 2021

bors r=rafiss

@craig
Copy link
Contributor

craig bot commented Jul 2, 2021

Build succeeded:

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.

sql: Allow session variables to be passed into FmtCtx sql: support IntervalStyle session parameter
4 participants