Ejemplo n.º 1
0
        public void TestGetSchema()
        {
            var expected = ImmutableList.Create(
                new ColumnMetadata("custkey", typeof(long), null),
                new ColumnMetadata("name", typeof(string), null),
                new ColumnMetadata("address", typeof(string), null),
                new ColumnMetadata("nationkey", typeof(long), null),
                new ColumnMetadata("phone", typeof(string), null),
                new ColumnMetadata("acctbal", typeof(double), null),
                new ColumnMetadata("mktsegment", typeof(string), null),
                new ColumnMetadata("comment", typeof(string), null)
                );

            var schema = SqlExecutor.GetSchema("select * from customer");

            GetColumnsComparer.Compare(expected, schema.Columns);
        }
Ejemplo n.º 2
0
        public async Task <Transport.TransportPartitionsResult> GetPartitions(bool canHandlePartitions, string sql, SqlParameters sqlParameters, HttpContext httpContext)
        {
            var sqlTree = _sqlParser.Parse(sql, out var errors);

            foreach (var header in httpContext.Request.Headers)
            {
                if (header.Key.StartsWith("P_", StringComparison.OrdinalIgnoreCase))
                {
                    var parameterName = header.Key.Substring(2);

                    if (header.Value.Count > 1)
                    {
                        throw new SqlErrorException("Two parameters found with the same name in the http headers.");
                    }
                    var value = header.Value.First();

                    var base64Value = Convert.ToBase64String(Encoding.UTF8.GetBytes(value));

                    //Add the parameter as base64 inline parameter
                    sqlTree.Statements.Insert(0, new SetVariableStatement()
                    {
                        VariableReference = new SqlParser.Expressions.VariableReference()
                        {
                            Name = parameterName
                        },
                        ScalarExpression = new Base64Literal()
                        {
                            Value = base64Value
                        }
                    });
                }
            }

            if (errors.Count > 0)
            {
                throw new SqlErrorException(errors.First().Message);
            }

            //Apply the row level security filter on the query
            await RowLevelSecurityHelper.ApplyRowLevelSecurity(sqlTree, httpContext, _metadataStore, _serviceProvider);

            var schema = _sqlExecutor.GetSchema(sqlTree, sqlParameters);

            //Take table name and get the partition resolver
            if (!_metadataStore.TryGetTable(schema.TableName, out var table))
            {
                throw new SqlErrorException($"The table {schema.TableName} was not found.");
            }

            //Get the partitions
            var discoveryService  = _serviceProvider.GetService <IDiscoveryService>();
            var partitionsBuilder = new PartitionsBuilder(sqlTree);
            var partitions        = await table.PartitionResolver.GetPartitions(canHandlePartitions, partitionsBuilder, httpContext, new PartitionOptions(_serviceProvider, discoveryService));

            var partitionListBuilder = ImmutableList.CreateBuilder <Transport.TransportPartition>();

            foreach (var partition in partitions)
            {
                List <TransportServiceLocation> locations = new List <TransportServiceLocation>();
                foreach (var location in partition.Locations)
                {
                    locations.Add(new TransportServiceLocation(location.Host, location.Tls));
                }
                partitionListBuilder.Add(new Transport.TransportPartition(locations, partition.SqlTree.Print()));
            }

            return(new Transport.TransportPartitionsResult(ConvertColumns(schema.Columns), partitionListBuilder.ToImmutable()));
        }