Skip to content

Commit ad5dcc3

Browse files
traskCopilot
andauthored
Preserve SQL query summary operation case (#18930)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent cd43630 commit ad5dcc3

30 files changed

Lines changed: 552 additions & 435 deletions

File tree

instrumentation-api-incubator/src/main/jflex/SqlSanitizerWithSummary.jflex

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ WHITESPACE = [ \t\r\n]+
8888
}
8989

9090
/** Appends an operation name (SELECT, INSERT, etc.) to the query summary. */
91+
private void appendOperationToSummary() {
92+
if (querySummaryBuilder.length() > 0) {
93+
querySummaryBuilder.append(' ');
94+
}
95+
// yytext() allocates a String; append directly from JFlex's buffer.
96+
querySummaryBuilder.append(zzBuffer, zzStartRead, zzMarkedPos - zzStartRead);
97+
}
98+
9199
private void appendOperationToSummary(String operationName) {
92100
if (querySummaryBuilder.length() > 0) {
93101
querySummaryBuilder.append(' ');
@@ -257,7 +265,7 @@ WHITESPACE = [ \t\r\n]+
257265
void handleSelect() {
258266
inEmbeddedSelect = true;
259267
selectParenLevel = parenLevel;
260-
appendOperationToSummary("SELECT");
268+
appendOperationToSummary();
261269
}
262270

263271
void handleFrom() {
@@ -427,7 +435,7 @@ WHITESPACE = [ \t\r\n]+
427435

428436
void handleSelect() {
429437
operation = new Select();
430-
appendOperationToSummary("SELECT");
438+
appendOperationToSummary();
431439
}
432440

433441
void handleIdentifier() {
@@ -450,7 +458,7 @@ WHITESPACE = [ \t\r\n]+
450458
// Once we've captured the DELETE table, any SELECT is a subquery
451459
if (identifierCaptured) {
452460
operation = new Select();
453-
appendOperationToSummary("SELECT");
461+
appendOperationToSummary();
454462
}
455463
}
456464

@@ -482,7 +490,7 @@ WHITESPACE = [ \t\r\n]+
482490
// Once we've captured the UPDATE table, any SELECT is a subquery
483491
if (identifierCaptured) {
484492
operation = new Select();
485-
appendOperationToSummary("SELECT");
493+
appendOperationToSummary();
486494
}
487495
}
488496

@@ -740,10 +748,10 @@ WHITESPACE = [ \t\r\n]+
740748
confirmPendingSubqueryIfNeeded();
741749
if (shouldStartMainOperation()) {
742750
setOperation(new Select());
743-
appendOperationToSummary("SELECT");
751+
appendOperationToSummary();
744752
} else if (operation instanceof Select) {
745753
// nested SELECT (subquery) - append SELECT to summary
746-
appendOperationToSummary("SELECT");
754+
appendOperationToSummary();
747755
}
748756
operation.handleSelect();
749757
}
@@ -753,7 +761,7 @@ WHITESPACE = [ \t\r\n]+
753761
"INSERT" {
754762
if (shouldStartMainOperation()) {
755763
setOperation(new Insert());
756-
appendOperationToSummary("INSERT");
764+
appendOperationToSummary();
757765
} else if (!insideComment) {
758766
cancelPendingSubqueryIfNeeded();
759767
operation.handleIdentifier();
@@ -764,7 +772,7 @@ WHITESPACE = [ \t\r\n]+
764772
"DELETE" {
765773
if (shouldStartMainOperation()) {
766774
setOperation(new Delete());
767-
appendOperationToSummary("DELETE");
775+
appendOperationToSummary();
768776
} else if (!insideComment) {
769777
cancelPendingSubqueryIfNeeded();
770778
operation.handleIdentifier();
@@ -775,7 +783,7 @@ WHITESPACE = [ \t\r\n]+
775783
"UPDATE" {
776784
if (shouldStartMainOperation()) {
777785
setOperation(new Update());
778-
appendOperationToSummary("UPDATE");
786+
appendOperationToSummary();
779787
} else if (!insideComment) {
780788
cancelPendingSubqueryIfNeeded();
781789
operation.handleIdentifier();
@@ -786,7 +794,7 @@ WHITESPACE = [ \t\r\n]+
786794
"CALL" {
787795
if (shouldStartNewOperation()) {
788796
setOperation(new Call());
789-
appendOperationToSummary("CALL");
797+
appendOperationToSummary();
790798
} else if (!insideComment) {
791799
cancelPendingSubqueryIfNeeded();
792800
operation.handleIdentifier();
@@ -797,7 +805,7 @@ WHITESPACE = [ \t\r\n]+
797805
"MERGE" {
798806
if (shouldStartNewOperation()) {
799807
setOperation(new Merge());
800-
appendOperationToSummary("MERGE");
808+
appendOperationToSummary();
801809
} else if (!insideComment) {
802810
cancelPendingSubqueryIfNeeded();
803811
operation.handleIdentifier();
@@ -808,7 +816,7 @@ WHITESPACE = [ \t\r\n]+
808816
"CREATE" {
809817
if (shouldStartNewOperation()) {
810818
setOperation(new Create());
811-
appendOperationToSummary("CREATE");
819+
appendOperationToSummary();
812820
} else if (!insideComment) {
813821
cancelPendingSubqueryIfNeeded();
814822
operation.handleIdentifier();
@@ -819,7 +827,7 @@ WHITESPACE = [ \t\r\n]+
819827
"DROP" {
820828
if (shouldStartNewOperation()) {
821829
setOperation(new Drop());
822-
appendOperationToSummary("DROP");
830+
appendOperationToSummary();
823831
} else if (!insideComment) {
824832
cancelPendingSubqueryIfNeeded();
825833
operation.handleIdentifier();
@@ -830,7 +838,7 @@ WHITESPACE = [ \t\r\n]+
830838
"ALTER" {
831839
if (shouldStartNewOperation()) {
832840
setOperation(new Alter());
833-
appendOperationToSummary("ALTER");
841+
appendOperationToSummary();
834842
} else if (!insideComment) {
835843
cancelPendingSubqueryIfNeeded();
836844
operation.handleIdentifier();
@@ -846,7 +854,7 @@ WHITESPACE = [ \t\r\n]+
846854
setOperation(new Values());
847855
// Only append VALUES to summary if at top level (not inside a subquery or CTE body)
848856
if (operationStack.isEmpty()) {
849-
appendOperationToSummary("VALUES");
857+
appendOperationToSummary();
850858
}
851859
}
852860
}
@@ -856,7 +864,7 @@ WHITESPACE = [ \t\r\n]+
856864
"EXECUTE" | "EXEC" {
857865
if (shouldStartNewOperation()) {
858866
setOperation(new Execute());
859-
appendOperationToSummary(yytext());
867+
appendOperationToSummary();
860868
} else if (!insideComment) {
861869
cancelPendingSubqueryIfNeeded();
862870
operation.handleIdentifier();
@@ -867,79 +875,79 @@ WHITESPACE = [ \t\r\n]+
867875
"TRUNCATE" {
868876
if (shouldStartNewOperation()) {
869877
setOperation(new Truncate());
870-
appendOperationToSummary("TRUNCATE");
878+
appendOperationToSummary();
871879
}
872880
appendCurrentFragment();
873881
if (isOverLimit()) return YYEOF;
874882
}
875883
"REPLACE" {
876884
if (shouldStartNewOperation()) {
877885
setOperation(new Replace());
878-
appendOperationToSummary("REPLACE");
886+
appendOperationToSummary();
879887
}
880888
appendCurrentFragment();
881889
if (isOverLimit()) return YYEOF;
882890
}
883891
"LOCK" {
884892
if (shouldStartNewOperation()) {
885893
setOperation(new Lock());
886-
appendOperationToSummary("LOCK");
894+
appendOperationToSummary();
887895
}
888896
appendCurrentFragment();
889897
if (isOverLimit()) return YYEOF;
890898
}
891899
"USE" {
892900
if (shouldStartNewOperation()) {
893901
setOperation(new Use());
894-
appendOperationToSummary("USE");
902+
appendOperationToSummary();
895903
}
896904
appendCurrentFragment();
897905
if (isOverLimit()) return YYEOF;
898906
}
899907
"BEGIN" {
900908
if (shouldStartNewOperation()) {
901909
setOperation(new TransactionControl());
902-
appendOperationToSummary("BEGIN");
910+
appendOperationToSummary();
903911
}
904912
appendCurrentFragment();
905913
if (isOverLimit()) return YYEOF;
906914
}
907915
"COMMIT" {
908916
if (shouldStartNewOperation()) {
909917
setOperation(new TransactionControl());
910-
appendOperationToSummary("COMMIT");
918+
appendOperationToSummary();
911919
}
912920
appendCurrentFragment();
913921
if (isOverLimit()) return YYEOF;
914922
}
915923
"ROLLBACK" {
916924
if (shouldStartNewOperation()) {
917925
setOperation(new TransactionControl());
918-
appendOperationToSummary("ROLLBACK");
926+
appendOperationToSummary();
919927
}
920928
appendCurrentFragment();
921929
if (isOverLimit()) return YYEOF;
922930
}
923931
"GRANT" {
924932
if (shouldStartNewOperation()) {
925933
setOperation(new Grant());
926-
appendOperationToSummary("GRANT");
934+
appendOperationToSummary();
927935
}
928936
appendCurrentFragment();
929937
if (isOverLimit()) return YYEOF;
930938
}
931939
"REVOKE" {
932940
if (shouldStartNewOperation()) {
933941
setOperation(new Revoke());
934-
appendOperationToSummary("REVOKE");
942+
appendOperationToSummary();
935943
}
936944
appendCurrentFragment();
937945
if (isOverLimit()) return YYEOF;
938946
}
939947
"SHOW" {
940948
if (shouldStartNewOperation()) {
941949
setOperation(new Show());
942-
appendOperationToSummary("SHOW");
950+
appendOperationToSummary();
943951
}
944952
appendCurrentFragment();
945953
if (isOverLimit()) return YYEOF;
@@ -955,7 +963,7 @@ WHITESPACE = [ \t\r\n]+
955963
// EXPLAIN is a prefix command - append to summary but don't set an operation,
956964
// so the inner statement (SELECT, INSERT, etc.) gets processed normally.
957965
if (!insideComment && operation == none) {
958-
appendOperationToSummary("EXPLAIN");
966+
appendOperationToSummary();
959967
}
960968
appendCurrentFragment();
961969
if (isOverLimit()) return YYEOF;
@@ -966,7 +974,9 @@ WHITESPACE = [ \t\r\n]+
966974
// hql/jpql queries may skip SELECT and start with FROM clause
967975
// treat such queries as SELECT queries
968976
setOperation(new Select());
969-
appendOperationToSummary("SELECT");
977+
// Derive the synthetic SELECT case from the matched FROM token.
978+
appendOperationToSummary(
979+
Character.isUpperCase(zzBuffer[zzStartRead]) ? "SELECT" : "select");
970980
}
971981
operation.handleFrom();
972982
}

0 commit comments

Comments
 (0)