Skip to content

Commit

Permalink
[Databricks] Preprocess Arrays before COPY INTO (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Dec 18, 2024
1 parent dcefa3a commit 5cc8dcb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
32 changes: 31 additions & 1 deletion clients/databricks/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log/slog"
"os"
"path/filepath"
"strings"

_ "github.com/databricks/databricks-sql-go"
"github.com/databricks/databricks-sql-go/driverctx"
Expand Down Expand Up @@ -120,9 +121,38 @@ func (s Store) PrepareTemporaryTable(ctx context.Context, tableData *optimizatio
}
}()

var ordinalColumns []string
for idx, column := range tableData.ReadOnlyInMemoryCols().ValidColumns() {
ordinalColumn := fmt.Sprintf("_c%d", idx)
switch column.KindDetails.Kind {
case typing.Array.Kind:
ordinalColumn = fmt.Sprintf(`PARSE_JSON(%s)`, ordinalColumn)
}

ordinalColumns = append(ordinalColumns, ordinalColumn)
}

// Copy file from DBFS -> table via COPY INTO, ref: https://docs.databricks.com/en/sql/language-manual/delta-copy-into.html
// We'll need \\\\N here because we need to string escape.
copyCommand := fmt.Sprintf(`COPY INTO %s BY POSITION FROM '%s' FILEFORMAT = CSV FORMAT_OPTIONS ('escape' = '"', 'delimiter' = '\t', 'header' = 'false', 'nullValue' = '\\\\N')`, tempTableID.FullyQualifiedName(), file.DBFSFilePath())
copyCommand := fmt.Sprintf(`
COPY INTO %s
BY POSITION
FROM (
SELECT %s FROM '%s'
)
FILEFORMAT = CSV
FORMAT_OPTIONS (
'escape' = '"',
'delimiter' = '\t',
'header' = 'false',
'nullValue' = '\\\\N'
);`,
// COPY INTO
tempTableID.FullyQualifiedName(),
// SELECT columns FROM file
strings.Join(ordinalColumns, ", "), file.DBFSFilePath(),
)

if _, err = s.ExecContext(ctx, copyCommand); err != nil {
return fmt.Errorf("failed to run COPY INTO for temporary table: %w", err)
}
Expand Down
8 changes: 8 additions & 0 deletions lib/typing/values/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func ToString(colVal any, colKind typing.KindDetails) (string, error) {
}
}
case typing.Array.Kind:
// If the column value is TOASTED, we should return an array with the TOASTED placeholder
// We're doing this to make sure that the value matches the schema.
if stringValue, ok := colVal.(string); ok {
if stringValue == constants.ToastUnavailableValuePlaceholder {
return fmt.Sprintf(`["%s"]`, constants.ToastUnavailableValuePlaceholder), nil
}
}

colValBytes, err := json.Marshal(colVal)
if err != nil {
return "", err
Expand Down
15 changes: 12 additions & 3 deletions lib/typing/values/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,18 @@ func TestToString(t *testing.T) {
}
{
// Array
val, err := ToString([]string{"foo", "bar"}, typing.Array)
assert.NoError(t, err)
assert.Equal(t, `["foo","bar"]`, val)
{
// Normal arrays
val, err := ToString([]string{"foo", "bar"}, typing.Array)
assert.NoError(t, err)
assert.Equal(t, `["foo","bar"]`, val)
}
{
// Toasted array
val, err := ToString(constants.ToastUnavailableValuePlaceholder, typing.Array)
assert.NoError(t, err)
assert.Equal(t, `["__debezium_unavailable_value"]`, val)
}
}
{
// Integer column
Expand Down

0 comments on commit 5cc8dcb

Please sign in to comment.