Skip to content

Commit 9aeb91d

Browse files
authored
fix: support hyphens in table names during dependency resolution (#21)
1 parent 61c73e1 commit 9aeb91d

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

task/bq2bq/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ var (
4646
)
4747

4848
tableDestinationPatterns = regexp.MustCompile("" +
49-
"(?i)(?:FROM)\\s*(?:/\\*\\s*([a-zA-Z0-9@_-]*)\\s*\\*/)?\\s+`?([\\w-]+)\\.([\\w-]+)\\.(\\w+)`?" +
49+
"(?i)(?:FROM)\\s*(?:/\\*\\s*([a-zA-Z0-9@_-]*)\\s*\\*/)?\\s+`?([\\w-]+)\\.([\\w-]+)\\.([\\w-]+)`?" +
5050
"|" +
51-
"(?i)(?:JOIN)\\s*(?:/\\*\\s*([a-zA-Z0-9@_-]*)\\s*\\*/)?\\s+`?([\\w-]+)\\.([\\w-]+)\\.(\\w+)`?" +
51+
"(?i)(?:JOIN)\\s*(?:/\\*\\s*([a-zA-Z0-9@_-]*)\\s*\\*/)?\\s+`?([\\w-]+)\\.([\\w-]+)\\.([\\w-]+)`?" +
5252
"|" +
53-
"(?i)(?:WITH)\\s*(?:/\\*\\s*([a-zA-Z0-9@_-]*)\\s*\\*/)?\\s+`?([\\w-]+)\\.([\\w-]+)\\.(\\w+)`?\\s+(?:AS)")
53+
"(?i)(?:WITH)\\s*(?:/\\*\\s*([a-zA-Z0-9@_-]*)\\s*\\*/)?\\s+`?([\\w-]+)\\.([\\w-]+)\\.([\\w-]+)`?\\s+(?:AS)")
5454

5555
queryCommentPatterns = regexp.MustCompile("(--.*)|(((/\\*)+?[\\w\\W]*?(\\*/)+))")
5656
helperPattern = regexp.MustCompile("(\\/\\*\\s*(@[a-zA-Z0-9_-]+)\\s*\\*\\/)")

task/bq2bq/main_test.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ package main
33
import (
44
"context"
55
"fmt"
6-
"github.com/odpf/optimus/run"
76
"reflect"
87
"sort"
98
"testing"
109
"time"
1110

11+
"cloud.google.com/go/bigquery"
12+
"github.com/googleapis/google-cloud-go-testing/bigquery/bqiface"
1213
"github.com/odpf/optimus/models"
14+
"github.com/odpf/optimus/run"
1315
"github.com/patrickmn/go-cache"
1416
"github.com/stretchr/testify/assert"
15-
16-
"cloud.google.com/go/bigquery"
17-
"github.com/googleapis/google-cloud-go-testing/bigquery/bqiface"
1817
"github.com/stretchr/testify/mock"
1918
)
2019

