-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Expose Parquet version and root repetition spec options (#567)
#### Summary Some old parquet readers like Snowflake imported need the root repetition to be undefined, and they also only support Parquet v1. This PR exposes both options. Related to cloudquery/cloudquery-issues#2106 (internal issue) ---
- Loading branch information
Showing
4 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,98 @@ | ||
package parquet | ||
|
||
import "github.com/invopop/jsonschema" | ||
import ( | ||
"fmt" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/apache/arrow/go/v17/parquet" | ||
"github.com/invopop/jsonschema" | ||
) | ||
|
||
var allowedVersions = []string{"v1.0", "v2.4", "v2.6", "v2Latest"} | ||
var allowedRootRepetitions = []string{"undefined", "required", "optional", "repeated"} | ||
|
||
// nolint:revive | ||
type ParquetSpec struct{} | ||
type ParquetSpec struct { | ||
Version string `json:"version,omitempty"` | ||
RootRepetition string `json:"root_repetition,omitempty"` | ||
} | ||
|
||
func (s *ParquetSpec) GetVersion() parquet.Version { | ||
switch s.Version { | ||
case "v1.0": | ||
return parquet.V1_0 | ||
case "v2.4": | ||
return parquet.V2_4 | ||
case "v2.6": | ||
return parquet.V2_6 | ||
case "v2Latest": | ||
return parquet.V2_LATEST | ||
} | ||
return parquet.V2_LATEST | ||
} | ||
|
||
func (s *ParquetSpec) GetRootRepetition() parquet.Repetition { | ||
switch s.RootRepetition { | ||
case "undefined": | ||
return parquet.Repetitions.Undefined | ||
case "required": | ||
return parquet.Repetitions.Required | ||
case "optional": | ||
return parquet.Repetitions.Optional | ||
case "repeated": | ||
return parquet.Repetitions.Repeated | ||
} | ||
return parquet.Repetitions.Repeated | ||
} | ||
|
||
func (ParquetSpec) JSONSchema() *jsonschema.Schema { | ||
properties := jsonschema.NewProperties() | ||
allowedVersionsAsAny := make([]any, len(allowedVersions)) | ||
for i, v := range allowedVersions { | ||
allowedVersionsAsAny[i] = v | ||
} | ||
properties.Set("version", &jsonschema.Schema{ | ||
Type: "string", | ||
Description: "Parquet format version", | ||
Enum: allowedVersionsAsAny, | ||
Default: "v2Latest", | ||
}) | ||
|
||
allowedRootRepetitionsAsAny := make([]any, len(allowedRootRepetitions)) | ||
for i, v := range allowedRootRepetitions { | ||
allowedRootRepetitionsAsAny[i] = v | ||
} | ||
properties.Set("root_repetition", &jsonschema.Schema{ | ||
Type: "string", | ||
Description: "Root repetition", | ||
Enum: allowedRootRepetitionsAsAny, | ||
Default: "repeated", | ||
}) | ||
|
||
return &jsonschema.Schema{ | ||
Description: "CloudQuery Parquet file output spec.", | ||
Properties: properties, | ||
Type: "object", | ||
AdditionalProperties: jsonschema.FalseSchema, // "additionalProperties": false | ||
} | ||
} | ||
|
||
func (*ParquetSpec) SetDefaults() { | ||
func (s *ParquetSpec) SetDefaults() { | ||
if s.Version == "" { | ||
s.Version = "v2Latest" | ||
} | ||
if s.RootRepetition == "" { | ||
s.RootRepetition = "repeated" | ||
} | ||
} | ||
|
||
func (*ParquetSpec) Validate() error { | ||
func (s *ParquetSpec) Validate() error { | ||
if !slices.Contains(allowedVersions, s.Version) { | ||
return fmt.Errorf("invalid version: %s. Allowed values are %s", s.Version, strings.Join(allowedVersions, ", ")) | ||
} | ||
if !slices.Contains(allowedRootRepetitions, s.RootRepetition) { | ||
return fmt.Errorf("invalid rootRepetition: %s. Allowed values are %s", s.RootRepetition, strings.Join(allowedRootRepetitions, ", ")) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters