Browse Source

ESQL: Extend `RENAME` syntax to allow a `new = old` syntax (#129212)

This extends RENAME's grammar to allow a new syntax: `| RENAME new_name
= old_name` This is supported along the existing `... old_name AS
new_name` syntax.

Closes #129208
Bogdan Pintea 4 months ago
parent
commit
6c7730d1c9

+ 10 - 0
docs/reference/query-languages/esql/_snippets/commands/layout/rename.md

@@ -8,6 +8,16 @@ The `RENAME` processing command renames one or more columns.
 RENAME old_name1 AS new_name1[, ..., old_nameN AS new_nameN]
 ```
 
+The following syntax is also supported {applies_to}`stack: ga 9.1`:
+
+```esql
+RENAME new_name1 = old_name1[, ..., new_nameN = old_nameN]
+```
+
+::::{tip}
+Both syntax options can be used interchangeably but we recommend sticking to one for consistency and readability.
+::::
+
 **Parameters**
 
 `old_nameX`

+ 47 - 0
x-pack/plugin/esql/qa/testFixtures/src/main/resources/rename.csv-spec

@@ -296,3 +296,50 @@ ROW a="keyword", b=5, c=null
 a:integer
 5
 ;
+
+useAssignmentOnly
+required_capability: rename_allow_assignment
+
+ROW a = 1, b = "two"
+| RENAME aa = a, bb = b
+;
+
+aa:integer | bb:keyword
+1          | two
+;
+
+useAssignmentAndASKeyword
+required_capability: rename_allow_assignment
+
+ROW a = 1, b = "two", c = null
+| RENAME aa = a, b AS bb, cc = c
+;
+
+aa:integer | bb:keyword | cc:null
+1          | two        | null
+;
+
+shadowWithAssignment
+required_capability: rename_allow_assignment
+
+ROW a = 1, b = "two"
+| RENAME a = b
+;
+
+a:keyword
+two
+;
+
+multipleRenamesWithAssignment
+required_capability: rename_sequential_processing
+required_capability: rename_allow_assignment
+
+ROW a="keyword", b=5, c=null
+| RENAME c = a, a = b
+| RENAME b = c
+| RENAME b = a, a = b
+;
+
+a:integer
+5
+;

+ 1 - 0
x-pack/plugin/esql/src/main/antlr/EsqlBaseParser.g4

@@ -212,6 +212,7 @@ renameCommand
 
 renameClause:
     oldName=qualifiedNamePattern AS newName=qualifiedNamePattern
+    | newName=qualifiedNamePattern ASSIGN oldName=qualifiedNamePattern
     ;
 
 dissectCommand

+ 5 - 0
x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

@@ -439,6 +439,11 @@ public class EsqlCapabilities {
          */
         RENAME_SEQUENTIAL_PROCESSING,
 
+        /**
+         * Support for assignment in RENAME, besides the use of `AS` keyword.
+         */
+        RENAME_ALLOW_ASSIGNMENT,
+
         /**
          * Support for removing empty attribute in merging output.
          * See <a href="https://github.com/elastic/elasticsearch/issues/126392"> ESQL: EVAL after STATS produces an empty column #126392 </a>

File diff suppressed because it is too large
+ 0 - 0
x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlBaseParser.interp


File diff suppressed because it is too large
+ 213 - 195
x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlBaseParser.java


+ 2 - 1
x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/ExpressionTests.java

@@ -585,7 +585,7 @@ public class ExpressionTests extends ESTestCase {
         String[] oldName = new String[] { "b", "a.c", "x.y", "a" };
         List<?> renamings;
         for (int i = 0; i < newName.length; i++) {
-            Rename r = renameExpression(oldName[i] + " AS " + newName[i]);
+            Rename r = renameExpression(randomBoolean() ? (oldName[i] + " AS " + newName[i]) : (newName[i] + " = " + oldName[i]));
             renamings = r.renamings();
             assertThat(renamings.size(), equalTo(1));
             assertThat(renamings.get(0), instanceOf(Alias.class));
@@ -612,6 +612,7 @@ public class ExpressionTests extends ESTestCase {
 
     public void testForbidWildcardProjectRename() {
         assertParsingException(() -> renameExpression("b* AS a*"), "line 1:17: Using wildcards [*] in RENAME is not allowed [b* AS a*]");
+        assertParsingException(() -> renameExpression("a* = b*"), "line 1:17: Using wildcards [*] in RENAME is not allowed [a* = b*]");
     }
 
     public void testSimplifyInWithSingleElementList() {

Some files were not shown because too many files changed in this diff