diff --git a/lib/destination/dml/merge.go b/lib/destination/dml/merge.go index aad0d1396..5167fc932 100644 --- a/lib/destination/dml/merge.go +++ b/lib/destination/dml/merge.go @@ -94,7 +94,8 @@ func (m *MergeArgument) GetParts() ([]string, error) { var equalitySQLParts []string for _, primaryKey := range m.PrimaryKeys { // We'll need to escape the primary key as well. - equalitySQL := fmt.Sprintf("c.%s = cc.%s", primaryKey.Name(m.Dialect), primaryKey.Name(m.Dialect)) + escapedPrimaryKey := m.Dialect.QuoteIdentifier(primaryKey.RawName()) + equalitySQL := fmt.Sprintf("c.%s = cc.%s", escapedPrimaryKey, escapedPrimaryKey) equalitySQLParts = append(equalitySQLParts, equalitySQL) } @@ -115,7 +116,7 @@ func (m *MergeArgument) GetParts() ([]string, error) { // LEFT JOIN table on pk(s) m.TableID.FullyQualifiedName(), strings.Join(equalitySQLParts, " and "), // Where PK is NULL (we only need to specify one primary key since it's covered with equalitySQL parts) - m.PrimaryKeys[0].Name(m.Dialect)), + m.Dialect.QuoteIdentifier(m.PrimaryKeys[0].RawName())), // UPDATE fmt.Sprintf(`UPDATE %s as c SET %s FROM %s as cc WHERE %s%s;`, // UPDATE table set col1 = cc. col1 @@ -142,7 +143,7 @@ func (m *MergeArgument) GetParts() ([]string, error) { var pks []string for _, pk := range m.PrimaryKeys { - pks = append(pks, pk.Name(m.Dialect)) + pks = append(pks, m.Dialect.QuoteIdentifier(pk.RawName())) } parts := []string{ @@ -159,7 +160,7 @@ func (m *MergeArgument) GetParts() ([]string, error) { // LEFT JOIN table on pk(s) m.TableID.FullyQualifiedName(), strings.Join(equalitySQLParts, " and "), // Where PK is NULL (we only need to specify one primary key since it's covered with equalitySQL parts) - m.PrimaryKeys[0].Name(m.Dialect)), + m.Dialect.QuoteIdentifier(m.PrimaryKeys[0].RawName())), // UPDATE fmt.Sprintf(`UPDATE %s as c SET %s FROM %s as cc WHERE %s%s AND COALESCE(cc.%s, false) = false;`, // UPDATE table set col1 = cc. col1 @@ -207,7 +208,9 @@ func (m *MergeArgument) GetStatement() (string, error) { var equalitySQLParts []string for _, primaryKey := range m.PrimaryKeys { // We'll need to escape the primary key as well. - equalitySQL := fmt.Sprintf("c.%s = cc.%s", primaryKey.Name(m.Dialect), primaryKey.Name(m.Dialect)) + escapedPrimaryKey := m.Dialect.QuoteIdentifier(primaryKey.RawName()) + + equalitySQL := fmt.Sprintf("c.%s = cc.%s", escapedPrimaryKey, escapedPrimaryKey) pkCol, isOk := m.Columns.GetColumn(primaryKey.RawName()) if !isOk { return "", fmt.Errorf("column: %s does not exist in columnToType: %v", primaryKey.RawName(), m.Columns) @@ -215,7 +218,7 @@ func (m *MergeArgument) GetStatement() (string, error) { if m.DestKind == constants.BigQuery && pkCol.KindDetails.Kind == typing.Struct.Kind { // BigQuery requires special casting to compare two JSON objects. - equalitySQL = fmt.Sprintf("TO_JSON_STRING(c.%s) = TO_JSON_STRING(cc.%s)", primaryKey.Name(m.Dialect), primaryKey.Name(m.Dialect)) + equalitySQL = fmt.Sprintf("TO_JSON_STRING(c.%s) = TO_JSON_STRING(cc.%s)", escapedPrimaryKey, escapedPrimaryKey) } equalitySQLParts = append(equalitySQLParts, equalitySQL) @@ -295,7 +298,8 @@ func (m *MergeArgument) GetMSSQLStatement() (string, error) { var equalitySQLParts []string for _, primaryKey := range m.PrimaryKeys { // We'll need to escape the primary key as well. - equalitySQL := fmt.Sprintf("c.%s = cc.%s", primaryKey.Name(m.Dialect), primaryKey.Name(m.Dialect)) + escapedPrimaryKey := m.Dialect.QuoteIdentifier(primaryKey.RawName()) + equalitySQL := fmt.Sprintf("c.%s = cc.%s", escapedPrimaryKey, escapedPrimaryKey) equalitySQLParts = append(equalitySQLParts, equalitySQL) }