77package org .elasticsearch .xpack .sql .parser ;
88
99import org .antlr .v4 .runtime .tree .ParseTree ;
10+ import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver ;
1011import org .elasticsearch .common .Strings ;
12+ import org .elasticsearch .core .Tuple ;
1113import org .elasticsearch .xpack .ql .plan .TableIdentifier ;
1214import org .elasticsearch .xpack .ql .tree .Source ;
1315import org .elasticsearch .xpack .sql .parser .SqlBaseParser .IdentifierContext ;
@@ -29,7 +31,56 @@ public TableIdentifier visitTableIdentifier(TableIdentifierContext ctx) {
2931 ParseTree tree = ctx .name != null ? ctx .name : ctx .TABLE_IDENTIFIER ();
3032 String index = tree .getText ();
3133
32- return new TableIdentifier (source , visitIdentifier (ctx .catalog ), unquoteIdentifier (index ));
34+ String cluster = visitIdentifier (ctx .catalog );
35+ String indexName = unquoteIdentifier (index );
36+ String selector = visitIdentifier (ctx .selector );
37+
38+ if (cluster != null && selector != null ) {
39+ throw new ParsingException (
40+ source ,
41+ "Invalid index name [{}:{}::{}], Selectors are not yet supported on remote cluster patterns" ,
42+ cluster ,
43+ indexName ,
44+ selector
45+ );
46+ }
47+
48+ if (selector != null ) {
49+ try {
50+ IndexNameExpressionResolver .SelectorResolver .validateIndexSelectorString (indexName , selector );
51+ } catch (Exception e ) {
52+ throw new ParsingException (source , e .getMessage ());
53+ }
54+ }
55+
56+ if (indexName .contains (IndexNameExpressionResolver .SelectorResolver .SELECTOR_SEPARATOR )) {
57+ if (selector != null ) {
58+ throw new ParsingException (
59+ source ,
60+ "Invalid index name [{}::{}], Invalid usage of :: separator, only one :: separator is allowed per expression" ,
61+ indexName ,
62+ selector
63+ );
64+ }
65+ try {
66+ Tuple <String , String > split = IndexNameExpressionResolver .splitSelectorExpression (indexName );
67+ indexName = split .v1 ();
68+ selector = split .v2 ();
69+ } catch (Exception e ) {
70+ throw new ParsingException (source , e .getMessage ());
71+ }
72+ if (selector != null ) {
73+ try {
74+ IndexNameExpressionResolver .SelectorResolver .validateIndexSelectorString (indexName , selector );
75+ } catch (Exception e ) {
76+ throw new ParsingException (source , "Invalid index name [{}::{}], {}" , indexName , selector , e .getMessage ());
77+ }
78+ }
79+ }
80+
81+ indexName = IndexNameExpressionResolver .combineSelectorExpression (indexName , selector );
82+
83+ return new TableIdentifier (source , cluster , indexName );
3384 }
3485
3586 @ Override
0 commit comments