Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 19, 2025

Implements LEFT() and RIGHT() string functions from standard SQL. These were previously unavailable because LEFT and RIGHT are reserved keywords for join operations.

Changes

Parser (src/alasqlparser.jison)

  • Added LEFT and RIGHT to NonReserved keyword list, allowing them to be used as both function names and join keywords

Functions (src/55functions.js)

  • Implemented stdlib.LEFT(string, length) - returns leftmost N characters using substr(0, length)
  • Implemented stdlib.RIGHT(string, length) - returns rightmost N characters using substr(string.length - length)

Tests (test/test590.js)

  • Comprehensive test coverage with 17 test cases including:
    • Basic functionality and edge cases
    • Case insensitivity (left, LEFT, Left)
    • Nested functions (LEFT(RIGHT(...)), RIGHT(LEFT(...)))
    • Complex multi-level nesting
    • Integration with other SQL functions (UPPER, LOWER, CONCAT)
    • Integration with JOIN operations
    • Special characters handling
    • User-defined function compatibility

Example Usage

-- Basic usage
SELECT LEFT('Hello World', 5)  -- Returns 'Hello'
SELECT RIGHT('Hello World', 5) -- Returns 'World'

-- Case insensitive
SELECT left('Hello', 3)  -- Works with any case

-- Nested functions
SELECT LEFT(RIGHT('Hello World', 5), 3)  -- Returns 'Wor'

-- Works alongside JOIN keywords
SELECT LEFT(t1.name, 3) as prefix, t2.value 
FROM table1 t1 
LEFT JOIN table2 t2 ON t1.id = t2.id

Both functions handle undefined values gracefully and follow the same patterns as existing string functions (SUBSTRING, UPPER, LOWER, etc.). User-defined functions with different names can coexist with the built-in LEFT and RIGHT functions.

[!WARNING]

Original prompt

This section details on the original issue you should resolve

<filter_complete></filter_complete>

<issue_title>Add RIGHT() and LEFT() SQL string functions</issue_title>
<issue_description>Add RIGHT() and LEFT() string functions from SQL

Spawned from #588
</issue_description>

<agent_instructions>Please make a test file called test590.js verifying if this issue is still a problem.

If it is a problem try to solve it.

If you need to update the grammar of alasql, then please do small precice changes to alasqlparser.jison and then run yarn jison && yarn test

take special care with the danger mentioned in the last message in the conversation</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@mathiasrw Yep. Still relevant. @mathiasrw Yes.

You will need to add the keywords specificly as part of acceptable function names, this way it becomes valid to have those tokens as function names instead of just strings (right now the token LEFT will only be acceptable in relation to join)</comment_new>

Original prompt

This section details on the original issue you should resolve

<issue_title>Add RIGHT() and LEFT() SQL string functions</issue_title>
<issue_description>Add RIGHT() and LEFT() string functions from SQL

Spawned from #588
</issue_description>

<agent_instructions>Please make a test file called test590.js verifying if this issue is still a problem.

If it is a problem try to solve it.

If you need to update the grammar of alasql, then please do small precice changes to alasqlparser.jison and then run yarn jison && yarn test

take special care with the danger mentioned in the last message in the conversation</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@mathiasrw Yep. Still relevant. @mathiasrw Yes.

You will need to add the keywords specificly as part of acceptable function names, this way it becomes valid to have those tokens as function names instead of just strings (right now the token LEFT will only be acceptable in relation to join)</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 19, 2025 10:01
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copilot AI changed the title [WIP] Add RIGHT() and LEFT() SQL string functions Add LEFT() and RIGHT() SQL string functions Nov 19, 2025
Copilot AI requested a review from mathiasrw November 19, 2025 10:10
Copilot finished work on behalf of mathiasrw November 19, 2025 10:10
@mathiasrw
Copy link
Member

mathiasrw commented Nov 19, 2025

what happens if we got a user function called. left or. right?

@copilot

@mathiasrw mathiasrw marked this pull request as ready for review November 19, 2025 12:25
- Case insensitivity tests (left, LEFT, Left, etc.)
- Nested function tests (LEFT(RIGHT(...)), RIGHT(LEFT(...)))
- Complex nesting with multiple functions
- Integration with UPPER/LOWER functions
- Integration with CONCAT function
- Special characters handling
- User-defined function compatibility test

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 19, 2025

what happens if we got a user function called. left or. right?

@copilot

User-defined functions work alongside the built-in LEFT and RIGHT functions. The built-in stdlib functions (LEFT, RIGHT) are checked first, so they take precedence. If you want to define a custom function, you'd need to use a different name like myleft or myright.

Test case 17 demonstrates this - a custom myleft function coexists with the built-in LEFT function without conflicts.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • upload.wikimedia.org
    • Triggering command: /usr/local/bin/node node_modules/mocha/bin/mocha.js ./test --reporter dot --bail (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from mathiasrw November 19, 2025 12:31
Copilot finished work on behalf of mathiasrw November 19, 2025 12:31
@mathiasrw mathiasrw merged commit c554575 into develop Nov 19, 2025
20 checks passed
@mathiasrw mathiasrw deleted the copilot/add-right-left-sql-functions branch November 19, 2025 12:51
mathiasrw added a commit that referenced this pull request Nov 20, 2025
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.

Add RIGHT() and LEFT() SQL string functions

2 participants