예제 #1
0
 public FieldToFetch(string name, SelectField queryField, string projectedName, bool canExtractFromIndex, bool isDocumentId)
 {
     Name                = name;
     QueryField          = queryField;
     ProjectedName       = projectedName;
     IsDocumentId        = isDocumentId;
     CanExtractFromIndex = canExtractFromIndex;
 }
예제 #2
0
        private static FieldToFetch GetFieldToFetch(
            IndexDefinitionBaseServerSide indexDefinition,
            QueryMetadata metadata,
            ProjectionBehavior?projectionBehavior,
            SelectField selectField,
            Dictionary <string, FieldToFetch> results,
            out string selectFieldKey,
            ref bool anyExtractableFromIndex,
            ref bool extractAllStoredFields,
            ref bool anyTimeSeries)
        {
            var mustExtractFromIndex  = projectionBehavior.FromIndexOnly();
            var maybeExtractFromIndex = projectionBehavior.FromIndexOrDefault();

            selectFieldKey = selectField.Alias ?? selectField.Name;
            var selectFieldName = selectField.Name;

            if (selectField.ValueTokenType != null)
            {
                return(new FieldToFetch(string.Empty, selectField, selectField.Alias,
                                        canExtractFromIndex: false, isDocumentId: false, isTimeSeries: false));
            }

            if (selectField.Function != null)
            {
                var isTimeSeries = metadata.DeclaredFunctions != null && metadata.DeclaredFunctions.TryGetValue(selectField.Function, out var func) && func.Type == DeclaredFunction.FunctionType.TimeSeries;
                if (isTimeSeries)
                {
                    anyTimeSeries = true;
                }

                var fieldToFetch = new FieldToFetch(
                    selectField.Name,
                    selectField,
                    selectField.Alias,
                    canExtractFromIndex: false,
                    isDocumentId: false,
                    isTimeSeries: isTimeSeries)
                {
                    FunctionArgs = new FieldToFetch[selectField.FunctionArgs.Length]
                };

                for (int j = 0; j < selectField.FunctionArgs.Length; j++)
                {
                    var ignored = false;
                    fieldToFetch.FunctionArgs[j] = GetFieldToFetch(
                        indexDefinition,
                        metadata,
                        projectionBehavior,
                        selectField.FunctionArgs[j],
                        null,
                        out _,
                        ref ignored,
                        ref ignored,
                        ref ignored
                        );
                }
                return(fieldToFetch);
            }

            if (selectField.IsCounter)
            {
                var fieldToFetch = new FieldToFetch(selectField.Name, selectField, selectField.Alias ?? selectField.Name,
                                                    canExtractFromIndex: false, isDocumentId: false, isTimeSeries: false);
                if (selectField.FunctionArgs != null)
                {
                    fieldToFetch.FunctionArgs = new FieldToFetch[0];
                }

                return(fieldToFetch);
            }

            if (selectFieldName == null)
            {
                if (selectField.IsGroupByKey == false)
                {
                    return(null);
                }

                if (selectField.GroupByKeys.Length == 1)
                {
                    selectFieldName = selectField.GroupByKeys[0].Name;

                    if (selectFieldKey == null)
                    {
                        selectFieldKey = selectFieldName;
                    }
                }
                else
                {
                    selectFieldKey = selectFieldKey ?? "Key";
                    return(new FieldToFetch(selectFieldKey, selectField.GroupByKeyNames));
                }
            }

            if (indexDefinition == null)
            {
                return(new FieldToFetch(selectFieldName, selectField, selectField.Alias, canExtractFromIndex: false, isDocumentId: false, isTimeSeries: false));
            }

            if (selectFieldName.Value.Length > 0)
            {
                if (selectFieldName == Constants.Documents.Indexing.Fields.DocumentIdFieldName)
                {
                    anyExtractableFromIndex = maybeExtractFromIndex;

                    return(new FieldToFetch(selectFieldName, selectField, selectField.Alias,
                                            canExtractFromIndex: indexDefinition is MapReduceIndexDefinition or AutoMapReduceIndexDefinition,
                                            isDocumentId: true, isTimeSeries: false));
                }

                if (selectFieldName.Value[0] == '_')
                {
                    if (selectFieldName == Constants.Documents.Indexing.Fields.AllStoredFields)
                    {
                        if (results == null)
                        {
                            ThrowInvalidFetchAllStoredDocuments();
                        }
                        Debug.Assert(results != null);
                        results.Clear(); // __all_stored_fields should only return stored fields so we are ensuring that no other fields will be returned

                        extractAllStoredFields = maybeExtractFromIndex;

                        foreach (var kvp in indexDefinition.MapFields)
                        {
                            var stored = kvp.Value.Storage == FieldStorage.Yes;
                            if (stored == false)
                            {
                                continue;
                            }

                            anyExtractableFromIndex = maybeExtractFromIndex;
                            results[kvp.Key]        = new FieldToFetch(kvp.Key, null, null, canExtractFromIndex: maybeExtractFromIndex, isDocumentId: false, isTimeSeries: false);
                        }

                        return(null);
                    }
                }
            }

            var bySourceAlias = ShouldTryToExtractBySourceAliasName(selectFieldName.Value, selectField);
            var key           = bySourceAlias
                    ? selectField.SourceAlias
                    : selectFieldName;

            var extract = mustExtractFromIndex || (maybeExtractFromIndex && indexDefinition.MapFields.TryGetValue(key, out var value) && value.Storage == FieldStorage.Yes);

            if (extract)
            {
                anyExtractableFromIndex = true;
            }

            if (bySourceAlias == false && maybeExtractFromIndex)
            {
                extract |= indexDefinition.HasDynamicFields;
            }

            return(new FieldToFetch(selectFieldName, selectField, selectField.Alias, extract, isDocumentId: false, isTimeSeries: false));
        }
