1717import org .apache .calcite .sql .type .SqlTypeName ;
1818import org .opensearch .analytics .schema .IpType ;
1919import org .opensearch .common .geo .ShapeRelation ;
20+ import org .opensearch .common .network .InetAddresses ;
2021import org .opensearch .dsl .converter .ConversionContext ;
2122import org .opensearch .dsl .converter .ConversionException ;
2223import org .opensearch .index .query .QueryBuilder ;
2324import org .opensearch .index .query .RangeQueryBuilder ;
2425
2526import java .math .BigDecimal ;
2627import java .net .InetAddress ;
27- import java .net .UnknownHostException ;
2828import java .util .ArrayList ;
2929import java .util .List ;
3030
@@ -270,19 +270,13 @@ private RexNode convertIpRange(RangeQueryBuilder rangeQuery, RelDataTypeField fi
270270
271271 if (rangeQuery .from () != null ) {
272272 byte [] fromBytes = encodeIpAsIpv6 (String .valueOf (rangeQuery .from ()));
273- if (fromBytes == null ) {
274- throw new ConversionException ("Failed to parse IP address value '" + rangeQuery .from () + "'" );
275- }
276273 RexNode literal = ctx .getRexBuilder ().makeLiteral (new ByteString (fromBytes ), varbinaryType , false );
277274 SqlOperator op = rangeQuery .includeLower () ? SqlStdOperatorTable .GREATER_THAN_OR_EQUAL : SqlStdOperatorTable .GREATER_THAN ;
278275 conditions .add (ctx .getRexBuilder ().makeCall (op , fieldRef , literal ));
279276 }
280277
281278 if (rangeQuery .to () != null ) {
282279 byte [] toBytes = encodeIpAsIpv6 (String .valueOf (rangeQuery .to ()));
283- if (toBytes == null ) {
284- throw new ConversionException ("Failed to parse IP address value '" + rangeQuery .to () + "'" );
285- }
286280 RexNode literal = ctx .getRexBuilder ().makeLiteral (new ByteString (toBytes ), varbinaryType , false );
287281 SqlOperator op = rangeQuery .includeUpper () ? SqlStdOperatorTable .LESS_THAN_OR_EQUAL : SqlStdOperatorTable .LESS_THAN ;
288282 conditions .add (ctx .getRexBuilder ().makeCall (op , fieldRef , literal ));
@@ -301,21 +295,31 @@ private RexNode convertIpRange(RangeQueryBuilder rangeQuery, RelDataTypeField fi
301295 * encoded as 10 zero bytes + 0xff 0xff + 4 IPv4 bytes (RFC 4291 section 2.5.5.2).
302296 * IPv6 is its raw 16 bytes. Identical to {@code CidrMatchFunctionAdapter.encodeIpAsIpv6}
303297 * and {@code InetAddressPoint.encode} byte layout.
298+ *
299+ * <p>Uses {@code InetAddresses.forString} for strict textual IP parsing without DNS
300+ * resolution, matching legacy behavior (IpFieldMapper uses InetAddresses.forString;
301+ * hostname input is rejected).
302+ *
303+ * @param value textual IPv4 or IPv6 address (e.g. "192.168.0.1" or "::1")
304+ * @return 16-byte IPv6-mapped encoding
305+ * @throws ConversionException if the value is not a valid literal IP address
304306 */
305- static byte [] encodeIpAsIpv6 (String value ) {
307+ static byte [] encodeIpAsIpv6 (String value ) throws ConversionException {
308+ final InetAddress inetAddress ;
306309 try {
307- byte [] addr = InetAddress .getByName (value ).getAddress ();
308- if (addr .length == 16 ) {
309- return addr ;
310- }
311- byte [] mapped = new byte [16 ];
312- mapped [10 ] = (byte ) 0xff ;
313- mapped [11 ] = (byte ) 0xff ;
314- System .arraycopy (addr , 0 , mapped , 12 , 4 );
315- return mapped ;
316- } catch (UnknownHostException e ) {
317- return null ;
310+ inetAddress = InetAddresses .forString (value );
311+ } catch (IllegalArgumentException e ) {
312+ throw new ConversionException ("Failed to parse IP address value '" + value + "': not a valid IPv4 or IPv6 literal" );
313+ }
314+ byte [] addr = inetAddress .getAddress ();
315+ if (addr .length == 16 ) {
316+ return addr ;
318317 }
318+ byte [] mapped = new byte [16 ];
319+ mapped [10 ] = (byte ) 0xff ;
320+ mapped [11 ] = (byte ) 0xff ;
321+ System .arraycopy (addr , 0 , mapped , 12 , 4 );
322+ return mapped ;
319323 }
320324
321325 /**
0 commit comments