Skip to content

Commit 99a397d

Browse files
committed
but create as and distributed by
1 parent 28ff1d2 commit 99a397d

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

tools/greenplum-to-pg-tests/cmd/extrace_sessions_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,45 @@ WHERE`,
3131
})
3232
}
3333
}
34+
35+
func TestCutGreenplumSpecific(t *testing.T) {
36+
table := []struct {
37+
name string
38+
from string
39+
to string
40+
}{
41+
{
42+
name: "CreateAs",
43+
from: `create table snb_eagle___hex_lavka_orders as
44+
select *
45+
from snb_geo_lavka___hex_lavka_orders;
46+
`,
47+
to: `
48+
select *
49+
from snb_geo_lavka___hex_lavka_orders;
50+
`,
51+
},
52+
{
53+
name: "CreateAndDistributedBy",
54+
from: ` CREATE TEMPORARY TABLE updated_tickets
55+
AS (
56+
SELECT DISTINCT chatterbox_ticket_id AS ticket_id
57+
FROM taxi_cdm_contactcenter___fct_chatterbox_ticket_event --fct_chatterbox_ticket_event
58+
WHERE utc_event_dttm BETWEEN ''2024-04-10 00:00:00''::timestamp AND ''2024-04-30 23:59:59''::timestamp
59+
)
60+
DISTRIBUTED BY (ticket_id);
61+
`,
62+
to: ` SELECT DISTINCT chatterbox_ticket_id AS ticket_id
63+
FROM taxi_cdm_contactcenter___fct_chatterbox_ticket_event --fct_chatterbox_ticket_event
64+
WHERE utc_event_dttm BETWEEN ''2024-04-10 00:00:00''::timestamp AND ''2024-04-30 23:59:59''::timestamp
65+
;
66+
`,
67+
},
68+
}
69+
for _, test := range table {
70+
t.Run(test.name, func(t *testing.T) {
71+
res := cutGreenplumSpecific(test.from)
72+
require.Equal(t, test.to, res)
73+
})
74+
}
75+
}

tools/greenplum-to-pg-tests/cmd/extraxtSessions.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ readLoop:
217217

218218
func checkQueries(rules Rules, stats *QueryStats, pgSchema *internal.PgSchema, db *ydb.Driver, sessions []internal.Session) {
219219
reasonFilter := regexp.MustCompile(extractSessionsConfig.filterReason)
220-
//checked := map[string]bool{}
221220

222221
errorLimit := extractSessionsConfig.errorLimit
223222
if errorLimit == 0 {
@@ -291,6 +290,7 @@ func checkQuery(stat *QueryStats, rules Rules, db *ydb.Driver, queryText string)
291290
queryText = strings.TrimSpace(queryText)
292291
queryText = fixSchemaNames(queryText)
293292
queryText = fixCreateTable(queryText)
293+
queryText = cutGreenplumSpecific(queryText)
294294

295295
ctx := context.Background()
296296
res, err := db.Query().Execute(
@@ -349,6 +349,19 @@ func fixCreateTable(queryText string) string {
349349
return queryText
350350
}
351351

352+
func cutGreenplumSpecific(q string) string {
353+
q = createAndDistributedByWithBrackets.ReplaceAllString(q, "$1;")
354+
q = createTableAsSelect.ReplaceAllLiteralString(q, "")
355+
q = distributedBy.ReplaceAllLiteralString(q, ";")
356+
return q
357+
}
358+
359+
var (
360+
createAndDistributedByWithBrackets = regexp.MustCompile(`(?is)CREATE\s+.*\sTABLE\s+.*\s+AS\s+\(\s*(.*)\s*\)\s+DISTRIBUTED\s+BY\s\(.*\);`)
361+
createTableAsSelect = regexp.MustCompile(`(?i)create\s+(temporary\s+)?table .* as`)
362+
distributedBy = regexp.MustCompile(`(?i)DISTRIBUTED BY ([^\)]+);`)
363+
)
364+
352365
type QueryStats struct {
353366
OkCount int
354367
TotalCount int

0 commit comments

Comments
 (0)