@@ -416,7 +416,7 @@ Status LanceTableReader::_validate_external_search_request() const {
416416 }
417417
418418 const auto & request = lance_scan_params.external_search_request ;
419- if (request.schema_version != 1 ) {
419+ if (request.schema_version != 1 && request. schema_version != 2 ) {
420420 return Status::NotSupported (" unsupported external search schema version: {}" ,
421421 request.schema_version );
422422 }
@@ -448,6 +448,31 @@ Status LanceTableReader::_validate_external_search_request() const {
448448 static_cast <int >(query_vector.element_type ));
449449 }
450450 const bool multi_vector = query_vector.__isset .num_vectors ;
451+ const bool has_distance_range =
452+ vector.__isset .distance_lower_bound || vector.__isset .distance_upper_bound ;
453+ if (has_distance_range && request.schema_version < 2 ) {
454+ return Status::InvalidArgument (" Lance distance bounds require search schema version 2" );
455+ }
456+ if (has_distance_range && multi_vector) {
457+ return Status::NotSupported (
458+ " Distance bounds are not supported for Lance multi-vector search" );
459+ }
460+ // Validate doubles before narrowing: non-finite or out-of-range values cannot reach the C API.
461+ const auto valid_bound = [](double bound) {
462+ return std::isfinite (bound) && std::abs (bound) <= std::numeric_limits<float >::max ();
463+ };
464+ if ((vector.__isset .distance_lower_bound && !valid_bound (vector.distance_lower_bound )) ||
465+ (vector.__isset .distance_upper_bound && !valid_bound (vector.distance_upper_bound ))) {
466+ return Status::InvalidArgument (" Lance distance bounds must be finite FLOAT values" );
467+ }
468+ if (vector.__isset .distance_lower_bound && vector.__isset .distance_upper_bound &&
469+ static_cast <float >(vector.distance_lower_bound ) >=
470+ static_cast <float >(vector.distance_upper_bound )) {
471+ return Status::InvalidArgument (
472+ " distance_lower_bound must be less than distance_upper_bound at FLOAT "
473+ " precision" );
474+ }
475+
451476 // The optional count distinguishes a query matrix, including a one-row matrix.
452477 if (multi_vector && query_vector.num_vectors <= 0 ) {
453478 return Status::InvalidArgument (
@@ -1177,6 +1202,17 @@ Status LanceTableReader::_configure_vector_search(LanceScanner* scanner,
11771202 }
11781203 }
11791204
1205+ if (vector.__isset .distance_lower_bound || vector.__isset .distance_upper_bound ) {
1206+ const float lower = static_cast <float >(vector.distance_lower_bound );
1207+ const float upper = static_cast <float >(vector.distance_upper_bound );
1208+ // nearest() clears the range; install bounds only after setting the query vector.
1209+ if (lance_scanner_set_distance_range (
1210+ scanner, vector.__isset .distance_lower_bound ? &lower : nullptr ,
1211+ vector.__isset .distance_upper_bound ? &upper : nullptr ) != 0 ) {
1212+ return lance_error (" set Lance vector distance range" );
1213+ }
1214+ }
1215+
11801216 // Exact refinement validates stored elements and gives indexed and unindexed candidates
11811217 // the same row-level score before either path truncates its results.
11821218 if (query.__isset .num_vectors && lance_scanner_set_refine_factor (scanner, 1 ) != 0 ) {
0 commit comments