github.com/vc42/parquet-go@v0.0.0-20240320194221-1a9adb5f23f5/page.go (about)

     1  package parquet
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"fmt"
     7  	"io"
     8  
     9  	"github.com/vc42/parquet-go/deprecated"
    10  	"github.com/vc42/parquet-go/encoding/plain"
    11  	"github.com/vc42/parquet-go/internal/bitpack"
    12  	"github.com/vc42/parquet-go/internal/unsafecast"
    13  )
    14  
    15  // Page values represent sequences of parquet values. From the Parquet
    16  // documentation: "Column chunks are a chunk of the data for a particular
    17  // column. They live in a particular row group and are guaranteed to be
    18  // contiguous in the file. Column chunks are divided up into pages. A page is
    19  // conceptually an indivisible unit (in terms of compression and encoding).
    20  // There can be multiple page types which are interleaved in a column chunk."
    21  //
    22  // https://github.com/apache/parquet-format#glossary
    23  type Page interface {
    24  	// Returns the type of values read from this page.
    25  	//
    26  	// The returned type can be used to encode the page data, in the case of
    27  	// an indexed page (which has a dictionary), the type is configured to
    28  	// encode the indexes stored in the page rather than the plain values.
    29  	Type() Type
    30  
    31  	// Returns the column index that this page belongs to.
    32  	Column() int
    33  
    34  	// If the page contains indexed values, calling this method returns the
    35  	// dictionary in which the values are looked up. Otherwise, the method
    36  	// returns nil.
    37  	Dictionary() Dictionary
    38  
    39  	// Returns the number of rows, values, and nulls in the page. The number of
    40  	// rows may be less than the number of values in the page if the page is
    41  	// part of a repeated column.
    42  	NumRows() int64
    43  	NumValues() int64
    44  	NumNulls() int64
    45  
    46  	// Returns the min and max values currently buffered in the writer.
    47  	//
    48  	// The third value is a boolean indicating whether the page bounds were
    49  	// available. Page bounds may not be known if the page contained no values
    50  	// or only nulls, or if they were read from a parquet file which had neither
    51  	// page statistics nor a page index.
    52  	Bounds() (min, max Value, ok bool)
    53  
    54  	// Returns the size of the page in bytes (uncompressed).
    55  	Size() int64
    56  
    57  	// Returns a reader exposing the values contained in the page.
    58  	//
    59  	// Depending on the underlying implementation, the returned reader may
    60  	// support reading an array of typed Go values by implementing interfaces
    61  	// like parquet.Int32Reader. Applications should use type assertions on
    62  	// the returned reader to determine whether those optimizations are
    63  	// available.
    64  	Values() ValueReader
    65  
    66  	// Buffer returns the page as a BufferedPage, which may be the page itself
    67  	// if it was already buffered.
    68  	Buffer() BufferedPage
    69  }
    70  
    71  // BufferedPage is an extension of the Page interface implemented by pages
    72  // that are buffered in memory.
    73  type BufferedPage interface {
    74  	Page
    75  
    76  	// Returns a copy of the page which does not share any of the buffers, but
    77  	// contains the same values, repetition and definition levels.
    78  	Clone() BufferedPage
    79  
    80  	// Returns a new page which is as slice of the receiver between row indexes
    81  	// i and j.
    82  	Slice(i, j int64) BufferedPage
    83  
    84  	// Expose the lists of repetition and definition levels of the page.
    85  	//
    86  	// The returned slices may be empty when the page has no repetition or
    87  	// definition levels.
    88  	RepetitionLevels() []byte
    89  	DefinitionLevels() []byte
    90  
    91  	// Returns the in-memory buffer holding the page values.
    92  	//
    93  	// The buffer has the page values serialized in the PLAIN encoding.
    94  	//
    95  	// The intent is for the returned value to be used as input parameter when
    96  	// calling the Encode method of the associated Type.
    97  	//
    98  	// The returned slice may be the same across multiple calls to this method,
    99  	// applications must treat the content as immutable.
   100  	Data() []byte
   101  }
   102  
   103  // CompressedPage is an extension of the Page interface implemented by pages
   104  // that have been compressed to their on-file representation.
   105  type CompressedPage interface {
   106  	Page
   107  
   108  	// Returns a representation of the page header.
   109  	PageHeader() PageHeader
   110  
   111  	// Returns a reader exposing the content of the compressed page.
   112  	PageData() io.Reader
   113  
   114  	// Returns the size of the page data.
   115  	PageSize() int64
   116  
   117  	// CRC returns the IEEE CRC32 checksum of the page.
   118  	CRC() uint32
   119  }
   120  
   121  // PageReader is an interface implemented by types that support producing a
   122  // sequence of pages.
   123  type PageReader interface {
   124  	// Reads and returns the next page from the sequence. When all pages have
   125  	// been read, or if the sequence was closed, the method returns io.EOF.
   126  	//
   127  	// The returned page and other objects derived from it remain valid until
   128  	// the next call to ReadPage, or until the sequence is closed. The page
   129  	// reader may use this property to optimize resource management by reusing
   130  	// memory across pages. Applications that need to acquire ownership of the
   131  	// returned page must clone by calling page.Buffer().Clone() to create a
   132  	// copy in memory.
   133  	ReadPage() (Page, error)
   134  }
   135  
   136  // PageWriter is an interface implemented by types that support writing pages
   137  // to an underlying storage medium.
   138  type PageWriter interface {
   139  	WritePage(Page) (int64, error)
   140  }
   141  
   142  // Pages is an interface implemented by page readers returned by calling the
   143  // Pages method of ColumnChunk instances.
   144  type Pages interface {
   145  	PageReader
   146  	RowSeeker
   147  	io.Closer
   148  }
   149  
   150  func copyPagesAndClose(w PageWriter, r Pages) (int64, error) {
   151  	defer r.Close()
   152  	return CopyPages(w, r)
   153  }
   154  
   155  type singlePage struct {
   156  	page Page
   157  	seek int64
   158  }
   159  
   160  func (r *singlePage) ReadPage() (Page, error) {
   161  	if r.page != nil {
   162  		if numRows := r.page.NumRows(); r.seek < numRows {
   163  			seek := r.seek
   164  			r.seek = numRows
   165  			if seek > 0 {
   166  				return r.page.Buffer().Slice(seek, numRows), nil
   167  			}
   168  			return r.page, nil
   169  		}
   170  	}
   171  	return nil, io.EOF
   172  }
   173  
   174  func (r *singlePage) SeekToRow(rowIndex int64) error {
   175  	r.seek = rowIndex
   176  	return nil
   177  }
   178  
   179  func (r *singlePage) Close() error {
   180  	r.page = nil
   181  	r.seek = 0
   182  	return nil
   183  }
   184  
   185  func onePage(page Page) Pages { return &singlePage{page: page} }
   186  
   187  // CopyPages copies pages from src to dst, returning the number of values that
   188  // were copied.
   189  //
   190  // The function returns any error it encounters reading or writing pages, except
   191  // for io.EOF from the reader which indicates that there were no more pages to
   192  // read.
   193  func CopyPages(dst PageWriter, src PageReader) (numValues int64, err error) {
   194  	for {
   195  		p, err := src.ReadPage()
   196  		if err != nil {
   197  			if err == io.EOF {
   198  				err = nil
   199  			}
   200  			return numValues, err
   201  		}
   202  		n, err := dst.WritePage(p)
   203  		numValues += n
   204  		if err != nil {
   205  			return numValues, err
   206  		}
   207  	}
   208  }
   209  
   210  func forEachPageSlice(page BufferedPage, wantSize int64, do func(BufferedPage) error) error {
   211  	numRows := page.NumRows()
   212  	if numRows == 0 {
   213  		return nil
   214  	}
   215  
   216  	pageSize := page.Size()
   217  	numPages := (pageSize + (wantSize - 1)) / wantSize
   218  	rowIndex := int64(0)
   219  	if numPages < 2 {
   220  		return do(page)
   221  	}
   222  
   223  	for numPages > 0 {
   224  		lastRowIndex := rowIndex + ((numRows - rowIndex) / numPages)
   225  		if err := do(page.Slice(rowIndex, lastRowIndex)); err != nil {
   226  			return err
   227  		}
   228  		rowIndex = lastRowIndex
   229  		numPages--
   230  	}
   231  
   232  	return nil
   233  }
   234  
   235  // errorPage is an implementation of the Page interface which always errors when
   236  // attempting to read its values.
   237  //
   238  // The error page declares that it contains one value (even if it does not)
   239  // as a way to ensure that it is not ignored due to being empty when written
   240  // to a file.
   241  type errorPage struct {
   242  	typ         Type
   243  	err         error
   244  	columnIndex int
   245  }
   246  
   247  func newErrorPage(typ Type, columnIndex int, msg string, args ...interface{}) *errorPage {
   248  	return &errorPage{
   249  		typ:         typ,
   250  		err:         fmt.Errorf(msg, args...),
   251  		columnIndex: columnIndex,
   252  	}
   253  }
   254  
   255  func (page *errorPage) Type() Type                        { return page.typ }
   256  func (page *errorPage) Column() int                       { return page.columnIndex }
   257  func (page *errorPage) Dictionary() Dictionary            { return nil }
   258  func (page *errorPage) NumRows() int64                    { return 1 }
   259  func (page *errorPage) NumValues() int64                  { return 1 }
   260  func (page *errorPage) NumNulls() int64                   { return 0 }
   261  func (page *errorPage) Bounds() (min, max Value, ok bool) { return }
   262  func (page *errorPage) Clone() BufferedPage               { return page }
   263  func (page *errorPage) Slice(i, j int64) BufferedPage     { return page }
   264  func (page *errorPage) Size() int64                       { return 1 }
   265  func (page *errorPage) RepetitionLevels() []byte          { return nil }
   266  func (page *errorPage) DefinitionLevels() []byte          { return nil }
   267  func (page *errorPage) Data() []byte                      { return nil }
   268  func (page *errorPage) Values() ValueReader               { return errorPageValues{page: page} }
   269  func (page *errorPage) Buffer() BufferedPage              { return page }
   270  
   271  type errorPageValues struct{ page *errorPage }
   272  
   273  func (r errorPageValues) ReadValues([]Value) (int, error) { return 0, r.page.err }
   274  func (r errorPageValues) Close() error                    { return nil }
   275  
   276  func errPageBoundsOutOfRange(i, j, n int64) error {
   277  	return fmt.Errorf("page bounds out of range [%d:%d]: with length %d", i, j, n)
   278  }
   279  
   280  type optionalPage struct {
   281  	base               BufferedPage
   282  	maxDefinitionLevel byte
   283  	definitionLevels   []byte
   284  }
   285  
   286  func newOptionalPage(base BufferedPage, maxDefinitionLevel byte, definitionLevels []byte) *optionalPage {
   287  	return &optionalPage{
   288  		base:               base,
   289  		maxDefinitionLevel: maxDefinitionLevel,
   290  		definitionLevels:   definitionLevels,
   291  	}
   292  }
   293  
   294  func (page *optionalPage) Type() Type { return page.base.Type() }
   295  
   296  func (page *optionalPage) Column() int { return page.base.Column() }
   297  
   298  func (page *optionalPage) Dictionary() Dictionary { return page.base.Dictionary() }
   299  
   300  func (page *optionalPage) NumRows() int64 { return int64(len(page.definitionLevels)) }
   301  
   302  func (page *optionalPage) NumValues() int64 { return int64(len(page.definitionLevels)) }
   303  
   304  func (page *optionalPage) NumNulls() int64 {
   305  	return int64(countLevelsNotEqual(page.definitionLevels, page.maxDefinitionLevel))
   306  }
   307  
   308  func (page *optionalPage) Bounds() (min, max Value, ok bool) { return page.base.Bounds() }
   309  
   310  func (page *optionalPage) Size() int64 { return page.base.Size() + int64(len(page.definitionLevels)) }
   311  
   312  func (page *optionalPage) RepetitionLevels() []byte { return nil }
   313  
   314  func (page *optionalPage) DefinitionLevels() []byte { return page.definitionLevels }
   315  
   316  func (page *optionalPage) Data() []byte { return page.base.Data() }
   317  
   318  func (page *optionalPage) Values() ValueReader {
   319  	return &optionalPageValues{
   320  		page:   page,
   321  		values: page.base.Values(),
   322  	}
   323  }
   324  
   325  func (page *optionalPage) Buffer() BufferedPage { return page }
   326  
   327  func (page *optionalPage) Clone() BufferedPage {
   328  	return newOptionalPage(
   329  		page.base.Clone(),
   330  		page.maxDefinitionLevel,
   331  		append([]byte{}, page.definitionLevels...),
   332  	)
   333  }
   334  
   335  func (page *optionalPage) Slice(i, j int64) BufferedPage {
   336  	numNulls1 := int64(countLevelsNotEqual(page.definitionLevels[:i], page.maxDefinitionLevel))
   337  	numNulls2 := int64(countLevelsNotEqual(page.definitionLevels[i:j], page.maxDefinitionLevel))
   338  	return newOptionalPage(
   339  		page.base.Slice(i-numNulls1, j-(numNulls1+numNulls2)),
   340  		page.maxDefinitionLevel,
   341  		page.definitionLevels[i:j],
   342  	)
   343  }
   344  
   345  type repeatedPage struct {
   346  	base               BufferedPage
   347  	maxRepetitionLevel byte
   348  	maxDefinitionLevel byte
   349  	definitionLevels   []byte
   350  	repetitionLevels   []byte
   351  }
   352  
   353  func newRepeatedPage(base BufferedPage, maxRepetitionLevel, maxDefinitionLevel byte, repetitionLevels, definitionLevels []byte) *repeatedPage {
   354  	return &repeatedPage{
   355  		base:               base,
   356  		maxRepetitionLevel: maxRepetitionLevel,
   357  		maxDefinitionLevel: maxDefinitionLevel,
   358  		definitionLevels:   definitionLevels,
   359  		repetitionLevels:   repetitionLevels,
   360  	}
   361  }
   362  
   363  func (page *repeatedPage) Type() Type { return page.base.Type() }
   364  
   365  func (page *repeatedPage) Column() int { return page.base.Column() }
   366  
   367  func (page *repeatedPage) Dictionary() Dictionary { return page.base.Dictionary() }
   368  
   369  func (page *repeatedPage) NumRows() int64 { return int64(countLevelsEqual(page.repetitionLevels, 0)) }
   370  
   371  func (page *repeatedPage) NumValues() int64 { return int64(len(page.definitionLevels)) }
   372  
   373  func (page *repeatedPage) NumNulls() int64 {
   374  	return int64(countLevelsNotEqual(page.definitionLevels, page.maxDefinitionLevel))
   375  }
   376  
   377  func (page *repeatedPage) Bounds() (min, max Value, ok bool) { return page.base.Bounds() }
   378  
   379  func (page *repeatedPage) Size() int64 {
   380  	return int64(len(page.repetitionLevels)) + int64(len(page.definitionLevels)) + page.base.Size()
   381  }
   382  
   383  func (page *repeatedPage) RepetitionLevels() []byte { return page.repetitionLevels }
   384  
   385  func (page *repeatedPage) DefinitionLevels() []byte { return page.definitionLevels }
   386  
   387  func (page *repeatedPage) Data() []byte { return page.base.Data() }
   388  
   389  func (page *repeatedPage) Values() ValueReader {
   390  	return &repeatedPageValues{
   391  		page:   page,
   392  		values: page.base.Values(),
   393  	}
   394  }
   395  
   396  func (page *repeatedPage) Buffer() BufferedPage { return page }
   397  
   398  func (page *repeatedPage) Clone() BufferedPage {
   399  	return newRepeatedPage(
   400  		page.base.Clone(),
   401  		page.maxRepetitionLevel,
   402  		page.maxDefinitionLevel,
   403  		append([]byte{}, page.repetitionLevels...),
   404  		append([]byte{}, page.definitionLevels...),
   405  	)
   406  }
   407  
   408  func (page *repeatedPage) Slice(i, j int64) BufferedPage {
   409  	numRows := page.NumRows()
   410  	if i < 0 || i > numRows {
   411  		panic(errPageBoundsOutOfRange(i, j, numRows))
   412  	}
   413  	if j < 0 || j > numRows {
   414  		panic(errPageBoundsOutOfRange(i, j, numRows))
   415  	}
   416  	if i > j {
   417  		panic(errPageBoundsOutOfRange(i, j, numRows))
   418  	}
   419  
   420  	rowIndex0 := 0
   421  	rowIndex1 := len(page.repetitionLevels)
   422  	rowIndex2 := len(page.repetitionLevels)
   423  
   424  	for k, def := range page.repetitionLevels {
   425  		if def == 0 {
   426  			if rowIndex0 == int(i) {
   427  				rowIndex1 = k
   428  				break
   429  			}
   430  			rowIndex0++
   431  		}
   432  	}
   433  
   434  	for k, def := range page.repetitionLevels[rowIndex1:] {
   435  		if def == 0 {
   436  			if rowIndex0 == int(j) {
   437  				rowIndex2 = rowIndex1 + k
   438  				break
   439  			}
   440  			rowIndex0++
   441  		}
   442  	}
   443  
   444  	numNulls1 := countLevelsNotEqual(page.definitionLevels[:rowIndex1], page.maxDefinitionLevel)
   445  	numNulls2 := countLevelsNotEqual(page.definitionLevels[rowIndex1:rowIndex2], page.maxDefinitionLevel)
   446  
   447  	i = int64(rowIndex1 - numNulls1)
   448  	j = int64(rowIndex2 - (numNulls1 + numNulls2))
   449  
   450  	return newRepeatedPage(
   451  		page.base.Slice(i, j),
   452  		page.maxRepetitionLevel,
   453  		page.maxDefinitionLevel,
   454  		page.repetitionLevels[rowIndex1:rowIndex2],
   455  		page.definitionLevels[rowIndex1:rowIndex2],
   456  	)
   457  }
   458  
   459  type booleanPage struct {
   460  	typ         Type
   461  	bits        []byte
   462  	offset      int32
   463  	numValues   int32
   464  	columnIndex int16
   465  }
   466  
   467  func newBooleanPage(typ Type, columnIndex int16, numValues int32, values []byte) *booleanPage {
   468  	return &booleanPage{
   469  		typ:         typ,
   470  		bits:        values[:bitpack.ByteCount(uint(numValues))],
   471  		numValues:   numValues,
   472  		columnIndex: ^columnIndex,
   473  	}
   474  }
   475  
   476  func (page *booleanPage) Type() Type { return page.typ }
   477  
   478  func (page *booleanPage) Column() int { return int(^page.columnIndex) }
   479  
   480  func (page *booleanPage) Dictionary() Dictionary { return nil }
   481  
   482  func (page *booleanPage) NumRows() int64 { return int64(page.numValues) }
   483  
   484  func (page *booleanPage) NumValues() int64 { return int64(page.numValues) }
   485  
   486  func (page *booleanPage) NumNulls() int64 { return 0 }
   487  
   488  func (page *booleanPage) Size() int64 { return int64(len(page.bits)) }
   489  
   490  func (page *booleanPage) RepetitionLevels() []byte { return nil }
   491  
   492  func (page *booleanPage) DefinitionLevels() []byte { return nil }
   493  
   494  func (page *booleanPage) Data() []byte { return page.bits }
   495  
   496  func (page *booleanPage) Values() ValueReader { return &booleanPageValues{page: page} }
   497  
   498  func (page *booleanPage) Buffer() BufferedPage { return page }
   499  
   500  func (page *booleanPage) valueAt(i int) bool {
   501  	j := uint32(int(page.offset)+i) / 8
   502  	k := uint32(int(page.offset)+i) % 8
   503  	return ((page.bits[j] >> k) & 1) != 0
   504  }
   505  
   506  func (page *booleanPage) min() bool {
   507  	for i := 0; i < int(page.numValues); i++ {
   508  		if !page.valueAt(i) {
   509  			return false
   510  		}
   511  	}
   512  	return page.numValues > 0
   513  }
   514  
   515  func (page *booleanPage) max() bool {
   516  	for i := 0; i < int(page.numValues); i++ {
   517  		if page.valueAt(i) {
   518  			return true
   519  		}
   520  	}
   521  	return false
   522  }
   523  
   524  func (page *booleanPage) bounds() (min, max bool) {
   525  	hasFalse, hasTrue := false, false
   526  
   527  	for i := 0; i < int(page.numValues); i++ {
   528  		v := page.valueAt(i)
   529  		if v {
   530  			hasTrue = true
   531  		} else {
   532  			hasFalse = true
   533  		}
   534  		if hasTrue && hasFalse {
   535  			break
   536  		}
   537  	}
   538  
   539  	min = !hasFalse
   540  	max = hasTrue
   541  	return min, max
   542  }
   543  
   544  func (page *booleanPage) Bounds() (min, max Value, ok bool) {
   545  	if ok = page.numValues > 0; ok {
   546  		minBool, maxBool := page.bounds()
   547  		min = page.makeValue(minBool)
   548  		max = page.makeValue(maxBool)
   549  	}
   550  	return min, max, ok
   551  }
   552  
   553  func (page *booleanPage) Clone() BufferedPage {
   554  	return &booleanPage{
   555  		typ:         page.typ,
   556  		bits:        append([]byte{}, page.bits...),
   557  		offset:      page.offset,
   558  		numValues:   page.numValues,
   559  		columnIndex: page.columnIndex,
   560  	}
   561  }
   562  
   563  func (page *booleanPage) Slice(i, j int64) BufferedPage {
   564  	off := i / 8
   565  	end := j / 8
   566  
   567  	if (j % 8) != 0 {
   568  		end++
   569  	}
   570  
   571  	return &booleanPage{
   572  		typ:         page.typ,
   573  		bits:        page.bits[off:end],
   574  		offset:      int32(i % 8),
   575  		numValues:   int32(j - i),
   576  		columnIndex: page.columnIndex,
   577  	}
   578  }
   579  
   580  func (page *booleanPage) makeValue(v bool) Value {
   581  	value := makeValueBoolean(v)
   582  	value.columnIndex = page.columnIndex
   583  	return value
   584  }
   585  
   586  type int32Page struct {
   587  	typ         Type
   588  	values      []int32
   589  	columnIndex int16
   590  }
   591  
   592  func newInt32Page(typ Type, columnIndex int16, numValues int32, values []byte) *int32Page {
   593  	return &int32Page{
   594  		typ:         typ,
   595  		values:      unsafecast.BytesToInt32(values)[:numValues],
   596  		columnIndex: ^columnIndex,
   597  	}
   598  }
   599  
   600  func (page *int32Page) Type() Type { return page.typ }
   601  
   602  func (page *int32Page) Column() int { return int(^page.columnIndex) }
   603  
   604  func (page *int32Page) Dictionary() Dictionary { return nil }
   605  
   606  func (page *int32Page) NumRows() int64 { return int64(len(page.values)) }
   607  
   608  func (page *int32Page) NumValues() int64 { return int64(len(page.values)) }
   609  
   610  func (page *int32Page) NumNulls() int64 { return 0 }
   611  
   612  func (page *int32Page) Size() int64 { return 4 * int64(len(page.values)) }
   613  
   614  func (page *int32Page) RepetitionLevels() []byte { return nil }
   615  
   616  func (page *int32Page) DefinitionLevels() []byte { return nil }
   617  
   618  func (page *int32Page) Data() []byte { return unsafecast.Int32ToBytes(page.values) }
   619  
   620  func (page *int32Page) Values() ValueReader { return &int32PageValues{page: page} }
   621  
   622  func (page *int32Page) Buffer() BufferedPage { return page }
   623  
   624  func (page *int32Page) min() int32 { return minInt32(page.values) }
   625  
   626  func (page *int32Page) max() int32 { return maxInt32(page.values) }
   627  
   628  func (page *int32Page) bounds() (min, max int32) { return boundsInt32(page.values) }
   629  
   630  func (page *int32Page) Bounds() (min, max Value, ok bool) {
   631  	if ok = len(page.values) > 0; ok {
   632  		minInt32, maxInt32 := page.bounds()
   633  		min = page.makeValue(minInt32)
   634  		max = page.makeValue(maxInt32)
   635  	}
   636  	return min, max, ok
   637  }
   638  
   639  func (page *int32Page) Clone() BufferedPage {
   640  	return &int32Page{
   641  		typ:         page.typ,
   642  		values:      append([]int32{}, page.values...),
   643  		columnIndex: page.columnIndex,
   644  	}
   645  }
   646  
   647  func (page *int32Page) Slice(i, j int64) BufferedPage {
   648  	return &int32Page{
   649  		typ:         page.typ,
   650  		values:      page.values[i:j],
   651  		columnIndex: page.columnIndex,
   652  	}
   653  }
   654  
   655  func (page *int32Page) makeValue(v int32) Value {
   656  	value := makeValueInt32(v)
   657  	value.columnIndex = page.columnIndex
   658  	return value
   659  }
   660  
   661  type int64Page struct {
   662  	typ         Type
   663  	values      []int64
   664  	columnIndex int16
   665  }
   666  
   667  func newInt64Page(typ Type, columnIndex int16, numValues int32, values []byte) *int64Page {
   668  	return &int64Page{
   669  		typ:         typ,
   670  		values:      unsafecast.BytesToInt64(values)[:numValues],
   671  		columnIndex: ^columnIndex,
   672  	}
   673  }
   674  
   675  func (page *int64Page) Type() Type { return page.typ }
   676  
   677  func (page *int64Page) Column() int { return int(^page.columnIndex) }
   678  
   679  func (page *int64Page) Dictionary() Dictionary { return nil }
   680  
   681  func (page *int64Page) NumRows() int64 { return int64(len(page.values)) }
   682  
   683  func (page *int64Page) NumValues() int64 { return int64(len(page.values)) }
   684  
   685  func (page *int64Page) NumNulls() int64 { return 0 }
   686  
   687  func (page *int64Page) Size() int64 { return 8 * int64(len(page.values)) }
   688  
   689  func (page *int64Page) RepetitionLevels() []byte { return nil }
   690  
   691  func (page *int64Page) DefinitionLevels() []byte { return nil }
   692  
   693  func (page *int64Page) Data() []byte { return unsafecast.Int64ToBytes(page.values) }
   694  
   695  func (page *int64Page) Values() ValueReader { return &int64PageValues{page: page} }
   696  
   697  func (page *int64Page) Buffer() BufferedPage { return page }
   698  
   699  func (page *int64Page) min() int64 { return minInt64(page.values) }
   700  
   701  func (page *int64Page) max() int64 { return maxInt64(page.values) }
   702  
   703  func (page *int64Page) bounds() (min, max int64) { return boundsInt64(page.values) }
   704  
   705  func (page *int64Page) Bounds() (min, max Value, ok bool) {
   706  	if ok = len(page.values) > 0; ok {
   707  		minInt64, maxInt64 := page.bounds()
   708  		min = page.makeValue(minInt64)
   709  		max = page.makeValue(maxInt64)
   710  	}
   711  	return min, max, ok
   712  }
   713  
   714  func (page *int64Page) Clone() BufferedPage {
   715  	return &int64Page{
   716  		typ:         page.typ,
   717  		values:      append([]int64{}, page.values...),
   718  		columnIndex: page.columnIndex,
   719  	}
   720  }
   721  
   722  func (page *int64Page) Slice(i, j int64) BufferedPage {
   723  	return &int64Page{
   724  		typ:         page.typ,
   725  		values:      page.values[i:j],
   726  		columnIndex: page.columnIndex,
   727  	}
   728  }
   729  
   730  func (page *int64Page) makeValue(v int64) Value {
   731  	value := makeValueInt64(v)
   732  	value.columnIndex = page.columnIndex
   733  	return value
   734  }
   735  
   736  type int96Page struct {
   737  	typ         Type
   738  	values      []deprecated.Int96
   739  	columnIndex int16
   740  }
   741  
   742  func newInt96Page(typ Type, columnIndex int16, numValues int32, values []byte) *int96Page {
   743  	return &int96Page{
   744  		typ:         typ,
   745  		values:      deprecated.BytesToInt96(values)[:numValues],
   746  		columnIndex: ^columnIndex,
   747  	}
   748  }
   749  
   750  func (page *int96Page) Type() Type { return page.typ }
   751  
   752  func (page *int96Page) Column() int { return int(^page.columnIndex) }
   753  
   754  func (page *int96Page) Dictionary() Dictionary { return nil }
   755  
   756  func (page *int96Page) NumRows() int64 { return int64(len(page.values)) }
   757  
   758  func (page *int96Page) NumValues() int64 { return int64(len(page.values)) }
   759  
   760  func (page *int96Page) NumNulls() int64 { return 0 }
   761  
   762  func (page *int96Page) Size() int64 { return 12 * int64(len(page.values)) }
   763  
   764  func (page *int96Page) RepetitionLevels() []byte { return nil }
   765  
   766  func (page *int96Page) DefinitionLevels() []byte { return nil }
   767  
   768  func (page *int96Page) Data() []byte { return deprecated.Int96ToBytes(page.values) }
   769  
   770  func (page *int96Page) Values() ValueReader { return &int96PageValues{page: page} }
   771  
   772  func (page *int96Page) Buffer() BufferedPage { return page }
   773  
   774  func (page *int96Page) min() deprecated.Int96 { return deprecated.MinInt96(page.values) }
   775  
   776  func (page *int96Page) max() deprecated.Int96 { return deprecated.MaxInt96(page.values) }
   777  
   778  func (page *int96Page) bounds() (min, max deprecated.Int96) {
   779  	return deprecated.MinMaxInt96(page.values)
   780  }
   781  
   782  func (page *int96Page) Bounds() (min, max Value, ok bool) {
   783  	if ok = len(page.values) > 0; ok {
   784  		minInt96, maxInt96 := page.bounds()
   785  		min = page.makeValue(minInt96)
   786  		max = page.makeValue(maxInt96)
   787  	}
   788  	return min, max, ok
   789  }
   790  
   791  func (page *int96Page) Clone() BufferedPage {
   792  	return &int96Page{
   793  		typ:         page.typ,
   794  		values:      append([]deprecated.Int96{}, page.values...),
   795  		columnIndex: page.columnIndex,
   796  	}
   797  }
   798  
   799  func (page *int96Page) Slice(i, j int64) BufferedPage {
   800  	return &int96Page{
   801  		typ:         page.typ,
   802  		values:      page.values[i:j],
   803  		columnIndex: page.columnIndex,
   804  	}
   805  }
   806  
   807  func (page *int96Page) makeValue(v deprecated.Int96) Value {
   808  	value := makeValueInt96(v)
   809  	value.columnIndex = page.columnIndex
   810  	return value
   811  }
   812  
   813  type floatPage struct {
   814  	typ         Type
   815  	values      []float32
   816  	columnIndex int16
   817  }
   818  
   819  func newFloatPage(typ Type, columnIndex int16, numValues int32, values []byte) *floatPage {
   820  	return &floatPage{
   821  		typ:         typ,
   822  		values:      unsafecast.BytesToFloat32(values)[:numValues],
   823  		columnIndex: ^columnIndex,
   824  	}
   825  }
   826  
   827  func (page *floatPage) Type() Type { return page.typ }
   828  
   829  func (page *floatPage) Column() int { return int(^page.columnIndex) }
   830  
   831  func (page *floatPage) Dictionary() Dictionary { return nil }
   832  
   833  func (page *floatPage) NumRows() int64 { return int64(len(page.values)) }
   834  
   835  func (page *floatPage) NumValues() int64 { return int64(len(page.values)) }
   836  
   837  func (page *floatPage) NumNulls() int64 { return 0 }
   838  
   839  func (page *floatPage) Size() int64 { return 4 * int64(len(page.values)) }
   840  
   841  func (page *floatPage) RepetitionLevels() []byte { return nil }
   842  
   843  func (page *floatPage) DefinitionLevels() []byte { return nil }
   844  
   845  func (page *floatPage) Data() []byte { return unsafecast.Float32ToBytes(page.values) }
   846  
   847  func (page *floatPage) Values() ValueReader { return &floatPageValues{page: page} }
   848  
   849  func (page *floatPage) Buffer() BufferedPage { return page }
   850  
   851  func (page *floatPage) min() float32 { return minFloat32(page.values) }
   852  
   853  func (page *floatPage) max() float32 { return maxFloat32(page.values) }
   854  
   855  func (page *floatPage) bounds() (min, max float32) { return boundsFloat32(page.values) }
   856  
   857  func (page *floatPage) Bounds() (min, max Value, ok bool) {
   858  	if ok = len(page.values) > 0; ok {
   859  		minFloat32, maxFloat32 := page.bounds()
   860  		min = page.makeValue(minFloat32)
   861  		max = page.makeValue(maxFloat32)
   862  	}
   863  	return min, max, ok
   864  }
   865  
   866  func (page *floatPage) Clone() BufferedPage {
   867  	return &floatPage{
   868  		typ:         page.typ,
   869  		values:      append([]float32{}, page.values...),
   870  		columnIndex: page.columnIndex,
   871  	}
   872  }
   873  
   874  func (page *floatPage) Slice(i, j int64) BufferedPage {
   875  	return &floatPage{
   876  		typ:         page.typ,
   877  		values:      page.values[i:j],
   878  		columnIndex: page.columnIndex,
   879  	}
   880  }
   881  
   882  func (page *floatPage) makeValue(v float32) Value {
   883  	value := makeValueFloat(v)
   884  	value.columnIndex = page.columnIndex
   885  	return value
   886  }
   887  
   888  type doublePage struct {
   889  	typ         Type
   890  	values      []float64
   891  	columnIndex int16
   892  }
   893  
   894  func newDoublePage(typ Type, columnIndex int16, numValues int32, values []byte) *doublePage {
   895  	return &doublePage{
   896  		typ:         typ,
   897  		values:      unsafecast.BytesToFloat64(values)[:numValues],
   898  		columnIndex: ^columnIndex,
   899  	}
   900  }
   901  
   902  func (page *doublePage) Type() Type { return page.typ }
   903  
   904  func (page *doublePage) Column() int { return int(^page.columnIndex) }
   905  
   906  func (page *doublePage) Dictionary() Dictionary { return nil }
   907  
   908  func (page *doublePage) NumRows() int64 { return int64(len(page.values)) }
   909  
   910  func (page *doublePage) NumValues() int64 { return int64(len(page.values)) }
   911  
   912  func (page *doublePage) NumNulls() int64 { return 0 }
   913  
   914  func (page *doublePage) Size() int64 { return 8 * int64(len(page.values)) }
   915  
   916  func (page *doublePage) RepetitionLevels() []byte { return nil }
   917  
   918  func (page *doublePage) DefinitionLevels() []byte { return nil }
   919  
   920  func (page *doublePage) Data() []byte { return unsafecast.Float64ToBytes(page.values) }
   921  
   922  func (page *doublePage) Values() ValueReader { return &doublePageValues{page: page} }
   923  
   924  func (page *doublePage) Buffer() BufferedPage { return page }
   925  
   926  func (page *doublePage) min() float64 { return minFloat64(page.values) }
   927  
   928  func (page *doublePage) max() float64 { return maxFloat64(page.values) }
   929  
   930  func (page *doublePage) bounds() (min, max float64) { return boundsFloat64(page.values) }
   931  
   932  func (page *doublePage) Bounds() (min, max Value, ok bool) {
   933  	if ok = len(page.values) > 0; ok {
   934  		minFloat64, maxFloat64 := page.bounds()
   935  		min = page.makeValue(minFloat64)
   936  		max = page.makeValue(maxFloat64)
   937  	}
   938  	return min, max, ok
   939  }
   940  
   941  func (page *doublePage) Clone() BufferedPage {
   942  	return &doublePage{
   943  		typ:         page.typ,
   944  		values:      append([]float64{}, page.values...),
   945  		columnIndex: page.columnIndex,
   946  	}
   947  }
   948  
   949  func (page *doublePage) Slice(i, j int64) BufferedPage {
   950  	return &doublePage{
   951  		typ:         page.typ,
   952  		values:      page.values[i:j],
   953  		columnIndex: page.columnIndex,
   954  	}
   955  }
   956  
   957  func (page *doublePage) makeValue(v float64) Value {
   958  	value := makeValueDouble(v)
   959  	value.columnIndex = page.columnIndex
   960  	return value
   961  }
   962  
   963  type byteArrayPage struct {
   964  	typ         Type
   965  	values      []byte
   966  	numValues   int32
   967  	columnIndex int16
   968  }
   969  
   970  func newByteArrayPage(typ Type, columnIndex int16, numValues int32, values []byte) *byteArrayPage {
   971  	return &byteArrayPage{
   972  		typ:         typ,
   973  		values:      values,
   974  		numValues:   numValues,
   975  		columnIndex: ^columnIndex,
   976  	}
   977  }
   978  
   979  func (page *byteArrayPage) Type() Type { return page.typ }
   980  
   981  func (page *byteArrayPage) Column() int { return int(^page.columnIndex) }
   982  
   983  func (page *byteArrayPage) Dictionary() Dictionary { return nil }
   984  
   985  func (page *byteArrayPage) NumRows() int64 { return int64(page.numValues) }
   986  
   987  func (page *byteArrayPage) NumValues() int64 { return int64(page.numValues) }
   988  
   989  func (page *byteArrayPage) NumNulls() int64 { return 0 }
   990  
   991  func (page *byteArrayPage) Size() int64 { return int64(len(page.values)) }
   992  
   993  func (page *byteArrayPage) RepetitionLevels() []byte { return nil }
   994  
   995  func (page *byteArrayPage) DefinitionLevels() []byte { return nil }
   996  
   997  func (page *byteArrayPage) Data() []byte { return page.values }
   998  
   999  func (page *byteArrayPage) Values() ValueReader { return &byteArrayPageValues{page: page} }
  1000  
  1001  func (page *byteArrayPage) Buffer() BufferedPage { return page }
  1002  
  1003  func (page *byteArrayPage) valueAt(offset uint32) []byte {
  1004  	length := binary.LittleEndian.Uint32(page.values[offset:])
  1005  	j := 4 + offset
  1006  	k := 4 + offset + length
  1007  	return page.values[j:k:k]
  1008  }
  1009  
  1010  func (page *byteArrayPage) min() (min []byte) {
  1011  	if len(page.values) > 0 {
  1012  		min = page.valueAt(0)
  1013  
  1014  		for i := 4 + len(min); i < len(page.values); {
  1015  			v := page.valueAt(uint32(i))
  1016  
  1017  			if bytes.Compare(v, min) < 0 {
  1018  				min = v
  1019  			}
  1020  
  1021  			i += 4
  1022  			i += len(v)
  1023  		}
  1024  	}
  1025  	return min
  1026  }
  1027  
  1028  func (page *byteArrayPage) max() (max []byte) {
  1029  	if len(page.values) > 0 {
  1030  		max = page.valueAt(0)
  1031  
  1032  		for i := 4 + len(max); i < len(page.values); {
  1033  			v := page.valueAt(uint32(i))
  1034  
  1035  			if bytes.Compare(v, max) > 0 {
  1036  				max = v
  1037  			}
  1038  
  1039  			i += 4
  1040  			i += len(v)
  1041  		}
  1042  	}
  1043  	return max
  1044  }
  1045  
  1046  func (page *byteArrayPage) bounds() (min, max []byte) {
  1047  	if len(page.values) > 0 {
  1048  		min = page.valueAt(0)
  1049  		max = min
  1050  
  1051  		for i := 4 + len(min); i < len(page.values); {
  1052  			v := page.valueAt(uint32(i))
  1053  
  1054  			switch {
  1055  			case bytes.Compare(v, min) < 0:
  1056  				min = v
  1057  			case bytes.Compare(v, max) > 0:
  1058  				max = v
  1059  			}
  1060  
  1061  			i += 4
  1062  			i += len(v)
  1063  		}
  1064  	}
  1065  	return min, max
  1066  }
  1067  
  1068  func (page *byteArrayPage) Bounds() (min, max Value, ok bool) {
  1069  	if ok = len(page.values) > 0; ok {
  1070  		minBytes, maxBytes := page.bounds()
  1071  		min = page.makeValueBytes(minBytes)
  1072  		max = page.makeValueBytes(maxBytes)
  1073  	}
  1074  	return min, max, ok
  1075  }
  1076  
  1077  func (page *byteArrayPage) cloneValues() []byte {
  1078  	values := make([]byte, len(page.values))
  1079  	copy(values, page.values)
  1080  	return values
  1081  }
  1082  
  1083  func (page *byteArrayPage) Clone() BufferedPage {
  1084  	return &byteArrayPage{
  1085  		typ:         page.typ,
  1086  		values:      page.cloneValues(),
  1087  		numValues:   page.numValues,
  1088  		columnIndex: page.columnIndex,
  1089  	}
  1090  }
  1091  
  1092  func (page *byteArrayPage) Slice(i, j int64) BufferedPage {
  1093  	numValues := j - i
  1094  
  1095  	off0 := uint32(0)
  1096  	for i > 0 {
  1097  		off0 += binary.LittleEndian.Uint32(page.values[off0:])
  1098  		off0 += plain.ByteArrayLengthSize
  1099  		i--
  1100  		j--
  1101  	}
  1102  
  1103  	off1 := off0
  1104  	for j > 0 {
  1105  		off1 += binary.LittleEndian.Uint32(page.values[off1:])
  1106  		off1 += plain.ByteArrayLengthSize
  1107  		j--
  1108  	}
  1109  
  1110  	return &byteArrayPage{
  1111  		typ:         page.typ,
  1112  		values:      page.values[off0:off1:off1],
  1113  		numValues:   int32(numValues),
  1114  		columnIndex: page.columnIndex,
  1115  	}
  1116  }
  1117  
  1118  func (page *byteArrayPage) makeValueBytes(v []byte) Value {
  1119  	value := makeValueBytes(ByteArray, v)
  1120  	value.columnIndex = page.columnIndex
  1121  	return value
  1122  }
  1123  
  1124  func (page *byteArrayPage) makeValueString(v string) Value {
  1125  	value := makeValueString(ByteArray, v)
  1126  	value.columnIndex = page.columnIndex
  1127  	return value
  1128  }
  1129  
  1130  type fixedLenByteArrayPage struct {
  1131  	typ         Type
  1132  	data        []byte
  1133  	size        int
  1134  	columnIndex int16
  1135  }
  1136  
  1137  func newFixedLenByteArrayPage(typ Type, columnIndex int16, numValues int32, data []byte) *fixedLenByteArrayPage {
  1138  	size := typ.Length()
  1139  	if (len(data) % size) != 0 {
  1140  		panic("cannot create fixed-length byte array page from input which is not a multiple of the type size")
  1141  	}
  1142  	if int(numValues) != len(data)/size {
  1143  		panic(fmt.Errorf("number of values mismatch in numValues and data arguments: %d != %d", numValues, len(data)/size))
  1144  	}
  1145  	return &fixedLenByteArrayPage{
  1146  		typ:         typ,
  1147  		data:        data,
  1148  		size:        size,
  1149  		columnIndex: ^columnIndex,
  1150  	}
  1151  }
  1152  
  1153  func (page *fixedLenByteArrayPage) Type() Type { return page.typ }
  1154  
  1155  func (page *fixedLenByteArrayPage) Column() int { return int(^page.columnIndex) }
  1156  
  1157  func (page *fixedLenByteArrayPage) Dictionary() Dictionary { return nil }
  1158  
  1159  func (page *fixedLenByteArrayPage) NumRows() int64 { return int64(len(page.data) / page.size) }
  1160  
  1161  func (page *fixedLenByteArrayPage) NumValues() int64 { return int64(len(page.data) / page.size) }
  1162  
  1163  func (page *fixedLenByteArrayPage) NumNulls() int64 { return 0 }
  1164  
  1165  func (page *fixedLenByteArrayPage) Size() int64 { return int64(len(page.data)) }
  1166  
  1167  func (page *fixedLenByteArrayPage) RepetitionLevels() []byte { return nil }
  1168  
  1169  func (page *fixedLenByteArrayPage) DefinitionLevels() []byte { return nil }
  1170  
  1171  func (page *fixedLenByteArrayPage) Data() []byte { return page.data }
  1172  
  1173  func (page *fixedLenByteArrayPage) Values() ValueReader {
  1174  	return &fixedLenByteArrayPageValues{page: page}
  1175  }
  1176  
  1177  func (page *fixedLenByteArrayPage) Buffer() BufferedPage { return page }
  1178  
  1179  func (page *fixedLenByteArrayPage) min() []byte { return minFixedLenByteArray(page.data, page.size) }
  1180  
  1181  func (page *fixedLenByteArrayPage) max() []byte { return maxFixedLenByteArray(page.data, page.size) }
  1182  
  1183  func (page *fixedLenByteArrayPage) bounds() (min, max []byte) {
  1184  	return boundsFixedLenByteArray(page.data, page.size)
  1185  }
  1186  
  1187  func (page *fixedLenByteArrayPage) Bounds() (min, max Value, ok bool) {
  1188  	if ok = len(page.data) > 0; ok {
  1189  		minBytes, maxBytes := page.bounds()
  1190  		min = page.makeValueBytes(minBytes)
  1191  		max = page.makeValueBytes(maxBytes)
  1192  	}
  1193  	return min, max, ok
  1194  }
  1195  
  1196  func (page *fixedLenByteArrayPage) Clone() BufferedPage {
  1197  	return &fixedLenByteArrayPage{
  1198  		typ:         page.typ,
  1199  		data:        append([]byte{}, page.data...),
  1200  		size:        page.size,
  1201  		columnIndex: page.columnIndex,
  1202  	}
  1203  }
  1204  
  1205  func (page *fixedLenByteArrayPage) Slice(i, j int64) BufferedPage {
  1206  	return &fixedLenByteArrayPage{
  1207  		typ:         page.typ,
  1208  		data:        page.data[i*int64(page.size) : j*int64(page.size)],
  1209  		size:        page.size,
  1210  		columnIndex: page.columnIndex,
  1211  	}
  1212  }
  1213  
  1214  func (page *fixedLenByteArrayPage) makeValueBytes(v []byte) Value {
  1215  	value := makeValueBytes(FixedLenByteArray, v)
  1216  	value.columnIndex = page.columnIndex
  1217  	return value
  1218  }
  1219  
  1220  func (page *fixedLenByteArrayPage) makeValueString(v string) Value {
  1221  	value := makeValueString(FixedLenByteArray, v)
  1222  	value.columnIndex = page.columnIndex
  1223  	return value
  1224  }
  1225  
  1226  type uint32Page struct {
  1227  	typ         Type
  1228  	values      []uint32
  1229  	columnIndex int16
  1230  }
  1231  
  1232  func newUint32Page(typ Type, columnIndex int16, numValues int32, values []byte) *uint32Page {
  1233  	return &uint32Page{
  1234  		typ:         typ,
  1235  		values:      unsafecast.BytesToUint32(values)[:numValues],
  1236  		columnIndex: ^columnIndex,
  1237  	}
  1238  }
  1239  
  1240  func (page *uint32Page) Type() Type { return page.typ }
  1241  
  1242  func (page *uint32Page) Column() int { return int(^page.columnIndex) }
  1243  
  1244  func (page *uint32Page) Dictionary() Dictionary { return nil }
  1245  
  1246  func (page *uint32Page) NumRows() int64 { return int64(len(page.values)) }
  1247  
  1248  func (page *uint32Page) NumValues() int64 { return int64(len(page.values)) }
  1249  
  1250  func (page *uint32Page) NumNulls() int64 { return 0 }
  1251  
  1252  func (page *uint32Page) Size() int64 { return 4 * int64(len(page.values)) }
  1253  
  1254  func (page *uint32Page) RepetitionLevels() []byte { return nil }
  1255  
  1256  func (page *uint32Page) DefinitionLevels() []byte { return nil }
  1257  
  1258  func (page *uint32Page) Data() []byte { return unsafecast.Uint32ToBytes(page.values) }
  1259  
  1260  func (page *uint32Page) Values() ValueReader { return &uint32PageValues{page: page} }
  1261  
  1262  func (page *uint32Page) Buffer() BufferedPage { return page }
  1263  
  1264  func (page *uint32Page) min() uint32 { return minUint32(page.values) }
  1265  
  1266  func (page *uint32Page) max() uint32 { return maxUint32(page.values) }
  1267  
  1268  func (page *uint32Page) bounds() (min, max uint32) { return boundsUint32(page.values) }
  1269  
  1270  func (page *uint32Page) Bounds() (min, max Value, ok bool) {
  1271  	if ok = len(page.values) > 0; ok {
  1272  		minUint32, maxUint32 := page.bounds()
  1273  		min = page.makeValue(minUint32)
  1274  		max = page.makeValue(maxUint32)
  1275  	}
  1276  	return min, max, ok
  1277  }
  1278  
  1279  func (page *uint32Page) Clone() BufferedPage {
  1280  	return &uint32Page{
  1281  		typ:         page.typ,
  1282  		values:      append([]uint32{}, page.values...),
  1283  		columnIndex: page.columnIndex,
  1284  	}
  1285  }
  1286  
  1287  func (page *uint32Page) Slice(i, j int64) BufferedPage {
  1288  	return &uint32Page{
  1289  		typ:         page.typ,
  1290  		values:      page.values[i:j],
  1291  		columnIndex: page.columnIndex,
  1292  	}
  1293  }
  1294  
  1295  func (page *uint32Page) makeValue(v uint32) Value {
  1296  	value := makeValueUint32(v)
  1297  	value.columnIndex = page.columnIndex
  1298  	return value
  1299  }
  1300  
  1301  type uint64Page struct {
  1302  	typ         Type
  1303  	values      []uint64
  1304  	columnIndex int16
  1305  }
  1306  
  1307  func newUint64Page(typ Type, columnIndex int16, numValues int32, values []byte) *uint64Page {
  1308  	return &uint64Page{
  1309  		typ:         typ,
  1310  		values:      unsafecast.BytesToUint64(values)[:numValues],
  1311  		columnIndex: ^columnIndex,
  1312  	}
  1313  }
  1314  
  1315  func (page *uint64Page) Type() Type { return page.typ }
  1316  
  1317  func (page *uint64Page) Column() int { return int(^page.columnIndex) }
  1318  
  1319  func (page *uint64Page) Dictionary() Dictionary { return nil }
  1320  
  1321  func (page *uint64Page) NumRows() int64 { return int64(len(page.values)) }
  1322  
  1323  func (page *uint64Page) NumValues() int64 { return int64(len(page.values)) }
  1324  
  1325  func (page *uint64Page) NumNulls() int64 { return 0 }
  1326  
  1327  func (page *uint64Page) Size() int64 { return 8 * int64(len(page.values)) }
  1328  
  1329  func (page *uint64Page) RepetitionLevels() []byte { return nil }
  1330  
  1331  func (page *uint64Page) DefinitionLevels() []byte { return nil }
  1332  
  1333  func (page *uint64Page) Data() []byte { return unsafecast.Uint64ToBytes(page.values) }
  1334  
  1335  func (page *uint64Page) Values() ValueReader { return &uint64PageValues{page: page} }
  1336  
  1337  func (page *uint64Page) Buffer() BufferedPage { return page }
  1338  
  1339  func (page *uint64Page) min() uint64 { return minUint64(page.values) }
  1340  
  1341  func (page *uint64Page) max() uint64 { return maxUint64(page.values) }
  1342  
  1343  func (page *uint64Page) bounds() (min, max uint64) { return boundsUint64(page.values) }
  1344  
  1345  func (page *uint64Page) Bounds() (min, max Value, ok bool) {
  1346  	if ok = len(page.values) > 0; ok {
  1347  		minUint64, maxUint64 := page.bounds()
  1348  		min = page.makeValue(minUint64)
  1349  		max = page.makeValue(maxUint64)
  1350  	}
  1351  	return min, max, ok
  1352  }
  1353  
  1354  func (page *uint64Page) Clone() BufferedPage {
  1355  	return &uint64Page{
  1356  		typ:         page.typ,
  1357  		values:      append([]uint64{}, page.values...),
  1358  		columnIndex: page.columnIndex,
  1359  	}
  1360  }
  1361  
  1362  func (page *uint64Page) Slice(i, j int64) BufferedPage {
  1363  	return &uint64Page{
  1364  		typ:         page.typ,
  1365  		values:      page.values[i:j],
  1366  		columnIndex: page.columnIndex,
  1367  	}
  1368  }
  1369  
  1370  func (page *uint64Page) makeValue(v uint64) Value {
  1371  	value := makeValueUint64(v)
  1372  	value.columnIndex = page.columnIndex
  1373  	return value
  1374  }
  1375  
  1376  type be128Page struct {
  1377  	typ         Type
  1378  	values      [][16]byte
  1379  	columnIndex int16
  1380  }
  1381  
  1382  func newBE128Page(typ Type, columnIndex int16, numValues int32, data []byte) *be128Page {
  1383  	if (len(data) % 16) != 0 {
  1384  		panic("cannot create fixed-length byte array page from input which is not a multiple of the type size")
  1385  	}
  1386  	if int(numValues) != len(data)/16 {
  1387  		panic(fmt.Errorf("number of values mismatch in numValues and data arguments: %d != %d", numValues, len(data)/16))
  1388  	}
  1389  	return &be128Page{
  1390  		typ:         typ,
  1391  		values:      unsafecast.BytesToUint128(data),
  1392  		columnIndex: ^columnIndex,
  1393  	}
  1394  }
  1395  
  1396  func (page *be128Page) Type() Type { return page.typ }
  1397  
  1398  func (page *be128Page) Column() int { return int(^page.columnIndex) }
  1399  
  1400  func (page *be128Page) Dictionary() Dictionary { return nil }
  1401  
  1402  func (page *be128Page) NumRows() int64 { return int64(len(page.values)) }
  1403  
  1404  func (page *be128Page) NumValues() int64 { return int64(len(page.values)) }
  1405  
  1406  func (page *be128Page) NumNulls() int64 { return 0 }
  1407  
  1408  func (page *be128Page) Size() int64 { return 16 * int64(len(page.values)) }
  1409  
  1410  func (page *be128Page) RepetitionLevels() []byte { return nil }
  1411  
  1412  func (page *be128Page) DefinitionLevels() []byte { return nil }
  1413  
  1414  func (page *be128Page) Data() []byte { return unsafecast.Uint128ToBytes(page.values) }
  1415  
  1416  func (page *be128Page) Values() ValueReader { return &be128PageValues{page: page} }
  1417  
  1418  func (page *be128Page) Buffer() BufferedPage { return page }
  1419  
  1420  func (page *be128Page) min() []byte { return minBE128(page.values) }
  1421  
  1422  func (page *be128Page) max() []byte { return maxBE128(page.values) }
  1423  
  1424  func (page *be128Page) bounds() (min, max []byte) { return boundsBE128(page.values) }
  1425  
  1426  func (page *be128Page) Bounds() (min, max Value, ok bool) {
  1427  	if ok = len(page.values) > 0; ok {
  1428  		minBytes, maxBytes := page.bounds()
  1429  		min = page.makeValueBytes(minBytes)
  1430  		max = page.makeValueBytes(maxBytes)
  1431  	}
  1432  	return min, max, ok
  1433  }
  1434  
  1435  func (page *be128Page) Clone() BufferedPage {
  1436  	return &be128Page{
  1437  		typ:         page.typ,
  1438  		values:      append([][16]byte{}, page.values...),
  1439  		columnIndex: page.columnIndex,
  1440  	}
  1441  }
  1442  
  1443  func (page *be128Page) Slice(i, j int64) BufferedPage {
  1444  	return &be128Page{
  1445  		typ:         page.typ,
  1446  		values:      page.values[i:j],
  1447  		columnIndex: page.columnIndex,
  1448  	}
  1449  }
  1450  
  1451  func (page *be128Page) makeValue(v *[16]byte) Value {
  1452  	return page.makeValueBytes(v[:])
  1453  }
  1454  
  1455  func (page *be128Page) makeValueBytes(v []byte) Value {
  1456  	value := makeValueBytes(FixedLenByteArray, v)
  1457  	value.columnIndex = page.columnIndex
  1458  	return value
  1459  }
  1460  
  1461  func (page *be128Page) makeValueString(v string) Value {
  1462  	value := makeValueString(FixedLenByteArray, v)
  1463  	value.columnIndex = page.columnIndex
  1464  	return value
  1465  }
  1466  
  1467  type nullPage struct {
  1468  	typ    Type
  1469  	column int
  1470  	count  int
  1471  }
  1472  
  1473  func newNullPage(typ Type, columnIndex int16, numValues int32) *nullPage {
  1474  	return &nullPage{
  1475  		typ:    typ,
  1476  		column: int(columnIndex),
  1477  		count:  int(numValues),
  1478  	}
  1479  }
  1480  
  1481  func (page *nullPage) Type() Type                        { return page.typ }
  1482  func (page *nullPage) Column() int                       { return page.column }
  1483  func (page *nullPage) Dictionary() Dictionary            { return nil }
  1484  func (page *nullPage) NumRows() int64                    { return int64(page.count) }
  1485  func (page *nullPage) NumValues() int64                  { return int64(page.count) }
  1486  func (page *nullPage) NumNulls() int64                   { return int64(page.count) }
  1487  func (page *nullPage) Bounds() (min, max Value, ok bool) { return }
  1488  func (page *nullPage) Size() int64                       { return 1 }
  1489  func (page *nullPage) Values() ValueReader {
  1490  	return &nullPageValues{column: page.column, remain: page.count}
  1491  }
  1492  func (page *nullPage) Buffer() BufferedPage { return page }
  1493  func (page *nullPage) Clone() BufferedPage  { return page }
  1494  func (page *nullPage) Slice(i, j int64) BufferedPage {
  1495  	return &nullPage{column: page.column, count: page.count - int(j-i)}
  1496  }
  1497  func (page *nullPage) RepetitionLevels() []byte { return nil }
  1498  func (page *nullPage) DefinitionLevels() []byte { return nil }
  1499  func (page *nullPage) Data() []byte             { return nil }