2828import org .apache .doris .common .UserException ;
2929import org .apache .doris .datasource .CatalogIf ;
3030import org .apache .doris .datasource .lance .index .LanceIndexInspectionExecutor ;
31+ import org .apache .doris .datasource .lance .index .LanceIndexSegmentGroup ;
3132import org .apache .doris .datasource .lance .metadata .LanceTableMetadata ;
3233import org .apache .doris .info .TableNameInfo ;
3334import org .apache .doris .mysql .privilege .PrivPredicate ;
4647
4748import java .util .ArrayList ;
4849import java .util .Arrays ;
49- import java .util .Collections ;
50+ import java .util .HashSet ;
5051import java .util .List ;
52+ import java .util .OptionalInt ;
53+ import java .util .Set ;
5154import java .util .concurrent .Future ;
5255import java .util .concurrent .TimeUnit ;
5356import java .util .concurrent .TimeoutException ;
@@ -71,14 +74,14 @@ public static ShowResultSetMetaData getResultSetMetaData() {
7174 }
7275
7376 public static void run (ConnectContext ctx , StmtExecutor executor , TableNameInfo tableName ,
74- String indexName , String computeGroup , BooleanSupplier cancelled ) throws Exception {
77+ List < String > columns , BooleanSupplier cancelled ) throws Exception {
7578 long started = System .nanoTime ();
7679 long elapsedMs = ctx .getStartTime () > 0 ? Math .max (0 , System .currentTimeMillis () - ctx .getStartTime ()) : 0 ;
7780 long remainingMs = TimeUnit .SECONDS .toMillis (ctx .getQueryTimeoutS ()) - elapsedMs ;
7881 long deadline = started + TimeUnit .MILLISECONDS .toNanos (Math .max (0 , remainingMs ));
7982 tableName .analyze (ctx );
8083 checkPrivileges (ctx , tableName );
81- List <Backend > targets = selectBackends (resolveComputeGroup (ctx , computeGroup ));
84+ List <Backend > targets = selectBackends (resolveComputeGroup (ctx ));
8285 // Resolve catalog/table initialization and snapshot IO in the bounded metadata pool.
8386 // The worker owns native resources until it returns, even if this caller stops waiting.
8487 String catalogName = tableName .getCtl ();
@@ -87,48 +90,83 @@ public static void run(ConnectContext ctx, StmtExecutor executor, TableNameInfo
8790 LanceTableMetadata metadata = LanceIndexInspectionExecutor .execute (() -> {
8891 CatalogIf <?> catalog = Env .getCurrentEnv ().getCatalogMgr ().getCatalog (catalogName );
8992 if (!(catalog instanceof LanceExternalCatalog )) {
90- throw new AnalysisException ("WARM UP INDEX requires a Lance catalog table" );
93+ throw new AnalysisException ("Index-only WARM UP SELECT requires a Lance catalog table" );
9194 }
9295 TableIf table = catalog .getDbOrAnalysisException (databaseName ).getTableOrAnalysisException (name );
9396 if (!(table instanceof LanceExternalTable )) {
94- throw new AnalysisException ("WARM UP INDEX requires a Lance catalog table" );
97+ throw new AnalysisException ("Index-only WARM UP SELECT requires a Lance catalog table" );
9598 }
9699 checkActive (deadline , cancelled );
97100 // Use the query access path, including REST-vended credentials, exactly once.
98101 return ((LanceExternalTable ) table ).loadMetadata ();
99102 }, deadline , cancelled );
100- PLanceIndexPrewarmRequest request = request (metadata , indexName );
101- execute (targets , request , deadline , cancelled , (backend , rpcRequest , timeoutMs ) ->
102- BackendServiceProxy .getInstance ().prewarmLanceIndexAsync (
103- new TNetworkAddress (backend .getHost (), backend .getBrpcPort ()), rpcRequest , timeoutMs ));
104- executor .sendResultSet (new ShowResultSet (RESULT_META , Collections .singletonList (Arrays .asList (
105- tableName .toSql (), indexName , Long .toString (metadata .getVersion ()), Integer .toString (targets .size ()),
106- Long .toString (TimeUnit .NANOSECONDS .toMillis (System .nanoTime () - started ))))));
103+ List <String > indexes = selectIndexes (metadata , columns );
104+ List <List <String >> rows = new ArrayList <>();
105+ // All indexes share the resolved snapshot, target set and statement deadline.
106+ for (String index : indexes ) {
107+ long indexStarted = System .nanoTime ();
108+ PLanceIndexPrewarmRequest request = request (metadata , index );
109+ execute (targets , request , deadline , cancelled , (backend , rpcRequest , timeoutMs ) ->
110+ BackendServiceProxy .getInstance ().prewarmLanceIndexAsync (
111+ new TNetworkAddress (backend .getHost (), backend .getBrpcPort ()), rpcRequest , timeoutMs ));
112+ rows .add (Arrays .asList (tableName .toSql (), index , Long .toString (metadata .getVersion ()),
113+ Integer .toString (targets .size ()),
114+ Long .toString (TimeUnit .NANOSECONDS .toMillis (System .nanoTime () - indexStarted ))));
115+ }
116+ executor .sendResultSet (new ShowResultSet (RESULT_META , rows ));
117+ }
118+
119+ static List <String > selectIndexes (LanceTableMetadata metadata , List <String > columns ) throws AnalysisException {
120+ Set <Integer > fields = new HashSet <>();
121+ for (String column : columns ) {
122+ List <String > matches = new ArrayList <>();
123+ metadata .getSchema ().getFields ().forEach (field -> {
124+ if (field .getName ().equalsIgnoreCase (column )) {
125+ matches .add (field .getName ());
126+ }
127+ });
128+ if (matches .size () != 1 ) {
129+ throw new AnalysisException ("Unknown or ambiguous index prewarm column: " + column );
130+ }
131+ OptionalInt field = metadata .getLanceFieldId (matches .get (0 ));
132+ if (!field .isPresent ()) {
133+ throw new AnalysisException ("Lance field ID is unavailable for index prewarm column: " + column );
134+ }
135+ fields .add (field .getAsInt ());
136+ }
137+ List <String > indexes = new ArrayList <>();
138+ for (LanceIndexSegmentGroup index : metadata .getIndexes ()) {
139+ if (columns .isEmpty () || index .getSegments ().stream ()
140+ .anyMatch (segment -> segment .getFieldIds ().stream ().anyMatch (fields ::contains ))) {
141+ indexes .add (index .getName ());
142+ }
143+ }
144+ if (indexes .isEmpty ()) {
145+ throw new AnalysisException ("No indexes match the WARM UP SELECT projection" );
146+ }
147+ return indexes ;
107148 }
108149
109150 static void checkPrivileges (ConnectContext ctx , TableNameInfo table ) throws AnalysisException {
110151 // Authorize before catalog lookup: resolving external metadata can perform remote IO.
111152 if (!Env .getCurrentEnv ().getAccessManager ().checkGlobalPriv (ctx , PrivPredicate .ADMIN )) {
112- throw new AnalysisException ("ADMIN privilege is required for WARM UP INDEX " );
153+ throw new AnalysisException ("ADMIN privilege is required for Index-only WARM UP SELECT " );
113154 }
114155 if (!Env .getCurrentEnv ().getAccessManager ().checkTblPriv (ctx ,
115156 new TableName (table .getCtl (), table .getDb (), table .getTbl ()), PrivPredicate .SELECT )) {
116- throw new AnalysisException ("SELECT denied for WARM UP INDEX on " + table .toSql ());
157+ throw new AnalysisException ("SELECT denied for Index-only WARM UP SELECT on " + table .toSql ());
117158 }
118159 }
119160
120- static ComputeGroup resolveComputeGroup (ConnectContext ctx , String name ) throws UserException {
161+ static ComputeGroup resolveComputeGroup (ConnectContext ctx ) throws UserException {
121162 if (Config .isCloudMode ()) {
122- String selected = name == null ? ctx .getCloudCluster () : name ;
163+ String selected = ctx .getCloudCluster ();
123164 if (!Env .getCurrentEnv ().getAccessManager ().checkCloudPriv (ctx .getCurrentUserIdentity (),
124165 selected , PrivPredicate .USAGE , ResourceTypeEnum .CLUSTER )) {
125166 throw new AnalysisException ("USAGE denied for compute group '" + selected + "'" );
126167 }
127168 return Env .getCurrentEnv ().getComputeGroupMgr ().getComputeGroupByName (selected );
128169 }
129- if (name != null ) {
130- throw new AnalysisException ("WITH COMPUTE GROUP requires cloud mode" );
131- }
132170 return ctx .getComputeGroup ();
133171 }
134172
0 commit comments