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

[Enhancement] Support insert match column by name #51181

Merged
merged 1 commit into from
Oct 24, 2024

Conversation

wyb
Copy link
Contributor

@wyb wyb commented Sep 20, 2024

Why I'm doing:

currently, insert only supports matching column by position,
the order that values are inserted into the columns of the table is determined by the order that the columns were declared in values or select column list.

What I'm doing:

support matching by name.
this allows the order of the columns in the table differs from the order of the select columns in select statement.

insert into t1 (k1, k2) properties("match_column_by" = "name") select k2, k1 from t2;

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

@wyb wyb requested review from a team as code owners September 20, 2024 01:23
@mergify mergify bot assigned wyb Sep 20, 2024
@wyb wyb force-pushed the insert_match_name branch 2 times, most recently from 7a3efe3 to ad739d2 Compare September 20, 2024 05:44
Copy link

sonarcloud bot commented Sep 29, 2024

Copy link

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[FE Incremental Coverage Report]

pass : 75 / 85 (88.24%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/InsertPlanner.java 29 36 80.56% [643, 644, 645, 646, 648, 649, 652]
🔵 com/starrocks/sql/analyzer/InsertAnalyzer.java 31 34 91.18% [237, 295, 345]
🔵 com/starrocks/sql/ast/InsertStmt.java 13 13 100.00% []
🔵 com/starrocks/common/ErrorCode.java 2 2 100.00% []

Copy link

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@luohaha luohaha enabled auto-merge (squash) October 23, 2024 02:43
@@ -320,7 +320,7 @@ public enum ErrorCode {
"No partitions have data available for loading. If you are sure there may be no data to be loaded, " +
"you can use `ADMIN SET FRONTEND CONFIG ('empty_load_as_error' = 'false')` " +
"to ensure such load jobs can succeed"),
ERR_INSERTED_COLUMN_MISMATCH(5604, new byte[] {'2', '2', '0', '0', '0'},
ERR_INSERT_COLUMN_COUNT_MISMATCH(5604, new byte[] {'2', '2', '0', '0', '0'},
Copy link
Contributor

Choose a reason for hiding this comment

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

Is 42601 more suitable?

@@ -329,6 +329,7 @@ public enum ErrorCode {
ERR_ROUTINE_LOAD_OFFSET_INVALID(5607, new byte[] {'0', '2', '0', '0', '0'},
"Consume offset: %d is greater than the latest offset: %d in kafka partition: %d. " +
"You can modify 'kafka_offsets' property through ALTER ROUTINE LOAD and RESUME the job"),
ERR_INSERT_COLUMN_NAME_MISMATCH(5608, new byte[] {'2', '2', '0', '0', '0'}, "%s column: %s has no matching %s column"),
Copy link
Contributor

Choose a reason for hiding this comment

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

42601?

} else {
Preconditions.checkState(insertStmt.isColumnMatchByName());
if (query instanceof ValuesRelation) {
throw new SemanticException("Insert match column by name does not support values()");
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use ErrorReport.reportSemanticException(42601, ...)

mentionedColumns =
((OlapTable) table).getBaseSchemaWithoutGeneratedColumn().stream()
.map(Column::getName).collect(Collectors.toSet());
mentionedColumns.addAll(((OlapTable) table).getBaseSchemaWithoutGeneratedColumn().stream().map(Column::getName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you tell me the reason to use addAll()?

Copy link
Contributor Author

@wyb wyb Oct 24, 2024

Choose a reason for hiding this comment

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

mentionedColumns is CASE_INSENSITIVE_ORDER set.
Collectors.toSet() returns hash set.

@luohaha luohaha merged commit beefb14 into StarRocks:main Oct 24, 2024
59 checks passed
ZiheLiu pushed a commit to ZiheLiu/starrocks that referenced this pull request Oct 31, 2024
renzhimin7 pushed a commit to renzhimin7/starrocks that referenced this pull request Nov 7, 2024
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.

5 participants