internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var entityKinds = rootElement
                              .GetDescendants <SyntaxElement>(s => s.Kind == SyntaxKind.TableKeyword ||
                                                              s.Kind == SyntaxKind.DatabaseKeyword)
                              .Select(s => s.Kind);

            if (!entityKinds.Any())
            {
                throw new DeltaException("Alter retention policy requires to act on a table or database (cluster isn't supported)");
            }
            var entityKind = entityKinds.First();
            var entityType = entityKind == SyntaxKind.TableKeyword
                ? EntityType.Table
                : EntityType.Database;
            var entityName = rootElement.GetDescendants <NameReference>().Last();
            var policyText = QuotedText.FromLiteral(
                rootElement.GetUniqueDescendant <LiteralExpression>(
                    "RetentionPolicy",
                    e => e.NameInParent == "RetentionPolicy"));
            var policy = Deserialize <JsonDocument>(policyText.Text);

            if (policy == null)
            {
                throw new DeltaException(
                          $"Can't extract policy objects from {policyText.ToScript()}");
            }

            return(new AlterRetentionPolicyCommand(
                       entityType,
                       EntityName.FromCode(entityName.Name),
                       policy));
        }
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var entityKinds = rootElement
                              .GetDescendants <SyntaxElement>(s => s.Kind == SyntaxKind.TableKeyword ||
                                                              s.Kind == SyntaxKind.DatabaseKeyword)
                              .Select(s => s.Kind);

            if (!entityKinds.Any())
            {
                throw new DeltaException("Alter caching policy requires to act on a table or database (cluster isn't supported)");
            }
            var entityKind = entityKinds.First();
            var entityType = entityKind == SyntaxKind.TableKeyword
                ? EntityType.Table
                : EntityType.Database;
            var entityName = rootElement
                             .GetDescendants <NameReference>(n => n.NameInParent == "TableName" ||
                                                             n.NameInParent == "DatabaseName" ||
                                                             n.NameInParent == "Selector")
                             .Last();

            var(hotData, hotIndex) = ExtractHotDurations(rootElement);

            return(new AlterCachingPolicyCommand(
                       entityType,
                       EntityName.FromCode(entityName.Name),
                       hotData,
                       hotIndex));
        }
示例#3
0
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var cleanTableName = rootElement
                                 .GetDescendants <TokenName>(e => e.NameInParent == "Name")
                                 .FirstOrDefault();
            var tableName = rootElement.GetDescendants <NameReference>().Last();

            if ((cleanTableName == null || cleanTableName.Name.Text == string.Empty) &&
                tableName == null)
            {
                throw new DeltaException("Can't find table name");
            }

            var policiesText = QuotedText.FromLiteral(
                rootElement.GetUniqueDescendant <LiteralExpression>(
                    "UpdatePolicy",
                    e => e.NameInParent == "UpdatePolicy"));
            var policies = Deserialize <UpdatePolicy[]>(policiesText.Text);

            if (policies == null)
            {
                throw new DeltaException(
                          $"Can't extract policy objects from {policiesText.ToScript()}");
            }

            return(new AlterUpdatePolicyCommand(
                       EntityName.FromCode(tableName.Name),
                       policies));
        }
示例#4
0
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var tableName = rootElement.GetUniqueDescendant <NameDeclaration>(
                "TableName",
                n => n.NameInParent == "TableName");
            var folder    = GetProperty(rootElement, SyntaxKind.FolderKeyword);
            var docString = GetProperty(rootElement, SyntaxKind.DocStringKeyword);
            var columns   = rootElement
                            .GetDescendants <NameDeclaration>(n => n.NameInParent == "ColumnName")
                            .Select(n => n.Parent)
                            .Select(n => new
            {
                Name = n.GetUniqueDescendant <NameDeclaration>("Table column name"),
                Type = n.GetUniqueDescendant <PrimitiveTypeExpression>("Table column type")
            })
                            .Select(c => new TableColumn(
                                        EntityName.FromCode(c.Name),
                                        c.Type.Type.Text));

            return(new CreateTableCommand(
                       EntityName.FromCode(tableName),
                       columns,
                       folder,
                       docString));
        }
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var tableNameDeclaration = rootElement.GetUniqueDescendant <NameDeclaration>(
                "Table Name",
                n => n.NameInParent == "TableName");
            var mappingNameExpression = rootElement.GetUniqueDescendant <LiteralExpression>(
                "Mapping Name",
                n => n.NameInParent == "MappingName");
            var mappingKindToken = rootElement.GetUniqueDescendant <SyntaxToken>(
                "Mapping Kind",
                n => n.NameInParent == "MappingKind");
            var mappingFormatExpression = rootElement.GetUniqueDescendant <LiteralExpression>(
                "Mapping Format",
                n => n.NameInParent == "MappingFormat");
            var mappingFormatFirstPart  = QuotedText.FromLiteral(mappingFormatExpression);
            var mappingFormatExtraParts = rootElement
                                          .GetDescendants <CompoundStringLiteralExpression>()
                                          .SelectMany(c => c.Tokens)
                                          .Select(t => QuotedText.FromToken(t));
            var mappingFormatParts = mappingFormatExtraParts
                                     .Prepend(mappingFormatFirstPart)
                                     .Select(q => q.Text);
            var mappingFormat = string.Concat(mappingFormatParts);
            var command       = new CreateMappingCommand(
                EntityName.FromCode(tableNameDeclaration),
                mappingKindToken.Text,
                QuotedText.FromLiteral(mappingNameExpression),
                QuotedText.FromText(mappingFormat) !);

            return(command);
        }
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var identifiers = rootElement
                .GetDescendants<SyntaxNode>(e => e.NameInParent == "Name"
                && e.Kind != SyntaxKind.BracketedName)
                .Select(t => EntityName.FromCode(t));
            var literals = rootElement
                .GetDescendants<LiteralExpression>(e => e.NameInParent == "DocString")
                .Select(l => new QuotedText(l.LiteralValue.ToString()!));

            if (identifiers.Count() < 1)
            {
                throw new DeltaException("There should be at least one identifier in the command");
            }
            if (identifiers.Count() != literals.Count() + 1)
            {
                throw new DeltaException("Mismatch number of identifiers vs literals");
            }

            var tableName = identifiers.First();
            var columns = identifiers
                .Skip(1)
                .Zip(literals, (id, lit) => new ColumnDocString(id, lit));

            return new AlterMergeTableColumnDocStringsCommand(tableName, columns);
        }
