trino_sql_parser 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fab1f747a3ffd1712e2047e1ae823f4a83426f0d69b297d98d5266db69fa3c0
4
- data.tar.gz: 398bb435ddc860cb2164e001e424aa64cd16da0ac6fec394c9b43be66741947e
3
+ metadata.gz: 75acc0afee42849c2cdb319a3f2359fd0f9e6ede1c650b0f71ed210db932dc81
4
+ data.tar.gz: fcfa4ace779c744892acc0382b79b438241334b7df21cc3b37d76fcc499dffcf
5
5
  SHA512:
6
- metadata.gz: 4816c2d9ed1d218a77f8fd8886b074aaebfc4cd839cb577458bcf39014bef8470c4aa8a6b8fd1093760b1b687f93f86020265993c9b7130450966fa8ac969902
7
- data.tar.gz: e73763ede15e0be526bd1e6498fcbc46a0917d66ad33e88159381724fb4923ddc7007f0e8e13bea54373b3f2e731ff19e01565caaf17d8e21481f75c6f7ed6d6
6
+ metadata.gz: 3209f53ed31c2318ff5582e6e4fa77140538a9cab4ed9f1cd9b62e077ab9d23d89ad42914d5785daa4d93c66b17b7d3afd32e7770925a8777cc7d5179b8f3723
7
+ data.tar.gz: de651e2d7e7326af8aa024b2e5c03e8b713595318194a773f6d43d3d923241da8d445f3a542d2af316008b08370178b47f99e06176245325ce5637b417c096d8
data/ChangeLog CHANGED
@@ -1,4 +1,10 @@
1
1
 
2
+ 2022-08-03 version 1.2.0:
3
+
4
+ * Update Trino 351 to Trino 391
5
+ * Add with_statement and idle_timeout options
6
+ * TrinoSqlParser#parse is thread-safe
7
+
2
8
  2021-01-05 version 1.1.0:
3
9
 
4
10
  * Update Presto 341 to Trino 351.
data/README.md CHANGED
@@ -79,7 +79,7 @@ rescue TrinoSqlParser::ParseError => e
79
79
  end
80
80
  ```
81
81
 
82
- Optionally, you can get ANTLR token list. It also supports multiple statements.
82
+ Optionally, you can set `with_tokens: true` option to get ANTLR token list. It also supports multiple statements.
83
83
 
84
84
  ```ruby
85
85
  require 'trino_sql_parser'
@@ -114,6 +114,15 @@ p statements[1]['tokens']
114
114
  TrinoSqlParser.java_cmd = "java" # java command (default: TRINO_SQL_PARSER_JAVA env var or "java")
115
115
  TrinoSqlParser.java_args = [] # command-line arguments of java_cmd
116
116
  TrinoSqlParser.java_env = {} # environment variables given to java_cmd
