feat: enhance range handling in MSSQL and Postgres extractors; update partition range generator logic
This commit is contained in:
@@ -30,6 +30,8 @@ func buildExtractQueryMssql(
|
|||||||
includeRange bool,
|
includeRange bool,
|
||||||
isMinInclusive bool,
|
isMinInclusive bool,
|
||||||
isMaxInclusive bool,
|
isMaxInclusive bool,
|
||||||
|
hasMin bool,
|
||||||
|
hasMax bool,
|
||||||
) string {
|
) string {
|
||||||
var sbQuery strings.Builder
|
var sbQuery strings.Builder
|
||||||
|
|
||||||
@@ -53,23 +55,32 @@ func buildExtractQueryMssql(
|
|||||||
|
|
||||||
fmt.Fprintf(&sbQuery, " FROM [%s].[%s]", tableInfo.Schema, tableInfo.Table)
|
fmt.Fprintf(&sbQuery, " FROM [%s].[%s]", tableInfo.Schema, tableInfo.Table)
|
||||||
|
|
||||||
if includeRange {
|
if includeRange && (hasMin || hasMax) {
|
||||||
fmt.Fprintf(&sbQuery, " WHERE [%s]", tableInfo.PrimaryKey)
|
sbQuery.WriteString(" WHERE ")
|
||||||
if isMinInclusive {
|
|
||||||
sbQuery.WriteString(" >=")
|
if hasMin {
|
||||||
} else {
|
fmt.Fprintf(&sbQuery, "[%s]", tableInfo.PrimaryKey)
|
||||||
sbQuery.WriteString(" >")
|
if isMinInclusive {
|
||||||
|
sbQuery.WriteString(" >=")
|
||||||
|
} else {
|
||||||
|
sbQuery.WriteString(" >")
|
||||||
|
}
|
||||||
|
sbQuery.WriteString(" @min")
|
||||||
}
|
}
|
||||||
|
|
||||||
sbQuery.WriteString(" @min AND ")
|
if hasMin && hasMax {
|
||||||
fmt.Fprintf(&sbQuery, "[%s]", tableInfo.PrimaryKey)
|
sbQuery.WriteString(" AND ")
|
||||||
if isMaxInclusive {
|
|
||||||
sbQuery.WriteString(" <=")
|
|
||||||
} else {
|
|
||||||
sbQuery.WriteString(" <")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sbQuery.WriteString(" @max")
|
if hasMax {
|
||||||
|
fmt.Fprintf(&sbQuery, "[%s]", tableInfo.PrimaryKey)
|
||||||
|
if isMaxInclusive {
|
||||||
|
sbQuery.WriteString(" <=")
|
||||||
|
} else {
|
||||||
|
sbQuery.WriteString(" <")
|
||||||
|
}
|
||||||
|
sbQuery.WriteString(" @max")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(&sbQuery, " ORDER BY [%s] ASC", tableInfo.PrimaryKey)
|
fmt.Fprintf(&sbQuery, " ORDER BY [%s] ASC", tableInfo.PrimaryKey)
|
||||||
@@ -114,14 +125,16 @@ func (mssqlEx *MssqlExtractor) Exec(
|
|||||||
indexPrimaryKey int,
|
indexPrimaryKey int,
|
||||||
chBatchesOut chan<- models.Batch,
|
chBatchesOut chan<- models.Batch,
|
||||||
) (int, error) {
|
) (int, error) {
|
||||||
query := buildExtractQueryMssql(tableInfo, columns, partition.HasRange, partition.Range.IsMinInclusive, partition.Range.IsMaxInclusive)
|
hasMin := partition.HasRange && partition.Range.Min > 0
|
||||||
|
hasMax := partition.HasRange && partition.Range.Max > 0
|
||||||
|
query := buildExtractQueryMssql(tableInfo, columns, partition.HasRange, partition.Range.IsMinInclusive, partition.Range.IsMaxInclusive, hasMin, hasMax)
|
||||||
|
|
||||||
var queryArgs []any
|
var queryArgs []any
|
||||||
if partition.HasRange {
|
if hasMin {
|
||||||
queryArgs = append(queryArgs,
|
queryArgs = append(queryArgs, sql.Named("min", partition.Range.Min))
|
||||||
sql.Named("min", partition.Range.Min),
|
}
|
||||||
sql.Named("max", partition.Range.Max),
|
if hasMax {
|
||||||
)
|
queryArgs = append(queryArgs, sql.Named("max", partition.Range.Max))
|
||||||
}
|
}
|
||||||
|
|
||||||
rowsRead := 0
|
rowsRead := 0
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ func buildExtractQueryPostgres(
|
|||||||
includeRange bool,
|
includeRange bool,
|
||||||
isMinInclusive bool,
|
isMinInclusive bool,
|
||||||
isMaxInclusive bool,
|
isMaxInclusive bool,
|
||||||
|
hasMin bool,
|
||||||
|
hasMax bool,
|
||||||
) string {
|
) string {
|
||||||
var sbColumns strings.Builder
|
var sbColumns strings.Builder
|
||||||
|
|
||||||
@@ -54,20 +56,34 @@ func buildExtractQueryPostgres(
|
|||||||
|
|
||||||
query := fmt.Sprintf(`SELECT %s FROM "%s"."%s"`, sbColumns.String(), sourceDbInfo.Schema, sourceDbInfo.Table)
|
query := fmt.Sprintf(`SELECT %s FROM "%s"."%s"`, sbColumns.String(), sourceDbInfo.Schema, sourceDbInfo.Table)
|
||||||
|
|
||||||
if includeRange {
|
if includeRange && (hasMin || hasMax) {
|
||||||
query += fmt.Sprintf(` WHERE "%s"`, sourceDbInfo.PrimaryKey)
|
query += " WHERE "
|
||||||
if isMinInclusive {
|
paramIdx := 1
|
||||||
query += " >="
|
|
||||||
} else {
|
if hasMin {
|
||||||
query += " >"
|
query += fmt.Sprintf(`"%s"`, sourceDbInfo.PrimaryKey)
|
||||||
|
if isMinInclusive {
|
||||||
|
query += " >="
|
||||||
|
} else {
|
||||||
|
query += " >"
|
||||||
|
}
|
||||||
|
query += fmt.Sprintf(" $%d", paramIdx)
|
||||||
|
paramIdx++
|
||||||
}
|
}
|
||||||
query += " $1 AND " + fmt.Sprintf(`"%s"`, sourceDbInfo.PrimaryKey)
|
|
||||||
if isMaxInclusive {
|
if hasMin && hasMax {
|
||||||
query += " <="
|
query += " AND "
|
||||||
} else {
|
}
|
||||||
query += " <"
|
|
||||||
|
if hasMax {
|
||||||
|
query += fmt.Sprintf(`"%s"`, sourceDbInfo.PrimaryKey)
|
||||||
|
if isMaxInclusive {
|
||||||
|
query += " <="
|
||||||
|
} else {
|
||||||
|
query += " <"
|
||||||
|
}
|
||||||
|
query += fmt.Sprintf(" $%d", paramIdx)
|
||||||
}
|
}
|
||||||
query += " $2"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
query += fmt.Sprintf(` ORDER BY "%s" ASC`, sourceDbInfo.PrimaryKey)
|
query += fmt.Sprintf(` ORDER BY "%s" ASC`, sourceDbInfo.PrimaryKey)
|
||||||
@@ -84,14 +100,16 @@ func (postgresEx *PostgresExtractor) Exec(
|
|||||||
indexPrimaryKey int,
|
indexPrimaryKey int,
|
||||||
chBatchesOut chan<- models.Batch,
|
chBatchesOut chan<- models.Batch,
|
||||||
) (int, error) {
|
) (int, error) {
|
||||||
query := buildExtractQueryPostgres(tableInfo, columns, partition.HasRange, partition.Range.IsMinInclusive, partition.Range.IsMaxInclusive)
|
hasMin := partition.HasRange && partition.Range.Min > 0
|
||||||
|
hasMax := partition.HasRange && partition.Range.Max > 0
|
||||||
|
query := buildExtractQueryPostgres(tableInfo, columns, partition.HasRange, partition.Range.IsMinInclusive, partition.Range.IsMaxInclusive, hasMin, hasMax)
|
||||||
|
|
||||||
var queryArgs []any
|
var queryArgs []any
|
||||||
if partition.HasRange {
|
if hasMin {
|
||||||
queryArgs = append(queryArgs,
|
queryArgs = append(queryArgs, partition.Range.Min)
|
||||||
partition.Range.Min,
|
}
|
||||||
partition.Range.Max,
|
if hasMax {
|
||||||
)
|
queryArgs = append(queryArgs, partition.Range.Max)
|
||||||
}
|
}
|
||||||
|
|
||||||
rowsRead := 0
|
rowsRead := 0
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func PartitionRangeGenerator(
|
|||||||
rowsPerPartition int64,
|
rowsPerPartition int64,
|
||||||
jobRange config.RangeConfig,
|
jobRange config.RangeConfig,
|
||||||
) ([]models.Partition, error) {
|
) ([]models.Partition, error) {
|
||||||
if jobRange.Max > 0 {
|
if jobRange.Min > 0 {
|
||||||
return []models.Partition{{
|
return []models.Partition{{
|
||||||
Id: uuid.New(),
|
Id: uuid.New(),
|
||||||
HasRange: true,
|
HasRange: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user