示例#7
0
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var nameReferences = rootElement.GetDescendants <NameReference>();
            var names          = nameReferences
                                 .Select(n => new EntityName(n.Name.SimpleName))
                                 .ToImmutableArray();

            return(new DropTablesCommand(names));
        }
示例#8
0
        private static QuotedText?GetProperty(SyntaxElement rootElement, SyntaxKind kind)
        {
            var literal = rootElement
                          .GetDescendants <SyntaxElement>(e => e.Kind == kind)
                          .Select(e => e.Parent.GetDescendants <LiteralExpression>().FirstOrDefault())
                          .FirstOrDefault();

            return(literal == null
                ? null
                : QuotedText.FromLiteral(literal));
        }
示例#9
0
        public static IReadOnlyList <TElement> GetImmediateDescendants <TElement>(
            this SyntaxElement parent,
            Func <TElement, bool>?predicate = null)
            where TElement : SyntaxElement
        {
            var descendants = parent.GetDescendants <TElement>(
                e => e.Parent == parent &&
                (predicate == null || predicate(e)));

            return(descendants);
        }
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var tableNameReference = rootElement.GetUniqueDescendant <NameReference>(
                "Table name",
                n => n.NameInParent == "TableName");
            var columnNameReferences = rootElement.GetDescendants <NameReference>(
                n => n.NameInParent != "TableName");
            var tableName   = new EntityName(tableNameReference.Name.SimpleName);
            var columnNames = columnNameReferences
                              .Select(n => new EntityName(n.Name.SimpleName))
                              .ToImmutableArray();

            return(new DropTableColumnsCommand(tableName, columnNames));
        }
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var nameReferences = rootElement.GetDescendants <NameReference>();

            if (nameReferences.Count != 2)
            {
                throw new DeltaException($"Expected 2 names but got {nameReferences.Count}");
            }

            var tableName      = new EntityName(nameReferences[0].Name.SimpleName);
            var columnName     = new EntityName(nameReferences[1].Name.SimpleName);
            var typeExpression = rootElement.GetUniqueDescendant <PrimitiveTypeExpression>("Primitive type");
            var type           = typeExpression.Type.ValueText;

            return(new AlterColumnTypeCommand(tableName, columnName, type));
        }
示例#12
0
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var tableName  = rootElement.GetDescendants <NameReference>().Last();
            var policyText = QuotedText.FromLiteral(
                rootElement.GetUniqueDescendant <LiteralExpression>(
                    "AutoDeletePolicy",
                    e => e.NameInParent == "AutoDeletePolicy"));
            var policy = Deserialize <JsonDocument>(policyText.Text);

            if (policy == null)
            {
                throw new DeltaException(
                          $"Can't extract policy objects from {policyText.ToScript()}");
            }

            return(new AlterAutoDeletePolicyCommand(EntityName.FromCode(tableName.Name), policy));
        }
示例#13
0
        public static TElement GetUniqueDescendant <TElement>(
            this SyntaxElement parent,
            string descendantNameForExceptionMessage,
            Func <TElement, bool>?predicate = null)
            where TElement : SyntaxElement
        {
            var descendants = parent.GetDescendants <TElement>(predicate);

            if (descendants.Count != 1)
            {
                throw new DeltaException(
                          $"There should be one-and-only-one {descendantNameForExceptionMessage} but there are {descendants.Count}",
                          parent.Root.ToString(IncludeTrivia.All));
            }

            return(descendants.First());
        }
示例#14
0
        public static IEnumerable <TElement> GetAtLeastOneDescendant <TElement>(
            this SyntaxElement parent,
            string descendantNameForExceptionMessage,
            Func <TElement, bool>?predicate = null)
            where TElement : SyntaxElement
        {
            var descendants = parent.GetDescendants <TElement>(predicate);

            if (!descendants.Any())
            {
                throw new DeltaException(
                          $"There should be at least one {descendantNameForExceptionMessage} but there are none",
                          parent.Root.ToString(IncludeTrivia.All));
            }

            return(descendants);
        }
