Skip to content

Commit

Permalink
Merge pull request #37 from ydb-platform/fix-column-names
Browse files Browse the repository at this point in the history
add ``  to columns name in query
  • Loading branch information
VPolka authored Oct 14, 2024
2 parents c10a707 + b4a69a5 commit 0dfe785
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 8 additions & 2 deletions internal/dst_table/gen_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type UpdatingData struct {
func (data *UpdatingData) GetSortUpdatingColumns() string {
sortColumns := make([]string, 0, len(data.ColumnValues))
for key := range data.ColumnValues {
sortColumns = append(sortColumns, key)
sortColumns = append(sortColumns, fmt.Sprintf("`%s`", key))
}
sort.Strings(sortColumns)

Expand Down Expand Up @@ -346,7 +346,13 @@ func GenListParam(ctx context.Context, tableMetaInfo TableMetaInfo, txsData []Up
func GenQueryFromUpdateTx(ctx context.Context, tableMetaInfo TableMetaInfo, txData []UpdatingData, localStatementNum int, globalStatementNum int) (QueryStatement, error) {
result := NewQueryStatement()
pName := "$p_" + string(fmt.Sprint(globalStatementNum)) + "_" + string(fmt.Sprint(localStatementNum))
allColumns := strings.Join(tableMetaInfo.PrimaryKey, ", ")

quotedKeys := make([]string, len(tableMetaInfo.PrimaryKey))
for i, key := range tableMetaInfo.PrimaryKey {
quotedKeys[i] = fmt.Sprintf("`%s`", key)
}
allColumns := strings.Join(quotedKeys, ", ")

if len(txData[0].ColumnValues) > 0 {
allColumns += ", " + txData[0].ColumnsString
}
Expand Down
12 changes: 6 additions & 6 deletions internal/dst_table/gen_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestGenUpdateQuery(t *testing.T) {
txData2, _ := reader.ParseTxData(ctx, []byte("{\"update\":{\"value1\":\"15\", \"value3\":1.00000009},\"key\":[16,\"16\"],\"ts\":[18446744073709551614,18446744073709551614]}"), 0)
query, err := GenQueryFromUpdateTx(ctx, GetTestTableMetaInfo(), []UpdatingData{CreateData(txData1), CreateData(txData2)}, 0, 1)
require.Nil(t, err)
assert.Equal(t, "UPSERT INTO path (key1, key2, value1, value2, value3, value4) SELECT key1, key2, value1, value2, value3, value4 FROM AS_TABLE($p_1_0);\n", query.Statement)
assert.Equal(t, "UPSERT INTO path (`key1`, `key2`, `value1`, `value2`, `value3`, `value4`) SELECT `key1`, `key2`, `value1`, `value2`, `value3`, `value4` FROM AS_TABLE($p_1_0);\n", query.Statement)
assert.Equal(t, len(query.Params), 1)
assert.Equal(t, query.Params[0].Name(), "$p_1_0")
expectedParams := "[" +
Expand Down Expand Up @@ -98,8 +98,8 @@ func TestGenQuery(t *testing.T) {
txData8, _ := reader.ParseTxData(ctx, []byte("{\"update\":{\"value1\":\"27\", \"value3\":1.00000009},\"key\":[17,\"17\"],\"ts\":[18446744073709551613,18446744073709551613]}"), 0)
query, err := GenQuery(ctx, GetTestTableMetaInfo(), []types.TxData{txData1, txData2, txData3, txData4, txData5, txData6, txData7, txData8}, 0)
require.Nil(t, err)
expectedResult := "UPSERT INTO path (key1, key2, value1, value2, value3, value4) SELECT key1, key2, value1, value2, value3, value4 FROM AS_TABLE($p_0_0);\n" +
"UPSERT INTO path (key1, key2, value1, value3) SELECT key1, key2, value1, value3 FROM AS_TABLE($p_0_1);\n" +
expectedResult := "UPSERT INTO path (`key1`, `key2`, `value1`, `value2`, `value3`, `value4`) SELECT `key1`, `key2`, `value1`, `value2`, `value3`, `value4` FROM AS_TABLE($p_0_0);\n" +
"UPSERT INTO path (`key1`, `key2`, `value1`, `value3`) SELECT `key1`, `key2`, `value1`, `value3` FROM AS_TABLE($p_0_1);\n" +
"DELETE FROM path ON SELECT * FROM AS_TABLE($p_0_2);\n"
assert.Equal(t, expectedResult, query.Query)
expectedParams := "{" +
Expand Down Expand Up @@ -130,8 +130,8 @@ func TestGenOnlyUpsertQuery(t *testing.T) {
txData5, _ := reader.ParseTxData(ctx, []byte("{\"update\":{\"value1\":\"27\", \"value3\":1.00000009},\"key\":[17,\"17\"],\"ts\":[18446744073709551613,18446744073709551613]}"), 0)
query, err := GenQuery(ctx, GetTestTableMetaInfo(), []types.TxData{txData1, txData2, txData3, txData4, txData5}, 0)
require.Nil(t, err)
expectedResult := "UPSERT INTO path (key1, key2, value1, value2, value3, value4) SELECT key1, key2, value1, value2, value3, value4 FROM AS_TABLE($p_0_0);\n" +
"UPSERT INTO path (key1, key2, value1, value3) SELECT key1, key2, value1, value3 FROM AS_TABLE($p_0_1);\n"
expectedResult := "UPSERT INTO path (`key1`, `key2`, `value1`, `value2`, `value3`, `value4`) SELECT `key1`, `key2`, `value1`, `value2`, `value3`, `value4` FROM AS_TABLE($p_0_0);\n" +
"UPSERT INTO path (`key1`, `key2`, `value1`, `value3`) SELECT `key1`, `key2`, `value1`, `value3` FROM AS_TABLE($p_0_1);\n"
assert.Equal(t, expectedResult, query.Query)
expectedParams := "{" +
"\"$p_0_0\":" +
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestGenQueryWithTimestamp(t *testing.T) {
txData1, _ := reader.ParseTxData(ctx, []byte("{\"update\":{\"value1\":\"15\", \"value5\":\"1970-01-01T00:00:01.000001Z\"},\"key\":[15,\"15\"],\"ts\":[18446744073709551615,18446744073709551615]}"), 0)
query, err := GenQueryFromUpdateTx(ctx, GetTestTableMetaInfo(), []UpdatingData{CreateData(txData1)}, 0, 1)
require.Nil(t, err)
assert.Equal(t, "UPSERT INTO path (key1, key2, value1, value5) SELECT key1, key2, value1, value5 FROM AS_TABLE($p_1_0);\n", query.Statement)
assert.Equal(t, "UPSERT INTO path (`key1`, `key2`, `value1`, `value5`) SELECT `key1`, `key2`, `value1`, `value5` FROM AS_TABLE($p_1_0);\n", query.Statement)
assert.Equal(t, len(query.Params), 1)
assert.Equal(t, query.Params[0].Name(), "$p_1_0")
expectedParams := "[" +
Expand Down

0 comments on commit 0dfe785

Please sign in to comment.