Skip to content

Commit c554575

Browse files
Copilotmathiasrw
andauthored
Breaking: Add LEFT() and RIGHT() SQL string functions to fix #590 (#2191)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]> Co-authored-by: Mathias Wulff <[email protected]>
1 parent 2c702de commit c554575

File tree

4 files changed

+203
-10
lines changed

4 files changed

+203
-10
lines changed

src/55functions.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ stdlib.LEN = stdlib.LENGTH = function (s) {
143143
};
144144
//stdlib.LENGTH = function(s) {return '('+s+').length'};
145145

146+
// Returns the left part of a character string with the specified number of characters.
147+
// see https://docs.microsoft.com/en-us/sql/t-sql/functions/left-transact-sql
148+
stdlib.LEFT = function (s, l) {
149+
return und(s, 'y.substr(0,' + l + ')');
150+
};
151+
146152
stdlib.LOWER = stdlib.LCASE = function (s) {
147153
return und(s, 'String(y).toLowerCase()');
148154
};
@@ -154,6 +160,12 @@ stdlib.LTRIM = function (s) {
154160
return und(s, 'y.replace(/^[ ]+/,"")');
155161
};
156162

163+
// Returns the right part of a character string with the specified number of characters.
164+
// see https://docs.microsoft.com/en-us/sql/t-sql/functions/right-transact-sql
165+
stdlib.RIGHT = function (s, l) {
166+
return und(s, 'y.substr(y.length-(' + l + '))');
167+
};
168+
157169
// Returns a character string after truncating all trailing spaces.
158170
// see https://docs.microsoft.com/en-us/sql/t-sql/functions/rtrim-transact-sql
159171
stdlib.RTRIM = function (s) {

src/alasqlparser.jison

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,14 +3026,14 @@ Reindex
30263026
;
30273027

30283028
NonReserved
3029-
: A|ABSENT|ABSOLUTE|ACCORDING|ACTION|ADA|ADD|ADMIN|AFTER|ALWAYS|ASC|ASSERTION|ASSIGNMENT|ATTRIBUTE|ATTRIBUTES|BASE64|BEFORE|BERNOULLI|BLOCKED|BOM|BREADTH|C|CASCADE|CATALOG|CATALOG_NAME|CHAIN|CHARACTERISTICS|CHARACTERS|CHARACTER_SET_CATALOG|CHARACTER_SET_NAME|CHARACTER_SET_SCHEMA|CLASS_ORIGIN|COBOL|COLLATION|COLLATION_CATALOG|COLLATION_NAME|COLLATION_SCHEMA|COLUMNS|COLUMN_NAME|COMMAND_FUNCTION|COMMAND_FUNCTION_CODE|COMMITTED|CONDITION_NUMBER|CONNECTION|CONNECTION_NAME|CONSTRAINTS|CONSTRAINT_CATALOG|CONSTRAINT_NAME|CONSTRAINT_SCHEMA|CONSTRUCTOR|CONTENT|CONTINUE|CONTROL|CURSOR_NAME|DATA|DATETIME_INTERVAL_CODE|DATETIME_INTERVAL_PRECISION|DB|DEFAULTS|DEFERRABLE|DEFERRED|DEFINED|DEFINER|DEGREE|DEPTH|DERIVED|DESC|DESCRIPTOR|DIAGNOSTICS|DISPATCH|DOCUMENT|DOMAIN|DYNAMIC_FUNCTION|DYNAMIC_FUNCTION_CODE|EMPTY|ENCODING|ENFORCED|EXCLUDE|EXCLUDING|EXPRESSION|FILE|FINAL|FIRST|FLAG|FOLLOWING|FORTRAN|FOUND|FS|G|GENERAL|GENERATED|GO|GOTO|GRANTED|HEX|HIERARCHY|ID|IGNORE|IMMEDIATE|IMMEDIATELY|IMPLEMENTATION|INCLUDING|INCREMENT|INDENT|INITIALLY|INPUT|INSTANCE|INSTANTIABLE|INSTEAD|INTEGRITY|INVOKER|ISOLATION|K|KEY|KEY_MEMBER|KEY_TYPE|LAST|LENGTH|LEVEL|LIBRARY|LIMIT|LINK|LOCATION|LOCATOR|M|MAP|MAPPING|MATCHED|MAXVALUE|MESSAGE_LENGTH|MESSAGE_OCTET_LENGTH|MESSAGE_TEXT|MINVALUE|MORE|MUMPS|NAME|NAMES|NAMESPACE|NESTING|NEXT|NFC|NFD|NFKC|NFKD|NIL|NORMALIZED|NULLABLE|NULLS|NUMBER|OBJECT|OCTETS|OFF|OPTION|OPTIONS|ORDERING|ORDINALITY|OTHERS|OUTPUT|OVERRIDING|P|PAD|PARAMETER_MODE|PARAMETER_NAME|PARAMETER_ORDINAL_POSITION|PARAMETER_SPECIFIC_CATALOG|PARAMETER_SPECIFIC_NAME|PARAMETER_SPECIFIC_SCHEMA|PARTIAL|PASCAL|PASSING|PASSTHROUGH|PATH|PERMISSION|PLACING|PLI|PRECEDING|PRESERVE|PRIOR|PRIVILEGES|PUBLIC|READ|RECOVERY|RELATIVE|REPEATABLE|REQUIRING|RESPECT|RESTART|RESTORE|RESTRICT|RETURNED_CARDINALITY|RETURNED_LENGTH|RETURNED_OCTET_LENGTH|RETURNED_SQLSTATE|RETURNING|ROLE|ROUTINE|ROUTINE_CATALOG|ROUTINE_NAME|ROUTINE_SCHEMA|ROW_COUNT|SCALE|SCHEMA|SCHEMA_NAME|SCOPE_CATALOG|SCOPE_NAME|SCOPE_SCHEMA|SECTION|SECURITY|SELECTIVE|SELF|SEQUENCE|SERIALIZABLE|SERVER|SERVER_NAME|SESSION|SETS|SIMPLE|SIZE|SOURCE|SPACE|SPECIFIC_NAME|STANDALONE|STATE|STATEMENT|STRIP|STRUCTURE|STYLE|SUBCLASS_ORIGIN|T|TABLE_NAME|TEMPORARY|TIES|TOKEN|TOP_LEVEL_COUNT|TRANSACTION|TRANSACTIONS_COMMITTED|TRANSACTIONS_ROLLED_BACK|TRANSACTION_ACTIVE|TRANSFORM|TRANSFORMS|TRIGGER_CATALOG|TRIGGER_NAME|TRIGGER_SCHEMA|TYPE|UNBOUNDED|UNCOMMITTED|UNDER|UNLINK|UNNAMED|UNTYPED|URI|USAGE|USER_DEFINED_TYPE_CATALOG|USER_DEFINED_TYPE_CODE|USER_DEFINED_TYPE_NAME|USER_DEFINED_TYPE_SCHEMA|VALID|VERSION|VIEW|WHITESPACE|WORK|WRAPPER|WRITE|XMLDECLARATION|XMLSCHEMA|YES|ZONE;
3029+
: A|ABSENT|ABSOLUTE|ACCORDING|ACTION|ADA|ADD|ADMIN|AFTER|ALWAYS|ASC|ASSERTION|ASSIGNMENT|ATTRIBUTE|ATTRIBUTES|BASE64|BEFORE|BERNOULLI|BLOCKED|BOM|BREADTH|C|CASCADE|CATALOG|CATALOG_NAME|CHAIN|CHARACTERISTICS|CHARACTERS|CHARACTER_SET_CATALOG|CHARACTER_SET_NAME|CHARACTER_SET_SCHEMA|CLASS_ORIGIN|COBOL|COLLATION|COLLATION_CATALOG|COLLATION_NAME|COLLATION_SCHEMA|COLUMNS|COLUMN_NAME|COMMAND_FUNCTION|COMMAND_FUNCTION_CODE|COMMITTED|CONDITION_NUMBER|CONNECTION|CONNECTION_NAME|CONSTRAINTS|CONSTRAINT_CATALOG|CONSTRAINT_NAME|CONSTRAINT_SCHEMA|CONSTRUCTOR|CONTENT|CONTINUE|CONTROL|CURSOR_NAME|DATA|DATETIME_INTERVAL_CODE|DATETIME_INTERVAL_PRECISION|DB|DEFAULTS|DEFERRABLE|DEFERRED|DEFINED|DEFINER|DEGREE|DEPTH|DERIVED|DESC|DESCRIPTOR|DIAGNOSTICS|DISPATCH|DOCUMENT|DOMAIN|DYNAMIC_FUNCTION|DYNAMIC_FUNCTION_CODE|EMPTY|ENCODING|ENFORCED|EXCLUDE|EXCLUDING|EXPRESSION|FILE|FINAL|FIRST|FLAG|FOLLOWING|FORTRAN|FOUND|FS|G|GENERAL|GENERATED|GO|GOTO|GRANTED|HEX|HIERARCHY|ID|IGNORE|IMMEDIATE|IMMEDIATELY|IMPLEMENTATION|INCLUDING|INCREMENT|INDENT|INITIALLY|INPUT|INSTANCE|INSTANTIABLE|INSTEAD|INTEGRITY|INVOKER|ISOLATION|K|KEY|KEY_MEMBER|KEY_TYPE|LAST|LEFT|LENGTH|LEVEL|LIBRARY|LIMIT|LINK|LOCATION|LOCATOR|M|MAP|MAPPING|MATCHED|MAXVALUE|MESSAGE_LENGTH|MESSAGE_OCTET_LENGTH|MESSAGE_TEXT|MINVALUE|MORE|MUMPS|NAME|NAMES|NAMESPACE|NESTING|NEXT|NFC|NFD|NFKC|NFKD|NIL|NORMALIZED|NULLABLE|NULLS|NUMBER|OBJECT|OCTETS|OFF|OPTION|OPTIONS|ORDERING|ORDINALITY|OTHERS|OUTPUT|OVERRIDING|P|PAD|PARAMETER_MODE|PARAMETER_NAME|PARAMETER_ORDINAL_POSITION|PARAMETER_SPECIFIC_CATALOG|PARAMETER_SPECIFIC_NAME|PARAMETER_SPECIFIC_SCHEMA|PARTIAL|PASCAL|PASSING|PASSTHROUGH|PATH|PERMISSION|PLACING|PLI|PRECEDING|PRESERVE|PRIOR|PRIVILEGES|PUBLIC|READ|RECOVERY|RELATIVE|REPEATABLE|REQUIRING|RESPECT|RESTART|RESTORE|RESTRICT|RETURNED_CARDINALITY|RETURNED_LENGTH|RETURNED_OCTET_LENGTH|RETURNED_SQLSTATE|RETURNING|RIGHT|ROLE|ROUTINE|ROUTINE_CATALOG|ROUTINE_NAME|ROUTINE_SCHEMA|ROW_COUNT|SCALE|SCHEMA|SCHEMA_NAME|SCOPE_CATALOG|SCOPE_NAME|SCOPE_SCHEMA|SECTION|SECURITY|SELECTIVE|SELF|SEQUENCE|SERIALIZABLE|SERVER|SERVER_NAME|SESSION|SETS|SIMPLE|SIZE|SOURCE|SPACE|SPECIFIC_NAME|STANDALONE|STATE|STATEMENT|STRIP|STRUCTURE|STYLE|SUBCLASS_ORIGIN|T|TABLE_NAME|TEMPORARY|TIES|TOKEN|TOP_LEVEL_COUNT|TRANSACTION|TRANSACTIONS_COMMITTED|TRANSACTIONS_ROLLED_BACK|TRANSACTION_ACTIVE|TRANSFORM|TRANSFORMS|TRIGGER_CATALOG|TRIGGER_NAME|TRIGGER_SCHEMA|TYPE|UNBOUNDED|UNCOMMITTED|UNDER|UNLINK|UNNAMED|UNTYPED|URI|USAGE|USER_DEFINED_TYPE_CATALOG|USER_DEFINED_TYPE_CODE|USER_DEFINED_TYPE_NAME|USER_DEFINED_TYPE_SCHEMA|VALID|VERSION|VIEW|WHITESPACE|WORK|WRAPPER|WRITE|XMLDECLARATION|XMLSCHEMA|YES|ZONE;
30303030

30313031
%%
30323032

30333033
// from https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html
30343034
// JSON.stringify([].slice.call(document.querySelectorAll('tr')).filter(x => x.children.length == 5 && x.children[2].innerText == 'reserved').map(x => x.children[0].innerText))
30353035

3036-
var nonReserved = ["A","ABSENT","ABSOLUTE","ACCORDING","ACTION","ADA","ADD","ADMIN","AFTER","ALWAYS","ASC","ASSERTION","ASSIGNMENT","ATTRIBUTE","ATTRIBUTES","BASE64","BEFORE","BERNOULLI","BLOCKED","BOM","BREADTH","C","CASCADE","CATALOG","CATALOG_NAME","CHAIN","CHARACTERISTICS","CHARACTERS","CHARACTER_SET_CATALOG","CHARACTER_SET_NAME","CHARACTER_SET_SCHEMA","CLASS_ORIGIN","COBOL","COLLATION","COLLATION_CATALOG","COLLATION_NAME","COLLATION_SCHEMA","COLUMNS","COLUMN_NAME","COMMAND_FUNCTION","COMMAND_FUNCTION_CODE","COMMITTED","CONDITION_NUMBER","CONNECTION","CONNECTION_NAME","CONSTRAINTS","CONSTRAINT_CATALOG","CONSTRAINT_NAME","CONSTRAINT_SCHEMA","CONSTRUCTOR","CONTENT","CONTINUE","CONTROL","CURSOR_NAME","DATA","DATETIME_INTERVAL_CODE","DATETIME_INTERVAL_PRECISION","DB","DEFAULTS","DEFERRABLE","DEFERRED","DEFINED","DEFINER","DEGREE","DEPTH","DERIVED","DESC","DESCRIPTOR","DIAGNOSTICS","DISPATCH","DOCUMENT","DOMAIN","DYNAMIC_FUNCTION","DYNAMIC_FUNCTION_CODE","EMPTY","ENCODING","ENFORCED","EXCLUDE","EXCLUDING","EXPRESSION","FILE","FINAL","FIRST","FLAG","FOLLOWING","FORTRAN","FOUND","FS","G","GENERAL","GENERATED","GO","GOTO","GRANTED","HEX","HIERARCHY","ID","IGNORE","IMMEDIATE","IMMEDIATELY","IMPLEMENTATION","INCLUDING","INCREMENT","INDENT","INITIALLY","INPUT","INSTANCE","INSTANTIABLE","INSTEAD","INTEGRITY","INVOKER","ISOLATION","K","KEY","KEY_MEMBER","KEY_TYPE","LAST","LENGTH","LEVEL","LIBRARY","LIMIT","LINK","LOCATION","LOCATOR","M","MAP","MAPPING","MATCHED","MAXVALUE","MESSAGE_LENGTH","MESSAGE_OCTET_LENGTH","MESSAGE_TEXT","MINVALUE","MORE","MUMPS","NAME","NAMES","NAMESPACE","NESTING","NEXT","NFC","NFD","NFKC","NFKD","NIL","NORMALIZED","NULLABLE","NULLS","NUMBER","OBJECT","OCTETS","OFF","OPTION","OPTIONS","ORDERING","ORDINALITY","OTHERS","OUTPUT","OVERRIDING","P","PAD","PARAMETER_MODE","PARAMETER_NAME","PARAMETER_ORDINAL_POSITION","PARAMETER_SPECIFIC_CATALOG","PARAMETER_SPECIFIC_NAME","PARAMETER_SPECIFIC_SCHEMA","PARTIAL","PASCAL","PASSING","PASSTHROUGH","PATH","PERMISSION","PLACING","PLI","PRECEDING","PRESERVE","PRIOR","PRIVILEGES","PUBLIC","READ","RECOVERY","RELATIVE","REPEATABLE","REQUIRING","RESPECT","RESTART","RESTORE","RESTRICT","RETURNED_CARDINALITY","RETURNED_LENGTH","RETURNED_OCTET_LENGTH","RETURNED_SQLSTATE","RETURNING","ROLE","ROUTINE","ROUTINE_CATALOG","ROUTINE_NAME","ROUTINE_SCHEMA","ROW_COUNT","SCALE","SCHEMA","SCHEMA_NAME","SCOPE_CATALOG","SCOPE_NAME","SCOPE_SCHEMA","SECTION","SECURITY","SELECTIVE","SELF","SEQUENCE","SERIALIZABLE","SERVER","SERVER_NAME","SESSION","SETS","SIMPLE","SIZE","SOURCE","SPACE","SPECIFIC_NAME","STANDALONE","STATE","STATEMENT","STRIP","STRUCTURE","STYLE","SUBCLASS_ORIGIN","T","TABLE_NAME","TEMPORARY","TIES","TOKEN","TOP_LEVEL_COUNT","TRANSACTION","TRANSACTIONS_COMMITTED","TRANSACTIONS_ROLLED_BACK","TRANSACTION_ACTIVE","TRANSFORM","TRANSFORMS","TRIGGER_CATALOG","TRIGGER_NAME","TRIGGER_SCHEMA","TYPE","UNBOUNDED","UNCOMMITTED","UNDER","UNLINK","UNNAMED","UNTYPED","URI","USAGE","USER_DEFINED_TYPE_CATALOG","USER_DEFINED_TYPE_CODE","USER_DEFINED_TYPE_NAME","USER_DEFINED_TYPE_SCHEMA","VALID","VERSION","VIEW","WHITESPACE","WORK","WRAPPER","WRITE","XMLDECLARATION","XMLSCHEMA","YES","ZONE"]
3036+
var nonReserved = ["A","ABSENT","ABSOLUTE","ACCORDING","ACTION","ADA","ADD","ADMIN","AFTER","ALWAYS","ASC","ASSERTION","ASSIGNMENT","ATTRIBUTE","ATTRIBUTES","BASE64","BEFORE","BERNOULLI","BLOCKED","BOM","BREADTH","C","CASCADE","CATALOG","CATALOG_NAME","CHAIN","CHARACTERISTICS","CHARACTERS","CHARACTER_SET_CATALOG","CHARACTER_SET_NAME","CHARACTER_SET_SCHEMA","CLASS_ORIGIN","COBOL","COLLATION","COLLATION_CATALOG","COLLATION_NAME","COLLATION_SCHEMA","COLUMNS","COLUMN_NAME","COMMAND_FUNCTION","COMMAND_FUNCTION_CODE","COMMITTED","CONDITION_NUMBER","CONNECTION","CONNECTION_NAME","CONSTRAINTS","CONSTRAINT_CATALOG","CONSTRAINT_NAME","CONSTRAINT_SCHEMA","CONSTRUCTOR","CONTENT","CONTINUE","CONTROL","CURSOR_NAME","DATA","DATETIME_INTERVAL_CODE","DATETIME_INTERVAL_PRECISION","DB","DEFAULTS","DEFERRABLE","DEFERRED","DEFINED","DEFINER","DEGREE","DEPTH","DERIVED","DESC","DESCRIPTOR","DIAGNOSTICS","DISPATCH","DOCUMENT","DOMAIN","DYNAMIC_FUNCTION","DYNAMIC_FUNCTION_CODE","EMPTY","ENCODING","ENFORCED","EXCLUDE","EXCLUDING","EXPRESSION","FILE","FINAL","FIRST","FLAG","FOLLOWING","FORTRAN","FOUND","FS","G","GENERAL","GENERATED","GO","GOTO","GRANTED","HEX","HIERARCHY","ID","IGNORE","IMMEDIATE","IMMEDIATELY","IMPLEMENTATION","INCLUDING","INCREMENT","INDENT","INITIALLY","INPUT","INSTANCE","INSTANTIABLE","INSTEAD","INTEGRITY","INVOKER","ISOLATION","K","KEY","KEY_MEMBER","KEY_TYPE","LAST","LEFT","LENGTH","LEVEL","LIBRARY","LIMIT","LINK","LOCATION","LOCATOR","M","MAP","MAPPING","MATCHED","MAXVALUE","MESSAGE_LENGTH","MESSAGE_OCTET_LENGTH","MESSAGE_TEXT","MINVALUE","MORE","MUMPS","NAME","NAMES","NAMESPACE","NESTING","NEXT","NFC","NFD","NFKC","NFKD","NIL","NORMALIZED","NULLABLE","NULLS","NUMBER","OBJECT","OCTETS","OFF","OPTION","OPTIONS","ORDERING","ORDINALITY","OTHERS","OUTPUT","OVERRIDING","P","PAD","PARAMETER_MODE","PARAMETER_NAME","PARAMETER_ORDINAL_POSITION","PARAMETER_SPECIFIC_CATALOG","PARAMETER_SPECIFIC_NAME","PARAMETER_SPECIFIC_SCHEMA","PARTIAL","PASCAL","PASSING","PASSTHROUGH","PATH","PERMISSION","PLACING","PLI","PRECEDING","PRESERVE","PRIOR","PRIVILEGES","PUBLIC","READ","RECOVERY","RELATIVE","REPEATABLE","REQUIRING","RESPECT","RESTART","RESTORE","RESTRICT","RETURNED_CARDINALITY","RETURNED_LENGTH","RETURNED_OCTET_LENGTH","RETURNED_SQLSTATE","RETURNING","RIGHT","ROLE","ROUTINE","ROUTINE_CATALOG","ROUTINE_NAME","ROUTINE_SCHEMA","ROW_COUNT","SCALE","SCHEMA","SCHEMA_NAME","SCOPE_CATALOG","SCOPE_NAME","SCOPE_SCHEMA","SECTION","SECURITY","SELECTIVE","SELF","SEQUENCE","SERIALIZABLE","SERVER","SERVER_NAME","SESSION","SETS","SIMPLE","SIZE","SOURCE","SPACE","SPECIFIC_NAME","STANDALONE","STATE","STATEMENT","STRIP","STRUCTURE","STYLE","SUBCLASS_ORIGIN","T","TABLE_NAME","TEMPORARY","TIES","TOKEN","TOP_LEVEL_COUNT","TRANSACTION","TRANSACTIONS_COMMITTED","TRANSACTIONS_ROLLED_BACK","TRANSACTION_ACTIVE","TRANSFORM","TRANSFORMS","TRIGGER_CATALOG","TRIGGER_NAME","TRIGGER_SCHEMA","TYPE","UNBOUNDED","UNCOMMITTED","UNDER","UNLINK","UNNAMED","UNTYPED","URI","USAGE","USER_DEFINED_TYPE_CATALOG","USER_DEFINED_TYPE_CODE","USER_DEFINED_TYPE_NAME","USER_DEFINED_TYPE_SCHEMA","VALID","VERSION","VIEW","WHITESPACE","WORK","WRAPPER","WRITE","XMLDECLARATION","XMLSCHEMA","YES","ZONE"]
30373037

30383038
parser.parseError = function(str, hash) {
30393039
if (hash.expected && hash.expected.indexOf("'LITERAL'") > -1 && /[a-zA-Z_][a-zA-Z_0-9]*/.test(hash.token) && nonReserved.indexOf(hash.token) > -1) {

0 commit comments

Comments
 (0)