117
+
118
+ TrinoSqlParser.new(
119
+ with_tokens: true, # Set true to include ANTLR token list in 'tokens' field of the result of #parse.
120
+ with_statement: true, # Set true to include AST in 'statement' field of the result of #parse.
121
+ # Be aware that the structure might be changed when you update trino_sql_parser
122
+ # version because it's not strictly defined in Trino documents.
123
+ idle_timeout: 2 # Number of seconds to wait until TrinoSqlParser kills a java process. Although
124
+ # it restarts a java process automatically, cold start takes time. Default is 2.
125
+ )
117
126
  ```
118
127
 
119
128
  ## Development
data/build.gradle CHANGED
@@ -1,5 +1,5 @@
1
1
  plugins {
2
- id 'com.github.johnrengelman.shadow' version '5.1.0'
2
+ id 'com.github.johnrengelman.shadow' version '7.1.2'
3
3
  }
4
4
  apply plugin: 'com.github.johnrengelman.shadow'
5
5
  apply plugin: 'java'
@@ -9,10 +9,10 @@ group = 'trino-sql-parser-support-process'
9
9
  version = '1.0.0'
10
10
 
11
11
  dependencies {
12
- compile 'io.trino:trino-parser:351'
13
- compile "com.fasterxml.jackson.core:jackson-databind:2.9.9"
14
- compile "com.fasterxml.jackson.datatype:jackson-datatype-guava:2.9.9"
15
- compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.9"
12
+ implementation 'io.trino:trino-parser:391'
13
+ implementation "com.fasterxml.jackson.core:jackson-databind:2.9.9"
14
+ implementation "com.fasterxml.jackson.datatype:jackson-datatype-guava:2.9.9"
15
+ implementation "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.9"
16
16
  }
17
17
 
18
18
  repositories {
Binary file
@@ -1,5 +1,5 @@
1
1
  distributionBase=GRADLE_USER_HOME
2
2
  distributionPath=wrapper/dists
3
- distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-bin.zip
3
+ distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
4
4
  zipStoreBase=GRADLE_USER_HOME
5
5
  zipStorePath=wrapper/dists
data/gradlew CHANGED
@@ -1,78 +1,129 @@
1
- #!/usr/bin/env sh
1
+ #!/bin/sh
2
+
3
+ #
4
+ # Copyright © 2015-2021 the original authors.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # https://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
2
18
 
3
19
  ##############################################################################
4
- ##
5
- ## Gradle start up script for UN*X
6
- ##
20
+ #
21
+ # Gradle start up script for POSIX generated by Gradle.
22
+ #
23
+ # Important for running:
24
+ #
25
+ # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
26
+ # noncompliant, but you have some other compliant shell such as ksh or
27
+ # bash, then to run this script, type that shell name before the whole
28
+ # command line, like:
29
+ #
30
+ # ksh Gradle
31
+ #
32
+ # Busybox and similar reduced shells will NOT work, because this script
33
+ # requires all of these POSIX shell features:
34
+ # * functions;
35
+ # * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
36
+ # «${var#prefix}», «${var%suffix}», and «$( cmd )»;
37
+ # * compound commands having a testable exit status, especially «case»;
38
+ # * various built-in commands including «command», «set», and «ulimit».
39
+ #
40
+ # Important for patching:
41
+ #
42
+ # (2) This script targets any POSIX shell, so it avoids extensions provided
43
+ # by Bash, Ksh, etc; in particular arrays are avoided.
44
+ #
45
+ # The "traditional" practice of packing multiple parameters into a
46
+ # space-separated string is a well documented source of bugs and security
47
+ # problems, so this is (mostly) avoided, by progressively accumulating
48
+ # options in "$@", and eventually passing that to Java.
49
+ #
50
+ # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
51
+ # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
52
+ # see the in-line comments for details.
53
+ #
54
+ # There are tweaks for specific operating systems such as AIX, CygWin,
55
+ # Darwin, MinGW, and NonStop.
56
+ #
57
+ # (3) This script is generated from the Groovy template
58
+ # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
59
+ # within the Gradle project.
60
+ #
61
+ # You can find Gradle at https://github.com/gradle/gradle/.
62
+ #
7
63
  ##############################################################################
8
64
 
9
65
  # Attempt to set APP_HOME
66
+
10
67
  # Resolve links: $0 may be a link
11
- PRG="$0"
12
- # Need this for relative symlinks.
13
- while [ -h "$PRG" ] ; do
14
- ls=`ls -ld "$PRG"`
15
- link=`expr "$ls" : '.*-> \(.*\)$'`
16
- if expr "$link" : '/.*' > /dev/null; then
17
- PRG="$link"
18
- else
19
- PRG=`dirname "$PRG"`"/$link"
20
- fi
68
+ app_path=$0
69
+
70
+ # Need this for daisy-chained symlinks.
71
+ while
72
+ APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
73
+ [ -h "$app_path" ]
74
+ do
75
+ ls=$( ls -ld "$app_path" )
76
+ link=${ls#*' -> '}
77
+ case $link in #(
78
+ /*) app_path=$link ;; #(
79
+ *) app_path=$APP_HOME$link ;;
80
+ esac
21
81
  done
22
- SAVED="`pwd`"
23
- cd "`dirname \"$PRG\"`/" >/dev/null
24
- APP_HOME="`pwd -P`"
25
- cd "$SAVED" >/dev/null
82
+
83
+ APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
26
84
 
27
85
  APP_NAME="Gradle"
