33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- package org .opensearch .sql .calcite ;
6+ package org .opensearch .sql .calcite . rel ;
77
88import java .util .ArrayList ;
99import java .util .List ;
1616import org .apache .logging .log4j .LogManager ;
1717import org .apache .logging .log4j .Logger ;
1818import org .opensearch .sql .ast .expression .QualifiedName ;
19+ import org .opensearch .sql .calcite .CalcitePlanContext ;
1920import org .opensearch .sql .calcite .plan .DynamicFieldsConstants ;
2021import org .opensearch .sql .expression .function .BuiltinFunctionName ;
2122import org .opensearch .sql .expression .function .PPLFuncImpTable ;
@@ -29,6 +30,25 @@ public class QualifiedNameResolver {
2930
3031 private static final Logger log = LogManager .getLogger (QualifiedNameResolver .class );
3132
33+ /** Resolve field in a specific input */
34+ public static Optional <RexNode > resolveField (
35+ int inputCount , int inputOrdinal , String fieldName , CalcitePlanContext context ) {
36+ List <String > inputFieldNames = context .fieldBuilder .getAllFieldNames (inputCount , inputOrdinal );
37+ if (inputFieldNames .contains (fieldName )) {
38+ return Optional .of (context .fieldBuilder .staticField (inputCount , inputOrdinal , fieldName ));
39+ } else if (context .fieldBuilder .isDynamicFieldsExist ()) {
40+ return Optional .of (context .fieldBuilder .dynamicField (fieldName ));
41+ }
42+ return Optional .empty ();
43+ }
44+
45+ public static RexNode resolveFieldOrThrow (
46+ int inputCount , int inputOrdinal , String fieldName , CalcitePlanContext context ) {
47+ return resolveField (inputCount , inputOrdinal , fieldName , context )
48+ .orElseThrow (
49+ () -> new IllegalArgumentException (String .format ("Field [%s] not found." , fieldName )));
50+ }
51+
3252 /**
3353 * Resolves a qualified name to a RexNode based on the current context.
3454 *
@@ -133,7 +153,7 @@ private static Optional<RexNode> resolveDynamicFields(
133153 if (inputFieldNames .get (i ).contains (DynamicFieldsConstants .DYNAMIC_FIELDS_MAP )) {
134154 String fieldName = String .join ("." , parts );
135155 RexNode dynamicField =
136- context .relBuilder .field (inputCount , i , DynamicFieldsConstants .DYNAMIC_FIELDS_MAP );
156+ context .relBuilder .field_ (inputCount , i , DynamicFieldsConstants .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