From 9e05bb6424a3d25b6f95c1b3e65aedc7c191ad53 Mon Sep 17 00:00:00 2001 From: Eric Harmeling Date: Wed, 6 Oct 2021 14:47:49 -0400 Subject: [PATCH 1/2] Added SHOW CREATE SCHEDULES diagram Release note: None --- docs/generated/sql/bnf/BUILD.bazel | 1 + docs/generated/sql/bnf/show_create_schedules_stmt.bnf | 3 +++ docs/generated/sql/bnf/show_var.bnf | 1 + docs/generated/sql/bnf/stmt_block.bnf | 5 +++++ pkg/cmd/docgen/diagrams.go | 3 +++ pkg/sql/parser/sql.y | 2 -- 6 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 docs/generated/sql/bnf/show_create_schedules_stmt.bnf diff --git a/docs/generated/sql/bnf/BUILD.bazel b/docs/generated/sql/bnf/BUILD.bazel index cef31d9a679d..bf0ac9f12616 100644 --- a/docs/generated/sql/bnf/BUILD.bazel +++ b/docs/generated/sql/bnf/BUILD.bazel @@ -183,6 +183,7 @@ genrule( "show_constraints.bnf", "show_constraints_stmt.bnf", "show_create_stmt.bnf", + "show_create_schedules_stmt.bnf", "show_databases_stmt.bnf", "show_default_privileges_stmt.bnf", "show_enums.bnf", diff --git a/docs/generated/sql/bnf/show_create_schedules_stmt.bnf b/docs/generated/sql/bnf/show_create_schedules_stmt.bnf new file mode 100644 index 000000000000..a40a8ed38dcb --- /dev/null +++ b/docs/generated/sql/bnf/show_create_schedules_stmt.bnf @@ -0,0 +1,3 @@ +show_create_schedules_stmt ::= + 'SHOW' 'CREATE' 'ALL' 'SCHEDULES' + | 'SHOW' 'CREATE' 'SCHEDULE' a_expr diff --git a/docs/generated/sql/bnf/show_var.bnf b/docs/generated/sql/bnf/show_var.bnf index e2e747556941..315d1d2b8be3 100644 --- a/docs/generated/sql/bnf/show_var.bnf +++ b/docs/generated/sql/bnf/show_var.bnf @@ -3,6 +3,7 @@ show_stmt ::= | show_columns_stmt | show_constraints_stmt | show_create_stmt + | show_create_schedules_stmt | show_csettings_stmt | show_databases_stmt | show_enums_stmt diff --git a/docs/generated/sql/bnf/stmt_block.bnf b/docs/generated/sql/bnf/stmt_block.bnf index fbc93af36c24..6801a01bb487 100644 --- a/docs/generated/sql/bnf/stmt_block.bnf +++ b/docs/generated/sql/bnf/stmt_block.bnf @@ -215,6 +215,7 @@ show_stmt ::= | show_columns_stmt | show_constraints_stmt | show_create_stmt + | show_create_schedules_stmt | show_csettings_stmt | show_databases_stmt | show_enums_stmt @@ -646,6 +647,10 @@ show_create_stmt ::= 'SHOW' 'CREATE' table_name | 'SHOW' 'CREATE' 'ALL' 'TABLES' +show_create_schedules_stmt ::= + 'SHOW' 'CREATE' 'ALL' 'SCHEDULES' + | 'SHOW' 'CREATE' 'SCHEDULE' a_expr + show_csettings_stmt ::= 'SHOW' 'CLUSTER' 'SETTING' var_name | 'SHOW' 'CLUSTER' 'SETTING' 'ALL' diff --git a/pkg/cmd/docgen/diagrams.go b/pkg/cmd/docgen/diagrams.go index 28b79262981b..203b34643f24 100644 --- a/pkg/cmd/docgen/diagrams.go +++ b/pkg/cmd/docgen/diagrams.go @@ -1358,6 +1358,9 @@ var specs = []stmtSpec{ stmt: "show_schedules_stmt", inline: []string{"schedule_state", "opt_schedule_executor_type"}, }, + { + name: "show_create_schedules_stmt", + }, { name: "show_schemas", stmt: "show_schemas_stmt", diff --git a/pkg/sql/parser/sql.y b/pkg/sql/parser/sql.y index 4c660cd4b3ee..6e7b6021e621 100644 --- a/pkg/sql/parser/sql.y +++ b/pkg/sql/parser/sql.y @@ -5667,13 +5667,11 @@ show_create_stmt: show_create_schedules_stmt: SHOW CREATE ALL SCHEDULES { - /* SKIP DOC */ $$.val = &tree.ShowCreateSchedules{} } | SHOW CREATE ALL SCHEDULES error // SHOW HELP: SHOW CREATE SCHEDULES | SHOW CREATE SCHEDULE a_expr { - /* SKIP DOC */ $$.val = &tree.ShowCreateSchedules{ScheduleID: $4.expr()} } | SHOW CREATE SCHEDULE error // SHOW HELP: SHOW CREATE SCHEDULES From 46f3fcc57bed60708e81c0c1d594a415c5f05c40 Mon Sep 17 00:00:00 2001 From: Yahor Yuzefovich Date: Wed, 6 Oct 2021 10:40:14 -0700 Subject: [PATCH 2/2] rowexec: fix zigzag joiner with ON expression in some cases Zigzag joiner needs to tell the row fetcher which columns are needed. Previously, we forgot to include the columns that are needed by the ON expression but are not needed in the output, so when evaluating such an ON expression, we would hit an internal error. This commit fixes the problem by including all columns referenced by the ON expression into the set of columns to be fetched. Release note (bug fix): Previously, CockroachDB could encounter an internal error when executing a zigzag join in some cases (when there are multiple filters present and at least one filter refers to the column that is part of STORING clause of the secondary index that is used by the zigzag join), and this has been fixed. --- .../logictest/testdata/logic_test/zigzag_join | 24 ++++++++ .../opt/exec/execbuilder/testdata/zigzag_join | 57 +++++++++++++++++++ pkg/sql/rowexec/zigzagjoiner.go | 9 +++ 3 files changed, 90 insertions(+) create mode 100644 pkg/sql/opt/exec/execbuilder/testdata/zigzag_join diff --git a/pkg/sql/logictest/testdata/logic_test/zigzag_join b/pkg/sql/logictest/testdata/logic_test/zigzag_join index a0a167a601dd..ce0e1b83c2a4 100644 --- a/pkg/sql/logictest/testdata/logic_test/zigzag_join +++ b/pkg/sql/logictest/testdata/logic_test/zigzag_join @@ -250,3 +250,27 @@ SELECT * FROM a@{FORCE_ZIGZAG=[6],FORCE_ZIGZAG=a_idx} WHERE a = 3 AND b = 7 statement error FORCE_ZIGZAG cannot be specified in conjunction with NO_ZIGZAG_JOIN SELECT * FROM a@{FORCE_ZIGZAG,NO_ZIGZAG_JOIN} WHERE a = 3 AND b = 7 + +# Regression tests for not fetching columns that are only needed by the ON +# expression (#71093). +statement ok +CREATE TABLE t71093 (a INT, b INT, c INT, d INT, INDEX a_idx(a) STORING (b), INDEX c_idx(c) STORING (d)); +INSERT INTO t71093 VALUES (0, 1, 2, 3) + +# ON expr needs the stored column from the left side. +query I +SELECT count(*) FROM t71093 WHERE a = 0 AND b = 1 AND c = 2 +---- +1 + +# ON expr needs the stored column from the right side. +query I +SELECT count(*) FROM t71093 WHERE a = 0 AND c = 2 AND d = 3 +---- +1 + +# ON expr needs the stored columns from both sides. +query I +SELECT count(*) FROM t71093 WHERE a = 0 AND b = 1 AND c = 2 AND d = 3 +---- +1 diff --git a/pkg/sql/opt/exec/execbuilder/testdata/zigzag_join b/pkg/sql/opt/exec/execbuilder/testdata/zigzag_join new file mode 100644 index 000000000000..5dee789ea03c --- /dev/null +++ b/pkg/sql/opt/exec/execbuilder/testdata/zigzag_join @@ -0,0 +1,57 @@ +# LogicTest: local + +# Make sure that the zigzag join is used in the regression tests for #71093. +statement ok +CREATE TABLE t71093 (a INT, b INT, c INT, d INT, INDEX a_idx(a) STORING (b), INDEX c_idx(c) STORING (d)); +INSERT INTO t71093 VALUES (0, 1, 2, 3) + +query T +EXPLAIN SELECT count(*) FROM t71093 WHERE a = 0 AND b = 1 AND c = 2 +---- +distribution: local +vectorized: true +· +• group (scalar) +│ +└── • zigzag join + pred: ((a = 0) AND (b = 1)) AND (c = 2) + left table: t71093@a_idx + left columns: (a, b) + left fixed values: 1 column + right table: t71093@c_idx + right columns: (c) + right fixed values: 1 column + +query T +EXPLAIN SELECT count(*) FROM t71093 WHERE a = 0 AND c = 2 AND d = 3 +---- +distribution: local +vectorized: true +· +• group (scalar) +│ +└── • zigzag join + pred: ((a = 0) AND (c = 2)) AND (d = 3) + left table: t71093@a_idx + left columns: (a) + left fixed values: 1 column + right table: t71093@c_idx + right columns: (c, d) + right fixed values: 1 column + +query T +EXPLAIN SELECT count(*) FROM t71093 WHERE a = 0 AND b = 1 AND c = 2 AND d = 3 +---- +distribution: local +vectorized: true +· +• group (scalar) +│ +└── • zigzag join + pred: (((a = 0) AND (b = 1)) AND (c = 2)) AND (d = 3) + left table: t71093@a_idx + left columns: (a, b) + left fixed values: 1 column + right table: t71093@c_idx + right columns: (c, d) + right fixed values: 1 column diff --git a/pkg/sql/rowexec/zigzagjoiner.go b/pkg/sql/rowexec/zigzagjoiner.go index c526a5bae578..2f731e140a24 100644 --- a/pkg/sql/rowexec/zigzagjoiner.go +++ b/pkg/sql/rowexec/zigzagjoiner.go @@ -466,6 +466,15 @@ func (z *zigzagJoiner) setupInfo( neededCols.Add(int(col)) } + // Add columns needed by OnExpr. + for _, v := range z.onCond.Vars.GetIndexedVars() { + // We only include the columns that come from this side (all such + // columns have the ordinals in [colOffset, maxCol) range). + if v.Idx >= colOffset && v.Idx < maxCol { + neededCols.Add(v.Idx - colOffset) + } + } + // Setup the RowContainers. info.container.Reset()