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

opt: internal error in optbuilder #49240

Closed
yuzefovich opened this issue May 19, 2020 · 4 comments · Fixed by #49287
Closed

opt: internal error in optbuilder #49240

yuzefovich opened this issue May 19, 2020 · 4 comments · Fixed by #49287
Assignees
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-sqlsmith

Comments

@yuzefovich
Copy link
Member

Extracted from #47397.

CREATE TABLE IF NOT EXISTS seed AS
        SELECT
                g::INT2 AS _int2,
                g::INT4 AS _int4,
                g::INT8 AS _int8,
                g::FLOAT4 AS _float4,
                g::FLOAT8 AS _float8,
                '2001-01-01'::DATE + g AS _date,
                '2001-01-01'::TIMESTAMP + g * '1 day'::INTERVAL AS _timestamp,
                '2001-01-01'::TIMESTAMPTZ + g * '1 day'::INTERVAL AS _timestamptz,
                g * '1 day'::INTERVAL AS _interval,
                g % 2 = 1 AS _bool,
                g::DECIMAL AS _decimal,
                g::STRING AS _string,
                g::STRING::BYTES AS _bytes,
                substring('00000000-0000-0000-0000-' || g::STRING || '00000000000', 1, 36)::UUID AS _uuid,
                '0.0.0.0'::INET + g AS _inet,
                g::STRING::JSONB AS _jsonb
        FROM
                generate_series(1, 5) AS g;

CREATE INDEX on seed (_int8, _float8, _date);

WITH
                        with_415 (col_2351)
                                AS (
                                        SELECT
                                                *
                                        FROM
                                                (
                                                        VALUES
                                                                ('9a61c55b-f746-4284-8bdc-c5e09a22385f':::UUID),
                                                                (crdb_internal.cluster_id()::UUID),
                                                                ('00000000-0000-0000-0000-000000000000':::UUID),
                                                                ('00000000-0000-0000-0000-000000000000':::UUID),
                                                                ('bf536146-18c5-4fa9-a8c4-d8ce042139a9':::UUID)
                                                )
                                                        AS tab_1010 (col_2351)
                                )
                SELECT
                        json_agg(tab_1012._float8)::JSONB AS col_2352,
                        max(tab_1011._timestamptz::TIMESTAMPTZ)::TIMESTAMPTZ AS col_2353,
                        percentile_cont(tab_1012._float8::FLOAT8)::FLOAT8 AS col_2354,
                        min(tab_1012._date::DATE)::DATE AS col_2355,
                        max(tab_1012._bytes::BYTES) OVER (PARTITION BY tab_1012._bytes ORDER BY tab_1012._bytes, tab_1012._jsonb ASC GROUPS BETWEEN (-5654078047671080934):::INT8 FOLLOWING AND UNBOUNDED FOLLOWING)::BYTES
                                AS col_2356,
                        power(tab_1012._decimal::DECIMAL, (-1.234E+401):::DECIMAL::DECIMAL)::DECIMAL AS col_2357
                FROM
                        defaultdb.public.seed@seed__int8__float8__date_idx AS tab_1011
                        JOIN defaultdb.public.seed@seed__int8__float8__date_idx AS tab_1012 ON
                                        (tab_1011._int4) = (tab_1012._int8)
                                        AND (tab_1011._int8) = (tab_1012._int2)
                                        AND (tab_1011._timestamp) = (tab_1012._timestamp)
                                        AND (tab_1011._float8) = (tab_1012._float4)
                GROUP BY
                        tab_1012._float8, tab_1012._decimal, tab_1011._timestamptz, tab_1012._jsonb, tab_1012._date, tab_1012._bytes
                HAVING
                        bool_and(tab_1012._bool::BOOL)::BOOL
                ORDER BY
                        tab_1012._date DESC;

results in

