-
Notifications
You must be signed in to change notification settings - Fork 203
Add Pipelines Show Command #6033
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
Open
tanishgupta-db
wants to merge
5
commits into
databricks:main
Choose a base branch
from
tanishgupta-db:preview-datasets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a77e404
preview-datasets v1
tanishgupta-db 58be02e
renamed to show and moved to pipelines
tanishgupta-db fd3d0e2
move static rendering to tableview lib
tanishgupta-db a7e2528
review fixes and handling Ctrl+C
tanishgupta-db 14eb197
changing to use the cmdio renderer
tanishgupta-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
|
|
||
| === table output | ||
|
|
||
| >>> [CLI] pipelines show main.default.people | ||
| id name | ||
| -- ----- | ||
| 1 alice | ||
| 2 bob | ||
|
|
||
| 2 rows | ||
|
|
||
| === json output | ||
|
|
||
| >>> [CLI] pipelines show main.default.people --output json | ||
| [ | ||
| { | ||
| "id": "1", | ||
| "name": "alice" | ||
| }, | ||
| { | ||
| "id": "2", | ||
| "name": "bob" | ||
| } | ||
| ] | ||
|
|
||
| === invalid table name | ||
|
|
||
| >>> [CLI] pipelines show people | ||
| Error: invalid table name "people": expected catalog.schema.table (Unity Catalog) or schema.table (legacy Hive metastore) | ||
|
|
||
| === wire SQL pins quoting and default limit | ||
|
|
||
| >>> print_requests.py //api/2.0/sql/statements | ||
| { | ||
| "method": "POST", | ||
| "path": "/api/2.0/sql/statements", | ||
| "body": { | ||
| "disposition": "INLINE", | ||
| "format": "JSON_ARRAY", | ||
| "on_wait_timeout": "CONTINUE", | ||
| "statement": "SELECT * FROM `main`.`default`.`people` LIMIT 10", | ||
| "wait_timeout": "10s", | ||
| "warehouse_id": "wh-test" | ||
| } | ||
| } | ||
| { | ||
| "method": "POST", | ||
| "path": "/api/2.0/sql/statements", | ||
| "body": { | ||
| "disposition": "INLINE", | ||
| "format": "JSON_ARRAY", | ||
| "on_wait_timeout": "CONTINUE", | ||
| "statement": "SELECT * FROM `main`.`default`.`people` LIMIT 10", | ||
| "wait_timeout": "10s", | ||
| "warehouse_id": "wh-test" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| title "table output\n" | ||
| trace $CLI pipelines show main.default.people | ||
|
|
||
| title "json output\n" | ||
| trace $CLI pipelines show main.default.people --output json | ||
|
|
||
| title "invalid table name\n" | ||
| musterr trace $CLI pipelines show people | ||
|
|
||
| title "wire SQL pins quoting and default limit\n" | ||
| trace print_requests.py //api/2.0/sql/statements |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| RecordRequests = true | ||
|
|
||
| [EnvMatrix] | ||
| DATABRICKS_BUNDLE_ENGINE = [] | ||
|
|
||
| [Env] | ||
| DATABRICKS_WAREHOUSE_ID = "wh-test" | ||
|
|
||
| # The testserver's SQL Statement Execution API is matcher-driven and fails | ||
| # unmatched statements by default; stub a canned result so the golden shows real | ||
| # columns and rows. The SDK POSTs to the path without a trailing slash. | ||
| [[Server]] | ||
| Pattern = "POST /api/2.0/sql/statements" | ||
| Response.Body = ''' | ||
| { | ||
| "statement_id": "s1", | ||
| "status": {"state": "SUCCEEDED"}, | ||
| "manifest": {"schema": {"columns": [{"name": "id"}, {"name": "name"}]}, "total_chunk_count": 1}, | ||
| "result": {"data_array": [["1", "alice"], ["2", "bob"]]} | ||
| } | ||
| ''' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
|
|
||
| === help | ||
| >>> [CLI] pipelines show --help | ||
| Preview a table's columns and sample rows on a SQL warehouse. | ||
|
|
||
| TABLE is a fully-qualified name: catalog.schema.table (Unity Catalog) or | ||
| schema.table (legacy Hive metastore). | ||
|
|
||
| Usage: | ||
| databricks pipelines show TABLE [flags] | ||
|
|
||
| Flags: | ||
| -h, --help help for show | ||
| -n, --limit int Maximum number of rows to fetch. (default 10) | ||
| --warehouse-id string SQL warehouse to run the query on. Defaults to DATABRICKS_WAREHOUSE_ID or a workspace default. | ||
|
|
||
| Global Flags: | ||
| --debug enable debug logging | ||
| -o, --output type output type: text or json (default text) | ||
| -p, --profile string ~/.databrickscfg profile | ||
| -t, --target string bundle target to use (if applicable) | ||
| --var strings set values for variables defined in project config. Example: --var="foo=bar" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Pin the command tree so any change to the command or its help shows up as a diff here. | ||
|
|
||
| title "help" | ||
| trace $CLI pipelines show --help |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # --help prints without authenticating, so no server stubs are needed. | ||
| [EnvMatrix] | ||
| DATABRICKS_BUNDLE_ENGINE = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,5 +20,6 @@ func Commands() []*cobra.Command { | |
| historyCommand(), | ||
| logsCommand(), | ||
| openCommand(), | ||
| showCommand(), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package pipelines | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| "github.com/databricks/cli/cmd/root" | ||
| "github.com/databricks/cli/libs/cmdctx" | ||
| "github.com/databricks/cli/libs/databrickscfg/cfgpickers" | ||
| "github.com/databricks/cli/libs/flags" | ||
| "github.com/databricks/cli/libs/sqlexec" | ||
| "github.com/databricks/databricks-sdk-go" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const defaultLimit = 10 | ||
|
|
||
| func showCommand() *cobra.Command { | ||
| var warehouseID string | ||
| var limit int | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "show TABLE", | ||
| Short: "Preview a table's columns and sample rows", | ||
| Long: `Preview a table's columns and sample rows on a SQL warehouse. | ||
|
|
||
| TABLE is a fully-qualified name: catalog.schema.table (Unity Catalog) or | ||
| schema.table (legacy Hive metastore).`, | ||
| Args: root.ExactArgs(1), | ||
| PreRunE: root.MustWorkspaceClient, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| // The CLI root doesn't cancel ctx on signals, so Ctrl-C / kill would | ||
| // otherwise leave Execute polling the warehouse until the query ends. | ||
| ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM) | ||
| defer stop() | ||
| w := cmdctx.WorkspaceClient(ctx) | ||
|
|
||
| if limit <= 0 { | ||
| return errors.New("--limit must be a positive integer") | ||
| } | ||
|
|
||
| quoted, err := quoteTableName(args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| warehouseID, err = resolveWarehouseID(ctx, w, warehouseID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| client := sqlexec.New(w.StatementExecution, warehouseID) | ||
| statement := fmt.Sprintf("SELECT * FROM %s LIMIT %d", quoted, limit) | ||
| result, err := client.Execute(ctx, statement) | ||
| if err != nil { | ||
| if ctx.Err() != nil { | ||
| fmt.Fprintln(cmd.ErrOrStderr(), "\nCancelled.") | ||
| return root.ErrAlreadyPrinted | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| return render(ctx, cmd, result.Columns, result.Rows) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&warehouseID, "warehouse-id", "", "SQL warehouse to run the query on. Defaults to DATABRICKS_WAREHOUSE_ID or a workspace default.") | ||
| cmd.Flags().IntVarP(&limit, "limit", "n", defaultLimit, "Maximum number of rows to fetch.") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| // warehouse to query either --warehouse-id flag, DATABRICKS_WAREHOUSE_ID config, | ||
| // or the workspace default warehouse. | ||
| func resolveWarehouseID(ctx context.Context, w *databricks.WorkspaceClient, flag string) (string, error) { | ||
| if flag != "" { | ||
| return flag, nil | ||
| } | ||
| if w.Config.WarehouseID != "" { | ||
| return w.Config.WarehouseID, nil | ||
| } | ||
| warehouse, err := cfgpickers.GetDefaultWarehouse(ctx, w) | ||
| if errors.Is(err, cfgpickers.ErrNoCompatibleWarehouses) { | ||
| return "", errors.New("no SQL warehouse available; pass --warehouse-id or set DATABRICKS_WAREHOUSE_ID") | ||
| } | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return warehouse.Id, nil | ||
| } | ||
|
|
||
| // writes the result as JSON (--output json) or a terminal-width-aware table. | ||
| func render(ctx context.Context, cmd *cobra.Command, columns []string, rows [][]string) error { | ||
| out := cmd.OutOrStdout() | ||
| switch root.OutputType(cmd) { | ||
| case flags.OutputJSON: | ||
| return renderJSON(out, columns, rows) | ||
| case flags.OutputText: | ||
| if len(columns) == 0 { | ||
| return nil | ||
| } | ||
| return renderTable(ctx, columns, rows, detectWidth(out)) | ||
| default: | ||
| return fmt.Errorf("unknown output type %s", root.OutputType(cmd)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package pipelines | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // validates a fully-qualified table name and returns it with each | ||
| // segment backtick-quoted. Accepts two-part (schema.table, Hive) | ||
| // and three-part (catalog.schema.table, Unity Catalog) names. | ||
| func quoteTableName(name string) (string, error) { | ||
| parts := strings.Split(name, ".") | ||
| if len(parts) < 2 || len(parts) > 3 { | ||
| return "", fmt.Errorf("invalid table name %q: expected catalog.schema.table (Unity Catalog) or schema.table (legacy Hive metastore)", name) | ||
| } | ||
|
|
||
| quoted := make([]string, len(parts)) | ||
| for i, part := range parts { | ||
| if part == "" { | ||
| return "", fmt.Errorf("invalid table name %q: empty identifier segment", name) | ||
| } | ||
| quoted[i] = "`" + strings.ReplaceAll(part, "`", "``") + "`" | ||
| } | ||
| return strings.Join(quoted, "."), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package pipelines | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestQuoteTableName(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| in string | ||
| want string | ||
| }{ | ||
| {"three part", "main.sales.orders", "`main`.`sales`.`orders`"}, | ||
| {"two part", "sales.orders", "`sales`.`orders`"}, | ||
| {"hive metastore", "hive_metastore.sales.orders", "`hive_metastore`.`sales`.`orders`"}, | ||
| {"leading digit segment", "cat.sch.2020_sales", "`cat`.`sch`.`2020_sales`"}, | ||
| {"hyphen segment", "cat.sch.order-items", "`cat`.`sch`.`order-items`"}, | ||
| {"space segment", "cat.sch.my table", "`cat`.`sch`.`my table`"}, | ||
| {"reserved word segment", "cat.sch.order", "`cat`.`sch`.`order`"}, | ||
| {"internal backtick doubled", "cat.sch.wei`rd", "`cat`.`sch`.`wei``rd`"}, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got, err := quoteTableName(tc.in) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tc.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestQuoteTableNameErrors(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| in string | ||
| }{ | ||
| {"one part", "orders"}, | ||
| {"four part", "a.b.c.d"}, | ||
| {"empty", ""}, | ||
| {"empty middle segment", "cat..table"}, | ||
| {"trailing dot", "cat.sch."}, | ||
| {"leading dot", ".sch.table"}, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| _, err := quoteTableName(tc.in) | ||
| assert.Error(t, err) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if a user passes in a table name that already has backticks, like "
main.default.people"?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume the backticks are literal characters in the name (e.g. an MV whose catalog is named
main). To address that correctly I double internal backticks, so such names are reachable.if the user meant "main.default.people" and pre-quoted, they'll see a not-found error.