Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Databricks] Preprocess Arrays before COPY INTO #1084

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran sql format so it's more legible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only change here is (SELECT %s FROM '%s')

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
Loading