예제 #3
0
 private static bool ShouldTryToExtractBySourceAliasName(string selectFieldName, SelectField selectField)
 {
     return(selectFieldName.Length == 0 &&
            selectField.HasSourceAlias &&
            selectField.SourceAlias != null);
 }
예제 #4
0
        private static FieldToFetch GetFieldToFetch(
            IndexDefinitionBase indexDefinition,
            SelectField selectField,
            Dictionary <string, FieldToFetch> results,
            out string selectFieldKey,
            ref bool anyExtractableFromIndex,
            ref bool extractAllStoredFields)
        {
            selectFieldKey = selectField.Alias ?? selectField.Name;
            var selectFieldName = selectField.Name;

            if (selectField.ValueTokenType != null)
            {
                return(new FieldToFetch(string.Empty, selectField, selectField.Alias,
                                        canExtractFromIndex: false, isDocumentId: false));
            }
            if (selectField.Function != null)
            {
                var fieldToFetch = new FieldToFetch(selectField.Name, selectField, selectField.Alias,
                                                    canExtractFromIndex: false, isDocumentId: false)
                {
                    FunctionArgs = new FieldToFetch[selectField.FunctionArgs.Length]
                };
                for (int j = 0; j < selectField.FunctionArgs.Length; j++)
                {
                    bool ignored = false;
                    fieldToFetch.FunctionArgs[j] = GetFieldToFetch(indexDefinition,
                                                                   selectField.FunctionArgs[j],
                                                                   null,
                                                                   out _,
                                                                   ref ignored,
                                                                   ref ignored
                                                                   );
                }
                return(fieldToFetch);
            }

            if (selectFieldName == null)
            {
                if (selectField.IsGroupByKey == false)
                {
                    return(null);
                }

                if (selectField.GroupByKeys.Length == 1)
                {
                    selectFieldName = selectField.GroupByKeys[0];

                    if (selectFieldKey == null)
                    {
                        selectFieldKey = selectFieldName;
                    }
                }
                else
                {
                    selectFieldKey = selectFieldKey ?? "Key";
                    return(new FieldToFetch(selectFieldKey, selectField.GroupByKeys));
                }
            }
            if (indexDefinition == null)
            {
                return(new FieldToFetch(selectFieldName, selectField, selectField.Alias, canExtractFromIndex: false, isDocumentId: false));
            }
            if (selectFieldName.Length > 0)
            {
                if (selectFieldName == Constants.Documents.Indexing.Fields.DocumentIdFieldName)
                {
                    anyExtractableFromIndex = true;
                    return(new FieldToFetch(selectFieldName, selectField, selectField.Alias, canExtractFromIndex: false, isDocumentId: true));
                }

                if (selectFieldName[0] == '_')
                {
                    if (selectFieldName == Constants.Documents.Indexing.Fields.AllStoredFields)
                    {
                        if (results == null)
                        {
                            ThrowInvalidFetchAllStoredDocuments();
                        }
                        Debug.Assert(results != null);
                        results.Clear(); // __all_stored_fields should only return stored fields so we are ensuring that no other fields will be returned

                        extractAllStoredFields = true;

                        foreach (var kvp in indexDefinition.MapFields)
                        {
                            var stored = kvp.Value.Storage == FieldStorage.Yes;
                            if (stored == false)
                            {
                                continue;
                            }

                            anyExtractableFromIndex = true;
                            results[kvp.Key]        = new FieldToFetch(kvp.Key, null, null, canExtractFromIndex: true, isDocumentId: false);
                        }

                        return(null);
                    }
                }
            }

            var extract = indexDefinition.MapFields.TryGetValue(selectFieldName, out var value) && value.Storage == FieldStorage.Yes;

            if (extract)
            {
                anyExtractableFromIndex = true;
            }

            return(new FieldToFetch(selectFieldName, selectField, selectField.Alias, extract | indexDefinition.HasDynamicFields, isDocumentId: false));
        }