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

Add replace statement to sql parser #12386

Merged
merged 16 commits into from
May 13, 2022

Conversation

adarshsanjeev
Copy link
Contributor

@adarshsanjeev adarshsanjeev commented Apr 1, 2022

Relevant Issue: #11929

  • Add custom replace statement to Druid SQL parser.
  • Edit DruidPlanner to convert relevant fields to Query Context.
  • Refactor common code with Insert statements to reuse them for replace where possible.


This PR has:

  • been self-reviewed.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.

@adarshsanjeev adarshsanjeev changed the title Added replace statement to sql parser (Work in progress) Added replace statement to sql parser Apr 1, 2022
@adarshsanjeev adarshsanjeev marked this pull request as ready for review April 4, 2022 06:18
@gianm gianm self-requested a review April 4, 2022 22:09
sql/src/main/codegen/includes/replace.ftl Outdated Show resolved Hide resolved
sql/src/main/codegen/includes/replace.ftl Show resolved Hide resolved
sql/src/main/codegen/includes/common.ftl Outdated Show resolved Hide resolved
sql/src/main/codegen/includes/common.ftl Outdated Show resolved Hide resolved
sql/src/main/codegen/includes/common.ftl Outdated Show resolved Hide resolved
sql/src/main/codegen/includes/replace.ftl Outdated Show resolved Hide resolved
sql/src/main/codegen/config.fmpp Outdated Show resolved Hide resolved
sql/src/main/codegen/config.fmpp Show resolved Hide resolved
sql/src/main/codegen/includes/common.ftl Outdated Show resolved Hide resolved
sql/src/main/codegen/includes/replace.ftl Outdated Show resolved Hide resolved
sql/src/main/codegen/includes/replace.ftl Outdated Show resolved Hide resolved
sql/src/main/codegen/includes/replace.ftl Outdated Show resolved Hide resolved
@adarshsanjeev adarshsanjeev changed the title Added replace statement to sql parser Add replace statement to sql parser Apr 6, 2022
Copy link
Contributor

@LakshSingla LakshSingla left a comment

Choose a reason for hiding this comment

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

Changes and tests look LGTM. Some NIT comments. Also, since we are reusing a lot of Calcite's rules for column and table names, can you please confirm we are not allowing a superset of the actual grammar to pass through the syntax phase? (For example, where we have a column list, we only want the column name and not anything like col1 as alias1. My guess is not since you have initialized the constructor, so it should be fine)

@adarshsanjeev
Copy link
Contributor Author

since we are reusing a lot of Calcite's rules for column and table names, can you please confirm we are not allowing a superset of the actual grammar to pass through the syntax phase?

The reuse is taken from the standard SqlInsert which DruidSqlInsert wraps, that has been restricted by trimming some of the syntax as you noted, so even at worst, it shouldn't be more than what is currently allowed in INSERT syntax.

Copy link
Contributor

@LakshSingla LakshSingla left a comment

Choose a reason for hiding this comment

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

LGTM

case LESS_THAN:
columnValuePair = createColumnValuePair(sqlBasicCall.getOperandList(), dateTimeZone);
return new BoundDimFilter(columnValuePair.left, null, columnValuePair.right, null, true, null, null, StringComparators.NUMERIC);
default:
Copy link
Contributor

Choose a reason for hiding this comment

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

we should also add support for BETWEEN if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added support. One thing to note is that BETWEEN in sql is inclusive for the interval. This might lead to some strange queries in most use cases, like __time BETWEEN TIMESTAMP '2000-01-01' AND TIMESTAMP '2000-01-31 23:59:59.999'. Should we stick with convention or make the end time exclusive?


import static org.junit.Assert.assertEquals;

public class CalciteReplaceDmlTest extends CalciteIngestionDmlTest
Copy link
Contributor

Choose a reason for hiding this comment

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

Please also add tests for negative scenarios where users are trying to use REPLACE in a way that is not supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are tests for the following negative scenarios

  • Invalid or missing overwrite where clause
  • Unsupported operator in overwrite where
  • A column other than __time is used in the query
  • Misaligned partition in the query
  • With no partition spec
  • Interval is empty

Are there any other scenarios I have missed that I should add tests for?

Copy link
Contributor

Choose a reason for hiding this comment

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

Question - Is ORDER BY or any other similar clauses prohibited in the REPLACE statement?
And, If so, can do tests for those need to be added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a test for this

Copy link
Contributor

@abhishekagarwal87 abhishekagarwal87 left a comment

Choose a reason for hiding this comment

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

+1 after CI

@abhishekagarwal87 abhishekagarwal87 merged commit 39b3487 into apache:master May 13, 2022
@abhishekagarwal87 abhishekagarwal87 added this to the 24.0.0 milestone Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants