How to add a partition boundary only when not exists in SQL Data Warehouse?

1.5k views Asked by At

I am using Azure SQL Data Warehouse Gen 1, and I create a partition table like this

CREATE TABLE [dbo].[StatsPerBin1](
[Bin1] [varchar](100) NOT NULL,
[TimeWindow] [datetime] NOT NULL,
[Count] [int] NOT NULL,
[Timestamp] [datetime] NOT NULL)
WITH
(
    DISTRIBUTION = HASH ( [Bin1] ),
    CLUSTERED INDEX([Bin1]),
    PARTITION
    (
        [TimeWindow] RANGE RIGHT FOR VALUES ()
     )
)

How should I split a partition only when there is no such boundary?

First I think if I can get partition boundaries by table name, then I can write a if statement to determine add partition boundary or not.

But I cannot find a way to associate a table with its corresponding partition values, the partition values of all partitions can be retrieved by

SELECT * FROM sys.partition_range_values

But it only contains function_id as identifier which I don't know how to join other tables so that I can get partition boundaries by table name.

2

There are 2 answers

0
Kal On

Have you tried joining sys.partition_range_values with sys.partition_functions view?

Granted we cannot create partition functions in SQL DW, but the view seems to be still supported.

0
tomsmith On

I know this is an out of date question, but I was having the same problem. Here is a query I ended up with that can get you started. It is modified slightly from a query for SQL Server documentation:

SELECT      s.[name]                        AS      [schema_name]
,           t.[name]                        AS      [table_name]
,           p.[partition_number]            AS      [partition_number]
,           rv.[value]                      AS      [partition_boundary_value]
,           p.[data_compression_desc]       AS      [partition_compression_desc]
FROM        sys.schemas s
JOIN        sys.tables t                    ON      t.[schema_id]         = s.[schema_id]
JOIN        sys.partitions p                ON      p.[object_id]         = t.[object_id]
JOIN        sys.indexes i                   ON      i.[object_id]         = p.[object_id]
                                            AND     i.[index_id]          = p.[index_id]
JOIN        sys.data_spaces ds              ON      ds.[data_space_id]    = i.[data_space_id]
LEFT JOIN   sys.partition_schemes ps        ON      ps.[data_space_id]    = ds.[data_space_id]
LEFT JOIN   sys.partition_functions pf      ON      pf.[function_id]      = ps.[function_id]
LEFT JOIN   sys.partition_range_values rv   ON      rv.[function_id]      = pf.[function_id]
                                            AND     rv.[boundary_id]      = p.[partition_number]