Skip to content

Conversation

@jycor
Copy link
Contributor

@jycor jycor commented Nov 2, 2021

Parser recognizes keywords like: LEADING, TRAILING, and BOTH.
TRIM function can now remove prefixes.

Fixed LTRIM And RTRIM to not remove all whitespace, and just space characters.

Also added functionality for RIGHT function.

fixes: dolthub/dolt#2199

Copy link
Member

@zachmu zachmu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start, see comments

Biggest missing piece is engine tests (look in queries.go). We need engine tests for every function we write. Engine tests should cover both literal and column values of various types.

trimType
}
// Cast to string
var pat_text string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more tests are in order, but you probably want to use Convert() of a string type rather than a simple type assertion like this

E.g. this works:

mysql> SELECT TRIM(LEADING 1 FROM '111barxxx');
+----------------------------------+
| TRIM(LEADING 1 FROM '111barxxx') |
+----------------------------------+
| barxxx                           |
+----------------------------------+

return "rtrim"
case bTrimType:
return "trim"
// Cast to string type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mysql> SELECT TRIM(LEADING 1 FROM 111222);
+-----------------------------+
| TRIM(LEADING 1 FROM 111222) |
+-----------------------------+
| 222                         |
+-----------------------------+

// Type implements the Expression interface.
func (t *Trim) Type() sql.Type { return sql.LongText }
// Evaluate direction
dir, err := t.dir.Eval(ctx, row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should be a static constant, no need to eval (see other PR comment)

expression.UnaryExpression
}

func NewLeftTrim(str sql.Expression) sql.Expression {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and NewRightTrim need to be updated in registry.go

@jycor jycor changed the title James/fix 2297 implement string functions Added Support for Full MySQL TRIM Syntax Nov 3, 2021
Copy link
Member

@zachmu zachmu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good, but need some more test cases

Expected: []sql.Row{{"2"}},
},
{
Query: `SELECT TRIM(LEADING 1 FROM 11111112)`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want a few test cases with a match of the trim phrase in the middle of the word that don't get trimmed, e.g. trim(trailing "a" from "baba") == bab, trim(trailing "ab" from "baba") == baba

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also want some tests cases where the expressions for trimming are not literals

Some where they are column values, some where they are function calls (e.g. concat("a", "b"))

lTrimType trimType = 'l'
rTrimType trimType = 'r'
bTrimType trimType = 'b'
LEADING string = "l"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should go in Vitess

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment still applies

Copy link
Member

@zachmu zachmu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks better, but a couple comment still need to be addressed

Query: `SELECT TRIM(LEADING "ooo" FROM TRIM("oooo"))`,
Expected: []sql.Row{{"o"}},
},

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra line, delete

lTrimType trimType = 'l'
rTrimType trimType = 'r'
bTrimType trimType = 'b'
LEADING string = "l"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment still applies

Query: `SELECT SUBSTRING_INDEX(mytable.s, "d", 1) AS s FROM mytable INNER JOIN othertable ON (SUBSTRING_INDEX(mytable.s, "d", 1) = SUBSTRING_INDEX(othertable.s2, "d", 1)) GROUP BY 1 HAVING s = 'secon'`,
Expected: []sql.Row{{"secon"}},
},

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line

@jycor jycor merged commit 4021ebb into main Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

trim(trailing ',' from column) syntax not supported

2 participants