28
- APP_BASE_NAME=`basename "$0"`
86
+ APP_BASE_NAME=${0##*/}
29
87
 
30
88
  # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31
- DEFAULT_JVM_OPTS=""
89
+ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
32
90
 
33
91
  # Use the maximum available, or set MAX_FD != -1 to use that value.
34
- MAX_FD="maximum"
92
+ MAX_FD=maximum
35
93
 
36
94
  warn () {
37
95
  echo "$*"
38
- }
96
+ } >&2
39
97
 
40
98
  die () {
41
99
  echo
42
100
  echo "$*"
43
101
  echo
44
102
  exit 1
45
- }
103
+ } >&2
46
104
 
47
105
  # OS specific support (must be 'true' or 'false').
48
106
  cygwin=false
49
107
  msys=false
50
108
  darwin=false
51
109
  nonstop=false
52
- case "`uname`" in
53
- CYGWIN* )
54
- cygwin=true
55
- ;;
56
- Darwin* )
57
- darwin=true
58
- ;;
59
- MINGW* )
60
- msys=true
61
- ;;
62
- NONSTOP* )
63
- nonstop=true
64
- ;;
110
+ case "$( uname )" in #(
111
+ CYGWIN* ) cygwin=true ;; #(
112
+ Darwin* ) darwin=true ;; #(
113
+ MSYS* | MINGW* ) msys=true ;; #(
114
+ NONSTOP* ) nonstop=true ;;
65
115
  esac
66
116
 
67
117
  CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68
118
 
119
+
69
120
  # Determine the Java command to use to start the JVM.
70
121
  if [ -n "$JAVA_HOME" ] ; then
71
122
  if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72
123
  # IBM's JDK on AIX uses strange locations for the executables
73
- JAVACMD="$JAVA_HOME/jre/sh/java"
124
+ JAVACMD=$JAVA_HOME/jre/sh/java
74
125
  else
75
- JAVACMD="$JAVA_HOME/bin/java"
126
+ JAVACMD=$JAVA_HOME/bin/java
76
127
  fi
77
128
  if [ ! -x "$JAVACMD" ] ; then
78
129
  die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
@@ -81,7 +132,7 @@ Please set the JAVA_HOME variable in your environment to match the
81
132
  location of your Java installation."
82
133
  fi
83
134
  else
84
- JAVACMD="java"
135
+ JAVACMD=java
85
136
  which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86
137
 
87
138
  Please set the JAVA_HOME variable in your environment to match the
@@ -89,84 +140,101 @@ location of your Java installation."
89
140
  fi
90
141
 
91
142
  # Increase the maximum file descriptors if we can.
92
- if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93
- MAX_FD_LIMIT=`ulimit -H -n`
94
- if [ $? -eq 0 ] ; then
95
- if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96
- MAX_FD="$MAX_FD_LIMIT"
97
- fi
98
- ulimit -n $MAX_FD
99
- if [ $? -ne 0 ] ; then
100
- warn "Could not set maximum file descriptor limit: $MAX_FD"
101
- fi
102
- else
103
- warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104
- fi
143
+ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144
+ case $MAX_FD in #(
145
+ max*)
146
+ MAX_FD=$( ulimit -H -n ) ||
147
+ warn "Could not query maximum file descriptor limit"
148
+ esac
149
+ case $MAX_FD in #(
150
+ '' | soft) :;; #(
151
+ *)
152
+ ulimit -n "$MAX_FD" ||
153
+ warn "Could not set maximum file descriptor limit to $MAX_FD"
154
+ esac
105
155
  fi
106
156
 
107
- # For Darwin, add options to specify how the application appears in the dock
108
- if $darwin; then
109
- GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110
- fi
157
+ # Collect all arguments for the java command, stacking in reverse order:
158
+ # * args from the command line
159
+ # * the main class name
160
+ # * -classpath
161
+ # * -D...appname settings
162
+ # * --module-path (only if needed)
163
+ # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
164
+
165
+ # For Cygwin or MSYS, switch paths to Windows format before running java
166
+ if "$cygwin" || "$msys" ; then
167
+ APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
168
+ CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
169
+
170
+ JAVACMD=$( cygpath --unix "$JAVACMD" )
111
171
 