ERROR: internal error: runtime error: index out of range [0] with length 0
SQLSTATE: XX000
DETAIL: stack trace:
github.com/cockroachdb/cockroach/pkg/util/errorutil/catch.go:29: ShouldCatch()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:169: func1()
runtime/panic.go:679: gopanic()
runtime/panic.go:75: goPanicIndex()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/scope.go:1075: replaceAggregate()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/scope.go:909: VisitPre()
github.com/cockroachdb/cockroach/pkg/sql/sem/tree/walk.go:711: WalkExpr()
github.com/cockroachdb/cockroach/pkg/sql/sem/tree/walk.go:127: Walk()
github.com/cockroachdb/cockroach/pkg/sql/sem/tree/walk.go:714: WalkExpr()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/scope.go:387: walkExprTree()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/scope.go:426: resolveType()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/project.go:150: analyzeSelectList()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/project.go:84: analyzeProjectionList()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:960: buildSelectClause()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:898: buildSelectStmtWithoutParens()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:871: func1()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/with.go:29: processWiths()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:870: buildSelect()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:253: buildStmt()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:217: buildStmtAtRoot()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:188: Build()
github.com/cockroachdb/cockroach/pkg/sql/plan_opt.go:433: buildExecMemo()
github.com/cockroachdb/cockroach/pkg/sql/plan_opt.go:169: makeOptimizerPlan()
...
@yuzefovich yuzefovich added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-sqlsmith labels May 19, 2020
@yuzefovich
Copy link
Member Author

Here is another reproduction:

SELECT
	percentile_cont(tab_693.col_1228::FLOAT8)::FLOAT8
		AS col_1229,
	min(
		tab_693.col_1227::GEOGRAPHY(GEOMETRY,4326)
	)::GEOGRAPHY(GEOMETRY,4326)
		AS col_1230
FROM
	(
		VALUES
			(
				'0101000020E6100000000000000000F03F000000000000F03F':::GEOGRAPHY(GEOMETRY,4326),
				(
					NULL::FLOAT8
					^ (-1.422878093178996):::FLOAT8::FLOAT8
				)::FLOAT8
			),
			(
				'0101000020E6100000000000000000F03F000000000000F03F':::GEOGRAPHY(GEOMETRY,4326),
				NULL
			),
			(
				'0101000020E6100000000000000000F03F000000000000F03F':::GEOGRAPHY(GEOMETRY,4326),
				(-0.16539339565147823):::FLOAT8
			),
			(
				'0101000020E6100000000000000000F03F000000000000F03F':::GEOGRAPHY(GEOMETRY,4326),
				0.7795488482746985:::FLOAT8
			),
			(
				'0101000020E6100000000000000000F03F000000000000F03F':::GEOGRAPHY(GEOMETRY,4326),
				0.5770151565411787:::FLOAT8
			),
			(
				'0101000020E6100000000000000000F03F000000000000F03F':::GEOGRAPHY(GEOMETRY,4326),
				0.07236366531198213:::FLOAT8
			)
	)
		AS tab_693 (col_1227, col_1228)
GROUP BY
	tab_693.col_1227, tab_693.col_1228
ORDER BY
	tab_693.col_1228 DESC,
	tab_693.col_1228 ASC,
	tab_693.col_1227 DESC,
	tab_693.col_1227 ASC,
	tab_693.col_1227 DESC
LIMIT
	86:::INT8;

@RaduBerinde
Copy link
Member

This is related to #47668. I believe we should be throwing an error earlier if percentile_cont is used without WITHIN GROUP (ORDER BY ..).
CC @Anthuang let me know if you want to work on the fix.

@maddyblue
Copy link
Contributor

Reduced:

SELECT percentile_cont(NULL);

@Anthuang
Copy link
Contributor

Sure!

Anthuang added a commit to Anthuang/cockroach that referenced this issue May 19, 2020
Currently, if an ordered-set aggregate contains no WITHIN GROUP clause
or no single ORDER BY column, no error is thrown and thus we end up with
a panic when we try to access the missing ORDER BY column.
Instead, throw a syntax error to the user.

Resolves: cockroachdb#49240

Release note: None
craig bot pushed a commit that referenced this issue May 20, 2020
49287: opt: throw error if ordered-set aggregate is malformed r=RaduBerinde a=Anthuang

Currently, if an ordered-set aggregate contains no WITHIN GROUP clause
or no single ORDER BY column, no error is thrown and thus we end up with
a panic when we try to access the missing ORDER BY column.
Instead, throw a syntax error to the user.

Resolves: #49240

Release note: None

Co-authored-by: Anthony Huang <anthuang@umich.edu>
craig bot pushed a commit that referenced this issue May 21, 2020
49287: opt: throw error if ordered-set aggregate is malformed r=RaduBerinde a=Anthuang

Currently, if an ordered-set aggregate contains no WITHIN GROUP clause
or no single ORDER BY column, no error is thrown and thus we end up with
a panic when we try to access the missing ORDER BY column.
Instead, throw a syntax error to the user.

Resolves: #49240

Release note: None

Co-authored-by: Anthony Huang <anthuang@umich.edu>
@craig craig bot closed this as completed in 1837115 May 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-sqlsmith
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants