33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- package org .opensearch .sql .calcite ;
6+ package org .opensearch .sql .calcite .rel ;
7+
8+ import static org .opensearch .sql .calcite .plan .DynamicFieldsConstants .DYNAMIC_FIELDS_MAP ;
79
810import java .util .ArrayList ;
911import java .util .List ;
1618import org .apache .logging .log4j .LogManager ;
1719import org .apache .logging .log4j .Logger ;
1820import org .opensearch .sql .ast .expression .QualifiedName ;
19- import org .opensearch .sql .calcite .plan . DynamicFieldsConstants ;
21+ import org .opensearch .sql .calcite .CalcitePlanContext ;
2022import org .opensearch .sql .expression .function .BuiltinFunctionName ;
2123import org .opensearch .sql .expression .function .PPLFuncImpTable ;
2224
@@ -29,6 +31,25 @@ public class QualifiedNameResolver {
2931
3032 private static final Logger log = LogManager .getLogger (QualifiedNameResolver .class );
3133
34+ /** Resolve field in a specific input */
35+ public static Optional <RexNode > resolveField (
36+ int inputCount , int inputOrdinal , String fieldName , CalcitePlanContext context ) {
37+ List <String > inputFieldNames = context .fieldBuilder .getAllFieldNames (inputCount , inputOrdinal );
38+ if (inputFieldNames .contains (fieldName )) {
39+ return Optional .of (context .fieldBuilder .staticField (inputCount , inputOrdinal , fieldName ));
40+ } else if (context .fieldBuilder .isDynamicFieldsExist ()) {
41+ return Optional .of (context .fieldBuilder .dynamicField (fieldName ));
42+ }
43+ return Optional .empty ();
44+ }
45+
46+ public static RexNode resolveFieldOrThrow (
47+ int inputCount , int inputOrdinal , String fieldName , CalcitePlanContext context ) {
48+ return resolveField (inputCount , inputOrdinal , fieldName , context )
49+ .orElseThrow (
50+ () -> new IllegalArgumentException (String .format ("Field [%s] not found." , fieldName )));
51+ }
52+
3253 /**
3354 * Resolves a qualified name to a RexNode based on the current context.
3455 *
@@ -130,10 +151,9 @@ private static Optional<RexNode> resolveDynamicFields(
130151 List <Set <String >> inputFieldNames = collectInputFieldNames (context , inputCount );
131152
132153 for (int i = 0 ; i < inputCount ; i ++) {
133- if (inputFieldNames .get (i ).contains (DynamicFieldsConstants . DYNAMIC_FIELDS_MAP )) {
154+ if (inputFieldNames .get (i ).contains (DYNAMIC_FIELDS_MAP )) {
134155 String fieldName = String .join ("." , parts );
135- RexNode dynamicField =
136- context .relBuilder .field (inputCount , i , DynamicFieldsConstants .DYNAMIC_FIELDS_MAP );
156+ RexNode dynamicField = context .relBuilder .field_ (inputCount , i , DYNAMIC_FIELDS_MAP );
137157 RexNode itemAccess = createItemAccess (dynamicField , fieldName , context );
138158 return Optional .of (itemAccess );
139159 }
@@ -149,7 +169,7 @@ private static Optional<RexNode> tryToResolveField(
149169 fieldName ,
150170 inputCount );
151171 try {
152- return Optional .of (context .relBuilder .field (inputCount , alias , fieldName ));
172+ return Optional .of (context .relBuilder .field_ (inputCount , alias , fieldName ));
153173 } catch (IllegalArgumentException e ) {
154174 log .debug ("tryToResolveField() failed: {}" , e .getMessage ());
155175 }
@@ -171,7 +191,7 @@ private static Optional<RexNode> resolveFieldWithoutAlias(
171191 int foundInput = findInputContainingFieldName (inputCount , inputFieldNames , fieldName );
172192 log .debug ("resolveFieldWithoutAlias() foundInput={}" , foundInput );
173193 if (foundInput != -1 ) {
174- RexNode fieldNode = context .relBuilder .field (inputCount , foundInput , fieldName );
194+ RexNode fieldNode = context .relBuilder .field_ (inputCount , foundInput , fieldName );
175195 return Optional .of (resolveFieldAccess (context , parts , 0 , length , fieldNode ));
176196 }
177197 }
@@ -219,7 +239,7 @@ private static Optional<RexNode> resolveRenamedField(
219239 String alias = parts .get (0 );
220240 for (String candidate : candidates ) {
221241 try {
222- return Optional .of (context .relBuilder .field (alias , candidate ));
242+ return Optional .of (context .relBuilder .field_ (alias , candidate ));
223243 } catch (IllegalArgumentException e1 ) {
224244 // Indicates the field was not found.
225245 }
@@ -260,7 +280,7 @@ private static Optional<RexNode> resolveCorrelationField(
260280 String fieldName = joinParts (parts , start , length );
261281 log .debug ("resolveCorrelationField() trying fieldName={}" , fieldName );
262282 if (fieldNameList .contains (fieldName )) {
263- RexNode field = context .relBuilder .field (correlation , fieldName );
283+ RexNode field = context .relBuilder .field_ (correlation , fieldName );
264284 return resolveFieldAccess (context , parts , start , length , field );
265285 }
266286 }
0 commit comments