112
- # For Cygwin, switch paths to Windows format before running java
113
- if $cygwin ; then
114
- APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115
- CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116
- JAVACMD=`cygpath --unix "$JAVACMD"`
117
-
118
- # We build the pattern for arguments to be converted via cygpath
119
- ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120
- SEP=""
121
- for dir in $ROOTDIRSRAW ; do
122
- ROOTDIRS="$ROOTDIRS$SEP$dir"
123
- SEP="|"
124
- done
125
- OURCYGPATTERN="(^($ROOTDIRS))"
126
- # Add a user-defined pattern to the cygpath arguments
127
- if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128
- OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129
- fi
130
172
  # Now convert the arguments - kludge to limit ourselves to /bin/sh
131
- i=0
132
- for arg in "$@" ; do
133
- CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134
- CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135
-
136
- if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137
- eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138
- else
139
- eval `echo args$i`="\"$arg\""
173
+ for arg do
174
+ if
175
+ case $arg in #(
176
+ -*) false ;; # don't mess with options #(
177
+ /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
178
+ [ -e "$t" ] ;; #(
179
+ *) false ;;
180
+ esac
181
+ then
182
+ arg=$( cygpath --path --ignore --mixed "$arg" )
140
183
  fi
141
- i=$((i+1))
184
+ # Roll the args list around exactly as many times as the number of
185
+ # args, so each arg winds up back in the position where it started, but
186
+ # possibly modified.
187
+ #
188
+ # NB: a `for` loop captures its iteration list before it begins, so
189
+ # changing the positional parameters here affects neither the number of
190
+ # iterations, nor the values presented in `arg`.
191
+ shift # remove old arg
192
+ set -- "$@" "$arg" # push replacement arg
142
193
  done
