Skip to content

Conversation

@kchasapis
Copy link

The interval was not allowed to be a column data type. With this patch we add this support.

We add support for the pseudo literals
CURRENT_TIME, CURRENT_TIMESTAMP, CURRENT_DATE.

Also, we add support for TIMESTAMP and TIME literals. The user can now explicitly set the type of the string value it follows with TIMESTAMP 'value' and TIME 'value.

DATE was already supported.

@kchasapis kchasapis force-pushed the interval_date_psedo_literals branch from 08fe178 to d2da311 Compare June 4, 2025 14:52
Copy link
Member

@dey4ss dey4ss left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! Before I go into detail reviewing your changes, can you please add some unit tests?

ColumnType type;
bool nullable;
std::vector<ReferencesSpecification*>* references;
Expr* defaultValue{nullptr};
Copy link
Member

Choose a reason for hiding this comment

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

This member is unused, isn't it?

Copy link
Author

Choose a reason for hiding this comment

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

@dey4ss I have deleted it.

@kchasapis kchasapis force-pushed the interval_date_psedo_literals branch from d2da311 to 9109fe0 Compare June 16, 2025 22:36
The interval was not allowed to be a column data type.
With this patch we add this support.

We add support for the pseudo literals
CURRENT_TIME, CURRENT_TIMESTAMP, CURRENT_DATE.

Also, we add support for TIMESTAMP and TIME literals.
The user can now explicitly set the type of the string value it follows
with TIMESTAMP 'value' and TIME 'value.

DATE was already supported.
@kchasapis kchasapis force-pushed the interval_date_psedo_literals branch from 9109fe0 to ef49bda Compare June 16, 2025 22:43
@kchasapis
Copy link
Author

Thank you for your contribution! Before I go into detail reviewing your changes, can you please add some unit tests?

Thanks for your message. I have added unit tests. Please, let me know if there is anything else needed from my side to get started in the review.

Copy link
Member

@dey4ss dey4ss left a comment

Choose a reason for hiding this comment

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

Sorry for the late review. We discussed the PR internally and we have a few suggestions.

Comment on lines +26 to +28
kExprCurrentTimestamp,
kExprCurrentDate,
kExprCurrentTime,
Copy link
Member

Choose a reason for hiding this comment

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

We discussed this internally and we prefer to have only an expression type per date/time/timestamp, not for the "current" version of the literal. Instead, please add a (boolean) flag to the Expr struct indicating whether the literal has the special "current" value.

Mini: The expression types are not really ordered by name. Could you bring them in the order date, time, timestamp, interval?

Comment on lines +196 to +206
static Expr* makeTimestampLiteral(char* val);

static Expr* makeTimeLiteral(char* val);

static Expr* makeIntervalLiteral(int64_t duration, DatetimeField unit);

static Expr* makeCurrentTimestamp();

static Expr* makeCurrentDate();

static Expr* makeCurrentTime();
Copy link
Member

Choose a reason for hiding this comment

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

Please see my previous comment. The "current" versions of these functions are not required when we always pass a char*. If the pointer is a nullptr, we could automatically set the flag to mark the "current" literal version.

@@ -1,4 +1,4 @@
# This file contains a list of strings that are NOT valid SQL queries.
# This file contains a list of strings that are valid SQL queries.
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch!

Comment on lines +1191 to +1193
| INTERVAL INTVAL datetime_field {
$$ = Expr::makeIntervalLiteral($2, $3);
}
Copy link
Member

Choose a reason for hiding this comment

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

Do you have a use case for this version? According to the SQL:2023 standard, only

INTERVAL 'INTVAL' duration_field

would be valid (note the quotation marks). However, we supported some more versions according to Postgres. INTERVAL INTVAL datetime_field does not seem to work in Postgres (see DB Fiddle, statements in comments raise errors).

Comment on lines +1232 to +1236
current_timestamp : CURRENT_TIMESTAMP { $$ = Expr::makeCurrentTimestamp(); };

current_date : CURRENT_DATE { $$ = Expr::makeCurrentDate(); };

current_time : CURRENT_TIME { $$ = Expr::makeCurrentTime(); };
Copy link
Member

Choose a reason for hiding this comment

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

Can you please merge these with the regular versions? E.g., merge current_timestamp with timestamp_literal to the following:

timestamp_literal : TIMESTAMP STRING {
  $$ = Expr::makeTimestampLiteral($2);
}
| CURRENT_TIMESTAMP {
  $$ = Expr::makeTimestampLiteral(nullptr);
};

Comment on lines +232 to +234
CURRENT_TIMESTAMP TOKEN(CURRENT_TIMESTAMP)
CURRENT_DATE TOKEN(CURRENT_DATE)
CURRENT_TIME TOKEN(CURRENT_TIME)
Copy link
Member

Choose a reason for hiding this comment

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

Can you please order lexicographically (date < time < timestamp)?

%type <expr> expr operand scalar_expr unary_expr binary_expr logic_expr exists_expr extract_expr cast_expr
%type <expr> function_expr between_expr expr_alias param_expr
%type <expr> column_name literal int_literal num_literal string_literal bool_literal date_literal interval_literal
%type <expr> timestamp_literal time_literal current_timestamp current_date current_time
Copy link
Member

Choose a reason for hiding this comment

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

(see previous comment) Please remove the current_ versions here.

Comment on lines +317 to +318
isType(kExprLiteralNull) || isType(kExprLiteralDate) || isType(kExprLiteralInterval) ||
isType(kExprLiteralTimestamp) || isType(kExprLiteralTime);
Copy link
Member

Choose a reason for hiding this comment

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

The "current" versions are missing here. However, we would suggest that they don't have own types anyways.

Comment on lines +783 to +785
SQLParser::parse(
"CREATE TABLE interval_table (c_interval INTERVAL )",
&result);
Copy link
Member

Choose a reason for hiding this comment

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

You can use TEST_PARSE_SINGLE_SQL here. This macro also asserts that the statement is valid and that it returns a single statement of the desired type and of the desired class.

TEST_PARSE_SINGLE_SQL("SELECT * FROM students WHERE graduation_date = CURRENT_DATE;",
kStmtSelect, SelectStatement, result, stmt);

ASSERT_TRUE(result.isValid());
Copy link
Member

Choose a reason for hiding this comment

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

pick here: TEST_PARSE_SINGLE_SQL already asserts this.

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.

2 participants