@@ -148,13 +147,13 @@ func TestBQ2BQ(t *testing.T) {
148147
scheduledAt := time.Date(2021, 1, 15, 2, 2, 2, 2, time.UTC)
149148
t.Run("should not compile assets if load method is not replace", func(t *testing.T) {
150149
compileRequest := models.CompileAssetsRequest{
151-
Config: models.PluginConfigs{
150+
Config: models.PluginConfigs{
152151
{
153152
Name: "LOAD_METHOD",
154153
Value: "MERGE",
155154
},
156155
},
157-
Assets: models.PluginAssets{
156+
Assets: models.PluginAssets{
158157
{
159158
Name: "query.sql",
160159
Value: `Select * from table where ts > "{{.DSTART}}"`,
@@ -167,27 +166,27 @@ func TestBQ2BQ(t *testing.T) {
167166
resp, err := b2b.CompileAssets(ctx, compileRequest)
168167
assert.Nil(t, err)
169168
assert.NotNil(t, resp)
170-
compAsset ,_ := compileRequest.Assets.Get("query.sql")
171-
respAsset ,_ := resp.Assets.Get("query.sql")
169+
compAsset, _ := compileRequest.Assets.Get("query.sql")
170+
respAsset, _ := resp.Assets.Get("query.sql")
172171
assert.Equal(t, compAsset.Value, respAsset.Value)
173172
})
174173
t.Run("should not compile assets if load method is replace but window size is less than equal partition delta", func(t *testing.T) {
175174
compileRequest := models.CompileAssetsRequest{
176-
PluginOptions: models.PluginOptions{
175+
PluginOptions: models.PluginOptions{
177176
DryRun: false,
178177
},
179-
Config: models.PluginConfigs{
178+
Config: models.PluginConfigs{
180179
{
181180
Name: "LOAD_METHOD",
182181
Value: "REPLACE",
183182
},
184183
},
185-
Window: models.JobSpecTaskWindow{
184+
Window: models.JobSpecTaskWindow{
186185
Size: time.Hour * 24,
187186
Offset: 0,
188187
TruncateTo: "w",
189188
},
190-
Assets: models.PluginAssets{
189+
Assets: models.PluginAssets{
191190
{
192191
Name: "query.sql",
193192
Value: `Select * from table where ts > "{{.DSTART}}"`,
@@ -203,26 +202,26 @@ func TestBQ2BQ(t *testing.T) {
203202
assert.Nil(t, err)
204203
assert.NotNil(t, resp)
205204
compAsset, _ := compileRequest.Assets.Get("query.sql")
206-
respAsset ,_ := resp.Assets.Get("query.sql")
205+
respAsset, _ := resp.Assets.Get("query.sql")
207206
assert.Equal(t, compAsset.Value, respAsset.Value)
208207
})
209208
t.Run("should compile assets if load method is replace and break the query into multiple parts", func(t *testing.T) {
210209
compileRequest := models.CompileAssetsRequest{
211-
PluginOptions: models.PluginOptions{
210+
PluginOptions: models.PluginOptions{
212211
DryRun: false,
213212
},
214-
Config: models.PluginConfigs{
213+
Config: models.PluginConfigs{
215214
{
216215
Name: "LOAD_METHOD",
217216
Value: "REPLACE",
218217
},
219218
},
220-
Window: models.JobSpecTaskWindow{
219+
Window: models.JobSpecTaskWindow{
221220
Size: time.Hour * 24 * 7,
222221
Offset: 0,
223222
TruncateTo: "w",
224223
},
225-
Assets: models.PluginAssets{
224+
Assets: models.PluginAssets{
226225
{
227226
Name: "query.sql",
228227
Value: `Select * from table where ts > "{{.DSTART}}"`,
@@ -250,7 +249,7 @@ Select * from table where ts > "2021-01-14T00:00:00Z"
250249
Select * from table where ts > "2021-01-15T00:00:00Z"
251250
--*--optimus-break-marker--*--
252251
Select * from table where ts > "2021-01-16T00:00:00Z"`
253-
respAsset ,_ := resp.Assets.Get("query.sql")
252+
respAsset, _ := resp.Assets.Get("query.sql")
254253
assert.Equal(t, compAsset, respAsset.Value)
255254
})
256255

@@ -318,6 +317,12 @@ Select * from table where ts > "2021-01-16T00:00:00Z"`
318317
Sources: newSet("data-engineering.testing.table1"),
319318
Ignored: newSet(),
320319
},
320+
{
321+
Name: "simple query with hyphenated table name",
322+
Query: "select * from data-engineering.testing.table_name-1",
323+
Sources: newSet("data-engineering.testing.table_name-1"),
324+
Ignored: newSet(),
325+
},
321326
{
322327
Name: "simple query with quotes",
323328
Query: "select * from `data-engineering.testing.table1`",

task/bq2bq/validate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ func (f *vFactory) NewFromRegex(re, message string) survey.Validator {
2020
return fmt.Errorf("was expecting a string, got %s", k.String())
2121
}
2222
val := v.(string)
23-
matched := regex.Match([]byte(val))
24-
if matched == false {
23+
if !regex.Match([]byte(val)) {
2524
return fmt.Errorf(message)
2625
}
2726
return nil

0 commit comments

Comments
 (0)