143
- case $i in
144
- (0) set -- ;;
145
- (1) set -- "$args0" ;;
146
- (2) set -- "$args0" "$args1" ;;
147
- (3) set -- "$args0" "$args1" "$args2" ;;
148
- (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149
- (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150
- (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151
- (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152
- (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153
- (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154
- esac
155
194
  fi
156
195
 
157
- # Escape application args
158
- save () {
159
- for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160
- echo " "
161
- }
162
- APP_ARGS=$(save "$@")
163
-
164
- # Collect all arguments for the java command, following the shell quoting and substitution rules
165
- eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166
-
167
- # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168
- if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169
- cd "$(dirname "$0")"
196
+ # Collect all arguments for the java command;
197
+ # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
198
+ # shell script including quotes and variable substitutions, so put them in
199
+ # double quotes to make sure that they get re-expanded; and
200
+ # * put everything else in single quotes, so that it's not re-expanded.
201
+
202
+ set -- \
203
+ "-Dorg.gradle.appname=$APP_BASE_NAME" \
204
+ -classpath "$CLASSPATH" \
205
+ org.gradle.wrapper.GradleWrapperMain \
206
+ "$@"
207
+
208
+ # Stop when "xargs" is not available.
209
+ if ! command -v xargs >/dev/null 2>&1
210
+ then
211
+ die "xargs is not available"
170
212
  fi
171
213
 
214
+ # Use "xargs" to parse quoted args.
215
+ #
216
+ # With -n1 it outputs one arg per line, with the quotes and backslashes removed.
217
+ #
218
+ # In Bash we could simply go:
219
+ #
220
+ # readarray ARGS < <( xargs -n1 <<<"$var" ) &&
221
+ # set -- "${ARGS[@]}" "$@"
222
+ #
223
+ # but POSIX shell has neither arrays nor command substitution, so instead we
224
+ # post-process each arg (as a line of input to sed) to backslash-escape any
225
+ # character that might be a shell metacharacter, then use eval to reverse
226
+ # that process (while maintaining the separation between arguments), and wrap
227
+ # the whole thing up as a single "set" statement.
228
+ #
229
+ # This will of course break if any of these variables contains a newline or
230
+ # an unmatched quote.
231
+ #
232
+
233
+ eval "set -- $(
234
+ printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
235
+ xargs -n1 |
236
+ sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
237
+ tr '\n' ' '
238
+ )" '"$@"'
239
+
172
240
  exec "$JAVACMD" "$@"
data/gradlew.bat CHANGED
@@ -1,4 +1,20 @@
1
- @if "%DEBUG%" == "" @echo off
1
+ @rem
2
+ @rem Copyright 2015 the original author or authors.
3
+ @rem
4
+ @rem Licensed under the Apache License, Version 2.0 (the "License");
5
+ @rem you may not use this file except in compliance with the License.
6
+ @rem You may obtain a copy of the License at
7
+ @rem
8
+ @rem https://www.apache.org/licenses/LICENSE-2.0
9
+ @rem
10
+ @rem Unless required by applicable law or agreed to in writing, software
11
+ @rem distributed under the License is distributed on an "AS IS" BASIS,
12
+ @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ @rem See the License for the specific language governing permissions and
14
+ @rem limitations under the License.
15
+ @rem
16
+
17
+ @if "%DEBUG%"=="" @echo off
2
18
  @rem ##########################################################################
3
19
  @rem
4
20
  @rem Gradle startup script for Windows
@@ -9,19 +25,22 @@
9
25
  if "%OS%"=="Windows_NT" setlocal
10
26
 
11
27
  set DIRNAME=%~dp0
12
- if "%DIRNAME%" == "" set DIRNAME=.
28
+ if "%DIRNAME%"=="" set DIRNAME=.
13
29
  set APP_BASE_NAME=%~n0
14
30
  set APP_HOME=%DIRNAME%
15
31
 
32
+ @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33
+ for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34
+
16
35
  @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17
- set DEFAULT_JVM_OPTS=
36
+ set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
18
37
 
19
38
  @rem Find java.exe
20
39
  if defined JAVA_HOME goto findJavaFromJavaHome
21
40
 
22
41
  set JAVA_EXE=java.exe
23
42
  %JAVA_EXE% -version >NUL 2>&1
24
- if "%ERRORLEVEL%" == "0" goto init
43
+ if %ERRORLEVEL% equ 0 goto execute
25
44
 
26
45
  echo.
27
46
  echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
@@ -35,7 +54,7 @@ goto fail
35
54
  set JAVA_HOME=%JAVA_HOME:"=%
36
55
  set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37
56
 
38
- if exist "%JAVA_EXE%" goto init
57
+ if exist "%JAVA_EXE%" goto execute
39
58
 
40
59
  echo.
41
60
  echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
@@ -45,38 +64,26 @@ echo location of your Java installation.
45
64
 
46
65
  goto fail
47
66
 
48
- :init
49
- @rem Get command-line arguments, handling Windows variants
50
-
51
- if not "%OS%" == "Windows_NT" goto win9xME_args
52
-
53
- :win9xME_args
54
- @rem Slurp the command line arguments.
55
- set CMD_LINE_ARGS=
56
- set _SKIP=2
57
-
58
- :win9xME_args_slurp
59
- if "x%~1" == "x" goto execute
60
-
61
- set CMD_LINE_ARGS=%*
62
-
63
67
  :execute
64
68
  @rem Setup the command line
65
69
 
66
70
  set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67
71
 
72
+
68
73
  @rem Execute Gradle
69
- "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
74
+ "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
70
75
 
71
76
  :end
72
77
  @rem End local scope for the variables with windows NT shell
73
- if "%ERRORLEVEL%"=="0" goto mainEnd
78
+ if %ERRORLEVEL% equ 0 goto mainEnd
74
79
 
75
80
  :fail
76
81
  rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77
82
  rem the _cmd.exe /c_ return code!
78
- if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79
- exit /b 1
83
+ set EXIT_CODE=%ERRORLEVEL%
84
+ if %EXIT_CODE% equ 0 set EXIT_CODE=1
85
+ if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
86
+ exit /b %EXIT_CODE%
80
87
 
81
88
  :mainEnd
82
89
  if "%OS%"=="Windows_NT" endlocal
@@ -2,9 +2,10 @@ require 'shellwords'
2
2
 
3
3
  class TrinoSqlParser
4
4
  class SupportProcess
5
- def initialize(idle_wait: 2, with_tokens:)
6
- @idle_wait = idle_wait
5
+ def initialize(idle_timeout:, with_tokens:, with_statement:)
6
+ @idle_timeout = idle_timeout
7
7
  @with_tokens = with_tokens
8
+ @with_statement = with_statement
8
9
  @mutex = Mutex.new
9
10
  @last_used_pid = nil
10
11
  @pipe = nil
@@ -23,6 +24,9 @@ class TrinoSqlParser
23
24
  if @with_tokens
24
25
  cmd << " --with-tokens"
25
26
  end
27
+ if @with_statement
28
+ cmd << " --with-statement"
29
+ end
26
30
 
27
31
  @pipe = IO.popen(TrinoSqlParser.java_env, cmd, "r+", external_encoding: 'UTF-8')
28
32
  @pid = @pipe.pid
@@ -40,17 +44,13 @@ class TrinoSqlParser
40
44
  end
41
45
  end
42
46
 
43
- def send_line(line)
47
+ def send_and_receive_line(line)
44
48
  @mutex.synchronize do # block kill! during execution
45
49
  start! unless @pipe
46
50
  @pipe.puts line
47
51
  @last_used_pid = @pipe.pid
52
+ @pipe.gets
48
53
  end
49
- nil
50
- end
51
-
52
- def receive_line
53
- @pipe.gets
54
54
  end
55
55
 
56
56
  private
@@ -60,7 +60,7 @@ class TrinoSqlParser
60
60
  done = Process.waitpid2(pid, Process::WNOHANG) rescue true
61
61
  break if done
62
62
 
63
- sleep @idle_wait
63
+ sleep @idle_timeout
64
64
  if @last_used_pid != pid
65
65
  kill! rescue nil
66
66
  end
@@ -1,3 +1,3 @@
1
1
  class TrinoSqlParser
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -35,8 +35,8 @@ class TrinoSqlParser
35
35
 
36
36
  require 'trino_sql_parser/support_process'
37
37
 
38
- def initialize(with_tokens: false)
39
- @support_process = SupportProcess.new(with_tokens: with_tokens)
38
+ def initialize(idle_timeout: 2, with_tokens: false, with_statement: false)
39
+ @support_process = SupportProcess.new(idle_timeout: idle_timeout, with_tokens: with_tokens, with_statement: with_statement)
40
40
  end
41
41
 
42
42
  def parse(sql)
@@ -47,8 +47,7 @@ class TrinoSqlParser
47
47
 
48
48
  success = false
49
49
  begin
50
- @support_process.send_line(request_line)
51
- response_line = @support_process.receive_line
50
+ response_line = @support_process.send_and_receive_line(request_line)
52
51
  raise "Process crashed" unless response_line
53
52
  response = JSON.parse(response_line)
54
53
  statements = response['statements']
@@ -6,7 +6,7 @@ RSpec.describe TrinoSqlParser do
6
6
  end
7
7
 
8
8
  it "raises ParseError::ParseError" do
9
- expect(lambda { parser.parse("...") }).to raise_error(TrinoSqlParser::ParseError)
9
+ expect { parser.parse("...") }.to raise_error(TrinoSqlParser::ParseError)
10
10
  end
11
11
 
12
12
  describe TrinoSqlParser::ParseError do
@@ -30,11 +30,37 @@ RSpec.describe TrinoSqlParser do
30
30
  end
31
31
 
32
32
  it "returns token list" do
33
- statements = parser.parse("select 1; select * from t")
33
+ statements = parser.parse("select 1; select func(time, '%S%S'), * from t")
34
34
  text, type, line, column = *statements[0]['tokens'].first
35
35
  expect(text).to eq("select")
36
36
  expect(line).to eq(1)
37
37
  expect(column).to eq(0)
38
+ expect(statements[0].has_key?('statements')).to eq(false)
39
+ end
40
+ end
41
+
42
+ context "with_statement" do
43
+ let(:parser) do
44
+ TrinoSqlParser.new(with_statement: true)
45
+ end
46
+
47
+ it "returns token list" do
48
+ statements = parser.parse("select 1; select func(time, '%Y%m%d') as f, * from t")
49
+
50
+ select_items = statements[1]['statement']['children'][0]['children'][0]['selectItems']
51
+
52
+ select_item_classes = select_items.map {|n| n['class'] }
53
+ expect(select_item_classes).to eq(['SingleColumn', 'AllColumns'])
54
+
55
+ func_alias = select_items[0]['alias']['value']
56
+ expect(func_alias).to eq('f')
57
+
58
+ func_arg_classes = select_items[0]['expression']['arguments'].map {|n| n['class'] }
59
+ func_arg_values = select_items[0]['expression']['arguments'].map {|n| n['value'] }
60
+ expect(func_arg_classes).to eq(['Identifier', 'StringLiteral'])
61
+ expect(func_arg_values).to eq(['time', '%Y%m%d'])
62
+
63
+ expect(statements[0].has_key?('tokens')).to eq(false)
38
64
  end
39
65
  end
40
66
  end
@@ -1,5 +1,6 @@
1
1
 
2
2
  import com.fasterxml.jackson.annotation.JsonFormat;
3
+ import com.fasterxml.jackson.annotation.JsonInclude;
3
4
  import com.fasterxml.jackson.annotation.JsonProperty;
4
5
  import com.fasterxml.jackson.annotation.JsonPropertyOrder;
5
6
  import io.trino.sql.parser.StatementSplitterWithOffsetRetained.Fragment;
@@ -40,17 +41,24 @@ public class JsonStatement
40
41
  private final int line;
41
42
  private final int column;
42
43
  private final List<JsonToken> tokens;
44
+ private final Statement statement;
43
45
 
44
- public JsonStatement(Fragment fragment, Statement statement, boolean withTokens)
46
+ public JsonStatement(Fragment fragment, Statement statement, boolean withTokens, boolean withStatement)
45
47
  {
46
- this.line= fragment.getLineOffset() + 1;
47
- this.column= fragment.getFirstLineColumnOffset();
48
+ this.line = fragment.getLineOffset() + 1;
49
+ this.column = fragment.getFirstLineColumnOffset();
48
50
  if (withTokens) {
49
51
  this.tokens = fragment.getTokens().stream().map(JsonToken::new).collect(toImmutableList());
50
52
  }
51
53
  else {
52
54
  this.tokens = null;
53
55
  }
56
+ if (withStatement) {
57
+ this.statement = statement;
58
+ }
59
+ else {
60
+ this.statement = null;
61
+ }
54
62
  }
55
63
 
56
64
  @JsonProperty
@@ -60,5 +68,10 @@ public class JsonStatement
60
68
  public int getColumn() { return column; }
61
69
 
62
70
  @JsonProperty
71
+ @JsonInclude(JsonInclude.Include.NON_NULL)
72
+ public Statement getStatement() { return statement; }
73
+
74
+ @JsonProperty
75
+ @JsonInclude(JsonInclude.Include.NON_NULL)
63
76
  public List<JsonToken> getTokens() { return tokens; }
64
77
  }
@@ -1,4 +1,6 @@
1
1
 
2
+ import com.fasterxml.jackson.annotation.JsonTypeInfo;
3
+ import com.fasterxml.jackson.databind.JavaType;
2
4
  import com.fasterxml.jackson.databind.ObjectMapper;
3
5
  import com.fasterxml.jackson.datatype.guava.GuavaModule;
4
6
  import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
@@ -22,12 +24,16 @@ public class TrinoSqlParserSupportProcess
22
24
  public static void main(String[] args) throws Exception
23
25
  {
24
26
  boolean withTokens = false;
27
+ boolean withStatement = false;
25
28
 
26
29
  for (String arg : args) {
27
30
  switch (arg) {
28
31
  case "--with-tokens":
29
32
  withTokens = true;
30
33
  break;
34
+ case "--with-statement":
35
+ withStatement = true;
36
+ break;
31
37
  default:
32
38
  System.err.println("Unknown argument: " + arg);
33
39
  System.exit(1);
@@ -37,6 +43,7 @@ public class TrinoSqlParserSupportProcess
37
43
  ObjectMapper mapper = new ObjectMapper();
38
44
  mapper.registerModule(new GuavaModule());
39
45
  mapper.registerModule(new Jdk8Module());
46
+ mapper.setDefaultTyping(new TrinoTreeNodeTypeResolver());
40
47
 
41
48
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in, UTF_8));
42
49
  while (true) {
@@ -46,13 +53,13 @@ public class TrinoSqlParserSupportProcess
46
53
  }
47
54
 
48
55
  JsonRequest request = mapper.readValue(line, JsonRequest.class);
49
- JsonResult result = parse(request.getSql(), withTokens);
56
+ JsonResult result = parse(request.getSql(), withTokens, withStatement);
50
57
  System.out.println(mapper.writeValueAsString(result));
51
58
  }
52
59
  }
53
60
 
54
61
  public static JsonResult parse(String sql,
55
- boolean withTokens)
62
+ boolean withTokens, boolean withStatement)
56
63
  {
57
64
  ImmutableList.Builder<JsonStatement> statements = ImmutableList.builder();
58
65
  ImmutableList.Builder<JsonErrorReport> errors = ImmutableList.builder();
@@ -66,7 +73,7 @@ public class TrinoSqlParserSupportProcess
66
73
  Statement statement = parser.createStatement(
67
74
  fragment.getStatement(),
68
75
  new ParsingOptions(DecimalLiteralTreatment.AS_DOUBLE));
69
- statements.add(new JsonStatement(fragment, statement, withTokens));
76
+ statements.add(new JsonStatement(fragment, statement, withTokens, withStatement));
70
77
  }
71
78
  catch (ParsingException ex) {
72
79
  errors.add(
@@ -79,4 +86,24 @@ public class TrinoSqlParserSupportProcess
79
86
 
80
87
  return new JsonResult(statements.build(), errors.build());
81
88
  }
89
+
90
+ // Use this type resolver to include class name as "class" field. The field
91
+ // name of "class" is safe because no node classes of io.trino.sql.tree.*
92
+ // define getClass() method.
93
+ private static class TrinoTreeNodeTypeResolver
94
+ extends ObjectMapper.DefaultTypeResolverBuilder
95
+ {
96
+ public TrinoTreeNodeTypeResolver() {
97
+ super(ObjectMapper.DefaultTyping.NON_FINAL);
98
+ init(JsonTypeInfo.Id.NAME, null);
99
+ inclusion(JsonTypeInfo.As.PROPERTY);
100
+ typeProperty("class");
101
+ }
102
+
103
+ @Override
104
+ public boolean useForType(JavaType t)
105
+ {
106
+ return t.getRawClass().getName().startsWith("io.trino.sql.tree");
107
+ }
108
+ }
82
109
  }
@@ -9,8 +9,8 @@ import java.util.List;
9
9
  import java.util.Set;
10
10
  import java.util.stream.Collectors;
11
11
  import java.util.stream.Stream;
12
- import org.antlr.v4.runtime.ANTLRInputStream;
13
12
  import org.antlr.v4.runtime.CharStream;
13
+ import org.antlr.v4.runtime.CharStreams;
14
14
  import org.antlr.v4.runtime.Token;
15
15
  import org.antlr.v4.runtime.TokenSource;
16
16
  import org.antlr.v4.runtime.Vocabulary;
@@ -106,9 +106,9 @@ public class StatementSplitterWithOffsetRetained
106
106
  }
107
107
  }
108
108
 
109
- private static TokenSource getLexer(String sql, Set<String> terminators)
109
+ public static TokenSource getLexer(String sql, Set<String> terminators)
110
110
  {
111
- CharStream stream = new CaseInsensitiveStream(new ANTLRInputStream(sql));
111
+ CharStream stream = new CaseInsensitiveStream(CharStreams.fromString(sql));
112
112
  return new DelimiterLexer(stream, terminators);
113
113
  }
114
114
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trino_sql_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadayuki Furuhashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-05 00:00:00.000000000 Z
11
+ date: 2022-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  - !ruby/object:Gem::Version
105
105
  version: '0'
106
106
  requirements: []
107
- rubygems_version: 3.0.3
107
+ rubygems_version: 3.3.7
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Trino SQL Parser for Ruby