Skip to content

Commit

Permalink
Merge pull request #691 from dolthub/fulghum/analyze
Browse files Browse the repository at this point in the history
Convert `ANALYZE` statements
  • Loading branch information
fulghum authored Sep 11, 2024
2 parents 0b0dfae + d7ae942 commit 6f18975
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
19 changes: 17 additions & 2 deletions server/ast/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ func nodeAnalyze(node *tree.Analyze) (vitess.Statement, error) {
if node == nil {
return nil, nil
}
// This functions a bit differently compared to the GMS version, so it's unsupported for now
return nil, fmt.Errorf("ANALYZE is not yet supported")

objectName, ok := node.Table.(*tree.UnresolvedObjectName)
if !ok {
return nil, fmt.Errorf("unsupported table type in Analyze node: %T", node.Table)
}

if objectName.Object() == "" && objectName.Schema() == "" && objectName.Catalog() == "" {
return nil, fmt.Errorf("ANALYZE all tables not supported; must specify a table")
}

return &vitess.Analyze{Tables: []vitess.TableName{
{
Name: vitess.NewTableIdent(objectName.Object()),
SchemaQualifier: vitess.NewTableIdent(objectName.Schema()),
DbQualifier: vitess.NewTableIdent(objectName.Catalog()),
},
}}, nil
}
7 changes: 3 additions & 4 deletions testing/bats/dataloading/french-towns-communes-francaises.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36921,7 +36921,6 @@ SELECT pg_catalog.setval('towns_id_seq', 36684, true);

COMMIT;

-- TODO: ANALYZE is not supported by Doltgres yet
-- ANALYZE Regions;
-- ANALYZE Departments;
-- ANALYZE Towns;
ANALYZE Regions;
ANALYZE Departments;
ANALYZE Towns;
6 changes: 3 additions & 3 deletions testing/generation/command_docs/output/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAnalyze(t *testing.T) {
Unimplemented("ANALYZE ( VERBOSE true , SKIP_LOCKED true )"),
Unimplemented("ANALYZE ( SKIP_LOCKED , SKIP_LOCKED true )"),
Unimplemented("ANALYZE ( SKIP_LOCKED true , SKIP_LOCKED true )"),
Parses("ANALYZE table_name"),
Converts("ANALYZE table_name"),
Unimplemented("ANALYZE ( VERBOSE ) table_name"),
Unimplemented("ANALYZE ( VERBOSE true ) table_name"),
Unimplemented("ANALYZE ( SKIP_LOCKED ) table_name"),
Expand Down Expand Up @@ -292,8 +292,8 @@ func TestAnalyze(t *testing.T) {
Unimplemented("ANALYZE ( SKIP_LOCKED , SKIP_LOCKED true ) table_name ( column_name , column_name ) , table_name ( column_name , column_name )"),
Unimplemented("ANALYZE ( SKIP_LOCKED true , SKIP_LOCKED true ) table_name ( column_name , column_name ) , table_name ( column_name , column_name )"),
Unimplemented("ANALYZE"),
Parses("ANALYZE VERBOSE"),
Parses("ANALYZE table_name"),
Converts("ANALYZE VERBOSE"),
Converts("ANALYZE table_name"),
Unimplemented("ANALYZE VERBOSE table_name"),
Unimplemented("ANALYZE table_name ( column_name )"),
Unimplemented("ANALYZE VERBOSE table_name ( column_name )"),
Expand Down
59 changes: 59 additions & 0 deletions testing/go/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package _go

import (
"testing"

"github.com/dolthub/go-mysql-server/sql"
)

func TestStats(t *testing.T) {
RunScripts(t, StatsTests)
}

var StatsTests = []ScriptTest{
{
Name: "ANALYZE statement",
SetUpScript: []string{
"CREATE TABLE t (pk int primary key);",
},
Assertions: []ScriptTestAssertion{
{
// TODO: Postgres will analyze ALL tables if ANALYZE is invoked without a table name, but
// our postgres parser and our analysis code doesn't support this yet.
Skip: true,
Query: "ANALYZE;",
Expected: []sql.Row{},
},
{
Query: "ANALYZE t;",
Expected: []sql.Row{},
},
{
Query: "ANALYZE public.t;",
Expected: []sql.Row{},
},
{
Query: "ANALYZE postgres.public.t;",
Expected: []sql.Row{},
},
{
Query: "ANALYZE doesnotexists.public.t;",
ExpectedErr: "ERROR: database not found: doesnotexists (errno 1049) (sqlstate HY000) (SQLSTATE XX000)",
},
},
},
}

0 comments on commit 6f18975

Please sign in to comment.