Skip to content

Commit 7a9d5bf

Browse files
yel0kReverseTM
authored andcommitted
Updated the calculation of the number of possible values to generation
1 parent 236b703 commit 7a9d5bf

File tree

18 files changed

+244
-146
lines changed

18 files changed

+244
-146
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ go 1.23.8
55
require (
66
github.com/apache/arrow-go/v18 v18.2.0
77
github.com/charmbracelet/huh/spinner v0.0.0-20250203114958-f07ae1af69ae
8+
github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3
89
github.com/google/uuid v1.6.0
910
github.com/hashicorp/go-retryablehttp v0.7.7
1011
github.com/ilyakaznacheev/cleanenv v1.5.0
1112
github.com/labstack/echo/v4 v4.13.3
1213
github.com/manifoldco/promptui v0.9.0
1314
github.com/moby/term v0.5.2
15+
github.com/otaviokr/topological-sort v1.1.0
1416
github.com/pkg/errors v0.9.1
1517
github.com/sashabaranov/go-openai v1.36.1
1618
github.com/spf13/afero v1.12.0
@@ -36,9 +38,7 @@ require (
3638
github.com/charmbracelet/x/term v0.2.1 // indirect
3739
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
3840
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
39-
github.com/emirpasic/gods v1.18.1 // indirect
4041
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
41-
github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3 // indirect
4242
github.com/goccy/go-json v0.10.5 // indirect
4343
github.com/golang/snappy v0.0.4 // indirect
4444
github.com/google/flatbuffers v25.2.10+incompatible // indirect
@@ -60,7 +60,6 @@ require (
6060
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
6161
github.com/muesli/cancelreader v0.2.2 // indirect
6262
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a // indirect
63-
github.com/otaviokr/topological-sort v1.1.0 // indirect
6463
github.com/pierrec/lz4/v4 v4.1.22 // indirect
6564
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
6665
github.com/rivo/uniseg v0.4.7 // indirect

go.sum

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,20 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
4141
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4242
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
4343
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44-
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
45-
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
4644
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
4745
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
4846
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
4947
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
5048
github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3 h1:fmFk0Wt3bBxxwZnu48jqMdaOR/IZ4vdtJFuaFV8MpIE=
5149
github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3/go.mod h1:bJWSKrZyQvfTnb2OudyUjurSG4/edverV7n82+K3JiM=
5250
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
51+
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
5352
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
5453
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
5554
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
5655
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
5756
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
5857
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
59-
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
60-
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
6158
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
6259
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
6360
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -79,7 +76,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
7976
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
8077
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
8178
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
82-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
8379
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
8480
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
8581
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
@@ -88,8 +84,6 @@ github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB1
8884
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
8985
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
9086
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
91-
github.com/heimdalr/dag v1.5.0 h1:hqVtijvY776P5OKP3QbdVBRt3Xxq6BYopz3XgklsGvo=
92-
github.com/heimdalr/dag v1.5.0/go.mod h1:lthekrHl01dddmzqyBQ1YZbi7XcVGGzjFo0jIky5knc=
9387
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
9488
github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=
9589
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
@@ -141,12 +135,15 @@ github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a h1:2MaM6YC3mGu54
141135
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a/go.mod h1:hxSnBBYLK21Vtq/PHd0S2FYCxBXzBua8ov5s1RobyRQ=
142136
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
143137
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
138+
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
144139
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
145140
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
146141
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
142+
github.com/onsi/ginkgo v1.16.1 h1:foqVmeWDD6yYpK+Yz3fHyNIxFYNxswxqNFjSKe+vI54=
147143
github.com/onsi/ginkgo v1.16.1/go.mod h1:CObGmKUOKaSC0RjmoAK7tKyn4Azo5P2IWuoMnvwxz1E=
148144
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
149145
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
146+
github.com/onsi/gomega v1.11.0 h1:+CqWgvj0OZycCaqclBD1pxKHAU+tOkHmQIWvDHq2aug=
150147
github.com/onsi/gomega v1.11.0/go.mod h1:azGKhqFUon9Vuj0YmTfLSmx0FUwqXYSTl5re8lQLTUg=
151148
github.com/otaviokr/topological-sort v1.1.0 h1:BrWj/bLOo9aZFUi0YN2/s4P/GRe2PSmb8cyX4w1ysNg=
152149
github.com/otaviokr/topological-sort v1.1.0/go.mod h1:77ZaKUg7Ir1nL6DPwEIQFm9iH2OS5xxVWvzZ8xPTCFg=
@@ -278,10 +275,12 @@ gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8
278275
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
279276
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
280277
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
278+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
281279
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
282280
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
283281
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
284282
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
283+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
285284
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
286285
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
287286
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/generator/common/utils.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"reflect"
10+
"regexp"
1011
"slices"
1112
"strconv"
1213
"strings"
@@ -357,3 +358,23 @@ func CtxClosed(ctx context.Context) bool {
357358
return false
358359
}
359360
}
361+
362+
func ExtractValuesFromTemplate(template string) []string {
363+
re := regexp.MustCompile(`{{\s*([^\s|(){}]+)[^}]*}}`)
364+
matches := re.FindAllStringSubmatch(template, -1)
365+
366+
values := make([]string, 0, len(matches))
367+
368+
for _, match := range matches {
369+
expr := match[0]
370+
val := match[1]
371+
372+
if strings.Contains(expr, "(") && strings.Contains(expr, ")") {
373+
continue
374+
}
375+
376+
values = append(values, val)
377+
}
378+
379+
return values
380+
}

internal/generator/common/utils_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,50 @@ func TestWalkWithFilter(t *testing.T) {
697697
t.Run(tc.name, func(t *testing.T) { testFunc(t, tc) })
698698
}
699699
}
700+
701+
func TestExtractValuesFromTemplate(t *testing.T) {
702+
type testCase struct {
703+
name string
704+
template string
705+
expected []string
706+
}
707+
708+
testCases := []testCase{
709+
{
710+
name: "Empty template",
711+
template: "",
712+
expected: []string{},
713+
},
714+
{
715+
name: "Valid template",
716+
template: "{{ foo }}.{{boo}}",
717+
expected: []string{"foo", "boo"},
718+
},
719+
{
720+
name: "Template with filters",
721+
template: "{{ foo | upper | lower }}",
722+
expected: []string{"foo"},
723+
},
724+
{
725+
name: "Template with functions",
726+
template: "{{ upper('foo') | lower }}@{{ boo }}",
727+
expected: []string{"boo"},
728+
},
729+
{
730+
name: "Invalid template",
731+
template: "{_{ foo }}",
732+
expected: []string{},
733+
},
734+
}
735+
736+
testFunc := func(t *testing.T, tc testCase) {
737+
t.Helper()
738+
739+
actual := ExtractValuesFromTemplate(tc.template)
740+
require.Equal(t, tc.expected, actual)
741+
}
742+
743+
for _, tc := range testCases {
744+
t.Run(tc.name, func(t *testing.T) { testFunc(t, tc) })
745+
}
746+
}

internal/generator/models/common.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package models
22

33
import (
44
"encoding/json"
5-
"github.com/otaviokr/topological-sort/toposort"
5+
"github.com/tarantool/sdvg/internal/generator/common"
66
"io"
77
"os"
88
"path/filepath"
99
"reflect"
10-
"regexp"
1110
"strings"
1211

1312
"github.com/ilyakaznacheev/cleanenv"
13+
"github.com/otaviokr/topological-sort/toposort"
1414
"github.com/pkg/errors"
1515
"gopkg.in/yaml.v3"
1616
)
@@ -122,45 +122,24 @@ func parseErrsToString(errs []error) string {
122122
return sb.String()
123123
}
124124

125-
func TopologicalSort(columns []*Column) ([]string, error) {
125+
func topologicalSort(columns []*Column) ([]string, error) {
126126
graph := make(map[string][]string)
127127
for _, c := range columns {
128128
graph[c.Name] = make([]string, 0)
129129

130-
if c.Type != "string" {
131-
continue
132-
}
133-
134130
for _, r := range c.Ranges {
135-
if r.StringParams.Template == "" {
131+
if r.StringParams == nil || r.StringParams.Template == "" {
136132
continue
137133
}
138134

139-
graph[c.Name] = extractValuesFromTemplate(r.StringParams.Template)
135+
graph[c.Name] = common.ExtractValuesFromTemplate(r.StringParams.Template)
140136
}
141137
}
142138

143139
sortedVertexes, err := toposort.ReverseTarjan(graph)
144140
if err != nil {
145-
return nil, err
141+
return nil, errors.New(err.Error())
146142
}
147143

148144
return sortedVertexes, nil
149145
}
150-
151-
func extractValuesFromTemplate(template string) []string {
152-
re := regexp.MustCompile(`{{\s*([^}]+)\s*}}`)
153-
matches := re.FindAllStringSubmatch(template, -1)
154-
155-
var values []string
156-
for _, match := range matches {
157-
expr := match[1]
158-
159-
parts := regexp.MustCompile(`\s*\|\s*|\s+`).Split(expr, -1)
160-
if len(parts) > 0 && parts[0] != "" && !strings.Contains(parts[0], "(") {
161-
values = append(values, parts[0])
162-
}
163-
}
164-
165-
return values
166-
}

internal/generator/models/common_test.go

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,10 @@
11
package models
22

33
import (
4-
"github.com/stretchr/testify/require"
54
"testing"
6-
)
7-
8-
func TestExtractValuesFromTemplate(t *testing.T) {
9-
type testCase struct {
10-
name string
11-
template string
12-
expected []string
13-
}
14-
15-
testCases := []testCase{
16-
{
17-
name: "Empty template",
18-
template: "",
19-
expected: nil,
20-
},
21-
{
22-
name: "Valid template",
23-
template: "{{ foo }}.{{boo}}",
24-
expected: []string{"foo", "boo"},
25-
},
26-
{
27-
name: "Template with filters",
28-
template: "{{ foo | upper | lower }}",
29-
expected: []string{"foo"},
30-
},
31-
{
32-
name: "Template with functions",
33-
template: "{{ upper('foo') | lower }}@{{ boo }}",
34-
expected: []string{"boo"},
35-
},
36-
{
37-
name: "Invalid template",
38-
template: "{_{ foo }}",
39-
expected: nil,
40-
},
41-
}
425

43-
testFunc := func(t *testing.T, tc testCase) {
44-
t.Helper()
45-
46-
actual := extractValuesFromTemplate(tc.template)
47-
require.Equal(t, tc.expected, actual)
48-
}
49-
50-
for _, tc := range testCases {
51-
t.Run(tc.name, func(t *testing.T) { testFunc(t, tc) })
52-
}
53-
}
6+
"github.com/stretchr/testify/require"
7+
)
548

559
func TestTopologicalSort(t *testing.T) {
5610
type testCase struct {
@@ -152,7 +106,7 @@ func TestTopologicalSort(t *testing.T) {
152106
testFunc := func(t *testing.T, tc testCase) {
153107
t.Helper()
154108

155-
actual, err := TopologicalSort(tc.columns)
109+
actual, err := topologicalSort(tc.columns)
156110
require.Equal(t, tc.wantErr, err != nil)
157111
require.Equal(t, tc.expected, actual)
158112
}

internal/generator/models/generator_model.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ type Model struct {
3030
RowsPerFile uint64 `backup:"true" json:"rows_per_file" yaml:"rows_per_file"`
3131
ModelDir string `backup:"true" json:"model_dir" yaml:"model_dir"`
3232
// The columns from the partitioning key with PartitionColumn.WriteToOutput == false, must be at the end of slice.
33-
Columns []*Column `backup:"true" json:"columns" yaml:"columns"`
34-
PartitionColumns []*PartitionColumn `backup:"true" json:"partition_columns" yaml:"partition_columns"`
33+
Columns []*Column `backup:"true" json:"columns" yaml:"columns"`
34+
ColumnsTopologicalOrder []string
35+
PartitionColumns []*PartitionColumn `backup:"true" json:"partition_columns" yaml:"partition_columns"`
3536
}
3637

3738
// PartitionColumn type is used to describe partition parameters for column.
@@ -80,6 +81,13 @@ func (m *Model) Parse() error {
8081

8182
m.shiftColumnsToEnd(nonWriteableColumns)
8283

84+
sortedColumns, err := topologicalSort(m.Columns)
85+
if err != nil {
86+
return errors.WithMessage(err, "failed to sorting columns by dependencies")
87+
}
88+
89+
m.ColumnsTopologicalOrder = sortedColumns
90+
8391
return nil
8492
}
8593

internal/generator/models/models_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,9 @@ parquet params:
11971197
expectedModel := tc.expected.Models[modelName]
11981198
gotModel := cfg.Models[modelName]
11991199

1200+
// skip ColumnsTopologicalOrder check
1201+
expectedModel.ColumnsTopologicalOrder = gotModel.ColumnsTopologicalOrder
1202+
12001203
for columnName := range expectedModel.Columns {
12011204
expectedColumn := expectedModel.Columns[columnName]
12021205
gotColumn := gotModel.Columns[columnName]

0 commit comments

Comments
 (0)