Skip to content

Commit

Permalink
Merge pull request #10135 from Mytherin/defaultpreparedstatement
Browse files Browse the repository at this point in the history
Fix #10008 - disallow parameters in DEFAULT clause, and remove unsupported SQLite code for handling parameters in shell
  • Loading branch information
Mytherin committed Jan 4, 2024
2 parents cbccbc7 + 8799854 commit 731829d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 32 deletions.
4 changes: 3 additions & 1 deletion src/main/prepared_statement_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ bool PreparedStatementData::RequireRebind(ClientContext &context, optional_ptr<c
for (auto &it : value_map) {
auto &identifier = it.first;
auto lookup = values->find(identifier);
D_ASSERT(lookup != values->end());
if (lookup == values->end()) {
break;
}
if (lookup->second.type() != it.second->return_type) {
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions src/planner/binder/statement/bind_create_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ void Binder::BindDefaultValues(const ColumnList &columns, vector<unique_ptr<Expr
// we bind a copy of the DEFAULT value because binding is destructive
// and we want to keep the original expression around for serialization
auto default_copy = column.DefaultValue().Copy();
if (default_copy->HasParameter()) {
throw BinderException("DEFAULT values cannot contain parameters");
}
ConstantBinder default_binder(*this, context, "DEFAULT value");
default_binder.target_type = column.Type();
bound_default = default_binder.Bind(default_copy);
Expand Down
12 changes: 12 additions & 0 deletions test/sql/catalog/table/create_table_parameters.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# name: test/sql/catalog/table/create_table_parameters.test
# description: Issue #10008 - DuckDB SIGSEGV when creating table with DEFAULT ?
# group: [table]

statement error
CREATE TABLE t0 ( c1 INT DEFAULT ? );
----
DEFAULT values cannot contain parameters

statement error
CREATE TABLE t0 ( c1 INT CHECK (?) );
----
32 changes: 1 addition & 31 deletions tools/shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -12540,37 +12540,7 @@ static void bind_table_init(ShellState *p){
** tables. The table must be in the TEMP schema.
*/
static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
int nVar;
int i;
int rc;
sqlite3_stmt *pQ = 0;

nVar = sqlite3_bind_parameter_count(pStmt);
if( nVar==0 ) return; /* Nothing to do */
if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
"key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
return; /* Parameter table does not exist */
}
rc = sqlite3_prepare_v2(pArg->db,
"SELECT value FROM temp.sqlite_parameters"
" WHERE key=?1", -1, &pQ, 0);
if( rc || pQ==0 ) return;
for(i=1; i<=nVar; i++){
char zNum[30];
const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
if( zVar==0 ){
sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
zVar = zNum;
}
sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
if( sqlite3_step(pQ)==SQLITE_ROW ){
sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
}else{
sqlite3_bind_null(pStmt, i);
}
sqlite3_reset(pQ);
}
sqlite3_finalize(pQ);
return;
}

/*
Expand Down

0 comments on commit 731829d

Please sign in to comment.