示例#15
0
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var entityKinds = rootElement
                              .GetDescendants <SyntaxElement>(s => s.Kind == SyntaxKind.TableKeyword ||
                                                              s.Kind == SyntaxKind.DatabaseKeyword)
                              .Select(s => s.Kind);

            if (!entityKinds.Any())
            {
                throw new DeltaException("Delete ingestionbatching policy requires to act on a table or database (cluster isn't supported)");
            }
            var entityKind = entityKinds.First();
            var entityType = entityKind == SyntaxKind.TableKeyword
                ? EntityType.Table
                : EntityType.Database;
            var entityName = rootElement.GetFirstDescendant <NameReference>();

            return(new DeleteIngestionBatchingPolicyCommand(entityType, EntityName.FromCode(entityName.Name)));
        }
示例#16
0
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var(folder, docString) = ExtractWithProperties(rootElement);
            Func <NameDeclaration, InnerTable> tableExtraction = (table) =>
            {
                var columns = table
                              .Parent
                              .GetDescendants <SeparatedElement>()
                              .Select(s => new TableColumn(
                                          EntityName.FromCode(s.GetUniqueDescendant <NameDeclaration>("Column Name")),
                                          s.GetUniqueDescendant <PrimitiveTypeExpression>("Column Type").Type.Text));

                return(new InnerTable(EntityName.FromCode(table), columns));
            };
            var tables = rootElement
                         .GetDescendants <NameDeclaration>(n => n.NameInParent == "TableName")
                         .Select(t => tableExtraction(t));

            return(new CreateTablesCommand(tables, folder, docString));
        }
示例#17
0
        private static (QuotedText?folder, QuotedText?docString) ExtractWithProperties(
            SyntaxElement rootElement)
        {
            var keywords = rootElement
                           .GetDescendants <SyntaxElement>(n => n.Kind == SyntaxKind.FolderKeyword ||
                                                           n.Kind == SyntaxKind.DocStringKeyword);

            if (!keywords.Any())
            {
                return(null, null);
            }
            else
            {
                var propertiesParent = keywords.First().Parent;
                var tokenSequence    = propertiesParent
                                       .GetDescendants <SyntaxToken>(n => n.Kind == SyntaxKind.StringLiteralToken)
                                       .Select(n => QuotedText.FromToken(n));
                var zip = keywords
                          .Select(n => n.Kind)
                          .Zip(tokenSequence, (k, t) => (k, t))
                          .ToImmutableArray();
                QuotedText?folder    = null;
                QuotedText?docString = null;

                foreach (var p in zip)
                {
                    if (p.k == SyntaxKind.FolderKeyword)
                    {
                        folder = p.t;
                    }
                    else
                    {
                        docString = p.t;
                    }
                }

                return(folder, docString);
            }
        }
        internal static CommandBase FromCode(SyntaxElement rootElement)
        {
            var tableNames = rootElement
                             .GetDescendants <SyntaxElement>(n => n.NameInParent == "Name"
                             //  Different type depending if it's a simple name or not
                                                             && (n is TokenName || n is LiteralExpression))
                             //.GetDescendants<LiteralExpression>(n => n.NameInParent == "Name")
                             //.GetDescendants<TokenName>(n => n.NameInParent == "Name")
                             .Select(t => EntityName.FromCode(t));
            var policyText = QuotedText.FromLiteral(
                rootElement.GetUniqueDescendant <LiteralExpression>(
                    "RetentionPolicy",
                    e => e.NameInParent == "RetentionPolicy"));
            var policy = Deserialize <JsonDocument>(policyText.Text);

            if (policy == null)
            {
                throw new DeltaException(
                          $"Can't extract policy objects from {policyText.ToScript()}");
            }

            return(new AlterTablesRetentionPolicyCommand(tableNames, policy));
        }
        private static IImmutableDictionary <SyntaxKind, TimeSpan> GetHotDurations(SyntaxElement rootElement)
        {
            var elements = rootElement.GetDescendants <SyntaxElement>();
            var builder  = ImmutableDictionary <SyntaxKind, TimeSpan> .Empty.ToBuilder();

            SyntaxKind?kind = null;

            foreach (var e in elements)
            {
                if (kind == null)
                {
                    if (e.Kind == SyntaxKind.HotKeyword ||
                        e.Kind == SyntaxKind.HotDataKeyword ||
                        e.Kind == SyntaxKind.HotIndexKeyword)
                    {
                        if (!e.IsMissing)
                        {
                            kind = e.Kind;
                        }
                    }
                }
                else
                {
                    if (e.Kind == SyntaxKind.TimespanLiteralToken)
                    {
                        var token = (SyntaxToken)e;
                        var t     = (TimeSpan)token.Value;

                        builder.Add(kind.Value, t);
                        kind = null;
                    }
                }
            }

            return(builder.ToImmutable());
        }