@@ -199,33 +199,38 @@ protected List<FieldSchema> getFieldSchemas(String collection) {
199199 protected List <ColumnMetadata > getCollectionFields (
200200 List <FieldSchema > fieldSchemas ) {
201201 List <ColumnMetadata > columnMetadatas = new LinkedList <>();
202- for (FieldSchema fieldSchema : fieldSchemas ) {
203- ColumnMetadata columnMetadata = new ColumnMetadata (fieldSchema .getName (),
204- fieldSchema .getName ());
205- columnMetadata .setDataType (fieldSchema .getDataType ());
206- columnMetadatas .add (columnMetadata );
202+ if (fieldSchemas != null ) {
203+ for (FieldSchema fieldSchema : fieldSchemas ) {
204+ ColumnMetadata columnMetadata = new ColumnMetadata (fieldSchema .getName (),
205+ fieldSchema .getName ());
206+ columnMetadata .setDataType (fieldSchema .getDataType ());
207+ columnMetadatas .add (columnMetadata );
208+ }
207209 }
210+
208211 return columnMetadatas ;
209212 }
210213
211214 protected ConditionMetadata buildDefaultConditionMetadata (
212215 List <FieldSchema > fieldSchemas ) {
213- for (FieldSchema fieldSchema : fieldSchemas ) {
214- DataType dataType = fieldSchema .getDataType ();
215- if (dataType == DataType .Float || dataType == DataType .Double
216- || dataType == DataType .Int8 || dataType == DataType .Int16
217- || dataType == DataType .Int32 || dataType == DataType .Int64 ) {
218- OperationMetadata operationMetadata = new RelationOperationMetadata (">" ,
219- fieldSchema .getName (), "0" );
220- return new ConditionMetadata (operationMetadata );
221- } else if (dataType == DataType .Bool ) {
222- OperationMetadata operationMetadata = new RelationOperationMetadata (
223- "==" , fieldSchema .getName (), "true" );
224- return new ConditionMetadata (operationMetadata );
225- } else if (dataType == DataType .String ) {
226- OperationMetadata operationMetadata = new RelationOperationMetadata (
227- "!=" , fieldSchema .getName (), " " );
228- return new ConditionMetadata (operationMetadata );
216+ if (fieldSchemas != null ) {
217+ for (FieldSchema fieldSchema : fieldSchemas ) {
218+ DataType dataType = fieldSchema .getDataType ();
219+ if (dataType == DataType .Float || dataType == DataType .Double
220+ || dataType == DataType .Int8 || dataType == DataType .Int16
221+ || dataType == DataType .Int32 || dataType == DataType .Int64 ) {
222+ OperationMetadata operationMetadata = new RelationOperationMetadata (">" ,
223+ fieldSchema .getName (), "0" );
224+ return new ConditionMetadata (operationMetadata );
225+ } else if (dataType == DataType .Bool ) {
226+ OperationMetadata operationMetadata = new RelationOperationMetadata (
227+ "==" , fieldSchema .getName (), "true" );
228+ return new ConditionMetadata (operationMetadata );
229+ } else if (dataType == DataType .String ) {
230+ OperationMetadata operationMetadata = new RelationOperationMetadata (
231+ "!=" , fieldSchema .getName (), " " );
232+ return new ConditionMetadata (operationMetadata );
233+ }
229234 }
230235 }
231236 return null ;
@@ -255,9 +260,16 @@ public QueryParam buildQueryParam(String sql) throws IOException {
255260
256261 protected RecordSet toSearchRecordSet (SelectorMetadata selectorMetadata ,
257262 R <SearchResults > result ) {
258- if (result .getData () == null )
263+ if (result == null ) {
264+ return null ;
265+ }
266+
267+ if (result .getException () != null ) { // 没对应数据时,查不到很正常,不应该抛异常 result.getData() == null) {
259268 throw new MoqlRuntimeException (result .getException ());
260- SearchResultData searchResultData = result .getData ().getResults ();
269+ }
270+
271+ SearchResults data = result .getData ();
272+ SearchResultData searchResultData = data == null ? null : data .getResults ();
261273 List <ColumnMetadata > columnMetadatas = selectorMetadata .getColumns ()
262274 .getColumns ();
263275 // setOutputDataType(columnMetadatas, searchResultData.getFieldsDataList());
@@ -290,14 +302,21 @@ protected DataType getIdType(SearchResultData searchResultData) {
290302
291303 protected RecordSet toQueryRecordSet (SelectorMetadata selectorMetadata ,
292304 R <QueryResults > result ) {
293- if (result .getData () == null )
305+ if (result == null ) {
306+ return null ;
307+ }
308+
309+ if (result .getException () != null ) { // 没对应数据时,查不到很正常,不应该抛异常 result.getData() == null) {
294310 throw new MoqlRuntimeException (result .getException ());
311+ }
312+
295313 QueryResults queryResults = result .getData ();
296314 List <ColumnMetadata > columnMetadatas = selectorMetadata .getColumns ()
297315 .getColumns ();
298316 // setOutputDataType(columnMetadatas, queryResults.getFieldsDataList());
299- int [] posMappings = posMappings (columnMetadatas ,
300- result .getData ().getFieldsDataList ());
317+
318+ QueryResults data = result .getData ();
319+ int [] posMappings = posMappings (columnMetadatas , data == null ? null : data .getFieldsDataList ());
301320 RecordSetMetadata recordSetMetadata = new RecordSetMetadata (
302321 (List ) columnMetadatas , null );
303322 return new RecordSetImpl (recordSetMetadata , new Date (), new Date (),
@@ -306,27 +325,39 @@ protected RecordSet toQueryRecordSet(SelectorMetadata selectorMetadata,
306325
307326 protected int [] posMappings (List <ColumnMetadata > columnMetadatas ,
308327 List <FieldData > fieldDatas ) {
309- int [] posMappings = new int [columnMetadatas .size ()];
328+ int mdSize = columnMetadatas == null ? 0 : columnMetadatas .size ();
329+ int [] posMappings = new int [mdSize ];
310330 int i = 0 ;
311- for (FieldData fieldData : fieldDatas ) {
312- posMappings [i ++] = findPos (columnMetadatas , fieldData .getFieldName ());
331+ if (mdSize > 0 ) {
332+ for (FieldData fieldData : fieldDatas ) {
333+ posMappings [i ++] = findPos (columnMetadatas , fieldData .getFieldName ());
334+ }
313335 }
314336 return posMappings ;
315337 }
316338
317339 protected int findPos (List <ColumnMetadata > columnMetadatas , String name ) {
318- int i = 0 ;
319- for (ColumnMetadata columnMetadata : columnMetadatas ) {
320- if (columnMetadata .getName ().equals (name ))
321- return i ;
322- i ++;
340+ if (columnMetadatas != null ) {
341+ int i = 0 ;
342+ for (ColumnMetadata columnMetadata : columnMetadatas ) {
343+ if (Objects .equals (columnMetadata .getName (), name )) {
344+ return i ;
345+ }
346+
347+ i ++;
348+ }
323349 }
350+
324351 throw new IllegalArgumentException (
325352 String .format ("There is no column named '%s'!" , name ));
326353 }
327354
328355 protected List getOutputColumns (List <ColumnMetadata > columnMetadatas ,
329356 DataType idType ) {
357+ if (columnMetadatas == null ) {
358+ columnMetadatas = new ArrayList <>();
359+ }
360+
330361 ColumnMetadata columnMetadata = new ColumnMetadata ("id" , "id" );
331362 columnMetadatas .add (0 , columnMetadata );
332363 columnMetadata .setDataType (idType );
@@ -338,22 +369,36 @@ protected List getOutputColumns(List<ColumnMetadata> columnMetadatas,
338369
339370 protected List <Object []> toRecords (SearchResultData resultData ,
340371 int [] posMappings ) {
372+ if (resultData == null ) {
373+ return null ;
374+ }
375+
341376 List <Object []> records = new LinkedList <>();
342377 int fieldCount = resultData .getFieldsDataCount () + 2 ;
343378 Iterator idIt = getIdIterator (resultData .getIds ());
344- Iterator idScoreIt = resultData .getScoresList ().iterator ();
379+
380+ List <Float > list = resultData .getScoresList ();
381+ Iterator idScoreIt = list == null ? null : list .iterator ();
345382 List <Iterator > fieldIterators = getFieldIterators (
346383 resultData .getFieldsDataList ());
347- while (idIt .hasNext ()) {
348- Object [] record = new Object [fieldCount ];
349- record [0 ] = idIt .next ();
350- record [1 ] = idScoreIt .next ();
351- int i = 0 ;
352- for (Iterator it : fieldIterators ) {
353- record [posMappings [i ++] + 2 ] = it .next ();
384+
385+ if (idIt != null ) {
386+ while (idIt .hasNext ()) {
387+ Object [] record = new Object [fieldCount ];
388+ record [0 ] = idIt .next ();
389+ record [1 ] = idScoreIt .next ();
390+ int i = 0 ;
391+
392+ if (fieldIterators != null ) {
393+ for (Iterator it : fieldIterators ) {
394+ record [posMappings [i ++] + 2 ] = it .next ();
395+ }
396+ }
397+
398+ records .add (record );
354399 }
355- records .add (record );
356400 }
401+
357402 return records ;
358403 }
359404
@@ -362,6 +407,10 @@ protected List<Object[]> toRecords(QueryResults queryResults,
362407 List <Object []> records = new LinkedList <>();
363408 List <Iterator > fieldIterators = getFieldIterators (
364409 queryResults .getFieldsDataList ());
410+ if (fieldIterators == null ) {
411+ return records ;
412+ }
413+
365414 while (true ) {
366415 Object [] record = new Object [fieldIterators .size ()];
367416 int i = 0 ;
@@ -382,6 +431,10 @@ protected List<Object[]> toRecords(QueryResults queryResults,
382431 }
383432
384433 protected Iterator getIdIterator (IDs ids ) {
434+ if (ids == null ) {
435+ return null ;
436+ }
437+
385438 if (ids .hasIntId ()) {
386439 LongArray longArray = ids .getIntId ();
387440 return longArray .getDataList ().iterator ();
@@ -393,9 +446,11 @@ protected Iterator getIdIterator(IDs ids) {
393446
394447 protected List <Iterator > getFieldIterators (List <FieldData > fieldDatas ) {
395448 List <Iterator > fieldIterators = new LinkedList <>();
396- for (FieldData fieldData : fieldDatas ) {
397- FieldDataWrapper dataWrapper = new FieldDataWrapper (fieldData );
398- fieldIterators .add (dataWrapper .getFieldData ().iterator ());
449+ if (fieldDatas != null ) {
450+ for (FieldData fieldData : fieldDatas ) {
451+ FieldDataWrapper dataWrapper = new FieldDataWrapper (fieldData );
452+ fieldIterators .add (dataWrapper .getFieldData ().iterator ());
453+ }
399454 }
400455 return fieldIterators ;
401456 }
@@ -426,7 +481,7 @@ protected BuilderProxy createBuilder(SelectorDefinition selectorDefinition)
426481 if (selectorImpl .getLimit () != null ) {
427482 buildLimitClause (builder , selectorImpl .getLimit ());
428483 }
429- if (paramMap .size () > 0 ) {
484+ if (paramMap .size () > 0 ) { // FIXME 这里永远走不进分支,paramMap 仅初始化未添加子项
430485 Gson gson = new GsonBuilder ().create ();
431486 builder .withParams (gson .toJson (paramMap ));
432487 }
@@ -445,23 +500,29 @@ protected void buildFromClause(BuilderProxy builder, Tables tables) {
445500 protected void buildSelectClause (BuilderProxy builder ,
446501 RecordSetOperator recordSetOperator ) {
447502 List <String > outputFields = new LinkedList <>();
448- for (Column column : recordSetOperator .getColumns ().getColumns ()) {
449- ColumnMetadata columnMetadata = column .getColumnMetadata ();
450- if (columnMetadata .getNestedSelector () != null ) {
451- throw new UnsupportedOperationException (
452- "Unsupport nested select clause!" );
453- } else if (columnMetadata .getCaseMetadata () != null ) {
454- throw new UnsupportedOperationException ("Unsupport case clause!" );
503+
504+ Columns columns = recordSetOperator == null ? null : recordSetOperator .getColumns ();
505+ List <Column > cols = columns == null ? null : columns .getColumns ();
506+ if (cols != null ) {
507+ for (Column column : cols ) {
508+ ColumnMetadata columnMetadata = column .getColumnMetadata ();
509+ if (columnMetadata .getNestedSelector () != null ) {
510+ throw new UnsupportedOperationException (
511+ "Unsupport nested select clause!" );
512+ } else if (columnMetadata .getCaseMetadata () != null ) {
513+ throw new UnsupportedOperationException ("Unsupport case clause!" );
514+ }
515+ outputFields .add (columnMetadata .getValue ());
455516 }
456- outputFields .add (columnMetadata .getValue ());
457517 }
518+
458519 builder .withOutFields (outputFields );
459520 }
460521
461522 protected void buildWhereClause (BuilderProxy builder , Condition condition )
462523 throws MoqlException {
463524 StringBuilder stringBuilder = new StringBuilder ();
464- buildOperand (builder , condition .getOperand (), stringBuilder );
525+ buildOperand (builder , condition == null ? null : condition .getOperand (), stringBuilder );
465526 if (stringBuilder .length () > 0 ) {
466527 builder .withExpr (stringBuilder .toString ());
467528 }
@@ -506,51 +567,60 @@ protected void buildOperand(BuilderProxy builder, Operand operand,
506567 stringBuilder .append (temp );
507568 } else if (operand instanceof Function ) {
508569 buildFunction (builder , (Function ) operand , stringBuilder );
509- } else {
510- stringBuilder .append (operand . toString () );
570+ } else if ( operand != null ) {
571+ stringBuilder .append (operand );
511572 }
512573 }
513574
514575 protected void buildFunction (BuilderProxy builder , Function function ,
515576 StringBuilder stringBuilder ) {
516- if (function .getName ().equals (RESERVED_FUNC_PARTITIONBY )) {
577+ String name = function == null ? null : function .getName ();
578+ if (name == null ) {
579+ return ;
580+ }
581+
582+ if (name .equals (RESERVED_FUNC_PARTITIONBY )) { // TODO 用 function instanceof PartitionBy 替代,避免可能的 ClassCastException
517583 PartitionBy partitionBy = (PartitionBy ) function ;
518584 builder .withPartitionNames (partitionBy .getPartitions ());
519- } else if (function . getName () .equals (RESERVED_FUNC_VMATCH )) {
585+ } else if (name .equals (RESERVED_FUNC_VMATCH )) {
520586 VMatch vMatch = (VMatch ) function ;
521587 builder .withVectorFieldName (vMatch .getVectorName ());
522588 builder .withVectors (vMatch .getVectorArray ());
523589 builder .withMetricType (vMatch .getMetricType ());
524- } else if (function . getName () .equals (RESERVED_FUNC_CONSISTENCYLEVEL )) {
590+ } else if (name .equals (RESERVED_FUNC_CONSISTENCYLEVEL )) {
525591 ConsistencyLevel consistencyLevel = (ConsistencyLevel ) function ;
526592 builder .withConsistencyLevel (consistencyLevel .getConsistencyLevel ());
527- } else if (function . getName () .equals (RESERVED_FUNC_GRACEFUL_TIME )) {
593+ } else if (name .equals (RESERVED_FUNC_GRACEFUL_TIME )) {
528594 GracefulTime gracefulTime = (GracefulTime ) function ;
529595 builder .withGracefulTime (gracefulTime .getGracefulTime ());
530- } else if (function . getName () .equals (RESERVED_FUNC_GUARANTEE_TIMESTAMP )) {
596+ } else if (name .equals (RESERVED_FUNC_GUARANTEE_TIMESTAMP )) {
531597 GuaranteeTimestamp guaranteeTimestamp = (GuaranteeTimestamp ) function ;
532598 builder .withGuaranteeTimestamp (
533599 guaranteeTimestamp .getGuaranteeTimestamp ());
534- } else if (function . getName () .equals (RESERVED_FUNC_ROUND_DECIMAL )) {
600+ } else if (name .equals (RESERVED_FUNC_ROUND_DECIMAL )) {
535601 RoundDecimal roundDecimal = (RoundDecimal ) function ;
536602 builder .withRoundDecimal (roundDecimal .getRoundDecimal ());
537- } else if (function . getName () .equals (RESERVED_FUNC_TRAVEL_TIMESTAMP )) {
603+ } else if (name .equals (RESERVED_FUNC_TRAVEL_TIMESTAMP )) {
538604 TravelTimestamp travelTimestamp = (TravelTimestamp ) function ;
539605 builder .withTravelTimestamp (travelTimestamp .getTravelTimestamp ());
540- } else if (function . getName () .equals (RESERVED_FUNC_NPROBE )) {
606+ } else if (name .equals (RESERVED_FUNC_NPROBE )) {
541607 NProbe nProbe = (NProbe ) function ;
542608 builder .withNProbe (nProbe .getnProbe ());
543- } else if (function . getName () .equals (RESERVED_FUNC_EF )) {
609+ } else if (name .equals (RESERVED_FUNC_EF )) {
544610 Ef ef = (Ef ) function ;
545611 builder .withEf (ef .getEf ());
546- } else if (function . getName () .equals (RESERVED_FUNC_SEARCHK )) {
612+ } else if (name .equals (RESERVED_FUNC_SEARCHK )) {
547613 SearchK searchK = (SearchK ) function ;
548614 builder .withSearchK (searchK .getSearchK ());
549615 }
550616 }
551617
552618 protected void buildLimitClause (BuilderProxy builder , Limit limit ) {
553- LimitMetadata limitMetadata = limit .getLimitMetadata ();
619+ LimitMetadata limitMetadata = limit == null ? null : limit .getLimitMetadata ();
620+ if (limitMetadata == null ) {
621+ return ;
622+ }
623+
554624 builder .withTopK (limitMetadata .getValue ());
555625 if (limitMetadata .getOffset () != 0 )
556626 builder .withOffset (limitMetadata .getOffset ());
0 commit comments